3 # Copyright 2011 MJ Ray and software.coop
4 # Koha is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # Koha is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17 # This Koha test module is a stub!
18 # Add more tests here!!!
21 use Test::More tests => 10;
27 use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
36 # Make sure we can rollback.
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39 my $dbh = C4::Context->dbh;
42 # FIXME: are we sure there is an member number 1?
43 logaction("MEMBERS","MODIFY",1,"test operation");
49 ok($success, "logaction seemed to work");
52 # FIXME: US formatted date hardcoded into test for now
53 $success = scalar(@{GetLogs("","","",undef,undef,"","")});
58 ok($success, "GetLogs returns results for an open search");
61 # FIXME: US formatted date hardcoded into test for now
62 my $date = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
63 $success = scalar(@{GetLogs( $date, $date, "", undef, undef, "", "") } );
68 ok($success, "GetLogs accepts dates in an All-matching search");
71 $success = scalar(@{GetLogs("","","",["MEMBERS"],["MODIFY"],1,"")});
76 ok($success, "GetLogs seemed to find ".$success." like our test record in a tighter search");
78 # We want numbers to be the same between runs.
79 $dbh->do("DELETE FROM action_logs;");
81 t::lib::Mocks::mock_preference('CronjobLog',0);
83 my $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
84 is($cronJobCount,0,"Cronjob not logged as expected.");
86 t::lib::Mocks::mock_preference('CronjobLog',1);
88 $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
89 is($cronJobCount,1,"Cronjob logged as expected.");
91 subtest "GetLogs should return all logs if dates are not set" => sub {
93 my $today = dt_from_string->add(minutes => -1);
94 my $yesterday = dt_from_string->add( days => -1 );
96 INSERT INTO action_logs (timestamp, user, module, action, object, info)
98 (?, 42, 'CATALOGUING', 'MODIFY', 4242, 'Record 42 has been modified by patron 4242 yesterday'),
99 (?, 43, 'CATALOGUING', 'MODIFY', 4242, 'Record 43 has been modified by patron 4242 today')
100 |, undef, output_pref({dt =>$yesterday, dateformat => 'iso'}), output_pref({dt => $today, dateformat => 'iso'}));
101 my $logs = GetLogs( undef, undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
102 is( scalar(@$logs), 2, 'GetLogs should return all logs regardless the dates' );
103 $logs = GetLogs( output_pref($today), undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
104 is( scalar(@$logs), 1, 'GetLogs should return the logs for today' );
107 subtest 'logaction(): interface is correctly logged' => sub {
111 # No interface passed, using C4::Context->interface
112 $dbh->do("DELETE FROM action_logs;");
113 C4::Context->interface( 'commandline' );
114 logaction( "MEMBERS", "MODIFY", 1, "test operation");
115 my $logs = GetLogs();
116 is( @{$logs}[0]->{ interface }, 'commandline', 'Interface correctly deduced (commandline)');
118 # No interface passed, using C4::Context->interface
119 $dbh->do("DELETE FROM action_logs;");
120 C4::Context->interface( 'opac' );
121 logaction( "MEMBERS", "MODIFY", 1, "test operation");
123 is( @{$logs}[0]->{ interface }, 'opac', 'Interface correctly deduced (opac)');
125 # Explicit interfaces
126 $dbh->do("DELETE FROM action_logs;");
127 C4::Context->interface( 'intranet' );
128 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'intranet');
130 is( @{$logs}[0]->{ interface }, 'intranet', 'Passed interface is respected (intranet)');
132 # Explicit interfaces
133 $dbh->do("DELETE FROM action_logs;");
134 C4::Context->interface( 'sip' );
135 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'sip');
137 is( @{$logs}[0]->{ interface }, 'sip', 'Passed interface is respected (sip)');
140 subtest 'GetLogs() respects interface filters' => sub {
144 $dbh->do("DELETE FROM action_logs;");
146 logaction( 'MEMBERS', 'MODIFY', 1, 'opac info', 'opac');
147 logaction( 'MEMBERS', 'MODIFY', 1, 'sip info', 'sip');
148 logaction( 'MEMBERS', 'MODIFY', 1, 'intranet info', 'intranet');
149 logaction( 'MEMBERS', 'MODIFY', 1, 'commandline info', 'commandline');
151 my $logs = scalar @{ GetLogs() };
152 is( $logs, 4, 'If no filter on interfaces is passed, all logs are returned');
154 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['opac']);
155 is( @{$logs}[0]->{ interface }, 'opac', 'Interface correctly filtered (opac)');
157 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['sip']);
158 is( @{$logs}[0]->{ interface }, 'sip', 'Interface correctly filtered (sip)');
160 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['intranet']);
161 is( @{$logs}[0]->{ interface }, 'intranet', 'Interface correctly filtered (intranet)');
163 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['commandline']);
164 is( @{$logs}[0]->{ interface }, 'commandline', 'Interface correctly filtered (commandline)');
167 $schema->storage->txn_rollback;