Bug 20144: [sql_modes] Fix date format in tests
[koha.git] / t / db_dependent / Illrequest / Config.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use t::lib::Mocks;
22 use t::lib::TestBuilder;
23 use Test::MockObject;
24 use Test::Exception;
25
26 use Test::More tests => 5;
27
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30 use_ok('Koha::Illrequest::Config');
31
32 my $base_limits = {
33     branch => { CPL => { count => 1, method => 'annual' } },
34     brw_cat => { A => { count => -1, method => 'active' } },
35     default => { count => 10, method => 'annual' },
36 };
37
38 my $base_censorship = { censor_notes_staff => 1, censor_reply_date => 1 };
39
40 subtest 'Basics' => sub {
41
42     plan tests => 19;
43
44     $schema->storage->txn_begin;
45
46     t::lib::Mocks::mock_config("interlibrary_loans", {});
47
48     my $config = Koha::Illrequest::Config->new;
49     isa_ok($config, "Koha::Illrequest::Config",
50            "Correctly create and load a config object.");
51
52     # backend:
53     is($config->backend, undef, "backend: Undefined backend is undefined.");
54     is($config->backend("Mock"), "Mock", "backend: setter works.");
55     is($config->backend, "Mock", "backend: setter is persistent.");
56
57     # backend_dir:
58     is($config->backend_dir, undef, "backend_dir: Undefined backend_dir is undefined.");
59     is($config->backend_dir("/tmp/"), "/tmp/", "backend_dir: setter works.");
60     is($config->backend_dir, "/tmp/", "backend_dir: setter is persistent.");
61
62     # partner_code:
63     is($config->partner_code, "ILLLIBS", "partner_code: Undefined partner_code is undefined.");
64     is($config->partner_code("ILLLIBSTST"), "ILLLIBSTST", "partner_code: setter works.");
65     is($config->partner_code, "ILLLIBSTST", "partner_code: setter is persistent.");
66
67     # limits:
68     is_deeply($config->limits, {}, "limits: Undefined limits is empty hash.");
69     is_deeply($config->limits($base_limits), $base_limits, "limits: setter works.");
70     is_deeply($config->limits, $base_limits, "limits: setter is persistent.");
71
72     # censorship:
73     is_deeply($config->censorship, { censor_notes_staff => 0, censor_reply_date => 0 },
74               "censorship: Undefined censorship is default values.");
75     is_deeply($config->censorship($base_censorship), $base_censorship, "censorship: setter works.");
76     is_deeply($config->censorship, $base_censorship, "censorship: setter is persistent.");
77
78     # getLimitRules
79     dies_ok( sub { $config->getLimitRules("FOO") }, "getLimitRules: die if not correct type.");
80     is_deeply($config->getLimitRules("brw_cat"), {
81         A => { count => -1, method => 'active' },
82         default => { count => 10, method => 'annual' },
83     }, "getLimitRules: fetch brw_cat limits.");
84     is_deeply($config->getLimitRules("branch"), {
85         CPL => { count => 1, method => 'annual' },
86         default => { count => 10, method => 'annual' },
87     }, "getLimitRules: fetch brw_cat limits.");
88
89     $schema->storage->txn_rollback;
90 };
91
92 # _load_unit_config:
93
94 subtest '_load_unit_config' => sub {
95
96     plan tests => 10;
97
98     $schema->storage->txn_begin;
99
100     my $config = Koha::Illrequest::Config->new;
101
102     dies_ok(
103         sub { Koha::Illrequest::Config::_load_unit_config({
104             id => 'durineadu', type => 'baz'
105         }) },
106         "_load_unit_config: die if ID is not default, and type is not branch or brw_cat."
107     );
108     is_deeply(
109         Koha::Illrequest::Config::_load_unit_config({
110             unit => {}, id => 'default', config => {}, test => 1
111         }), {}, "_load_unit_config: invocation without id returns unmodified config."
112     );
113
114     is_deeply(
115         Koha::Illrequest::Config::_load_unit_config({
116             unit => { api_key => 'foo', api_auth => 'bar' },
117             id => "CPL", type => 'branch', config => {}
118         }),
119         { credentials => { api_keys => { CPL => { api_key => 'foo', api_auth => 'bar' } } } },
120         "_load_unit_config: add auth values."
121     );
122
123     # Populate request_limits
124     is_deeply(
125         Koha::Illrequest::Config::_load_unit_config({
126             unit => { request_limit => [ 'heelo', 1234 ] },
127             id => "CPL", type => 'branch', config => {}
128         }), {}, "_load_unit_config: invalid request_limit structure."
129     );
130     is_deeply(
131         Koha::Illrequest::Config::_load_unit_config({
132             unit => { request_limit => { method => 'eudiren', count => '-5465' } },
133             id => "CPL", type => 'branch', config => {}
134         }), {}, "_load_unit_config: invalid method & count."
135     );
136     is_deeply(
137         Koha::Illrequest::Config::_load_unit_config({
138             unit => { request_limit => { method => 'annual', count => 6 } },
139             id => "default", config => {}
140         }),
141         { limits => { default => { method => 'annual', count => 6 } } },
142         "_load_unit_config: correct default request_limits."
143     );
144
145     # Populate prefix
146     is_deeply(
147         Koha::Illrequest::Config::_load_unit_config({
148             unit => { prefix => 'Foo-ill' },
149             id => "default", config => {}
150         }),
151         { prefixes => { default => 'Foo-ill' } },
152         "_load_unit_config: correct default prefix."
153     );
154     is_deeply(
155         Koha::Illrequest::Config::_load_unit_config({
156             unit => { prefix => 'Foo-ill' },
157             id => "A", config => {}, type => 'brw_cat'
158         }),
159         { prefixes => { brw_cat => { A => 'Foo-ill' } } },
160         "_load_unit_config: correct brw_cat prefix."
161     );
162
163     # Populate digital_recipient
164     is_deeply(
165         Koha::Illrequest::Config::_load_unit_config({
166             unit => { digital_recipient => 'borrower' },
167             id => "default", config => {}
168         }),
169         { digital_recipients => { default => 'borrower' } },
170         "_load_unit_config: correct default digital_recipient."
171     );
172     is_deeply(
173         Koha::Illrequest::Config::_load_unit_config({
174             unit => { digital_recipient => 'branch' },
175             id => "A", config => {}, type => 'brw_cat'
176         }),
177         { digital_recipients => { brw_cat => { A => 'branch' } } },
178         "_load_unit_config: correct brw_cat digital_recipient."
179     );
180
181     $schema->storage->txn_rollback;
182 };
183
184 # _load_configuration:
185
186 # We have already tested _load_unit_config, so we are reasonably confident
187 # that the per-branch, per-borrower_category & default sections parsing is
188 # good.
189 #
190 # Now we need to ensure that Arrays & Hashes are handled correctly, that
191 # censorship & ill partners are loaded correctly and that the backend
192 # directory is set correctly.
193
194 subtest '_load_configuration' => sub {
195
196     plan tests => 9;
197
198     $schema->storage->txn_begin;
199
200     my $config = Koha::Illrequest::Config->new;
201
202     # Return basic configuration
203     is_deeply(
204         Koha::Illrequest::Config::_load_configuration({}, 0),
205         {
206             backend_directory  => undef,
207             censorship         => {
208                 censor_notes_staff => 0,
209                 censor_reply_date => 0,
210             },
211             limits             => {},
212             digital_recipients => {},
213             prefixes           => {},
214             partner_code       => 'ILLLIBS',
215             raw_config         => {},
216         },
217         "load_configuration: return the base configuration."
218     );
219
220     # Return correct backend_dir
221     is_deeply(
222         Koha::Illrequest::Config::_load_configuration({ backend_directory => '/tmp/' }, 0),
223         {
224             backend_directory  => '/tmp/',
225             censorship         => {
226                 censor_notes_staff => 0,
227                 censor_reply_date => 0,
228             },
229             limits             => {},
230             digital_recipients => {},
231             prefixes           => {},
232             partner_code       => 'ILLLIBS',
233             raw_config         => { backend_directory => '/tmp/' },
234         },
235         "load_configuration: return the correct backend_dir."
236     );
237
238     # Map over branch configs
239     my $xml_config = {
240         backend_directory => '/tmp/',
241         branch => [
242             { code => '1', request_limit => { method => 'annual', count => 1 } },
243             { code => '2', prefix => '2-prefix' },
244             { code => '3', digital_recipient => 'branch' }
245         ]
246     };
247     is_deeply(
248         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
249         {
250             backend_directory  => '/tmp/',
251             censorship         => {
252                 censor_notes_staff => 0,
253                 censor_reply_date => 0,
254             },
255             limits             => { branch => { 1 => { method => 'annual', count => 1 } } },
256             digital_recipients => { branch => { 3 => 'branch' } },
257             prefixes           => { branch => { 2 => '2-prefix' } },
258             partner_code       => 'ILLLIBS',
259             raw_config         => $xml_config,
260         },
261         "load_configuration: multi branch config parsed correctly."
262     );
263     # Single branch config
264     $xml_config = {
265         backend_directory => '/tmp/',
266         branch => {
267             code => '1',
268             request_limit => { method => 'annual', count => 1 },
269             prefix => '2-prefix',
270             digital_recipient => 'branch',
271         }
272     };
273     is_deeply(
274         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
275         {
276             backend_directory  => '/tmp/',
277             censorship         => {
278                 censor_notes_staff => 0,
279                 censor_reply_date => 0,
280             },
281             limits             => { branch => { 1 => { method => 'annual', count => 1 } } },
282             digital_recipients => { branch => { 1 => 'branch' } },
283             prefixes           => { branch => { 1 => '2-prefix' } },
284             partner_code       => 'ILLLIBS',
285             raw_config         => $xml_config,
286         },
287         "load_configuration: single branch config parsed correctly."
288     );
289
290     # Map over borrower_category settings
291     $xml_config = {
292         backend_directory => '/tmp/',
293         borrower_category => [
294             { code => 'A', request_limit => { method => 'annual', count => 1 } },
295             { code => 'B', prefix => '2-prefix' },
296             { code => 'C', digital_recipient => 'branch' }
297         ]
298     };
299     is_deeply(
300         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
301         {
302             backend_directory  => '/tmp/',
303             censorship         => {
304                 censor_notes_staff => 0,
305                 censor_reply_date => 0,
306             },
307             limits             => { brw_cat => { A => { method => 'annual', count => 1 } } },
308             digital_recipients => { brw_cat => { C => 'branch' } },
309             prefixes           => { brw_cat => { B => '2-prefix' } },
310             partner_code       => 'ILLLIBS',
311             raw_config         => $xml_config,
312         },
313         "load_configuration: multi borrower_category config parsed correctly."
314     );
315     # Single borrower_category config
316     $xml_config = {
317         backend_directory => '/tmp/',
318         borrower_category => {
319             code => '1',
320             request_limit => { method => 'annual', count => 1 },
321             prefix => '2-prefix',
322             digital_recipient => 'branch',
323         }
324     };
325     is_deeply(
326         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
327         {
328             backend_directory  => '/tmp/',
329             censorship         => {
330                 censor_notes_staff => 0,
331                 censor_reply_date => 0,
332             },
333             limits             => { brw_cat => { 1 => { method => 'annual', count => 1 } } },
334             digital_recipients => { brw_cat => { 1 => 'branch' } },
335             prefixes           => { brw_cat => { 1 => '2-prefix' } },
336             partner_code       => 'ILLLIBS',
337             raw_config         => $xml_config,
338         },
339         "load_configuration: single borrower_category config parsed correctly."
340     );
341
342     # Default Configuration
343     $xml_config = {
344         backend_directory => '/tmp/',
345         request_limit => { method => 'annual', count => 1 },
346         prefix => '2-prefix',
347         digital_recipient => 'branch',
348     };
349     is_deeply(
350         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
351         {
352             backend_directory  => '/tmp/',
353             censorship         => {
354                 censor_notes_staff => 0,
355                 censor_reply_date => 0,
356             },
357             limits             => { default => { method => 'annual', count => 1 } },
358             digital_recipients => { default => 'branch' },
359             prefixes           => { default => '2-prefix' },
360             partner_code       => 'ILLLIBS',
361             raw_config         => $xml_config,
362         },
363         "load_configuration: parse the default configuration."
364     );
365
366     # Censorship
367     $xml_config = {
368         backend_directory => '/tmp/',
369         staff_request_comments => 'hide',
370         reply_date => 'hide'
371     };
372     is_deeply(
373         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
374         {
375             backend_directory  => '/tmp/',
376             censorship         => {
377                 censor_notes_staff => 1,
378                 censor_reply_date => 1,
379             },
380             limits             => {},
381             digital_recipients => {},
382             prefixes           => {},
383             partner_code       => 'ILLLIBS',
384             raw_config         => $xml_config,
385         },
386         "load_configuration: parse censorship settings configuration."
387     );
388
389     # Partner library category
390     is_deeply(
391         Koha::Illrequest::Config::_load_configuration({ partner_code => 'FOOBAR' }),
392         {
393             backend_directory  => undef,
394             censorship         => {
395                 censor_notes_staff => 0,
396                 censor_reply_date => 0,
397             },
398             limits             => {},
399             digital_recipients => {},
400             prefixes           => {},
401             partner_code       => 'FOOBAR',
402             raw_config         => { partner_code => 'FOOBAR' },
403         },
404         "load_configuration: Set partner code."
405     );
406
407     $schema->storage->txn_rollback;
408 };
409
410
411 subtest 'Final tests' => sub {
412
413     plan tests => 10;
414
415     $schema->storage->txn_begin;
416
417     t::lib::Mocks::mock_config("interlibrary_loans", {});
418
419     my $config = Koha::Illrequest::Config->new;
420
421     # getPrefixes (error & undef):
422     dies_ok( sub { $config->getPrefixes("FOO") }, "getPrefixes: die if not correct type.");
423     is_deeply($config->getPrefixes("brw_cat"), { default => undef},
424               "getPrefixes: Undefined brw_cat prefix is undefined.");
425     is_deeply($config->getPrefixes("branch"), { default => undef},
426               "getPrefixes: Undefined branch prefix is undefined.");
427
428     # getDigitalRecipients (error & undef):
429     dies_ok( sub { $config->getDigitalRecipients("FOO") },
430              "getDigitalRecipients: die if not correct type.");
431     is_deeply($config->getDigitalRecipients("brw_cat"), { default => undef},
432               "getDigitalRecipients: Undefined brw_cat dig rec is undefined.");
433     is_deeply($config->getDigitalRecipients("branch"), { default => undef},
434               "getDigitalRecipients: Undefined branch dig rec is undefined.");
435
436     $config->{configuration} = Koha::Illrequest::Config::_load_configuration({
437             backend_directory => '/tmp/',
438             prefix => 'DEFAULT-prefix',
439             digital_recipient => 'branch',
440             borrower_category => [
441                 { code => 'B', prefix => '2-prefix' },
442                 { code => 'C', digital_recipient => 'branch' }
443             ],
444             branch => [
445                 { code => '1', prefix => 'T-prefix' },
446                 { code => '2', digital_recipient => 'borrower' }
447             ]
448         }, 0
449     );
450
451     # getPrefixes (values):
452     is_deeply($config->getPrefixes("brw_cat"),
453               { B => '2-prefix', default => 'DEFAULT-prefix' },
454               "getPrefixes: return configuration brw_cat prefixes.");
455     is_deeply($config->getPrefixes("branch"),
456               { 1 => 'T-prefix', default => 'DEFAULT-prefix' },
457               "getPrefixes: return configuration branch prefixes.");
458
459     # getDigitalRecipients (values):
460     is_deeply($config->getDigitalRecipients("brw_cat"),
461               { C => 'branch', default => 'branch' },
462               "getDigitalRecipients: return brw_cat digital_recipients.");
463     is_deeply($config->getDigitalRecipients("branch"),
464               { 2 => 'borrower', default => 'branch' },
465               "getDigitalRecipients: return branch digital_recipients.");
466
467     $schema->storage->txn_rollback;
468 };
469
470
471 1;