Bug 34842: Fix Illrequest/Config.t if DB upgraded twice
[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     t::lib::Mocks::mock_preference('ILLPartnerCode', undef);
48
49     my $config = Koha::Illrequest::Config->new;
50     isa_ok($config, "Koha::Illrequest::Config",
51            "Correctly create and load a config object.");
52
53     # backend:
54     is($config->backend, undef, "backend: Undefined backend is undefined.");
55     is($config->backend("Mock"), "Mock", "backend: setter works.");
56     is($config->backend, "Mock", "backend: setter is persistent.");
57
58     # backend_dir:
59     is($config->backend_dir, undef, "backend_dir: Undefined backend_dir is undefined.");
60     is($config->backend_dir("/tmp/"), "/tmp/", "backend_dir: setter works.");
61     is($config->backend_dir, "/tmp/", "backend_dir: setter is persistent.");
62
63     # partner_code:
64     is($config->partner_code, 'IL', "partner_code: Undefined partner_code fallback to 'IL'.");
65     is($config->partner_code("ILTST"), "ILTST", "partner_code: setter works.");
66     is($config->partner_code, "ILTST", "partner_code: setter is persistent.");
67
68     # limits:
69     is_deeply($config->limits, {}, "limits: Undefined limits is empty hash.");
70     is_deeply($config->limits($base_limits), $base_limits, "limits: setter works.");
71     is_deeply($config->limits, $base_limits, "limits: setter is persistent.");
72
73     # censorship:
74     is_deeply($config->censorship, { censor_notes_staff => 0, censor_reply_date => 0 },
75               "censorship: Undefined censorship is default values.");
76     is_deeply($config->censorship($base_censorship), $base_censorship, "censorship: setter works.");
77     is_deeply($config->censorship, $base_censorship, "censorship: setter is persistent.");
78
79     # getLimitRules
80     dies_ok( sub { $config->getLimitRules("FOO") }, "getLimitRules: die if not correct type.");
81     is_deeply($config->getLimitRules("brw_cat"), {
82         A => { count => -1, method => 'active' },
83         default => { count => 10, method => 'annual' },
84     }, "getLimitRules: fetch brw_cat limits.");
85     is_deeply($config->getLimitRules("branch"), {
86         CPL => { count => 1, method => 'annual' },
87         default => { count => 10, method => 'annual' },
88     }, "getLimitRules: fetch brw_cat limits.");
89
90     $schema->storage->txn_rollback;
91 };
92
93 # _load_unit_config:
94
95 subtest '_load_unit_config' => sub {
96
97     plan tests => 10;
98
99     $schema->storage->txn_begin;
100
101     my $config = Koha::Illrequest::Config->new;
102
103     dies_ok(
104         sub { Koha::Illrequest::Config::_load_unit_config({
105             id => 'durineadu', type => 'baz'
106         }) },
107         "_load_unit_config: die if ID is not default, and type is not branch or brw_cat."
108     );
109     is_deeply(
110         Koha::Illrequest::Config::_load_unit_config({
111             unit => {}, id => 'default', config => {}, test => 1
112         }), {}, "_load_unit_config: invocation without id returns unmodified config."
113     );
114
115     is_deeply(
116         Koha::Illrequest::Config::_load_unit_config({
117             unit => { api_key => 'foo', api_auth => 'bar' },
118             id => "CPL", type => 'branch', config => {}
119         }),
120         { credentials => { api_keys => { CPL => { api_key => 'foo', api_auth => 'bar' } } } },
121         "_load_unit_config: add auth values."
122     );
123
124     # Populate request_limits
125     is_deeply(
126         Koha::Illrequest::Config::_load_unit_config({
127             unit => { request_limit => [ 'heelo', 1234 ] },
128             id => "CPL", type => 'branch', config => {}
129         }), {}, "_load_unit_config: invalid request_limit structure."
130     );
131     is_deeply(
132         Koha::Illrequest::Config::_load_unit_config({
133             unit => { request_limit => { method => 'eudiren', count => '-5465' } },
134             id => "CPL", type => 'branch', config => {}
135         }), {}, "_load_unit_config: invalid method & count."
136     );
137     is_deeply(
138         Koha::Illrequest::Config::_load_unit_config({
139             unit => { request_limit => { method => 'annual', count => 6 } },
140             id => "default", config => {}
141         }),
142         { limits => { default => { method => 'annual', count => 6 } } },
143         "_load_unit_config: correct default request_limits."
144     );
145
146     # Populate prefix
147     is_deeply(
148         Koha::Illrequest::Config::_load_unit_config({
149             unit => { prefix => 'Foo-ill' },
150             id => "default", config => {}
151         }),
152         { prefixes => { default => 'Foo-ill' } },
153         "_load_unit_config: correct default prefix."
154     );
155     is_deeply(
156         Koha::Illrequest::Config::_load_unit_config({
157             unit => { prefix => 'Foo-ill' },
158             id => "A", config => {}, type => 'brw_cat'
159         }),
160         { prefixes => { brw_cat => { A => 'Foo-ill' } } },
161         "_load_unit_config: correct brw_cat prefix."
162     );
163
164     # Populate digital_recipient
165     is_deeply(
166         Koha::Illrequest::Config::_load_unit_config({
167             unit => { digital_recipient => 'borrower' },
168             id => "default", config => {}
169         }),
170         { digital_recipients => { default => 'borrower' } },
171         "_load_unit_config: correct default digital_recipient."
172     );
173     is_deeply(
174         Koha::Illrequest::Config::_load_unit_config({
175             unit => { digital_recipient => 'branch' },
176             id => "A", config => {}, type => 'brw_cat'
177         }),
178         { digital_recipients => { brw_cat => { A => 'branch' } } },
179         "_load_unit_config: correct brw_cat digital_recipient."
180     );
181
182     $schema->storage->txn_rollback;
183 };
184
185 # _load_configuration:
186
187 # We have already tested _load_unit_config, so we are reasonably confident
188 # that the per-branch, per-borrower_category & default sections parsing is
189 # good.
190 #
191 # Now we need to ensure that Arrays & Hashes are handled correctly, that
192 # censorship & ill partners are loaded correctly and that the backend
193 # directory is set correctly.
194
195 subtest '_load_configuration' => sub {
196
197     plan tests => 10;
198
199     $schema->storage->txn_begin;
200
201     my $config = Koha::Illrequest::Config->new;
202     t::lib::Mocks::mock_preference('ILLPartnerCode', 'IL');
203
204     # Return basic configuration
205     is_deeply(
206         Koha::Illrequest::Config::_load_configuration({}, 0),
207         {
208             backend_directory  => undef,
209             censorship         => {
210                 censor_notes_staff => 0,
211                 censor_reply_date => 0,
212             },
213             limits             => {},
214             digital_recipients => {},
215             prefixes           => {},
216             partner_code       => 'IL',
217             raw_config         => {},
218         },
219         "load_configuration: return the base configuration."
220     );
221
222     # Return correct backend_dir
223     is_deeply(
224         Koha::Illrequest::Config::_load_configuration({ backend_directory => '/tmp/' }, 0),
225         {
226             backend_directory  => '/tmp/',
227             censorship         => {
228                 censor_notes_staff => 0,
229                 censor_reply_date => 0,
230             },
231             limits             => {},
232             digital_recipients => {},
233             prefixes           => {},
234             partner_code       => 'IL',
235             raw_config         => { backend_directory => '/tmp/' },
236         },
237         "load_configuration: return the correct backend_dir."
238     );
239
240     # Map over branch configs
241     my $xml_config = {
242         backend_directory => '/tmp/',
243         branch => [
244             { code => '1', request_limit => { method => 'annual', count => 1 } },
245             { code => '2', prefix => '2-prefix' },
246             { code => '3', digital_recipient => 'branch' }
247         ]
248     };
249     is_deeply(
250         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
251         {
252             backend_directory  => '/tmp/',
253             censorship         => {
254                 censor_notes_staff => 0,
255                 censor_reply_date => 0,
256             },
257             limits             => { branch => { 1 => { method => 'annual', count => 1 } } },
258             digital_recipients => { branch => { 3 => 'branch' } },
259             prefixes           => { branch => { 2 => '2-prefix' } },
260             partner_code       => 'IL',
261             raw_config         => $xml_config,
262         },
263         "load_configuration: multi branch config parsed correctly."
264     );
265     # Single branch config
266     $xml_config = {
267         backend_directory => '/tmp/',
268         branch => {
269             code => '1',
270             request_limit => { method => 'annual', count => 1 },
271             prefix => '2-prefix',
272             digital_recipient => 'branch',
273         }
274     };
275     is_deeply(
276         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
277         {
278             backend_directory  => '/tmp/',
279             censorship         => {
280                 censor_notes_staff => 0,
281                 censor_reply_date => 0,
282             },
283             limits             => { branch => { 1 => { method => 'annual', count => 1 } } },
284             digital_recipients => { branch => { 1 => 'branch' } },
285             prefixes           => { branch => { 1 => '2-prefix' } },
286             partner_code       => 'IL',
287             raw_config         => $xml_config,
288         },
289         "load_configuration: single branch config parsed correctly."
290     );
291
292     # Map over borrower_category settings
293     $xml_config = {
294         backend_directory => '/tmp/',
295         borrower_category => [
296             { code => 'A', request_limit => { method => 'annual', count => 1 } },
297             { code => 'B', prefix => '2-prefix' },
298             { code => 'C', digital_recipient => 'branch' }
299         ]
300     };
301     is_deeply(
302         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
303         {
304             backend_directory  => '/tmp/',
305             censorship         => {
306                 censor_notes_staff => 0,
307                 censor_reply_date => 0,
308             },
309             limits             => { brw_cat => { A => { method => 'annual', count => 1 } } },
310             digital_recipients => { brw_cat => { C => 'branch' } },
311             prefixes           => { brw_cat => { B => '2-prefix' } },
312             partner_code       => 'IL',
313             raw_config         => $xml_config,
314         },
315         "load_configuration: multi borrower_category config parsed correctly."
316     );
317     # Single borrower_category config
318     $xml_config = {
319         backend_directory => '/tmp/',
320         borrower_category => {
321             code => '1',
322             request_limit => { method => 'annual', count => 1 },
323             prefix => '2-prefix',
324             digital_recipient => 'branch',
325         }
326     };
327     is_deeply(
328         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
329         {
330             backend_directory  => '/tmp/',
331             censorship         => {
332                 censor_notes_staff => 0,
333                 censor_reply_date => 0,
334             },
335             limits             => { brw_cat => { 1 => { method => 'annual', count => 1 } } },
336             digital_recipients => { brw_cat => { 1 => 'branch' } },
337             prefixes           => { brw_cat => { 1 => '2-prefix' } },
338             partner_code       => 'IL',
339             raw_config         => $xml_config,
340         },
341         "load_configuration: single borrower_category config parsed correctly."
342     );
343
344     # Default Configuration
345     $xml_config = {
346         backend_directory => '/tmp/',
347         request_limit => { method => 'annual', count => 1 },
348         prefix => '2-prefix',
349         digital_recipient => 'branch',
350     };
351     is_deeply(
352         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
353         {
354             backend_directory  => '/tmp/',
355             censorship         => {
356                 censor_notes_staff => 0,
357                 censor_reply_date => 0,
358             },
359             limits             => { default => { method => 'annual', count => 1 } },
360             digital_recipients => { default => 'branch' },
361             prefixes           => { default => '2-prefix' },
362             partner_code       => 'IL',
363             raw_config         => $xml_config,
364         },
365         "load_configuration: parse the default configuration."
366     );
367
368     # Censorship
369     $xml_config = {
370         backend_directory => '/tmp/',
371         staff_request_comments => 'hide',
372         reply_date => 'hide'
373     };
374     is_deeply(
375         Koha::Illrequest::Config::_load_configuration($xml_config, 0),
376         {
377             backend_directory  => '/tmp/',
378             censorship         => {
379                 censor_notes_staff => 1,
380                 censor_reply_date => 1,
381             },
382             limits             => {},
383             digital_recipients => {},
384             prefixes           => {},
385             partner_code       => 'IL',
386             raw_config         => $xml_config,
387         },
388         "load_configuration: parse censorship settings configuration."
389     );
390
391     # Partner library category
392     is_deeply(
393         Koha::Illrequest::Config::_load_configuration( { partner_code => 'FOOBAR' } ),
394         {
395             backend_directory => undef,
396             censorship        => {
397                 censor_notes_staff => 0,
398                 censor_reply_date  => 0,
399             },
400             limits             => {},
401             digital_recipients => {},
402             prefixes           => {},
403             partner_code       => 'IL',
404             raw_config         => { partner_code => 'FOOBAR' },
405         },
406         q{'partner_code' not read from the config file, default value 'IL' used instead}
407     );
408
409     t::lib::Mocks::mock_preference( 'ILLPartnerCode', 'FOOBAR' );
410
411     is_deeply(
412         Koha::Illrequest::Config::_load_configuration(),
413         {
414             backend_directory => undef,
415             censorship        => {
416                 censor_notes_staff => 0,
417                 censor_reply_date  => 0,
418             },
419             limits             => {},
420             digital_recipients => {},
421             prefixes           => {},
422             partner_code       => 'FOOBAR',
423             raw_config         => {},
424         },
425         q{'ILLPartnerCode' takes precedence over default value for 'partner_code'}
426     );
427
428     $schema->storage->txn_rollback;
429 };
430
431
432 subtest 'Final tests' => sub {
433
434     plan tests => 9;
435
436     $schema->storage->txn_begin;
437
438     t::lib::Mocks::mock_config("interlibrary_loans", {});
439
440     my $config = Koha::Illrequest::Config->new;
441
442     # getPrefixes (error & undef):
443     is($config->getPrefixes(), undef,
444               "getPrefixes: Undefined branch prefix is undefined.");
445
446     is($config->has_branch(), undef, "Config does not have branch when no config loaded");
447
448     # getDigitalRecipients (error & undef):
449     dies_ok( sub { $config->getDigitalRecipients("FOO") },
450              "getDigitalRecipients: die if not correct type.");
451     is_deeply($config->getDigitalRecipients("brw_cat"), { default => undef},
452               "getDigitalRecipients: Undefined brw_cat dig rec is undefined.");
453     is_deeply($config->getDigitalRecipients("branch"), { default => undef},
454               "getDigitalRecipients: Undefined branch dig rec is undefined.");
455
456     $config->{configuration} = Koha::Illrequest::Config::_load_configuration({
457             backend_directory => '/tmp/',
458             prefix => 'DEFAULT-prefix',
459             digital_recipient => 'branch',
460             borrower_category => [
461                 { code => 'B', prefix => '2-prefix' },
462                 { code => 'C', digital_recipient => 'branch' }
463             ],
464             branch => [
465                 { code => '1', prefix => 'T-prefix' },
466                 { code => '2', digital_recipient => 'borrower' }
467             ]
468         }, 0
469     );
470
471     # getPrefixes (values):
472     is_deeply($config->getPrefixes(),
473               { '1' => 'T-prefix' },
474               "getPrefixes: return configuration branch prefixes.");
475
476     # getDigitalRecipients (values):
477     is_deeply($config->getDigitalRecipients("brw_cat"),
478               { C => 'branch', default => 'branch' },
479               "getDigitalRecipients: return brw_cat digital_recipients.");
480     is_deeply($config->getDigitalRecipients("branch"),
481               { 2 => 'borrower', default => 'branch' },
482               "getDigitalRecipients: return branch digital_recipients.");
483
484     # has_branch test
485     ok($config->has_branch(), "has_branch returns true if branch defined in configuration");
486
487     $schema->storage->txn_rollback;
488 };
489
490
491 1;