Bug 18882: DBIC Schema changes
[koha.git] / t / db_dependent / Log.t
blobdbdf0e2fbff7a71b8eecf854d01180b7fb6131b1
1 #!/usr/bin/perl
3 # Copyright 2011 MJ Ray and software.coop
4 # This Koha test module is a stub!
5 # Add more tests here!!!
7 use Modern::Perl;
8 use Test::More tests => 10;
10 use C4::Context;
11 use Koha::Database;
12 use Koha::DateUtils;
14 use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
16 $| = 1;
18 BEGIN {
19 use_ok('C4::Log');
21 my $success;
23 # Make sure we can rollback.
24 my $schema = Koha::Database->new->schema;
25 $schema->storage->txn_begin;
26 my $dbh = C4::Context->dbh;
28 eval {
29 # FIXME: are we sure there is an member number 1?
30 logaction("MEMBERS","MODIFY",1,"test operation");
31 $success = 1;
32 } or do {
33 diag($@);
34 $success = 0;
36 ok($success, "logaction seemed to work");
38 eval {
39 # FIXME: US formatted date hardcoded into test for now
40 $success = scalar(@{GetLogs("","","",undef,undef,"","")});
41 } or do {
42 diag($@);
43 $success = 0;
45 ok($success, "GetLogs returns results for an open search");
47 eval {
48 # FIXME: US formatted date hardcoded into test for now
49 my $date = output_pref( { dt => dt_from_string, datenonly => 1, dateformat => 'iso' } );
50 $success = scalar(@{GetLogs( $date, $date, "", undef, undef, "", "") } );
51 } or do {
52 diag($@);
53 $success = 0;
55 ok($success, "GetLogs accepts dates in an All-matching search");
57 eval {
58 $success = scalar(@{GetLogs("","","",["MEMBERS"],["MODIFY"],1,"")});
59 } or do {
60 diag($@);
61 $success = 0;
63 ok($success, "GetLogs seemed to find ".$success." like our test record in a tighter search");
65 # We want numbers to be the same between runs.
66 $dbh->do("DELETE FROM action_logs;");
68 t::lib::Mocks::mock_preference('CronjobLog',0);
69 cronlogaction();
70 my $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
71 is($cronJobCount,0,"Cronjob not logged as expected.");
73 t::lib::Mocks::mock_preference('CronjobLog',1);
74 cronlogaction();
75 $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
76 is($cronJobCount,1,"Cronjob logged as expected.");
78 subtest "GetLogs should return all logs if dates are not set" => sub {
79 plan tests => 2;
80 my $today = dt_from_string->add(minutes => -1);
81 my $yesterday = dt_from_string->add( days => -1 );
82 $dbh->do(q|
83 INSERT INTO action_logs (timestamp, user, module, action, object, info)
84 VALUES
85 (?, 42, 'CATALOGUING', 'MODIFY', 4242, 'Record 42 has been modified by patron 4242 yesterday'),
86 (?, 43, 'CATALOGUING', 'MODIFY', 4242, 'Record 43 has been modified by patron 4242 today')
87 |, undef, output_pref({dt =>$yesterday, dateformat => 'iso'}), output_pref({dt => $today, dateformat => 'iso'}));
88 my $logs = GetLogs( undef, undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
89 is( scalar(@$logs), 2, 'GetLogs should return all logs regardless the dates' );
90 $logs = GetLogs( output_pref($today), undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
91 is( scalar(@$logs), 1, 'GetLogs should return the logs for today' );
94 subtest 'logaction(): interface is correctly logged' => sub {
96 plan tests => 4;
98 # No interface passed, using C4::Context->interface
99 $dbh->do("DELETE FROM action_logs;");
100 C4::Context->interface( 'commandline' );
101 logaction( "MEMBERS", "MODIFY", 1, "test operation");
102 my $logs = GetLogs();
103 is( @{$logs}[0]->{ interface }, 'commandline', 'Interface correctly deduced (commandline)');
105 # No interface passed, using C4::Context->interface
106 $dbh->do("DELETE FROM action_logs;");
107 C4::Context->interface( 'opac' );
108 logaction( "MEMBERS", "MODIFY", 1, "test operation");
109 $logs = GetLogs();
110 is( @{$logs}[0]->{ interface }, 'opac', 'Interface correctly deduced (opac)');
112 # Explicit interfaces
113 $dbh->do("DELETE FROM action_logs;");
114 C4::Context->interface( 'intranet' );
115 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'intranet');
116 $logs = GetLogs();
117 is( @{$logs}[0]->{ interface }, 'intranet', 'Passed interface is respected (intranet)');
119 # Explicit interfaces
120 $dbh->do("DELETE FROM action_logs;");
121 C4::Context->interface( 'sip' );
122 logaction( "MEMBERS", "MODIFY", 1, 'test info', 'sip');
123 $logs = GetLogs();
124 is( @{$logs}[0]->{ interface }, 'sip', 'Passed interface is respected (sip)');
127 subtest 'GetLogs() respects interface filters' => sub {
129 plan tests => 5;
131 $dbh->do("DELETE FROM action_logs;");
133 logaction( 'MEMBERS', 'MODIFY', 1, 'opac info', 'opac');
134 logaction( 'MEMBERS', 'MODIFY', 1, 'sip info', 'sip');
135 logaction( 'MEMBERS', 'MODIFY', 1, 'intranet info', 'intranet');
136 logaction( 'MEMBERS', 'MODIFY', 1, 'commandline info', 'commandline');
138 my $logs = scalar @{ GetLogs() };
139 is( $logs, 4, 'If no filter on interfaces is passed, all logs are returned');
141 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['opac']);
142 is( @{$logs}[0]->{ interface }, 'opac', 'Interface correctly filtered (opac)');
144 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['sip']);
145 is( @{$logs}[0]->{ interface }, 'sip', 'Interface correctly filtered (sip)');
147 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['intranet']);
148 is( @{$logs}[0]->{ interface }, 'intranet', 'Interface correctly filtered (intranet)');
150 $logs = GetLogs(undef,undef,undef,undef,undef,undef,undef,['commandline']);
151 is( @{$logs}[0]->{ interface }, 'commandline', 'Interface correctly filtered (commandline)');
154 $schema->storage->txn_rollback;