Bug 19893: Support for joined subfields in mappings
[koha.git] / t / db_dependent / Illrequest / Config.t
blobb80babdc8a64049fc8ce95dad7c1cbc9e70bced1
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Koha::Database;
21 use t::lib::Mocks;
22 use t::lib::TestBuilder;
23 use Test::MockObject;
24 use Test::Exception;
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');
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' },
38 my $base_censorship = { censor_notes_staff => 1, censor_reply_date => 1 };
40 subtest 'Basics' => sub {
42 plan tests => 19;
44 $schema->storage->txn_begin;
46 t::lib::Mocks::mock_config("interlibrary_loans", {});
48 my $config = Koha::Illrequest::Config->new;
49 isa_ok($config, "Koha::Illrequest::Config",
50 "Correctly create and load a config object.");
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.");
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.");
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.");
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.");
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.");
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.");
89 $schema->storage->txn_rollback;
92 # _load_unit_config:
94 subtest '_load_unit_config' => sub {
96 plan tests => 10;
98 $schema->storage->txn_begin;
100 my $config = Koha::Illrequest::Config->new;
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."
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."
114 is_deeply(
115 Koha::Illrequest::Config::_load_unit_config({
116 unit => { api_key => 'foo', api_auth => 'bar' },
117 id => "CPL", type => 'branch', config => {}
119 { credentials => { api_keys => { CPL => { api_key => 'foo', api_auth => 'bar' } } } },
120 "_load_unit_config: add auth values."
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."
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."
136 is_deeply(
137 Koha::Illrequest::Config::_load_unit_config({
138 unit => { request_limit => { method => 'annual', count => 6 } },
139 id => "default", config => {}
141 { limits => { default => { method => 'annual', count => 6 } } },
142 "_load_unit_config: correct default request_limits."
145 # Populate prefix
146 is_deeply(
147 Koha::Illrequest::Config::_load_unit_config({
148 unit => { prefix => 'Foo-ill' },
149 id => "default", config => {}
151 { prefixes => { default => 'Foo-ill' } },
152 "_load_unit_config: correct default prefix."
154 is_deeply(
155 Koha::Illrequest::Config::_load_unit_config({
156 unit => { prefix => 'Foo-ill' },
157 id => "A", config => {}, type => 'brw_cat'
159 { prefixes => { brw_cat => { A => 'Foo-ill' } } },
160 "_load_unit_config: correct brw_cat prefix."
163 # Populate digital_recipient
164 is_deeply(
165 Koha::Illrequest::Config::_load_unit_config({
166 unit => { digital_recipient => 'borrower' },
167 id => "default", config => {}
169 { digital_recipients => { default => 'borrower' } },
170 "_load_unit_config: correct default digital_recipient."
172 is_deeply(
173 Koha::Illrequest::Config::_load_unit_config({
174 unit => { digital_recipient => 'branch' },
175 id => "A", config => {}, type => 'brw_cat'
177 { digital_recipients => { brw_cat => { A => 'branch' } } },
178 "_load_unit_config: correct brw_cat digital_recipient."
181 $schema->storage->txn_rollback;
184 # _load_configuration:
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.
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.
194 subtest '_load_configuration' => sub {
196 plan tests => 9;
198 $schema->storage->txn_begin;
200 my $config = Koha::Illrequest::Config->new;
202 # Return basic configuration
203 is_deeply(
204 Koha::Illrequest::Config::_load_configuration({}, 0),
206 backend_directory => undef,
207 censorship => {
208 censor_notes_staff => 0,
209 censor_reply_date => 0,
211 limits => {},
212 digital_recipients => {},
213 prefixes => {},
214 partner_code => 'ILLLIBS',
215 raw_config => {},
217 "load_configuration: return the base configuration."
220 # Return correct backend_dir
221 is_deeply(
222 Koha::Illrequest::Config::_load_configuration({ backend_directory => '/tmp/' }, 0),
224 backend_directory => '/tmp/',
225 censorship => {
226 censor_notes_staff => 0,
227 censor_reply_date => 0,
229 limits => {},
230 digital_recipients => {},
231 prefixes => {},
232 partner_code => 'ILLLIBS',
233 raw_config => { backend_directory => '/tmp/' },
235 "load_configuration: return the correct backend_dir."
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' }
247 is_deeply(
248 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
250 backend_directory => '/tmp/',
251 censorship => {
252 censor_notes_staff => 0,
253 censor_reply_date => 0,
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,
261 "load_configuration: multi branch config parsed correctly."
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',
273 is_deeply(
274 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
276 backend_directory => '/tmp/',
277 censorship => {
278 censor_notes_staff => 0,
279 censor_reply_date => 0,
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,
287 "load_configuration: single branch config parsed correctly."
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' }
299 is_deeply(
300 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
302 backend_directory => '/tmp/',
303 censorship => {
304 censor_notes_staff => 0,
305 censor_reply_date => 0,
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,
313 "load_configuration: multi borrower_category config parsed correctly."
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',
325 is_deeply(
326 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
328 backend_directory => '/tmp/',
329 censorship => {
330 censor_notes_staff => 0,
331 censor_reply_date => 0,
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,
339 "load_configuration: single borrower_category config parsed correctly."
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',
349 is_deeply(
350 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
352 backend_directory => '/tmp/',
353 censorship => {
354 censor_notes_staff => 0,
355 censor_reply_date => 0,
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,
363 "load_configuration: parse the default configuration."
366 # Censorship
367 $xml_config = {
368 backend_directory => '/tmp/',
369 staff_request_comments => 'hide',
370 reply_date => 'hide'
372 is_deeply(
373 Koha::Illrequest::Config::_load_configuration($xml_config, 0),
375 backend_directory => '/tmp/',
376 censorship => {
377 censor_notes_staff => 1,
378 censor_reply_date => 1,
380 limits => {},
381 digital_recipients => {},
382 prefixes => {},
383 partner_code => 'ILLLIBS',
384 raw_config => $xml_config,
386 "load_configuration: parse censorship settings configuration."
389 # Partner library category
390 is_deeply(
391 Koha::Illrequest::Config::_load_configuration({ partner_code => 'FOOBAR' }),
393 backend_directory => undef,
394 censorship => {
395 censor_notes_staff => 0,
396 censor_reply_date => 0,
398 limits => {},
399 digital_recipients => {},
400 prefixes => {},
401 partner_code => 'FOOBAR',
402 raw_config => { partner_code => 'FOOBAR' },
404 "load_configuration: Set partner code."
407 $schema->storage->txn_rollback;
411 subtest 'Final tests' => sub {
413 plan tests => 9;
415 $schema->storage->txn_begin;
417 t::lib::Mocks::mock_config("interlibrary_loans", {});
419 my $config = Koha::Illrequest::Config->new;
421 # getPrefixes (error & undef):
422 is($config->getPrefixes(), undef,
423 "getPrefixes: Undefined branch prefix is undefined.");
425 is($config->has_branch(), undef, "Config does not have branch when no config loaded");
427 # getDigitalRecipients (error & undef):
428 dies_ok( sub { $config->getDigitalRecipients("FOO") },
429 "getDigitalRecipients: die if not correct type.");
430 is_deeply($config->getDigitalRecipients("brw_cat"), { default => undef},
431 "getDigitalRecipients: Undefined brw_cat dig rec is undefined.");
432 is_deeply($config->getDigitalRecipients("branch"), { default => undef},
433 "getDigitalRecipients: Undefined branch dig rec is undefined.");
435 $config->{configuration} = Koha::Illrequest::Config::_load_configuration({
436 backend_directory => '/tmp/',
437 prefix => 'DEFAULT-prefix',
438 digital_recipient => 'branch',
439 borrower_category => [
440 { code => 'B', prefix => '2-prefix' },
441 { code => 'C', digital_recipient => 'branch' }
443 branch => [
444 { code => '1', prefix => 'T-prefix' },
445 { code => '2', digital_recipient => 'borrower' }
447 }, 0
450 # getPrefixes (values):
451 is_deeply($config->getPrefixes(),
452 { '1' => 'T-prefix' },
453 "getPrefixes: return configuration branch prefixes.");
455 # getDigitalRecipients (values):
456 is_deeply($config->getDigitalRecipients("brw_cat"),
457 { C => 'branch', default => 'branch' },
458 "getDigitalRecipients: return brw_cat digital_recipients.");
459 is_deeply($config->getDigitalRecipients("branch"),
460 { 2 => 'borrower', default => 'branch' },
461 "getDigitalRecipients: return branch digital_recipients.");
463 # has_branch test
464 ok($config->has_branch(), "has_branch returns true if branch defined in configuration");
466 $schema->storage->txn_rollback;