3 # This file is part of Koha.
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.
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.
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>.
22 use t::lib::TestBuilder;
26 use Test::More tests => 5;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30 use_ok('Koha::Illrequest::Config');
33 branch => { CPL => { count => 1, method => 'annual' } },
34 brw_cat => { A => { count => -1, method => 'active' } },
35 default => { count => 10, method => 'annual' },
38 my $base_censorship = { censor_notes_staff => 1, censor_reply_date => 1 };
40 subtest 'Basics' => sub {
44 $schema->storage->txn_begin;
46 t::lib::Mocks::mock_config("interlibrary_loans", {});
47 t::lib::Mocks::mock_preference('ILLPartnerCode', undef);
49 my $config = Koha::Illrequest::Config->new;
50 isa_ok($config, "Koha::Illrequest::Config",
51 "Correctly create and load a config object.");
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.");
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.");
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.");
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.");
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.");
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.");
90 $schema->storage->txn_rollback;
95 subtest '_load_unit_config' => sub {
99 $schema->storage->txn_begin;
101 my $config = Koha::Illrequest::Config->new;
104 sub { Koha::Illrequest::Config::_load_unit_config({
105 id => 'durineadu', type => 'baz'
107 "_load_unit_config: die if ID is not default, and type is not branch or brw_cat."
110 Koha::Illrequest::Config::_load_unit_config({
111 unit => {}, id => 'default', config => {}, test => 1
112 }), {}, "_load_unit_config: invocation without id returns unmodified config."
116 Koha::Illrequest::Config::_load_unit_config({
117 unit => { api_key => 'foo', api_auth => 'bar' },
118 id => "CPL", type => 'branch', config => {}
120 { credentials => { api_keys => { CPL => { api_key => 'foo', api_auth => 'bar' } } } },
121 "_load_unit_config: add auth values."
124 # Populate request_limits
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."
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."
138 Koha::Illrequest::Config::_load_unit_config({
139 unit => { request_limit => { method => 'annual', count => 6 } },
140 id => "default", config => {}
142 { limits => { default => { method => 'annual', count => 6 } } },
143 "_load_unit_config: correct default request_limits."
148 Koha::Illrequest::Config::_load_unit_config({
149 unit => { prefix => 'Foo-ill' },
150 id => "default", config => {}
152 { prefixes => { default => 'Foo-ill' } },
153 "_load_unit_config: correct default prefix."
156 Koha::Illrequest::Config::_load_unit_config({
157 unit => { prefix => 'Foo-ill' },
158 id => "A", config => {}, type => 'brw_cat'
160 { prefixes => { brw_cat => { A => 'Foo-ill' } } },
161 "_load_unit_config: correct brw_cat prefix."
164 # Populate digital_recipient
166 Koha::Illrequest::Config::_load_unit_config({
167 unit => { digital_recipient => 'borrower' },
168 id => "default", config => {}
170 { digital_recipients => { default => 'borrower' } },
171 "_load_unit_config: correct default digital_recipient."
174 Koha::Illrequest::Config::_load_unit_config({
175 unit => { digital_recipient => 'branch' },
176 id => "A", config => {}, type => 'brw_cat'
178 { digital_recipients => { brw_cat => { A => 'branch' } } },
179 "_load_unit_config: correct brw_cat digital_recipient."
182 $schema->storage->txn_rollback;
185 # _load_configuration:
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
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.
195 subtest '_load_configuration' => sub {
199 $schema->storage->txn_begin;
201 my $config = Koha::Illrequest::Config->new;
202 t::lib::Mocks::mock_preference('ILLPartnerCode', 'IL');
204 # Return basic configuration
206 Koha::Illrequest::Config::_load_configuration({}, 0),
208 backend_directory => undef,
210 censor_notes_staff => 0,
211 censor_reply_date => 0,
214 digital_recipients => {},
216 partner_code => 'IL',
219 "load_configuration: return the base configuration."
222 # Return correct backend_dir
224 Koha::Illrequest::Config::_load_configuration({ backend_directory => '/tmp/' }, 0),
226 backend_directory => '/tmp/',
228 censor_notes_staff => 0,
229 censor_reply_date => 0,
232 digital_recipients => {},
234 partner_code => 'IL',
235 raw_config => { backend_directory => '/tmp/' },
237 "load_configuration: return the correct backend_dir."
240 # Map over branch configs
242 backend_directory => '/tmp/',
244 { code => '1', request_limit => { method => 'annual', count => 1 } },
245 { code => '2', prefix => '2-prefix' },
246 { code => '3', digital_recipient => 'branch' }
250 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
252 backend_directory => '/tmp/',
254 censor_notes_staff => 0,
255 censor_reply_date => 0,
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,
263 "load_configuration: multi branch config parsed correctly."
265 # Single branch config
267 backend_directory => '/tmp/',
270 request_limit => { method => 'annual', count => 1 },
271 prefix => '2-prefix',
272 digital_recipient => 'branch',
276 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
278 backend_directory => '/tmp/',
280 censor_notes_staff => 0,
281 censor_reply_date => 0,
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,
289 "load_configuration: single branch config parsed correctly."
292 # Map over borrower_category settings
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' }
302 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
304 backend_directory => '/tmp/',
306 censor_notes_staff => 0,
307 censor_reply_date => 0,
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,
315 "load_configuration: multi borrower_category config parsed correctly."
317 # Single borrower_category config
319 backend_directory => '/tmp/',
320 borrower_category => {
322 request_limit => { method => 'annual', count => 1 },
323 prefix => '2-prefix',
324 digital_recipient => 'branch',
328 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
330 backend_directory => '/tmp/',
332 censor_notes_staff => 0,
333 censor_reply_date => 0,
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,
341 "load_configuration: single borrower_category config parsed correctly."
344 # Default Configuration
346 backend_directory => '/tmp/',
347 request_limit => { method => 'annual', count => 1 },
348 prefix => '2-prefix',
349 digital_recipient => 'branch',
352 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
354 backend_directory => '/tmp/',
356 censor_notes_staff => 0,
357 censor_reply_date => 0,
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,
365 "load_configuration: parse the default configuration."
370 backend_directory => '/tmp/',
371 staff_request_comments => 'hide',
375 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
377 backend_directory => '/tmp/',
379 censor_notes_staff => 1,
380 censor_reply_date => 1,
383 digital_recipients => {},
385 partner_code => 'IL',
386 raw_config => $xml_config,
388 "load_configuration: parse censorship settings configuration."
391 # Partner library category
393 Koha::Illrequest::Config::_load_configuration( { partner_code => 'FOOBAR' } ),
395 backend_directory => undef,
397 censor_notes_staff => 0,
398 censor_reply_date => 0,
401 digital_recipients => {},
403 partner_code => 'IL',
404 raw_config => { partner_code => 'FOOBAR' },
406 q{'partner_code' not read from the config file, default value 'IL' used instead}
409 t::lib::Mocks::mock_preference( 'ILLPartnerCode', 'FOOBAR' );
412 Koha::Illrequest::Config::_load_configuration(),
414 backend_directory => undef,
416 censor_notes_staff => 0,
417 censor_reply_date => 0,
420 digital_recipients => {},
422 partner_code => 'FOOBAR',
425 q{'ILLPartnerCode' takes precedence over default value for 'partner_code'}
428 $schema->storage->txn_rollback;
432 subtest 'Final tests' => sub {
436 $schema->storage->txn_begin;
438 t::lib::Mocks::mock_config("interlibrary_loans", {});
440 my $config = Koha::Illrequest::Config->new;
442 # getPrefixes (error & undef):
443 is($config->getPrefixes(), undef,
444 "getPrefixes: Undefined branch prefix is undefined.");
446 is($config->has_branch(), undef, "Config does not have branch when no config loaded");
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.");
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' }
465 { code => '1', prefix => 'T-prefix' },
466 { code => '2', digital_recipient => 'borrower' }
471 # getPrefixes (values):
472 is_deeply($config->getPrefixes(),
473 { '1' => 'T-prefix' },
474 "getPrefixes: return configuration branch prefixes.");
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.");
485 ok($config->has_branch(), "has_branch returns true if branch defined in configuration");
487 $schema->storage->txn_rollback;