Bug 23830: Make Koha::AuthorisedValues use Koha::Objects::Limit::Library
[koha.git] / t / db_dependent / Illrequest / Logger.t
1
2 #!/usr/bin/perl
3
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19 use Modern::Perl;
20
21 use Koha::Database;
22
23 use Test::More tests => 2;
24 use Test::MockModule;
25 use Test::MockObject;
26 use t::lib::Mocks;
27
28 my $schema = Koha::Database->new->schema;
29
30 # Mock the modules we use
31 my $c4_log = Test::MockModule->new('C4::Log');
32 $c4_log->mock('logaction', sub { 1 });
33 my $c4_tpl = Test::MockModule->new('C4::Templates');
34 $c4_tpl->mock('_get_template_file',
35     sub { return ('htdocs', 'theme', 'lang', 'base/'); });
36
37 use_ok('Koha::Illrequest::Logger');
38
39 subtest 'Basics' => sub {
40
41     plan tests => 7;
42
43     $schema->storage->txn_begin;
44
45     my $logger = Koha::Illrequest::Logger->new;
46
47     # new()
48     #
49     ok( defined($logger), 'new() returned something' );
50     ok( $logger->isa('Koha::Illrequest::Logger'),
51         'new() returns the correct object' );
52
53     # This is an incomplete data hashref, we use it to
54     # test validation of the data before logging
55     my $log_obj = {
56         modulename   => 'modulename',
57         actionname   => 'actionname',
58         infos        => 'infos'
59     };
60
61     # log_something()
62     #
63     # Do we only log when the pref is set (currently unset)
64     is($logger->log_something(), '',
65         'logaction() not being called without pref being set');
66
67     # Set the pref
68     t::lib::Mocks::mock_preference( 'IllLog', 1 );
69     # We should not log without all the required data, we are still
70     # using the incomplete hashref
71     is($logger->log_something(), '',
72         'logaction() being called when data is incomplete');
73
74     # Fix the data hashref, then test that logging occurs
75     $log_obj->{objectnumber} = 'objectnumber';
76     is($logger->log_something($log_obj), 1,
77         'logaction() being called when pref is set and data is complete');
78
79     # log_maybe()
80     #
81     is($logger->log_maybe({}, {}), '',
82         'log_maybe() does not log with incomplete data');
83
84     # get_log_template()
85     #
86     is(
87         $logger->get_log_template({
88             request => {},
89             origin => 'core',
90             action => 'STATUS_CHANGE'
91         }),
92         'base/status_change.tt',
93         'get_log_template() fetches correct core template'
94     );
95
96     $schema->storage->txn_rollback;
97 };
98
99 1;