Bug 15939: Action logs - Do not default dates to today
[koha.git] / t / db_dependent / Log.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2011 MJ Ray and software.coop
4 # This Koha test module is a stub!  
5 # Add more tests here!!!
6
7 use Modern::Perl;
8 use Test::More tests => 8;
9
10 use C4::Context;
11 use Koha::DateUtils;
12
13 use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
14 use Data::Dumper;
15
16 $| = 1;
17
18 BEGIN {
19         use_ok('C4::Log');
20 }
21 my $success;
22
23 eval {
24     # FIXME: are we sure there is an member number 1?
25     # FIXME: can we remove this log entry somehow?
26     logaction("MEMBERS","MODIFY",1,"test operation");
27     $success = 1;
28 } or do {
29     diag($@);
30     $success = 0;
31 };
32 ok($success, "logaction seemed to work");
33
34 eval {
35     # FIXME: US formatted date hardcoded into test for now
36     $success = scalar(@{GetLogs("","","",undef,undef,"","")});
37 } or do {
38     diag($@);
39     $success = 0;
40 };
41 ok($success, "GetLogs returns results for an open search");
42
43 eval {
44     # FIXME: US formatted date hardcoded into test for now
45     my $date = output_pref( { dt => dt_from_string, datenonly => 1, dateformat => 'iso' } );
46     $success = scalar(@{GetLogs( $date, $date, "", undef, undef, "", "") } );
47 } or do {
48     diag($@);
49     $success = 0;
50 };
51 ok($success, "GetLogs accepts dates in an All-matching search");
52
53 eval {
54     $success = scalar(@{GetLogs("","","",["MEMBERS"],["MODIFY"],1,"")});
55 } or do {
56     diag($@);
57     $success = 0;
58 };
59 ok($success, "GetLogs seemed to find ".$success." like our test record in a tighter search");
60
61 # Make sure we can rollback.
62 my $dbh = C4::Context->dbh;
63 $dbh->{AutoCommit} = 0;
64 $dbh->{RaiseError} = 1;
65
66 # We want numbers to be the same between runs.
67 $dbh->do("DELETE FROM action_logs;");
68
69 t::lib::Mocks::mock_preference('CronjobLog',0);
70 cronlogaction();
71 my $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
72 is($cronJobCount,0,"Cronjob not logged as expected.");
73
74 t::lib::Mocks::mock_preference('CronjobLog',1);
75 cronlogaction();
76 $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
77 is($cronJobCount,1,"Cronjob logged as expected.");
78
79 subtest "GetLogs should return all logs if dates are not set" => sub {
80     plan tests => 2;
81     my $today = dt_from_string->add(minutes => -1);
82     my $yesterday = dt_from_string->add( days => -1 );
83     $dbh->do(q|
84         INSERT INTO action_logs (timestamp, user, module, action, object, info)
85         VALUES
86         (?, 42, 'CATALOGUING', 'MODIFY', 4242, 'Record 42 has been modified by patron 4242 yesterday'),
87         (?, 43, 'CATALOGUING', 'MODIFY', 4242, 'Record 43 has been modified by patron 4242 today')
88     |, undef, output_pref({dt =>$yesterday, dateformat => 'iso'}), output_pref({dt => $today, dateformat => 'iso'}));
89     my $logs = GetLogs( undef, undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
90     is( scalar(@$logs), 2, 'GetLogs should return all logs regardless the dates' );
91     $logs = GetLogs( output_pref($today), undef, undef, ['CATALOGUING'], ['MODIFY'], 4242 );
92     is( scalar(@$logs), 1, 'GetLogs should return the logs for today' );
93 };
94
95 $dbh->rollback();