Bug 36508: Unit tests
[koha.git] / t / db_dependent / Log.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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>.
16
17 use Modern::Perl;
18 use Data::Dumper qw( Dumper );
19 use Test::More tests => 5;
20
21 use C4::Context;
22 use C4::Log qw( logaction cronlogaction );
23 use C4::Auth qw( checkpw );
24 use Koha::Database;
25 use Koha::ActionLogs;
26
27 use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
28 use t::lib::TestBuilder;
29
30 # Make sure we can rollback.
31 our $schema  = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 subtest 'Existing tests' => sub {
35     plan tests => 3;
36
37     my $success;
38     eval {
39         # FIXME: are we sure there is an member number 1?
40         logaction("MEMBERS","MODIFY",1,"test operation");
41         $success = 1;
42     } or do {
43         diag($@);
44         $success = 0;
45     };
46     ok($success, "logaction seemed to work");
47
48     # We want numbers to be the same between runs.
49     Koha::ActionLogs->search->delete;
50
51     t::lib::Mocks::mock_preference('CronjobLog',0);
52     cronlogaction();
53     is(Koha::ActionLogs->search({ module => 'CRONJOBS' })->count,0,"Cronjob not logged as expected.");
54
55     t::lib::Mocks::mock_preference('CronjobLog',1);
56     cronlogaction();
57     is(Koha::ActionLogs->search({ module => 'CRONJOBS' })->count,1,"Cronjob logged as expected.");
58 };
59
60 subtest 'logaction(): interface is correctly logged' => sub {
61
62     plan tests => 4;
63
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)');
70
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)');
77
78     # Explicit interfaces
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)');
84
85     # Explicit interfaces
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)');
91 };
92
93 subtest 'logaction / trace' => sub {
94     plan tests => 2;
95
96     C4::Context->interface( 'intranet' );
97     t::lib::Mocks::mock_preference('ActionLogsTraceDepth',0);
98
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' );
104
105     t::lib::Mocks::mock_preference('ActionLogsTraceDepth',0);
106 };
107
108 subtest 'GDPR logging' => sub {
109     plan tests => 6;
110
111     my $builder = t::lib::TestBuilder->new;
112     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
113
114     t::lib::Mocks::mock_userenv({ patron => $patron });
115     logaction( 'AUTH', 'FAILURE', $patron->id, '', 'opac' );
116     my $logs = Koha::ActionLogs->search(
117         {
118             user   => $patron->id,
119             module => 'AUTH',
120             action => 'FAILURE',
121             object => $patron->id,
122         }
123     );
124     is( $logs->count, 1, 'We should find one auth failure' );
125
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(
133         {
134             module => 'AUTH',
135             action => 'FAILURE',
136             info   => { -like => '%'.$patron->userid.'%' },
137         }
138     );
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(
143         {
144             module => 'AUTH',
145             action => 'FAILURE',
146             info   => { -like => '%'.$patron->userid.'%' },
147         }
148     );
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(
155         {
156             user   => $patron->id,
157             module => 'AUTH',
158             action => 'SUCCESS',
159             object => $patron->id,
160         }
161     );
162
163     is( $logs->count, 1, 'We expect only one auth success line for this patron' );
164 };
165
166 subtest 'Reduce log size by unblessing Koha objects' => sub {
167     plan tests => 7;
168
169     my $builder = t::lib::TestBuilder->new;
170     my $item = $builder->build_sample_item;
171
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' );
177
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' );
182
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' );
187
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' );
191
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' );
195 };
196
197 $schema->storage->txn_rollback;