Line 0
Link Here
|
0 |
- |
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 |
# A mock response from C4::Log::GetLogs() |
31 |
my $logs = [ |
32 |
{ |
33 |
info => '{"log_origin": "core"}', |
34 |
action => 'STATUS_CHANGE', |
35 |
timestamp => 1538478742 |
36 |
}, |
37 |
{ |
38 |
info => '{"log_origin": "core"}', |
39 |
action => 'STATUS_CHANGE', |
40 |
timestamp => 1538478732 |
41 |
}, |
42 |
{ |
43 |
info => '{"log_origin": "core"}', |
44 |
action => 'STATUS_CHANGE', |
45 |
timestamp => 1538478752 |
46 |
} |
47 |
]; |
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/'); }); |
55 |
|
56 |
use_ok('Koha::Illrequest::Logger'); |
57 |
|
58 |
subtest 'Basics' => sub { |
59 |
|
60 |
plan tests => 10; |
61 |
|
62 |
$schema->storage->txn_begin; |
63 |
|
64 |
my $logger = Koha::Illrequest::Logger->new; |
65 |
|
66 |
# new() |
67 |
# |
68 |
ok( defined($logger), 'new() returned something' ); |
69 |
ok( $logger->isa('Koha::Illrequest::Logger'), |
70 |
'new() returns the correct object' ); |
71 |
is_deeply($logger->{data}, {modulename => 'ILL'}, |
72 |
'new() setting modulename'); |
73 |
|
74 |
# This is an incomplete data hashref, we use it to |
75 |
# test validation of the data before logging |
76 |
my $log_obj = { |
77 |
modulename => 'modulename', |
78 |
actionname => 'actionname', |
79 |
infos => 'infos' |
80 |
}; |
81 |
|
82 |
# set_data() |
83 |
# |
84 |
$logger->set_data($log_obj); |
85 |
is_deeply($logger->{data}->{actionname}, 'actionname', |
86 |
'set_data() setter works'); |
87 |
|
88 |
# log_something() |
89 |
# |
90 |
# Do we only log when the pref is set (currently unset) |
91 |
is($logger->log_something(), '', |
92 |
'logaction() not being called without pref being set'); |
93 |
|
94 |
# Set the pref |
95 |
t::lib::Mocks::mock_preference( 'IllLog', 1 ); |
96 |
# We should not log without all the required data, we are still |
97 |
# using the incomplete hashref |
98 |
is($logger->log_something(), '', |
99 |
'logaction() being called when data is incomplete'); |
100 |
|
101 |
# Fix the data hashref, then test that logging occurs |
102 |
$log_obj->{objectnumber} = 'objectnumber'; |
103 |
$logger->set_data($log_obj); |
104 |
is($logger->log_something(), 1, |
105 |
'logaction() being called when pref is set and data is complete'); |
106 |
|
107 |
# log_maybe() |
108 |
# |
109 |
is($logger->log_maybe({}, {}), '', |
110 |
'log_maybe() does not log with incomplete data'); |
111 |
|
112 |
# get_log_template() |
113 |
# |
114 |
is( |
115 |
$logger->get_log_template( |
116 |
{}, |
117 |
{ origin => 'core', 'action' => 'STATUS_CHANGE' } |
118 |
), |
119 |
'base/status_change.tt', |
120 |
'get_log_template() fetches correct core template' |
121 |
); |
122 |
|
123 |
# get_request_logs |
124 |
# |
125 |
my $res_obj = [ |
126 |
{ |
127 |
'template' => 'mock', |
128 |
'info' => { 'log_origin' => 'core' }, |
129 |
'timestamp' => 1538478752, |
130 |
'action' => 'STATUS_CHANGE' |
131 |
}, |
132 |
{ |
133 |
'template' => 'mock', |
134 |
'action' => 'STATUS_CHANGE', |
135 |
'timestamp' => 1538478742, |
136 |
'info' => { 'log_origin' => 'core' } |
137 |
}, |
138 |
{ |
139 |
'template' => 'mock', |
140 |
'action' => 'STATUS_CHANGE', |
141 |
'info' => { 'log_origin' => 'core' }, |
142 |
'timestamp' => 1538478732 |
143 |
} |
144 |
]; |
145 |
my $me_mock = Test::MockModule->new('Koha::Illrequest::Logger'); |
146 |
$me_mock->mock('get_log_template', sub { return 'mock'; }); |
147 |
my $mock_req = Test::MockObject->new(); |
148 |
$mock_req->mock('id', sub { 1 }); |
149 |
is_deeply($logger->get_request_logs($mock_req), $res_obj, |
150 |
'get_request_logs() returns logs property structured and sorted'); |
151 |
|
152 |
$schema->storage->txn_rollback; |
153 |
}; |
154 |
|
155 |
1; |