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->placed, $illrq->{placed},
66 "Placed getter works.");
67 is($illrq_obj->replied, $illrq->{replied},
68 "Replied getter works.");
69 is($illrq_obj->updated, $illrq->{updated},
70 "Updated getter works.");
71 is($illrq_obj->completed, $illrq->{completed},
72 "Completed getter works.");
73 is($illrq_obj->medium, $illrq->{medium},
74 "Medium getter works.");
75 is($illrq_obj->accessurl, $illrq->{accessurl},
76 "Accessurl getter works.");
77 is($illrq_obj->cost, $illrq->{cost},
78 "Cost getter works.");
79 is($illrq_obj->price_paid, $illrq->{price_paid},
80 "Price_paid getter works.");
81 is($illrq_obj->notesopac, $illrq->{notesopac},
82 "Notesopac getter works.");
83 is($illrq_obj->notesstaff, $illrq->{notesstaff},
84 "Notesstaff getter works.");
85 is($illrq_obj->orderid, $illrq->{orderid},
86 "Orderid getter works.");
87 is($illrq_obj->backend, $illrq->{backend},
88 "Backend getter works.");
90 is($illrq_obj->get_type, undef,
91 'get_type() returns undef if no type is set');
93 source => 'Illrequestattribute',
95 illrequest_id => $illrq_obj->illrequest_id,
100 is($illrq_obj->get_type, 'book',
101 'get_type() returns correct type if set');
103 isnt($illrq_obj->status, 'COMP',
104 "ILL is not currently marked complete.");
105 $illrq_obj->mark_completed;
106 is($illrq_obj->status, 'COMP',
107 "ILL is now marked complete.");
111 is(Koha::Illrequests->search->count, 0,
112 "No illrequest found after delete.");
114 $schema->storage->txn_rollback;
117 subtest 'Working with related objects' => sub {
121 $schema->storage->txn_begin;
123 Koha::Illrequests->search->delete;
125 my $patron = $builder->build({ source => 'Borrower' });
126 my $illrq = $builder->build({
127 source => 'Illrequest',
128 value => { borrowernumber => $patron->{borrowernumber} }
130 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
132 isa_ok($illrq_obj->patron, 'Koha::Patron',
133 "OK accessing related patron.");
136 source => 'Illrequestattribute',
137 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
140 source => 'Illrequestattribute',
141 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
144 source => 'Illrequestattribute',
145 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
148 is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
149 "Fetching expected number of Illrequestattributes for our request.");
151 my $illrq1 = $builder->build({ source => 'Illrequest' });
153 source => 'Illrequestattribute',
154 value => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
157 is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
158 "Fetching expected number of Illrequestattributes for our request.");
161 is(Koha::Illrequestattributes->search->count, 1,
162 "Correct number of illrequestattributes after delete.");
164 isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
165 "Borrower was not deleted after illrq delete.");
167 $schema->storage->txn_rollback;
170 subtest 'Status Graph tests' => sub {
174 $schema->storage->txn_begin;
176 my $illrq = $builder->build({source => 'Illrequest'});
177 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
179 # _core_status_graph tests: it's just a constant, so here we just make
180 # sure it returns a hashref.
181 is(ref $illrq_obj->_core_status_graph, "HASH",
182 "_core_status_graph returns a hash.");
184 # _status_graph_union: let's try different merge operations.
187 $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
188 $illrq_obj->_core_status_graph,
189 "core_status_graph + null = core_status_graph"
194 $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
195 $illrq_obj->_core_status_graph,
196 "null + core_status_graph = core_status_graph"
199 # Correct merge behaviour
201 $illrq_obj->_status_graph_union({
209 prev_actions => [ 'REQ' ],
211 next_actions => [ 'REQ' ],
216 prev_actions => [ 'QER' ],
218 next_actions => [ 'QER' ],
221 prev_actions => [ 'REQ' ],
223 next_actions => [ 'REQ' ],
226 "REQ atom + linking QER = cyclical status graph"
229 $schema->storage->txn_rollback;
232 subtest 'Backend testing (mocks)' => sub {
236 $schema->storage->txn_begin;
238 # testing load_backend & available_backends requires that we have at least
239 # the Dummy plugin installed. load_backend & available_backends don't
240 # currently have tests as a result.
242 t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' } );
243 my $backend = Test::MockObject->new;
244 $backend->set_isa('Koha::Illbackends::Mock');
245 $backend->set_always('name', 'Mock');
247 my $patron = $builder->build({ source => 'Borrower' });
248 my $illrq = $builder->build_object({
249 class => 'Koha::Illrequests',
250 value => { borrowernumber => $patron->{borrowernumber} }
253 $illrq->_backend($backend);
255 isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
256 "OK accessing mocked backend.");
258 # _backend_capability tests:
259 # We need to test whether this optional feature of a mocked backend
260 # behaves as expected.
261 # 3 scenarios: feature not implemented, feature implemented, but requested
262 # capability is not provided by backend, & feature is implemented &
263 # capability exists. This method can be used to implement custom backend
264 # functionality, such as unmediated in the BLDSS backend (also see
266 $backend->set_always('capabilities', undef);
267 is($illrq->_backend_capability('Test'), 0,
268 "0 returned on Mock not implementing capabilities.");
270 $backend->set_always('capabilities', 0);
271 is($illrq->_backend_capability('Test'), 0,
272 "0 returned on Mock not implementing Test capability.");
274 $backend->set_always('capabilities', sub { return 'bar'; } );
275 is($illrq->_backend_capability('Test'), 'bar',
276 "'bar' returned on Mock implementing Test capability.");
278 # metadata test: we need to be sure that we return the arbitrary values
283 my ( $self, $rq ) = @_;
285 ID => $rq->illrequest_id,
286 Title => $rq->patron->borrowernumber
294 ID => $illrq->illrequest_id,
295 Title => $illrq->patron->borrowernumber
302 # No backend graph extension
303 $backend->set_always('status_graph', {});
304 is_deeply($illrq->capabilities('COMP'),
306 prev_actions => [ 'REQ' ],
309 ui_method_name => 'Mark completed',
310 method => 'mark_completed',
312 ui_method_icon => 'fa-check',
314 "Dummy status graph for COMP.");
315 is($illrq->capabilities('UNKNOWN'), undef,
316 "Dummy status graph for UNKNOWN.");
317 is_deeply($illrq->capabilities(),
318 $illrq->_core_status_graph,
319 "Dummy full status graph.");
320 # Simple backend graph extension
321 $backend->set_always('status_graph',
324 prev_actions => [ 'REQ' ],
326 next_actions => [ 'REQ' ],
329 is_deeply($illrq->capabilities('QER'),
331 prev_actions => [ 'REQ' ],
333 next_actions => [ 'REQ' ],
335 "Simple status graph for QER.");
336 is($illrq->capabilities('UNKNOWN'), undef,
337 "Simple status graph for UNKNOWN.");
338 is_deeply($illrq->capabilities(),
339 $illrq->_status_graph_union(
340 $illrq->_core_status_graph,
343 prev_actions => [ 'REQ' ],
345 next_actions => [ 'REQ' ],
349 "Simple full status graph.");
353 # No backend graph extension
354 $backend->set_always('status_graph', {});
355 is($illrq->custom_capability('unknown', {}), 0,
356 "Unknown candidate.");
358 # Simple backend graph extension
359 $backend->set_always('status_graph',
362 prev_actions => [ 'REQ' ],
364 method => 'identity',
365 next_actions => [ 'REQ' ],
368 $backend->mock('identity',
369 sub { my ( $self, $params ) = @_; return $params->{other}; });
370 is($illrq->custom_capability('identity', { test => 1, method => 'blah' })->{test}, 1,
371 "Resolve identity custom_capability");
373 $schema->storage->txn_rollback;
377 subtest 'Backend core methods' => sub {
381 $schema->storage->txn_begin;
383 # Build infrastructure
384 my $backend = Test::MockObject->new;
385 $backend->set_isa('Koha::Illbackends::Mock');
386 $backend->set_always('name', 'Mock');
387 $backend->mock('capabilities', sub { return 'Mock'; });
389 my $config = Test::MockObject->new;
390 $config->set_always('backend_dir', "/tmp");
391 $config->set_always('getLimitRules',
392 { default => { count => 0, method => 'active' } });
394 my $illrq = $builder->build_object({
395 class => 'Koha::Illrequests',
396 value => { backend => undef }
398 $illrq->_config($config);
400 # Test error conditions (no backend)
401 throws_ok { $illrq->load_backend; }
402 'Koha::Exceptions::Ill::InvalidBackendId',
403 'Exception raised correctly';
405 throws_ok { $illrq->load_backend(''); }
406 'Koha::Exceptions::Ill::InvalidBackendId',
407 'Exception raised correctly';
409 # Now load the mocked backend
410 $illrq->_backend($backend);
413 is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
417 template => "/tmp/Mock/intra-includes/bar.inc",
418 opac_template => "/tmp/Mock/opac-includes/bar.inc",
423 # we are testing simple cases.
424 $backend->set_series('create',
425 { stage => 'bar', method => 'create' },
426 { stage => 'commit', method => 'create' },
427 { stage => 'commit', method => 'create' },
428 { stage => 'commit', method => 'create' },
429 { stage => 'commit', method => 'create' });
431 is_deeply($illrq->backend_create({test => 1}),
433 stage => 'bar', method => 'create',
434 template => "/tmp/Mock/intra-includes/create.inc",
435 opac_template => "/tmp/Mock/opac-includes/create.inc",
437 "Backend create: arbitrary stage.");
439 is_deeply($illrq->backend_create({test => 1}),
441 stage => 'commit', method => 'create', permitted => 0,
442 template => "/tmp/Mock/intra-includes/create.inc",
443 opac_template => "/tmp/Mock/opac-includes/create.inc",
445 "Backend create: arbitrary stage, not permitted.");
446 is($illrq->status, "QUEUED", "Backend create: queued if restricted.");
447 $config->set_always('getLimitRules', {});
448 $illrq->status('NEW');
449 is_deeply($illrq->backend_create({test => 1}),
451 stage => 'commit', method => 'create', permitted => 1,
452 template => "/tmp/Mock/intra-includes/create.inc",
453 opac_template => "/tmp/Mock/opac-includes/create.inc",
455 "Backend create: arbitrary stage, permitted.");
456 is($illrq->status, "NEW", "Backend create: not-queued.");
458 # Test that enabling the unmediated workflow causes the backend's
459 # 'unmediated_ill' method to be called
460 t::lib::Mocks::mock_preference('ILLModuleUnmediated', '1');
464 my ($self, $name) = @_;
465 if ($name eq 'unmediated_ill') {
467 return { unmediated_ill => 1 };
472 $illrq->status('NEW');
474 $illrq->backend_create({test => 1}),
476 'opac_template' => '/tmp/Mock/opac-includes/.inc',
477 'template' => '/tmp/Mock/intra-includes/.inc',
478 'unmediated_ill' => 1
480 "Backend create: commit stage, permitted, ILLModuleUnmediated enabled."
483 # Test that disabling the unmediated workflow causes the backend's
484 # 'unmediated_ill' method to be NOT called
485 t::lib::Mocks::mock_preference('ILLModuleUnmediated', '0');
486 $illrq->status('NEW');
488 $illrq->backend_create({test => 1}),
490 stage => 'commit', method => 'create', permitted => 1,
491 template => "/tmp/Mock/intra-includes/create.inc",
492 opac_template => "/tmp/Mock/opac-includes/create.inc",
494 "Backend create: commit stage, permitted, ILLModuleUnmediated disabled."
498 $backend->set_series('renew', { stage => 'bar', method => 'renew' });
499 is_deeply($illrq->backend_renew({test => 1}),
501 stage => 'bar', method => 'renew',
502 template => "/tmp/Mock/intra-includes/renew.inc",
503 opac_template => "/tmp/Mock/opac-includes/renew.inc",
505 "Backend renew: arbitrary stage.");
508 $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
509 is_deeply($illrq->backend_cancel({test => 1}),
511 stage => 'bar', method => 'cancel',
512 template => "/tmp/Mock/intra-includes/cancel.inc",
513 opac_template => "/tmp/Mock/opac-includes/cancel.inc",
515 "Backend cancel: arbitrary stage.");
517 # backend_update_status
518 $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
519 is_deeply($illrq->backend_update_status({test => 1}),
521 stage => 'bar', method => 'update_status',
522 template => "/tmp/Mock/intra-includes/update_status.inc",
523 opac_template => "/tmp/Mock/opac-includes/update_status.inc",
525 "Backend update_status: arbitrary stage.");
528 $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
529 is_deeply($illrq->backend_confirm({test => 1}),
531 stage => 'bar', method => 'confirm',
532 template => "/tmp/Mock/intra-includes/confirm.inc",
533 opac_template => "/tmp/Mock/opac-includes/confirm.inc",
535 "Backend confirm: arbitrary stage.");
537 $config->set_always('partner_code', "ILLTSTLIB");
538 $backend->set_always('metadata', { Test => "Foobar" });
539 my $illbrn = $builder->build({
541 value => { branchemail => "", branchreplyto => "" }
543 my $partner1 = $builder->build({
544 source => 'Borrower',
545 value => { categorycode => "ILLTSTLIB" },
547 my $partner2 = $builder->build({
548 source => 'Borrower',
549 value => { categorycode => "ILLTSTLIB" },
551 my $gen_conf = $illrq->generic_confirm({
552 current_branchcode => $illbrn->{branchcode}
554 isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
555 "Generic confirm: draft contains metadata."
557 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
558 "Generic cofnirm: partner 1 is correct."
560 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
561 "Generic confirm: partner 2 is correct."
564 dies_ok { $illrq->generic_confirm({
565 current_branchcode => $illbrn->{branchcode},
568 "Generic confirm: missing to dies OK.";
570 dies_ok { $illrq->generic_confirm({
571 current_branchcode => $illbrn->{branchcode},
572 partners => $partner1->{email},
575 "Generic confirm: missing from dies OK.";
577 $schema->storage->txn_rollback;
581 subtest 'Helpers' => sub {
585 $schema->storage->txn_begin;
587 # Build infrastructure
588 my $backend = Test::MockObject->new;
589 $backend->set_isa('Koha::Illbackends::Mock');
590 $backend->set_always('name', 'Mock');
592 my $config = Test::MockObject->new;
593 $config->set_always('backend_dir', "/tmp");
595 my $patron = $builder->build({
596 source => 'Borrower',
597 value => { categorycode => "A" }
599 my $illrq = $builder->build({
600 source => 'Illrequest',
601 value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
603 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
604 $illrq_obj->_config($config);
605 $illrq_obj->_backend($backend);
608 $config->set_series('getPrefixes',
609 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
610 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
611 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
612 "getPrefix: branch");
613 $config->set_series('getPrefixes',
614 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
615 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
616 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
617 "getPrefix: default");
618 $config->set_always('getPrefixes', {});
619 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
620 "getPrefix: the empty prefix");
623 $config->set_series('getPrefixes',
624 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
625 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
626 is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
627 $config->set_series('getPrefixes',
628 { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
629 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
630 is($illrq_obj->id_prefix, "", "id_prefix: default");
632 # requires_moderation
633 $illrq_obj->status('NEW')->store;
634 is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
635 $illrq_obj->status('CANCREQ')->store;
636 is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
638 $schema->storage->txn_rollback;
642 subtest 'Censorship' => sub {
646 $schema->storage->txn_begin;
648 # Build infrastructure
649 my $backend = Test::MockObject->new;
650 $backend->set_isa('Koha::Illbackends::Mock');
651 $backend->set_always('name', 'Mock');
653 my $config = Test::MockObject->new;
654 $config->set_always('backend_dir', "/tmp");
656 my $illrq = $builder->build({source => 'Illrequest'});
657 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
658 $illrq_obj->_config($config);
659 $illrq_obj->_backend($backend);
661 $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
663 my $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564 });
664 is_deeply($censor_out, { foo => 'bar', baz => 564, display_reply_date => 1 },
665 "_censor: not OPAC, reply_date = 1");
667 $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564, opac => 1 });
668 is_deeply($censor_out, {
669 foo => 'bar', baz => 564, censor_notes_staff => 1,
670 display_reply_date => 1, opac => 1
671 }, "_censor: notes_staff = 0, reply_date = 0");
673 $schema->storage->txn_rollback;
676 subtest 'Checking Limits' => sub {
680 $schema->storage->txn_begin;
682 # Build infrastructure
683 my $backend = Test::MockObject->new;
684 $backend->set_isa('Koha::Illbackends::Mock');
685 $backend->set_always('name', 'Mock');
687 my $config = Test::MockObject->new;
688 $config->set_always('backend_dir', "/tmp");
690 my $illrq = $builder->build({source => 'Illrequest'});
691 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
692 $illrq_obj->_config($config);
693 $illrq_obj->_backend($backend);
696 $config->set_series('getLimitRules',
697 { CPL => { count => 1, method => 'test' } },
698 { default => { count => 0, method => 'active' } });
699 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
700 { count => 1, method => 'test' },
701 "getLimits: by value.");
702 is_deeply($illrq_obj->getLimits({ type => 'branch' }),
703 { count => 0, method => 'active' },
704 "getLimits: by default.");
705 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
706 { count => -1, method => 'active' },
707 "getLimits: by hard-coded.");
710 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
711 1, "_limit_counter: Initial branch annual count.");
712 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
713 1, "_limit_counter: Initial branch active count.");
714 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
715 1, "_limit_counter: Initial patron annual count.");
716 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
717 1, "_limit_counter: Initial patron active count.");
719 source => 'Illrequest',
721 branchcode => $illrq_obj->branchcode,
722 borrowernumber => $illrq_obj->borrowernumber,
725 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
726 2, "_limit_counter: Add a qualifying request for branch annual count.");
727 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
728 2, "_limit_counter: Add a qualifying request for branch active count.");
729 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
730 2, "_limit_counter: Add a qualifying request for patron annual count.");
731 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
732 2, "_limit_counter: Add a qualifying request for patron active count.");
734 source => 'Illrequest',
736 branchcode => $illrq_obj->branchcode,
737 borrowernumber => $illrq_obj->borrowernumber,
738 placed => "2005-05-31",
741 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
742 2, "_limit_counter: Add an out-of-date branch request.");
743 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
744 3, "_limit_counter: Add a qualifying request for branch active count.");
745 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
746 2, "_limit_counter: Add an out-of-date patron request.");
747 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
748 3, "_limit_counter: Add a qualifying request for patron active count.");
750 source => 'Illrequest',
752 branchcode => $illrq_obj->branchcode,
753 borrowernumber => $illrq_obj->borrowernumber,
757 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
758 3, "_limit_counter: Add a qualifying request for branch annual count.");
759 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
760 3, "_limit_counter: Add a completed request for branch active count.");
761 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
762 3, "_limit_counter: Add a qualifying request for patron annual count.");
763 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
764 3, "_limit_counter: Add a completed request for patron active count.");
768 # We've tested _limit_counter, so all we need to test here is whether the
769 # current counts of 3 for each work as they should against different
770 # configuration declarations.
773 $config->set_always('getLimitRules', undef);
774 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
775 librarycode => $illrq_obj->branchcode}),
776 1, "check_limits: no configuration => no limits.");
779 $config->set_always('getLimitRules',
780 { $illrq_obj->branchcode => { count => 1, method => 'active' } });
781 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
782 librarycode => $illrq_obj->branchcode}),
783 0, "check_limits: branch active limit exceeded.");
784 $config->set_always('getLimitRules',
785 { $illrq_obj->branchcode => { count => 1, method => 'annual' } });
786 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
787 librarycode => $illrq_obj->branchcode}),
788 0, "check_limits: branch annual limit exceeded.");
789 $config->set_always('getLimitRules',
790 { $illrq_obj->branchcode => { count => 4, method => 'active' } });
791 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
792 librarycode => $illrq_obj->branchcode}),
793 1, "check_limits: branch active limit OK.");
794 $config->set_always('getLimitRules',
795 { $illrq_obj->branchcode => { count => 4, method => 'annual' } });
796 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
797 librarycode => $illrq_obj->branchcode}),
798 1, "check_limits: branch annual limit OK.");
801 $config->set_always('getLimitRules',
802 { $illrq_obj->patron->categorycode => { count => 1, method => 'active' } });
803 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
804 librarycode => $illrq_obj->branchcode}),
805 0, "check_limits: patron category active limit exceeded.");
806 $config->set_always('getLimitRules',
807 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
808 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
809 librarycode => $illrq_obj->branchcode}),
810 0, "check_limits: patron category annual limit exceeded.");
811 $config->set_always('getLimitRules',
812 { $illrq_obj->patron->categorycode => { count => 4, method => 'active' } });
813 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
814 librarycode => $illrq_obj->branchcode}),
815 1, "check_limits: patron category active limit OK.");
816 $config->set_always('getLimitRules',
817 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
818 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
819 librarycode => $illrq_obj->branchcode}),
820 1, "check_limits: patron category annual limit OK.");
822 # One rule cancels the other
823 $config->set_series('getLimitRules',
824 # Branch rules allow request
825 { $illrq_obj->branchcode => { count => 4, method => 'active' } },
826 # Patron rule forbids it
827 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
828 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
829 librarycode => $illrq_obj->branchcode}),
830 0, "check_limits: patron category veto overrides branch OK.");
831 $config->set_series('getLimitRules',
832 # Branch rules allow request
833 { $illrq_obj->branchcode => { count => 1, method => 'active' } },
834 # Patron rule forbids it
835 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
836 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
837 librarycode => $illrq_obj->branchcode}),
838 0, "check_limits: branch veto overrides patron category OK.");
840 $schema->storage->txn_rollback;
843 subtest 'Custom statuses' => sub {
847 $schema->storage->txn_begin;
849 my $cat = Koha::AuthorisedValueCategories->search(
851 category_name => 'ILLSTATUS'
855 if ($cat->count == 0) {
856 $cat = $builder->build_object(
858 class => 'Koha::AuthorisedValueCategory',
860 category_name => 'ILLSTATUS'
866 my $av = $builder->build_object(
868 class => 'Koha::AuthorisedValues',
870 category => 'ILLSTATUS'
875 is($av->category, 'ILLSTATUS',
876 "Successfully created authorised value for custom status");
878 my $ill_req = $builder->build_object(
880 class => 'Koha::Illrequests',
882 status_alias => $av->authorised_value
886 isa_ok($ill_req->statusalias, 'Koha::AuthorisedValue',
887 "statusalias correctly returning Koha::AuthorisedValue object");
889 $ill_req->status("COMP");
890 is($ill_req->statusalias, undef,
891 "Koha::Illrequest->status overloading resetting status_alias");
893 $schema->storage->txn_rollback;