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 Data::Dumper qw( Dumper );
19 use Test::More tests => 5;
22 use C4::Log qw( logaction cronlogaction );
23 use C4::Auth qw( checkpw );
27 use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
28 use t::lib::TestBuilder;
30 # Make sure we can rollback.
31 our $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
34 subtest 'Existing tests' => sub {
39 # FIXME: are we sure there is an member number 1?
40 logaction("MEMBERS","MODIFY",1,"test operation");
46 ok($success, "logaction seemed to work");
48 # We want numbers to be the same between runs.
49 Koha::ActionLogs->search->delete;
51 t::lib::Mocks::mock_preference('CronjobLog',0);
53 is(Koha::ActionLogs->search({ module => 'CRONJOBS' })->count,0,"Cronjob not logged as expected.");
55 t::lib::Mocks::mock_preference('CronjobLog',1);
57 is(Koha::ActionLogs->search({ module => 'CRONJOBS' })->count,1,"Cronjob logged as expected.");
60 subtest 'logaction(): interface is correctly logged' => sub {
64 # No interface passed, using C4::Context->interface
65 Koha::ActionLogs->search->delete;
66 C4::Context->interface( 'commandline' );
67 logaction( "MEMBERS", "MODIFY", 1, "test operation");
68 my $log = Koha::ActionLogs->search->next;
69 is( $log->interface, 'commandline', 'Interface correctly deduced (commandline)');
71 # No interface passed, using C4::Context->interface
72 Koha::ActionLogs->search->delete;
73 C4::Context->interface( 'opac' );
74 logaction( "MEMBERS", "MODIFY", 1, "test operation");
75 $log = Koha::ActionLogs->search->next;
76 is( $log->interface, 'opac', 'Interface correctly deduced (opac)');
79 Koha::ActionLogs->search->delete;
80 C4::Context->interface( 'intranet' );
81 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'intranet');
82 $log = Koha::ActionLogs->search->next;
83 is( $log->interface, 'intranet', 'Passed interface is respected (intranet)');
86 Koha::ActionLogs->search->delete;
87 C4::Context->interface( 'sip' );
88 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'sip');
89 $log = Koha::ActionLogs->search->next;
90 is( $log->interface, 'sip', 'Passed interface is respected (sip)');
93 subtest 'logaction / trace' => sub {
96 C4::Context->interface( 'intranet' );
97 t::lib::Mocks::mock_preference('ActionLogsTraceDepth',0);
99 logaction( "MEMBERS", "MODIFY", 1, "test1");
100 is( Koha::ActionLogs->search({ info => 'test1' })->last->trace, undef, 'No trace at level 0' );
101 t::lib::Mocks::mock_preference('ActionLogsTraceDepth',2);
102 logaction( "MEMBERS", "MODIFY", 1, "test2");
103 like( Koha::ActionLogs->search({ info => 'test2' })->last->trace, qr/("line":.*){2}/, 'Found two trace levels' );
105 t::lib::Mocks::mock_preference('ActionLogsTraceDepth',0);
108 subtest 'GDPR logging' => sub {
111 my $builder = t::lib::TestBuilder->new;
112 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
114 t::lib::Mocks::mock_userenv({ patron => $patron });
115 logaction( 'AUTH', 'FAILURE', $patron->id, '', 'opac' );
116 my $logs = Koha::ActionLogs->search(
121 object => $patron->id,
124 is( $logs->count, 1, 'We should find one auth failure' );
126 t::lib::Mocks::mock_preference('AuthFailureLog', 1);
127 my $strong_password = 'N0tStr0ngAnyM0reN0w:)';
128 $patron->set_password({ password => $strong_password });
129 my @ret = checkpw( $patron->userid, 'WrongPassword', undef, undef, 1);
130 is( $ret[0], 0, 'Authentication failed' );
131 # Look for auth failure but NOT on patron id, pass userid in info parameter
132 $logs = Koha::ActionLogs->search(
136 info => { -like => '%'.$patron->userid.'%' },
139 is( $logs->count, 1, 'We should find one auth failure with this userid' );
140 t::lib::Mocks::mock_preference('AuthFailureLog', 0);
141 @ret = checkpw( $patron->userid, 'WrongPassword', undef, undef, 1);
142 $logs = Koha::ActionLogs->search(
146 info => { -like => '%'.$patron->userid.'%' },
149 is( $logs->count, 1, 'Still only one failure with this userid' );
150 t::lib::Mocks::mock_preference('AuthSuccessLog', 1);
151 @ret = checkpw( $patron->userid, $strong_password, undef, undef, 1);
152 is( $ret[0], 1, 'Authentication succeeded' );
153 # Now we can look for patron id
154 $logs = Koha::ActionLogs->search(
159 object => $patron->id,
163 is( $logs->count, 1, 'We expect only one auth success line for this patron' );
166 subtest 'Reduce log size by unblessing Koha objects' => sub {
169 my $builder = t::lib::TestBuilder->new;
170 my $item = $builder->build_sample_item;
172 logaction( 'MY_MODULE', 'TEST01', $item->itemnumber, $item, 'opac' );
173 my $str = Dumper($item->unblessed);
174 my $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST01', object => $item->itemnumber });
175 is( $logs->count, 1, 'Action found' );
176 is( length($logs->next->info), length($str), 'Length exactly identical' );
178 logaction( 'CATALOGUING', 'MODIFY', $item->itemnumber, $item, 'opac' );
179 $logs = Koha::ActionLogs->search({ module => 'CATALOGUING', action => 'MODIFY', object => $item->itemnumber });
180 is( substr($logs->next->info, 0, 5), 'item ', 'Prefix item' );
181 is( length($logs->reset->next->info), 5+length($str), 'Length + 5' );
183 my $hold = $builder->build_object({ class => 'Koha::Holds' });
184 logaction( 'MY_CIRC_MODULE', 'TEST', $item->itemnumber, $hold, 'opac' );
185 $logs = Koha::ActionLogs->search({ module => 'MY_CIRC_MODULE', action => 'TEST', object => $item->itemnumber });
186 is( length($logs->next->info), length( Dumper($hold->unblessed)), 'Length of dumped unblessed hold' );
188 logaction( 'MY_MODULE', 'TEST02', $item->itemnumber, [], 'opac' );
189 $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST02', object => $item->itemnumber });
190 like( $logs->next->info, qr/^ARRAY\(/, 'Dumped arrayref' );
192 logaction( 'MY_MODULE', 'TEST03', $item->itemnumber, $builder, 'opac' );
193 $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST03', object => $item->itemnumber });
194 like( $logs->next->info, qr/^t::lib::TestBuilder/, 'Dumped TestBuilder object' );
197 $schema->storage->txn_rollback;