Bug 10361: (follow-up) add tests for search history functions
[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/AddSearchHistory PurgeSearchHistory/); # GetSearchHistory is not exported
25
26 # Start transaction
27 my $dbh = C4::Context->dbh;
28 $dbh->{AutoCommit} = 0;
29 $dbh->{RaiseError} = 1;
30
31 # start with a clean slate
32 $dbh->do('DELETE FROM search_history');
33 is(_get_history_count(), 0, 'starting off with nothing in search_history');
34
35 # Add some history rows.  Note that we're ignoring the return value;
36 # since the search_history table doesn't have an auto_increment
37 # column, it appears that the value of $dbh->last_insert_id() is
38 # useless for determining if the insert failed.
39 AddSearchHistory(12345, 'session_1', 'query_desc_1', 'query_cgi_1', 5);
40 AddSearchHistory(12345, 'session_1', 'query_desc_2', 'query_cgi_2', 6);
41 AddSearchHistory(12345, 'session_1', 'query_desc_3', 'query_cgi_3', 7);
42 AddSearchHistory(56789, 'session_2', 'query_desc_4', 'query_cgi_4', 8);
43 is(_get_history_count(), 4, 'successfully added four search_history rows');
44
45 # We're not testing GetSearchHistory at present because it is broken...
46 # see bug 10677
47
48 # munge some dates
49 my $sth = $dbh->prepare('
50     UPDATE search_history
51     SET time = DATE_SUB(time, INTERVAL ? DAY)
52     WHERE query_desc = ?
53 ');
54 $sth->execute(46, 'query_desc_1');
55 $sth->execute(31, 'query_desc_2');
56
57 PurgeSearchHistory(45);
58 is(_get_history_count(), 3, 'purged history older than 45 days');
59 PurgeSearchHistory(30);
60 is(_get_history_count(), 2, 'purged history older than 30 days');
61 PurgeSearchHistory(-1);
62 is(_get_history_count(), 0, 'purged all history');
63
64 sub _get_history_count {
65     my $count_sth = $dbh->prepare('SELECT COUNT(*) FROM search_history');
66     $count_sth->execute();
67     my $count = $count_sth->fetchall_arrayref();
68     return $count->[0]->[0];
69 }
70
71 $dbh->rollback;