Bug 20750: Add unit tests
[koha.git] / t / db_dependent / Illrequest / Logger.t
blobe3b5670221651e89167af962b5fa9cf5f62c36a5
2 #!/usr/bin/perl
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
21 use Koha::Database;
23 use Test::More tests => 2;
24 use Test::MockModule;
25 use Test::MockObject;
26 use t::lib::Mocks;
28 my $schema = Koha::Database->new->schema;
30 # A mock response from C4::Log::GetLogs()
31 my $logs = [
32     {
33         info      => '{"log_origin": "core"}',
34         action    => 'STATUS_CHANGE',
35         timestamp => '2018-10-02 11:12:22'
36     },
37     {
38         info      => '{"log_origin": "core"}',
39         action    => 'STATUS_CHANGE',
40         timestamp => '2018-10-02 11:12:12'
41     },
42     {
43         info      => '{"log_origin": "core"}',
44         action    => 'STATUS_CHANGE',
45         timestamp => '2018-10-02 11:12:32'
46     }
48 # Mock the modules we use
49 my $c4_log = Test::MockModule->new('C4::Log');
50 $c4_log->mock('logaction', sub { 1 });
51 $c4_log->mock('GetLogs', sub { return $logs; });
52 my $c4_tpl = Test::MockModule->new('C4::Templates');
53 $c4_tpl->mock('_get_template_file',
54     sub { return ('htdocs', 'theme', 'lang', 'base/'); });
56 use_ok('Koha::Illrequest::Logger');
58 subtest 'Basics' => sub {
60     plan tests => 7;
62     $schema->storage->txn_begin;
64     my $logger = Koha::Illrequest::Logger->new;
66     # new()
67     #
68     ok( defined($logger), 'new() returned something' );
69     ok( $logger->isa('Koha::Illrequest::Logger'),
70         'new() returns the correct object' );
72     # This is an incomplete data hashref, we use it to
73     # test validation of the data before logging
74     my $log_obj = {
75         modulename   => 'modulename',
76         actionname   => 'actionname',
77         infos        => 'infos'
78     };
80     # log_something()
81     #
82     # Do we only log when the pref is set (currently unset)
83     is($logger->log_something(), '',
84         'logaction() not being called without pref being set');
86     # Set the pref
87     t::lib::Mocks::mock_preference( 'IllLog', 1 );
88     # We should not log without all the required data, we are still
89     # using the incomplete hashref
90     is($logger->log_something(), '',
91         'logaction() being called when data is incomplete');
93     # Fix the data hashref, then test that logging occurs
94     $log_obj->{objectnumber} = 'objectnumber';
95     is($logger->log_something($log_obj), 1,
96         'logaction() being called when pref is set and data is complete');
98     # log_maybe()
99     #
100     is($logger->log_maybe({}, {}), '',
101         'log_maybe() does not log with incomplete data');
103     # get_log_template()
104     #
105     is(
106         $logger->get_log_template({
107             request => {},
108             origin => 'core',
109             action => 'STATUS_CHANGE'
110         }),
111         'base/status_change.tt',
112         'get_log_template() fetches correct core template'
113     );
115     $schema->storage->txn_rollback;