Bug 7162: (RM followup) fix the number of tests
[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 # munge some dates
47 my $sth = $dbh->prepare('
48     UPDATE search_history
49     SET time = DATE_SUB(time, INTERVAL ? DAY)
50     WHERE query_desc = ?
51 ');
52 $sth->execute(46, 'query_desc_1');
53 $sth->execute(31, 'query_desc_2');
54
55 PurgeSearchHistory(45);
56 is(_get_history_count(), 3, 'purged history older than 45 days');
57 PurgeSearchHistory(30);
58 is(_get_history_count(), 2, 'purged history older than 30 days');
59 PurgeSearchHistory(-1);
60 is(_get_history_count(), 0, 'purged all history');
61
62 sub _get_history_count {
63     my $count_sth = $dbh->prepare('SELECT COUNT(*) FROM search_history');
64     $count_sth->execute();
65     my $count = $count_sth->fetchall_arrayref();
66     return $count->[0]->[0];
67 }
68
69 $dbh->rollback;