Bug 10807: (follow-up) fix UT t/db_dependent/Search_SearchHistory.t
[koha.git] / t / db_dependent / Search_SearchHistory.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 Equinox Software, Inc.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 6;
22
23 use C4::Context;
24 use_ok('C4::Search', qw/PurgeSearchHistory/); # GetSearchHistory is not exported
25 use C4::Search::History;
26
27 # Start transaction
28 my $dbh = C4::Context->dbh;
29 $dbh->{AutoCommit} = 0;
30 $dbh->{RaiseError} = 1;
31
32 # start with a clean slate
33 $dbh->do('DELETE FROM search_history');
34 is(_get_history_count(), 0, 'starting off with nothing in search_history');
35
36 # Add some history rows.  Note that we're ignoring the return value;
37 # since the search_history table doesn't have an auto_increment
38 # column, it appears that the value of $dbh->last_insert_id() is
39 # useless for determining if the insert failed.
40 C4::Search::History::add({userid => 12345, sessionid => 'session_1', query_desc => 'query_desc_1', query_cgi => 'query_cgi_1', total => 5});
41 C4::Search::History::add({userid => 12345, sessionid => 'session_1', query_desc => 'query_desc_2', query_cgi => 'query_cgi_3', total => 6});
42 C4::Search::History::add({userid => 12345, sessionid => 'session_1', query_desc => 'query_desc_3', query_cgi => 'query_cgi_3', total => 7});
43 C4::Search::History::add({userid => 56789, sessionid => 'session_2', query_desc => 'query_desc_4', query_cgi => 'query_cgi_4', total => 8});
44 is(_get_history_count(), 4, 'successfully added four search_history rows');
45
46 # We're not testing GetSearchHistory at present because it is broken...
47 # see bug 10677
48
49 # munge some dates
50 my $sth = $dbh->prepare('
51     UPDATE search_history
52     SET time = DATE_SUB(time, INTERVAL ? DAY)
53     WHERE query_desc = ?
54 ');
55 $sth->execute(46, 'query_desc_1');
56 $sth->execute(31, 'query_desc_2');
57
58 PurgeSearchHistory(45);
59 is(_get_history_count(), 3, 'purged history older than 45 days');
60 PurgeSearchHistory(30);
61 is(_get_history_count(), 2, 'purged history older than 30 days');
62 PurgeSearchHistory(-1);
63 is(_get_history_count(), 0, 'purged all history');
64
65 sub _get_history_count {
66     my $count_sth = $dbh->prepare('SELECT COUNT(*) FROM search_history');
67     $count_sth->execute();
68     my $count = $count_sth->fetchall_arrayref();
69     return $count->[0]->[0];
70 }
71
72 $dbh->rollback;