Bug 26922: Regression tests
[koha.git] / t / db_dependent / Log.t
blob3ce974bc695d9d75e502469e2dd120cc983499d9
1 #!/usr/bin/perl
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
8 # version.
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>.
17 use Modern::Perl;
18 use Test::More tests => 3;
20 use C4::Context;
21 use C4::Log;
22 use C4::Auth qw/checkpw/;
23 use Koha::Database;
24 use Koha::DateUtils;
25 use Koha::ActionLogs;
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;
33 our $dbh = C4::Context->dbh;
35 subtest 'Existing tests' => sub {
36 plan tests => 3;
38 my $success;
39 eval {
40 # FIXME: are we sure there is an member number 1?
41 logaction("MEMBERS","MODIFY",1,"test operation");
42 $success = 1;
43 } or do {
44 diag($@);
45 $success = 0;
47 ok($success, "logaction seemed to work");
49 # We want numbers to be the same between runs.
50 $dbh->do("DELETE FROM action_logs;");
52 t::lib::Mocks::mock_preference('CronjobLog',0);
53 cronlogaction();
54 my $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
55 is($cronJobCount,0,"Cronjob not logged as expected.");
57 t::lib::Mocks::mock_preference('CronjobLog',1);
58 cronlogaction();
59 $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
60 is($cronJobCount,1,"Cronjob logged as expected.");
63 subtest 'logaction(): interface is correctly logged' => sub {
65 plan tests => 4;
67 # No interface passed, using C4::Context->interface
68 $dbh->do("DELETE FROM action_logs;");
69 C4::Context->interface( 'commandline' );
70 logaction( "MEMBERS", "MODIFY", 1, "test operation");
71 my $log = Koha::ActionLogs->search->next;
72 is( $log->interface, 'commandline', 'Interface correctly deduced (commandline)');
74 # No interface passed, using C4::Context->interface
75 $dbh->do("DELETE FROM action_logs;");
76 C4::Context->interface( 'opac' );
77 logaction( "MEMBERS", "MODIFY", 1, "test operation");
78 $log = Koha::ActionLogs->search->next;
79 is( $log->interface, 'opac', 'Interface correctly deduced (opac)');
81 # Explicit interfaces
82 $dbh->do("DELETE FROM action_logs;");
83 C4::Context->interface( 'intranet' );
84 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'intranet');
85 $log = Koha::ActionLogs->search->next;
86 is( $log->interface, 'intranet', 'Passed interface is respected (intranet)');
88 # Explicit interfaces
89 $dbh->do("DELETE FROM action_logs;");
90 C4::Context->interface( 'sip' );
91 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'sip');
92 $log = Koha::ActionLogs->search->next;
93 is( $log->interface, 'sip', 'Passed interface is respected (sip)');
96 subtest 'GDPR logging' => sub {
97 plan tests => 6;
99 my $builder = t::lib::TestBuilder->new;
100 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
102 t::lib::Mocks::mock_userenv({ patron => $patron });
103 logaction( 'AUTH', 'FAILURE', $patron->id, '', 'opac' );
104 my $logs = Koha::ActionLogs->search(
106 user => $patron->id,
107 module => 'AUTH',
108 action => 'FAILURE',
109 object => $patron->id,
112 is( $logs->count, 1, 'We should find one auth failure' );
114 t::lib::Mocks::mock_preference('AuthFailureLog', 1);
115 my $strong_password = 'N0tStr0ngAnyM0reN0w:)';
116 $patron->set_password({ password => $strong_password });
117 my @ret = checkpw( $dbh, $patron->userid, 'WrongPassword', undef, undef, 1);
118 is( $ret[0], 0, 'Authentication failed' );
119 # Look for auth failure but NOT on patron id, pass userid in info parameter
120 $logs = Koha::ActionLogs->search(
122 module => 'AUTH',
123 action => 'FAILURE',
124 info => { -like => '%'.$patron->userid.'%' },
127 is( $logs->count, 1, 'We should find one auth failure with this userid' );
128 t::lib::Mocks::mock_preference('AuthFailureLog', 0);
129 @ret = checkpw( $dbh, $patron->userid, 'WrongPassword', undef, undef, 1);
130 $logs = Koha::ActionLogs->search(
132 module => 'AUTH',
133 action => 'FAILURE',
134 info => { -like => '%'.$patron->userid.'%' },
137 is( $logs->count, 1, 'Still only one failure with this userid' );
138 t::lib::Mocks::mock_preference('AuthSuccessLog', 1);
139 @ret = checkpw( $dbh, $patron->userid, $strong_password, undef, undef, 1);
140 is( $ret[0], 1, 'Authentication succeeded' );
141 # Now we can look for patron id
142 $logs = Koha::ActionLogs->search(
144 user => $patron->id,
145 module => 'AUTH',
146 action => 'SUCCESS',
147 object => $patron->id,
151 is( $logs->count, 1, 'We expect only one auth success line for this patron' );
154 $schema->storage->txn_rollback;