3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Test::More tests => 5;
25 use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
26 use t::lib::TestBuilder;
28 # Make sure we can rollback.
29 our $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
31 our $dbh = C4::Context->dbh;
33 subtest 'Existing tests' => sub {
38 # FIXME: are we sure there is an member number 1?
39 logaction("MEMBERS","MODIFY",1,"test operation");
45 ok($success, "logaction seemed to work");
48 # FIXME: US formatted date hardcoded into test for now
49 $success = scalar(@{GetLogs("","","",undef,undef,"","")});
54 ok($success, "GetLogs returns results for an open search");
57 # FIXME: US formatted date hardcoded into test for now
58 my $date = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
59 $success = scalar(@{GetLogs( $date, $date, "", undef, undef, "", "") } );
64 ok($success, "GetLogs accepts dates in an All-matching search");
67 $success = scalar(@{GetLogs("","","",["MEMBERS"],["MODIFY"],1,"")});
72 ok($success, "GetLogs seemed to find ".$success." like our test record in a tighter search");
74 # We want numbers to be the same between runs.
75 $dbh->do("DELETE FROM action_logs;");
77 t::lib::Mocks::mock_preference('CronjobLog',0);
79 my $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
80 is($cronJobCount,0,"Cronjob not logged as expected.");
82 t::lib::Mocks::mock_preference('CronjobLog',1);
84 $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
85 is($cronJobCount,1,"Cronjob logged as expected.");
88 subtest "GetLogs should return all logs if dates are not set" => sub {
90 my $today = dt_from_string->add(minutes => -1);
91 my $yesterday = dt_from_string->add( days => -1 );
93 INSERT INTO action_logs (timestamp, user, module, action, object, info)
95 (?, 42, 'CATALOGUING', 'MODIFY', 4242, 'Record 42 has been modified by patron 4242 yesterday'),
96 (?, 43, 'CATALOGUING', 'MODIFY', 4242, 'Record 43 has been modified by patron 4242 today')
97 |, undef, output_pref({dt =>$yesterday, dateformat => 'iso'}), output_pref({dt => $today, dateformat => 'iso'}));
98 my $logs = GetLogs( undef, undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
99 is( scalar(@$logs), 2, 'GetLogs should return all logs regardless the dates' );
100 $logs = GetLogs( output_pref($today), undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
101 is( scalar(@$logs), 1, 'GetLogs should return the logs for today' );
104 subtest 'logaction(): interface is correctly logged' => sub {
108 # No interface passed, using C4::Context->interface
109 $dbh->do("DELETE FROM action_logs;");
110 C4::Context->interface( 'commandline' );
111 logaction( "MEMBERS", "MODIFY", 1, "test operation");
112 my $logs = GetLogs();
113 is( @{$logs}[0]->{ interface }, 'commandline', 'Interface correctly deduced (commandline)');
115 # No interface passed, using C4::Context->interface
116 $dbh->do("DELETE FROM action_logs;");
117 C4::Context->interface( 'opac' );
118 logaction( "MEMBERS", "MODIFY", 1, "test operation");
120 is( @{$logs}[0]->{ interface }, 'opac', 'Interface correctly deduced (opac)');
122 # Explicit interfaces
123 $dbh->do("DELETE FROM action_logs;");
124 C4::Context->interface( 'intranet' );
125 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'intranet');
127 is( @{$logs}[0]->{ interface }, 'intranet', 'Passed interface is respected (intranet)');
129 # Explicit interfaces
130 $dbh->do("DELETE FROM action_logs;");
131 C4::Context->interface( 'sip' );
132 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'sip');
134 is( @{$logs}[0]->{ interface }, 'sip', 'Passed interface is respected (sip)');
137 subtest 'GetLogs() respects interface filters' => sub {
141 $dbh->do("DELETE FROM action_logs;");
143 logaction( 'MEMBERS', 'MODIFY', 1, 'opac info', 'opac');
144 logaction( 'MEMBERS', 'MODIFY', 1, 'sip info', 'sip');
145 logaction( 'MEMBERS', 'MODIFY', 1, 'intranet info', 'intranet');
146 logaction( 'MEMBERS', 'MODIFY', 1, 'commandline info', 'commandline');
148 my $logs = scalar @{ GetLogs() };
149 is( $logs, 4, 'If no filter on interfaces is passed, all logs are returned');
151 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['opac']);
152 is( @{$logs}[0]->{ interface }, 'opac', 'Interface correctly filtered (opac)');
154 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['sip']);
155 is( @{$logs}[0]->{ interface }, 'sip', 'Interface correctly filtered (sip)');
157 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['intranet']);
158 is( @{$logs}[0]->{ interface }, 'intranet', 'Interface correctly filtered (intranet)');
160 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['commandline']);
161 is( @{$logs}[0]->{ interface }, 'commandline', 'Interface correctly filtered (commandline)');
164 subtest 'GDPR logging' => sub {
167 my $builder = t::lib::TestBuilder->new;
168 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
170 t::lib::Mocks::mock_userenv({ patron => $patron });
171 logaction( 'AUTH', 'FAILURE', $patron->id, '', 'opac' );
172 my $logs = GetLogs( undef, undef, $patron->id, ['AUTH'], ['FAILURE'], $patron->id );
173 is( @$logs, 1, 'We should find one auth failure for this patron' );
177 $schema->storage->txn_rollback;