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 => 4;
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 'GDPR logging' => sub {
96 my $builder = t::lib::TestBuilder->new;
97 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
99 t::lib::Mocks::mock_userenv({ patron => $patron });
100 logaction( 'AUTH', 'FAILURE', $patron->id, '', 'opac' );
101 my $logs = Koha::ActionLogs->search(
106 object => $patron->id,
109 is( $logs->count, 1, 'We should find one auth failure' );
111 t::lib::Mocks::mock_preference('AuthFailureLog', 1);
112 my $strong_password = 'N0tStr0ngAnyM0reN0w:)';
113 $patron->set_password({ password => $strong_password });
114 my @ret = checkpw( $patron->userid, 'WrongPassword', undef, undef, 1);
115 is( $ret[0], 0, 'Authentication failed' );
116 # Look for auth failure but NOT on patron id, pass userid in info parameter
117 $logs = Koha::ActionLogs->search(
121 info => { -like => '%'.$patron->userid.'%' },
124 is( $logs->count, 1, 'We should find one auth failure with this userid' );
125 t::lib::Mocks::mock_preference('AuthFailureLog', 0);
126 @ret = checkpw( $patron->userid, 'WrongPassword', undef, undef, 1);
127 $logs = Koha::ActionLogs->search(
131 info => { -like => '%'.$patron->userid.'%' },
134 is( $logs->count, 1, 'Still only one failure with this userid' );
135 t::lib::Mocks::mock_preference('AuthSuccessLog', 1);
136 @ret = checkpw( $patron->userid, $strong_password, undef, undef, 1);
137 is( $ret[0], 1, 'Authentication succeeded' );
138 # Now we can look for patron id
139 $logs = Koha::ActionLogs->search(
144 object => $patron->id,
148 is( $logs->count, 1, 'We expect only one auth success line for this patron' );
151 subtest 'Reduce log size by unblessing Koha objects' => sub {
154 my $builder = t::lib::TestBuilder->new;
155 my $item = $builder->build_sample_item;
157 logaction( 'MY_MODULE', 'TEST01', $item->itemnumber, $item, 'opac' );
158 my $str = Dumper($item->unblessed);
159 my $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST01', object => $item->itemnumber });
160 is( $logs->count, 1, 'Action found' );
161 is( length($logs->next->info), length($str), 'Length exactly identical' );
163 logaction( 'CATALOGUING', 'MODIFY', $item->itemnumber, $item, 'opac' );
164 $logs = Koha::ActionLogs->search({ module => 'CATALOGUING', action => 'MODIFY', object => $item->itemnumber });
165 is( substr($logs->next->info, 0, 5), 'item ', 'Prefix item' );
166 is( length($logs->reset->next->info), 5+length($str), 'Length + 5' );
168 my $hold = $builder->build_object({ class => 'Koha::Holds' });
169 logaction( 'MY_CIRC_MODULE', 'TEST', $item->itemnumber, $hold, 'opac' );
170 $logs = Koha::ActionLogs->search({ module => 'MY_CIRC_MODULE', action => 'TEST', object => $item->itemnumber });
171 is( length($logs->next->info), length( Dumper($hold->unblessed)), 'Length of dumped unblessed hold' );
173 logaction( 'MY_MODULE', 'TEST02', $item->itemnumber, [], 'opac' );
174 $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST02', object => $item->itemnumber });
175 like( $logs->next->info, qr/^ARRAY\(/, 'Dumped arrayref' );
177 logaction( 'MY_MODULE', 'TEST03', $item->itemnumber, $builder, 'opac' );
178 $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST03', object => $item->itemnumber });
179 like( $logs->next->info, qr/^t::lib::TestBuilder/, 'Dumped TestBuilder object' );
182 $schema->storage->txn_rollback;