3 # This file is part of Koha.
5 # Copyright 2013 Equinox Software, Inc.
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
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.
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>.
21 use Test
::More tests
=> 6;
24 use_ok
('C4::Search', qw
/PurgeSearchHistory/); # GetSearchHistory is not exported
25 use C4
::Search
::History
;
28 my $dbh = C4
::Context
->dbh;
29 $dbh->{AutoCommit
} = 0;
30 $dbh->{RaiseError
} = 1;
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');
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');
47 my $sth = $dbh->prepare('
49 SET time = DATE_SUB(time, INTERVAL ? DAY)
52 $sth->execute(46, 'query_desc_1');
53 $sth->execute(31, 'query_desc_2');
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');
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];