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>.
20 use File::Basename qw/basename/;
22 use Koha::Illrequestattributes;
23 use Koha::Illrequest::Config;
25 use Koha::AuthorisedValueCategories;
26 use Koha::AuthorisedValues;
28 use t::lib::TestBuilder;
33 use Test::More tests => 11;
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
37 use_ok('Koha::Illrequest');
38 use_ok('Koha::Illrequests');
40 subtest 'Basic object tests' => sub {
44 $schema->storage->txn_begin;
46 Koha::Illrequests->search->delete;
47 my $illrq = $builder->build({ source => 'Illrequest' });
48 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
50 isa_ok($illrq_obj, 'Koha::Illrequest',
51 "Correctly create and load an illrequest object.");
52 isa_ok($illrq_obj->_config, 'Koha::Illrequest::Config',
53 "Created a config object as part of Illrequest creation.");
55 is($illrq_obj->illrequest_id, $illrq->{illrequest_id},
56 "Illrequest_id getter works.");
57 is($illrq_obj->borrowernumber, $illrq->{borrowernumber},
58 "Borrowernumber getter works.");
59 is($illrq_obj->biblio_id, $illrq->{biblio_id},
60 "Biblio_Id getter works.");
61 is($illrq_obj->branchcode, $illrq->{branchcode},
62 "Branchcode getter works.");
63 is($illrq_obj->status, $illrq->{status},
64 "Status getter works.");
65 is($illrq_obj->status_alias, $illrq->{status_alias},
66 "Status_alias getter works.");
67 is($illrq_obj->placed, $illrq->{placed},
68 "Placed getter works.");
69 is($illrq_obj->replied, $illrq->{replied},
70 "Replied getter works.");
71 is($illrq_obj->updated, $illrq->{updated},
72 "Updated getter works.");
73 is($illrq_obj->completed, $illrq->{completed},
74 "Completed getter works.");
75 is($illrq_obj->medium, $illrq->{medium},
76 "Medium getter works.");
77 is($illrq_obj->accessurl, $illrq->{accessurl},
78 "Accessurl getter works.");
79 is($illrq_obj->cost, $illrq->{cost},
80 "Cost getter works.");
81 is($illrq_obj->price_paid, $illrq->{price_paid},
82 "Price_paid getter works.");
83 is($illrq_obj->notesopac, $illrq->{notesopac},
84 "Notesopac getter works.");
85 is($illrq_obj->notesstaff, $illrq->{notesstaff},
86 "Notesstaff getter works.");
87 is($illrq_obj->orderid, $illrq->{orderid},
88 "Orderid getter works.");
89 is($illrq_obj->backend, $illrq->{backend},
90 "Backend getter works.");
92 is($illrq_obj->get_type, undef,
93 'get_type() returns undef if no type is set');
95 source => 'Illrequestattribute',
97 illrequest_id => $illrq_obj->illrequest_id,
102 is($illrq_obj->get_type, 'book',
103 'get_type() returns correct type if set');
105 isnt($illrq_obj->status, 'COMP',
106 "ILL is not currently marked complete.");
107 $illrq_obj->mark_completed;
108 is($illrq_obj->status, 'COMP',
109 "ILL is now marked complete.");
113 is(Koha::Illrequests->search->count, 0,
114 "No illrequest found after delete.");
116 $schema->storage->txn_rollback;
119 subtest 'Working with related objects' => sub {
123 $schema->storage->txn_begin;
125 Koha::Illrequests->search->delete;
127 my $patron = $builder->build({ source => 'Borrower' });
128 my $illrq = $builder->build({
129 source => 'Illrequest',
130 value => { borrowernumber => $patron->{borrowernumber} }
132 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
134 isa_ok($illrq_obj->patron, 'Koha::Patron',
135 "OK accessing related patron.");
138 source => 'Illrequestattribute',
139 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
142 source => 'Illrequestattribute',
143 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
146 source => 'Illrequestattribute',
147 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
150 is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
151 "Fetching expected number of Illrequestattributes for our request.");
153 my $illrq1 = $builder->build({ source => 'Illrequest' });
155 source => 'Illrequestattribute',
156 value => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
159 is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
160 "Fetching expected number of Illrequestattributes for our request.");
163 is(Koha::Illrequestattributes->search->count, 1,
164 "Correct number of illrequestattributes after delete.");
166 isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
167 "Borrower was not deleted after illrq delete.");
169 $schema->storage->txn_rollback;
172 subtest 'Status Graph tests' => sub {
176 $schema->storage->txn_begin;
178 my $illrq = $builder->build({source => 'Illrequest'});
179 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
181 # _core_status_graph tests: it's just a constant, so here we just make
182 # sure it returns a hashref.
183 is(ref $illrq_obj->_core_status_graph, "HASH",
184 "_core_status_graph returns a hash.");
186 # _status_graph_union: let's try different merge operations.
189 $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
190 $illrq_obj->_core_status_graph,
191 "core_status_graph + null = core_status_graph"
196 $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
197 $illrq_obj->_core_status_graph,
198 "null + core_status_graph = core_status_graph"
201 # Correct merge behaviour
203 $illrq_obj->_status_graph_union({
211 prev_actions => [ 'REQ' ],
213 next_actions => [ 'REQ' ],
218 prev_actions => [ 'QER' ],
220 next_actions => [ 'QER' ],
223 prev_actions => [ 'REQ' ],
225 next_actions => [ 'REQ' ],
228 "REQ atom + linking QER = cyclical status graph"
231 $schema->storage->txn_rollback;
234 subtest 'Backend testing (mocks)' => sub {
238 $schema->storage->txn_begin;
240 # testing load_backend & available_backends requires that we have at least
241 # the Dummy plugin installed. load_backend & available_backends don't
242 # currently have tests as a result.
244 t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' } );
245 my $backend = Test::MockObject->new;
246 $backend->set_isa('Koha::Illbackends::Mock');
247 $backend->set_always('name', 'Mock');
249 my $patron = $builder->build({ source => 'Borrower' });
250 my $illrq = $builder->build_object({
251 class => 'Koha::Illrequests',
252 value => { borrowernumber => $patron->{borrowernumber} }
255 $illrq->_backend($backend);
257 isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
258 "OK accessing mocked backend.");
260 # _backend_capability tests:
261 # We need to test whether this optional feature of a mocked backend
262 # behaves as expected.
263 # 3 scenarios: feature not implemented, feature implemented, but requested
264 # capability is not provided by backend, & feature is implemented &
265 # capability exists. This method can be used to implement custom backend
266 # functionality, such as unmediated in the BLDSS backend (also see
268 $backend->set_always('capabilities', undef);
269 is($illrq->_backend_capability('Test'), 0,
270 "0 returned on Mock not implementing capabilities.");
272 $backend->set_always('capabilities', 0);
273 is($illrq->_backend_capability('Test'), 0,
274 "0 returned on Mock not implementing Test capability.");
276 $backend->set_always('capabilities', sub { return 'bar'; } );
277 is($illrq->_backend_capability('Test'), 'bar',
278 "'bar' returned on Mock implementing Test capability.");
280 # metadata test: we need to be sure that we return the arbitrary values
285 my ( $self, $rq ) = @_;
287 ID => $rq->illrequest_id,
288 Title => $rq->patron->borrowernumber
296 ID => $illrq->illrequest_id,
297 Title => $illrq->patron->borrowernumber
304 # No backend graph extension
305 $backend->set_always('status_graph', {});
306 is_deeply($illrq->capabilities('COMP'),
308 prev_actions => [ 'REQ' ],
311 ui_method_name => 'Mark completed',
312 method => 'mark_completed',
314 ui_method_icon => 'fa-check',
316 "Dummy status graph for COMP.");
317 is($illrq->capabilities('UNKNOWN'), undef,
318 "Dummy status graph for UNKNOWN.");
319 is_deeply($illrq->capabilities(),
320 $illrq->_core_status_graph,
321 "Dummy full status graph.");
322 # Simple backend graph extension
323 $backend->set_always('status_graph',
326 prev_actions => [ 'REQ' ],
328 next_actions => [ 'REQ' ],
331 is_deeply($illrq->capabilities('QER'),
333 prev_actions => [ 'REQ' ],
335 next_actions => [ 'REQ' ],
337 "Simple status graph for QER.");
338 is($illrq->capabilities('UNKNOWN'), undef,
339 "Simple status graph for UNKNOWN.");
340 is_deeply($illrq->capabilities(),
341 $illrq->_status_graph_union(
342 $illrq->_core_status_graph,
345 prev_actions => [ 'REQ' ],
347 next_actions => [ 'REQ' ],
351 "Simple full status graph.");
355 # No backend graph extension
356 $backend->set_always('status_graph', {});
357 is($illrq->custom_capability('unknown', {}), 0,
358 "Unknown candidate.");
360 # Simple backend graph extension
361 $backend->set_always('status_graph',
364 prev_actions => [ 'REQ' ],
366 method => 'identity',
367 next_actions => [ 'REQ' ],
370 $backend->mock('identity',
371 sub { my ( $self, $params ) = @_; return $params->{other}; });
372 is($illrq->custom_capability('identity', { test => 1, method => 'blah' })->{test}, 1,
373 "Resolve identity custom_capability");
375 $schema->storage->txn_rollback;
379 subtest 'Backend core methods' => sub {
383 $schema->storage->txn_begin;
385 # Build infrastructure
386 my $backend = Test::MockObject->new;
387 $backend->set_isa('Koha::Illbackends::Mock');
388 $backend->set_always('name', 'Mock');
390 my $config = Test::MockObject->new;
391 $config->set_always('backend_dir', "/tmp");
392 $config->set_always('getLimitRules',
393 { default => { count => 0, method => 'active' } });
395 my $illrq = $builder->build_object({
396 class => 'Koha::Illrequests',
397 value => { backend => undef }
399 $illrq->_config($config);
401 # Test error conditions (no backend)
402 throws_ok { $illrq->load_backend; }
403 'Koha::Exceptions::Ill::InvalidBackendId',
404 'Exception raised correctly';
406 throws_ok { $illrq->load_backend(''); }
407 'Koha::Exceptions::Ill::InvalidBackendId',
408 'Exception raised correctly';
410 # Now load the mocked backend
411 $illrq->_backend($backend);
414 is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
418 template => "/tmp/Mock/intra-includes/bar.inc",
419 opac_template => "/tmp/Mock/opac-includes/bar.inc",
424 # we are testing simple cases.
425 $backend->set_series('create',
426 { stage => 'bar', method => 'create' },
427 { stage => 'commit', method => 'create' },
428 { stage => 'commit', method => 'create' });
430 is_deeply($illrq->backend_create({test => 1}),
432 stage => 'bar', method => 'create',
433 template => "/tmp/Mock/intra-includes/create.inc",
434 opac_template => "/tmp/Mock/opac-includes/create.inc",
436 "Backend create: arbitrary stage.");
438 is_deeply($illrq->backend_create({test => 1}),
440 stage => 'commit', method => 'create', permitted => 0,
441 template => "/tmp/Mock/intra-includes/create.inc",
442 opac_template => "/tmp/Mock/opac-includes/create.inc",
444 "Backend create: arbitrary stage, not permitted.");
445 is($illrq->status, "QUEUED", "Backend create: queued if restricted.");
446 $config->set_always('getLimitRules', {});
447 $illrq->status('NEW');
448 is_deeply($illrq->backend_create({test => 1}),
450 stage => 'commit', method => 'create', permitted => 1,
451 template => "/tmp/Mock/intra-includes/create.inc",
452 opac_template => "/tmp/Mock/opac-includes/create.inc",
454 "Backend create: arbitrary stage, permitted.");
455 is($illrq->status, "NEW", "Backend create: not-queued.");
458 $backend->set_series('renew', { stage => 'bar', method => 'renew' });
459 is_deeply($illrq->backend_renew({test => 1}),
461 stage => 'bar', method => 'renew',
462 template => "/tmp/Mock/intra-includes/renew.inc",
463 opac_template => "/tmp/Mock/opac-includes/renew.inc",
465 "Backend renew: arbitrary stage.");
468 $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
469 is_deeply($illrq->backend_cancel({test => 1}),
471 stage => 'bar', method => 'cancel',
472 template => "/tmp/Mock/intra-includes/cancel.inc",
473 opac_template => "/tmp/Mock/opac-includes/cancel.inc",
475 "Backend cancel: arbitrary stage.");
477 # backend_update_status
478 $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
479 is_deeply($illrq->backend_update_status({test => 1}),
481 stage => 'bar', method => 'update_status',
482 template => "/tmp/Mock/intra-includes/update_status.inc",
483 opac_template => "/tmp/Mock/opac-includes/update_status.inc",
485 "Backend update_status: arbitrary stage.");
488 $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
489 is_deeply($illrq->backend_confirm({test => 1}),
491 stage => 'bar', method => 'confirm',
492 template => "/tmp/Mock/intra-includes/confirm.inc",
493 opac_template => "/tmp/Mock/opac-includes/confirm.inc",
495 "Backend confirm: arbitrary stage.");
497 $config->set_always('partner_code', "ILLTSTLIB");
498 $backend->set_always('metadata', { Test => "Foobar" });
499 my $illbrn = $builder->build({
501 value => { branchemail => "", branchreplyto => "" }
503 my $partner1 = $builder->build({
504 source => 'Borrower',
505 value => { categorycode => "ILLTSTLIB" },
507 my $partner2 = $builder->build({
508 source => 'Borrower',
509 value => { categorycode => "ILLTSTLIB" },
511 my $gen_conf = $illrq->generic_confirm({
512 current_branchcode => $illbrn->{branchcode}
514 isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
515 "Generic confirm: draft contains metadata."
517 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
518 "Generic cofnirm: partner 1 is correct."
520 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
521 "Generic confirm: partner 2 is correct."
524 dies_ok { $illrq->generic_confirm({
525 current_branchcode => $illbrn->{branchcode},
528 "Generic confirm: missing to dies OK.";
530 dies_ok { $illrq->generic_confirm({
531 current_branchcode => $illbrn->{branchcode},
532 partners => $partner1->{email},
535 "Generic confirm: missing from dies OK.";
537 $schema->storage->txn_rollback;
541 subtest 'Helpers' => sub {
545 $schema->storage->txn_begin;
547 # Build infrastructure
548 my $backend = Test::MockObject->new;
549 $backend->set_isa('Koha::Illbackends::Mock');
550 $backend->set_always('name', 'Mock');
552 my $config = Test::MockObject->new;
553 $config->set_always('backend_dir', "/tmp");
555 my $patron = $builder->build({
556 source => 'Borrower',
557 value => { categorycode => "A" }
559 my $illrq = $builder->build({
560 source => 'Illrequest',
561 value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
563 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
564 $illrq_obj->_config($config);
565 $illrq_obj->_backend($backend);
568 $config->set_series('getPrefixes',
569 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
570 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
571 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
572 "getPrefix: branch");
573 $config->set_series('getPrefixes',
574 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
575 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
576 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
577 "getPrefix: default");
578 $config->set_always('getPrefixes', {});
579 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
580 "getPrefix: the empty prefix");
583 $config->set_series('getPrefixes',
584 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
585 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
586 is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
587 $config->set_series('getPrefixes',
588 { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
589 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
590 is($illrq_obj->id_prefix, "", "id_prefix: default");
592 # requires_moderation
593 $illrq_obj->status('NEW')->store;
594 is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
595 $illrq_obj->status('CANCREQ')->store;
596 is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
598 $schema->storage->txn_rollback;
602 subtest 'Censorship' => sub {
606 $schema->storage->txn_begin;
608 # Build infrastructure
609 my $backend = Test::MockObject->new;
610 $backend->set_isa('Koha::Illbackends::Mock');
611 $backend->set_always('name', 'Mock');
613 my $config = Test::MockObject->new;
614 $config->set_always('backend_dir', "/tmp");
616 my $illrq = $builder->build({source => 'Illrequest'});
617 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
618 $illrq_obj->_config($config);
619 $illrq_obj->_backend($backend);
621 $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
623 my $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564 });
624 is_deeply($censor_out, { foo => 'bar', baz => 564, display_reply_date => 1 },
625 "_censor: not OPAC, reply_date = 1");
627 $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564, opac => 1 });
628 is_deeply($censor_out, {
629 foo => 'bar', baz => 564, censor_notes_staff => 1,
630 display_reply_date => 1, opac => 1
631 }, "_censor: notes_staff = 0, reply_date = 0");
633 $schema->storage->txn_rollback;
636 subtest 'Checking Limits' => sub {
640 $schema->storage->txn_begin;
642 # Build infrastructure
643 my $backend = Test::MockObject->new;
644 $backend->set_isa('Koha::Illbackends::Mock');
645 $backend->set_always('name', 'Mock');
647 my $config = Test::MockObject->new;
648 $config->set_always('backend_dir', "/tmp");
650 my $illrq = $builder->build({source => 'Illrequest'});
651 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
652 $illrq_obj->_config($config);
653 $illrq_obj->_backend($backend);
656 $config->set_series('getLimitRules',
657 { CPL => { count => 1, method => 'test' } },
658 { default => { count => 0, method => 'active' } });
659 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
660 { count => 1, method => 'test' },
661 "getLimits: by value.");
662 is_deeply($illrq_obj->getLimits({ type => 'branch' }),
663 { count => 0, method => 'active' },
664 "getLimits: by default.");
665 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
666 { count => -1, method => 'active' },
667 "getLimits: by hard-coded.");
670 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
671 1, "_limit_counter: Initial branch annual count.");
672 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
673 1, "_limit_counter: Initial branch active count.");
674 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
675 1, "_limit_counter: Initial patron annual count.");
676 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
677 1, "_limit_counter: Initial patron active count.");
679 source => 'Illrequest',
681 branchcode => $illrq_obj->branchcode,
682 borrowernumber => $illrq_obj->borrowernumber,
685 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
686 2, "_limit_counter: Add a qualifying request for branch annual count.");
687 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
688 2, "_limit_counter: Add a qualifying request for branch active count.");
689 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
690 2, "_limit_counter: Add a qualifying request for patron annual count.");
691 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
692 2, "_limit_counter: Add a qualifying request for patron active count.");
694 source => 'Illrequest',
696 branchcode => $illrq_obj->branchcode,
697 borrowernumber => $illrq_obj->borrowernumber,
698 placed => "2005-05-31",
701 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
702 2, "_limit_counter: Add an out-of-date branch request.");
703 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
704 3, "_limit_counter: Add a qualifying request for branch active count.");
705 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
706 2, "_limit_counter: Add an out-of-date patron request.");
707 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
708 3, "_limit_counter: Add a qualifying request for patron active count.");
710 source => 'Illrequest',
712 branchcode => $illrq_obj->branchcode,
713 borrowernumber => $illrq_obj->borrowernumber,
717 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
718 3, "_limit_counter: Add a qualifying request for branch annual count.");
719 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
720 3, "_limit_counter: Add a completed request for branch active count.");
721 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
722 3, "_limit_counter: Add a qualifying request for patron annual count.");
723 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
724 3, "_limit_counter: Add a completed request for patron active count.");
728 # We've tested _limit_counter, so all we need to test here is whether the
729 # current counts of 3 for each work as they should against different
730 # configuration declarations.
733 $config->set_always('getLimitRules', undef);
734 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
735 librarycode => $illrq_obj->branchcode}),
736 1, "check_limits: no configuration => no limits.");
739 $config->set_always('getLimitRules',
740 { $illrq_obj->branchcode => { count => 1, method => 'active' } });
741 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
742 librarycode => $illrq_obj->branchcode}),
743 0, "check_limits: branch active limit exceeded.");
744 $config->set_always('getLimitRules',
745 { $illrq_obj->branchcode => { count => 1, method => 'annual' } });
746 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
747 librarycode => $illrq_obj->branchcode}),
748 0, "check_limits: branch annual limit exceeded.");
749 $config->set_always('getLimitRules',
750 { $illrq_obj->branchcode => { count => 4, method => 'active' } });
751 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
752 librarycode => $illrq_obj->branchcode}),
753 1, "check_limits: branch active limit OK.");
754 $config->set_always('getLimitRules',
755 { $illrq_obj->branchcode => { count => 4, method => 'annual' } });
756 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
757 librarycode => $illrq_obj->branchcode}),
758 1, "check_limits: branch annual limit OK.");
761 $config->set_always('getLimitRules',
762 { $illrq_obj->patron->categorycode => { count => 1, method => 'active' } });
763 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
764 librarycode => $illrq_obj->branchcode}),
765 0, "check_limits: patron category active limit exceeded.");
766 $config->set_always('getLimitRules',
767 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
768 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
769 librarycode => $illrq_obj->branchcode}),
770 0, "check_limits: patron category annual limit exceeded.");
771 $config->set_always('getLimitRules',
772 { $illrq_obj->patron->categorycode => { count => 4, method => 'active' } });
773 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
774 librarycode => $illrq_obj->branchcode}),
775 1, "check_limits: patron category active limit OK.");
776 $config->set_always('getLimitRules',
777 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
778 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
779 librarycode => $illrq_obj->branchcode}),
780 1, "check_limits: patron category annual limit OK.");
782 # One rule cancels the other
783 $config->set_series('getLimitRules',
784 # Branch rules allow request
785 { $illrq_obj->branchcode => { count => 4, method => 'active' } },
786 # Patron rule forbids it
787 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
788 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
789 librarycode => $illrq_obj->branchcode}),
790 0, "check_limits: patron category veto overrides branch OK.");
791 $config->set_series('getLimitRules',
792 # Branch rules allow request
793 { $illrq_obj->branchcode => { count => 1, method => 'active' } },
794 # Patron rule forbids it
795 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
796 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
797 librarycode => $illrq_obj->branchcode}),
798 0, "check_limits: branch veto overrides patron category OK.");
800 $schema->storage->txn_rollback;
803 subtest 'Custom statuses' => sub {
807 $schema->storage->txn_begin;
809 my $cat = Koha::AuthorisedValueCategories->search(
811 category_name => 'ILLSTATUS'
815 if ($cat->count == 0) {
816 $cat = $builder->build_object(
818 class => 'Koha::AuthorisedValueCategory',
820 category_name => 'ILLSTATUS'
826 my $av = $builder->build_object(
828 class => 'Koha::AuthorisedValues',
830 category => 'ILLSTATUS'
835 is($av->category, 'ILLSTATUS',
836 "Successfully created authorised value for custom status");
838 my $ill_req = $builder->build_object(
840 class => 'Koha::Illrequests',
842 status_alias => $av->authorised_value
846 isa_ok($ill_req->statusalias, 'Koha::AuthorisedValue',
847 "statusalias correctly returning Koha::AuthorisedValue object");
849 $ill_req->status("COMP");
850 is($ill_req->statusalias, undef,
851 "Koha::Illrequest->status overloading resetting status_alias");
853 $schema->storage->txn_rollback;