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 C4::Circulation qw( AddIssue AddReturn );
25 use Koha::Illrequestattributes;
26 use Koha::Illrequest::Config;
32 use Koha::MessageAttributes;
33 use Koha::Notice::Templates;
34 use Koha::AuthorisedValueCategories;
35 use Koha::AuthorisedValues;
37 use t::lib::TestBuilder;
41 use Test::Deep qw/ cmp_deeply ignore /;
44 use Test::More tests => 13;
46 my $schema = Koha::Database->new->schema;
47 my $builder = t::lib::TestBuilder->new;
48 use_ok('Koha::Illrequest');
49 use_ok('Koha::Illrequests');
51 subtest 'Basic object tests' => sub {
55 $schema->storage->txn_begin;
57 Koha::Illrequests->search->delete;
58 my $illrq = $builder->build({ source => 'Illrequest' });
59 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
61 isa_ok($illrq_obj, 'Koha::Illrequest',
62 "Correctly create and load an illrequest object.");
63 isa_ok($illrq_obj->_config, 'Koha::Illrequest::Config',
64 "Created a config object as part of Illrequest creation.");
66 is($illrq_obj->illrequest_id, $illrq->{illrequest_id},
67 "Illrequest_id getter works.");
68 is($illrq_obj->borrowernumber, $illrq->{borrowernumber},
69 "Borrowernumber getter works.");
70 is($illrq_obj->biblio_id, $illrq->{biblio_id},
71 "Biblio_Id getter works.");
72 is($illrq_obj->branchcode, $illrq->{branchcode},
73 "Branchcode getter works.");
74 is($illrq_obj->status, $illrq->{status},
75 "Status getter works.");
76 is($illrq_obj->placed, $illrq->{placed},
77 "Placed getter works.");
78 is($illrq_obj->replied, $illrq->{replied},
79 "Replied getter works.");
80 is($illrq_obj->updated, $illrq->{updated},
81 "Updated getter works.");
82 is($illrq_obj->completed, $illrq->{completed},
83 "Completed getter works.");
84 is($illrq_obj->medium, $illrq->{medium},
85 "Medium getter works.");
86 is($illrq_obj->accessurl, $illrq->{accessurl},
87 "Accessurl getter works.");
88 is($illrq_obj->cost, $illrq->{cost},
89 "Cost getter works.");
90 is($illrq_obj->price_paid, $illrq->{price_paid},
91 "Price_paid getter works.");
92 is($illrq_obj->notesopac, $illrq->{notesopac},
93 "Notesopac getter works.");
94 is($illrq_obj->notesstaff, $illrq->{notesstaff},
95 "Notesstaff getter works.");
96 is($illrq_obj->orderid, $illrq->{orderid},
97 "Orderid getter works.");
98 is($illrq_obj->backend, $illrq->{backend},
99 "Backend getter works.");
101 is($illrq_obj->get_type, undef,
102 'get_type() returns undef if no type is set');
104 source => 'Illrequestattribute',
106 illrequest_id => $illrq_obj->illrequest_id,
111 is($illrq_obj->get_type, 'book',
112 'get_type() returns correct type if set');
114 isnt($illrq_obj->status, 'COMP',
115 "ILL is not currently marked complete.");
116 $illrq_obj->mark_completed;
117 is($illrq_obj->status, 'COMP',
118 "ILL is now marked complete.");
122 is(Koha::Illrequests->search->count, 0,
123 "No illrequest found after delete.");
125 $schema->storage->txn_rollback;
128 subtest 'Working with related objects' => sub {
132 $schema->storage->txn_begin;
134 Koha::Illrequests->search->delete;
136 my $patron = $builder->build({ source => 'Borrower' });
137 my $illrq = $builder->build({
138 source => 'Illrequest',
139 value => { borrowernumber => $patron->{borrowernumber} }
141 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
143 isa_ok($illrq_obj->patron, 'Koha::Patron',
144 "OK accessing related patron.");
147 source => 'Illrequestattribute',
148 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
151 source => 'Illrequestattribute',
152 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
155 source => 'Illrequestattribute',
156 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
159 is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
160 "Fetching expected number of Illrequestattributes for our request.");
162 my $illrq1 = $builder->build({ source => 'Illrequest' });
164 source => 'Illrequestattribute',
165 value => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
168 is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
169 "Fetching expected number of Illrequestattributes for our request.");
171 is($illrq_obj->biblio, undef, "->biblio returns undef if no biblio");
172 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
173 my $req_bib = $builder->build_object({
174 class => 'Koha::Illrequests',
176 biblio_id => $biblio->biblionumber
179 isa_ok($req_bib->biblio, 'Koha::Biblio', "OK accessing related biblio");
182 is(Koha::Illrequestattributes->search->count, 1,
183 "Correct number of illrequestattributes after delete.");
185 isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
186 "Borrower was not deleted after illrq delete.");
188 $schema->storage->txn_rollback;
191 subtest 'Status Graph tests' => sub {
195 $schema->storage->txn_begin;
197 my $illrq = $builder->build({source => 'Illrequest'});
198 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
200 # _core_status_graph tests: it's just a constant, so here we just make
201 # sure it returns a hashref.
202 is(ref $illrq_obj->_core_status_graph, "HASH",
203 "_core_status_graph returns a hash.");
205 # _status_graph_union: let's try different merge operations.
208 $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
209 $illrq_obj->_core_status_graph,
210 "core_status_graph + null = core_status_graph"
215 $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
216 $illrq_obj->_core_status_graph,
217 "null + core_status_graph = core_status_graph"
220 # Correct merge behaviour
222 $illrq_obj->_status_graph_union({
230 prev_actions => [ 'REQ' ],
232 next_actions => [ 'REQ' ],
237 prev_actions => [ 'QER' ],
239 next_actions => [ 'QER' ],
242 prev_actions => [ 'REQ' ],
244 next_actions => [ 'REQ' ],
247 "REQ atom + linking QER = cyclical status graph"
250 # Create a new node, with no prev_actions and no next_actions. This should
251 # protect us against regressions related to bug 22280.
259 # Add the new node to the core_status_grpah
260 my $new_graph = $illrq_obj->_status_graph_union( $new_node, $illrq_obj->_core_status_graph);
261 # Compare the updated graph to the expected graph
262 # The structure we compare against here is just a copy of the structure found
263 # in Koha::Illrequest::_core_status_graph() + the new node we created above
264 cmp_deeply( $new_graph,
272 prev_actions => [ ], # Actions containing buttons
273 # leading to this status
274 id => 'NEW', # ID of this status
275 name => 'New request', # UI name of this status
276 ui_method_name => 'New request', # UI name of method leading
278 method => 'create', # method to this status
279 next_actions => [ 'REQ', 'GENREQ', 'KILL' ], # buttons to add to all
280 # requests with this status
281 ui_method_icon => 'fa-plus', # UI Style class
284 prev_actions => [ 'NEW', 'REQREV', 'QUEUED', 'CANCREQ' ],
287 ui_method_name => 'Confirm request',
289 next_actions => [ 'REQREV', 'COMP', 'CHK' ],
290 ui_method_icon => 'fa-check',
293 prev_actions => [ 'NEW', 'REQREV' ],
295 name => 'Requested from partners',
296 ui_method_name => 'Place request with partners',
297 method => 'generic_confirm',
298 next_actions => [ 'COMP', 'CHK' ],
299 ui_method_icon => 'fa-send-o',
302 prev_actions => [ 'REQ' ],
304 name => 'Request reverted',
305 ui_method_name => 'Revert Request',
307 next_actions => [ 'REQ', 'GENREQ', 'KILL' ],
308 ui_method_icon => 'fa-times',
313 name => 'Queued request',
316 next_actions => [ 'REQ', 'KILL' ],
320 prev_actions => [ 'NEW' ],
322 name => 'Cancellation requested',
325 next_actions => [ 'KILL', 'REQ' ],
329 prev_actions => [ 'REQ' ],
332 ui_method_name => 'Mark completed',
333 method => 'mark_completed',
334 next_actions => [ 'CHK' ],
335 ui_method_icon => 'fa-check',
338 prev_actions => [ 'QUEUED', 'REQREV', 'NEW', 'CANCREQ' ],
341 ui_method_name => 'Delete request',
344 ui_method_icon => 'fa-trash',
347 prev_actions => [ 'REQ', 'GENREQ', 'COMP' ],
349 name => 'Checked out',
350 ui_method_name => 'Check out',
351 needs_prefs => [ 'CirculateILL' ],
352 needs_perms => [ 'user_circulate_circulate_remaining_permissions' ],
353 needs_all => ignore(),
354 method => 'check_out',
356 ui_method_icon => 'fa-upload',
359 prev_actions => [ 'CHK' ],
361 name => 'Returned to library',
362 ui_method_name => 'Check in',
363 method => 'check_in',
364 next_actions => [ 'COMP' ],
365 ui_method_icon => 'fa-download',
368 "new node + core_status_graph = bigger status graph"
369 ) || diag explain $new_graph;
371 $schema->storage->txn_rollback;
374 subtest 'Backend testing (mocks)' => sub {
378 $schema->storage->txn_begin;
380 # testing load_backend & available_backends requires that we have at least
381 # the Dummy plugin installed. load_backend & available_backends don't
382 # currently have tests as a result.
384 t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' } );
385 my $backend = Test::MockObject->new;
386 $backend->set_isa('Koha::Illbackends::Mock');
387 $backend->set_always('name', 'Mock');
389 my $patron = $builder->build({ source => 'Borrower' });
390 my $illrq = $builder->build_object({
391 class => 'Koha::Illrequests',
394 $illrq->_backend($backend);
396 isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
397 "OK accessing mocked backend.");
399 # _backend_capability tests:
400 # We need to test whether this optional feature of a mocked backend
401 # behaves as expected.
402 # 3 scenarios: feature not implemented, feature implemented, but requested
403 # capability is not provided by backend, & feature is implemented &
404 # capability exists. This method can be used to implement custom backend
405 # functionality, such as unmediated in the BLDSS backend (also see
407 $backend->set_always('capabilities', undef);
408 is($illrq->_backend_capability('Test'), 0,
409 "0 returned on Mock not implementing capabilities.");
411 $backend->set_always('capabilities', 0);
412 is($illrq->_backend_capability('Test'), 0,
413 "0 returned on Mock not implementing Test capability.");
415 $backend->set_always('capabilities', sub { return 'bar'; } );
416 is($illrq->_backend_capability('Test'), 'bar',
417 "'bar' returned on Mock implementing Test capability.");
419 # metadata test: we need to be sure that we return the arbitrary values
424 my ( $self, $rq ) = @_;
426 ID => $rq->illrequest_id,
427 Title => $rq->patron->borrowernumber
435 ID => $illrq->illrequest_id,
436 Title => $illrq->patron->borrowernumber
443 # No backend graph extension
444 $backend->set_always('status_graph', {});
445 is_deeply($illrq->capabilities('COMP'),
447 prev_actions => [ 'REQ' ],
450 ui_method_name => 'Mark completed',
451 method => 'mark_completed',
452 next_actions => [ 'CHK' ],
453 ui_method_icon => 'fa-check',
455 "Dummy status graph for COMP.");
456 is($illrq->capabilities('UNKNOWN'), undef,
457 "Dummy status graph for UNKNOWN.");
458 is_deeply($illrq->capabilities(),
459 $illrq->_core_status_graph,
460 "Dummy full status graph.");
461 # Simple backend graph extension
462 $backend->set_always('status_graph',
465 prev_actions => [ 'REQ' ],
467 next_actions => [ 'REQ' ],
470 is_deeply($illrq->capabilities('QER'),
472 prev_actions => [ 'REQ' ],
474 next_actions => [ 'REQ' ],
476 "Simple status graph for QER.");
477 is($illrq->capabilities('UNKNOWN'), undef,
478 "Simple status graph for UNKNOWN.");
479 is_deeply($illrq->capabilities(),
480 $illrq->_status_graph_union(
481 $illrq->_core_status_graph,
484 prev_actions => [ 'REQ' ],
486 next_actions => [ 'REQ' ],
490 "Simple full status graph.");
494 # No backend graph extension
495 $backend->set_always('status_graph', {});
496 is($illrq->custom_capability('unknown', {}), 0,
497 "Unknown candidate.");
499 # Simple backend graph extension
500 $backend->set_always('status_graph',
503 prev_actions => [ 'REQ' ],
505 method => 'identity',
506 next_actions => [ 'REQ' ],
509 $backend->mock('identity',
510 sub { my ( $self, $params ) = @_; return $params->{other}; });
511 is($illrq->custom_capability('identity', { test => 1, method => 'blah' })->{test}, 1,
512 "Resolve identity custom_capability");
514 $schema->storage->txn_rollback;
518 subtest 'Backend core methods' => sub {
522 $schema->storage->txn_begin;
524 # Build infrastructure
525 my $backend = Test::MockObject->new;
526 $backend->set_isa('Koha::Illbackends::Mock');
527 $backend->set_always('name', 'Mock');
528 $backend->mock('capabilities', sub { return 'Mock'; });
530 my $config = Test::MockObject->new;
531 $config->set_always('backend_dir', "/tmp");
532 $config->set_always('getLimitRules',
533 { default => { count => 0, method => 'active' } });
535 my $illrq = $builder->build_object({
536 class => 'Koha::Illrequests',
537 value => { backend => undef }
539 $illrq->_config($config);
541 # Test error conditions (no backend)
542 throws_ok { $illrq->load_backend; }
543 'Koha::Exceptions::Ill::InvalidBackendId',
544 'Exception raised correctly';
546 throws_ok { $illrq->load_backend(''); }
547 'Koha::Exceptions::Ill::InvalidBackendId',
548 'Exception raised correctly';
550 # Now load the mocked backend
551 $illrq->_backend($backend);
554 is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
558 template => "/tmp/Mock/intra-includes/bar.inc",
559 opac_template => "/tmp/Mock/opac-includes/bar.inc",
564 # we are testing simple cases.
565 $backend->set_series('create',
566 { stage => 'bar', method => 'create' },
567 { stage => 'commit', method => 'create' },
568 { stage => 'commit', method => 'create' },
569 { stage => 'commit', method => 'create' },
570 { stage => 'commit', method => 'create' });
572 is_deeply($illrq->backend_create({test => 1}),
574 stage => 'bar', method => 'create',
575 template => "/tmp/Mock/intra-includes/create.inc",
576 opac_template => "/tmp/Mock/opac-includes/create.inc",
578 "Backend create: arbitrary stage.");
580 is_deeply($illrq->backend_create({test => 1}),
582 stage => 'commit', method => 'create', permitted => 0,
583 template => "/tmp/Mock/intra-includes/create.inc",
584 opac_template => "/tmp/Mock/opac-includes/create.inc",
586 "Backend create: arbitrary stage, not permitted.");
587 is($illrq->status, "QUEUED", "Backend create: queued if restricted.");
588 $config->set_always('getLimitRules', {});
589 $illrq->status('NEW');
590 is_deeply($illrq->backend_create({test => 1}),
592 stage => 'commit', method => 'create', permitted => 1,
593 template => "/tmp/Mock/intra-includes/create.inc",
594 opac_template => "/tmp/Mock/opac-includes/create.inc",
596 "Backend create: arbitrary stage, permitted.");
597 is($illrq->status, "NEW", "Backend create: not-queued.");
599 # Test that enabling the unmediated workflow causes the backend's
600 # 'unmediated_ill' method to be called
601 t::lib::Mocks::mock_preference('ILLModuleUnmediated', '1');
605 my ($self, $name) = @_;
606 if ($name eq 'unmediated_ill') {
608 return { unmediated_ill => 1 };
613 $illrq->status('NEW');
615 $illrq->backend_create({test => 1}),
617 'opac_template' => '/tmp/Mock/opac-includes/.inc',
618 'template' => '/tmp/Mock/intra-includes/.inc',
619 'unmediated_ill' => 1
621 "Backend create: commit stage, permitted, ILLModuleUnmediated enabled."
624 # Test that disabling the unmediated workflow causes the backend's
625 # 'unmediated_ill' method to be NOT called
626 t::lib::Mocks::mock_preference('ILLModuleUnmediated', '0');
627 $illrq->status('NEW');
629 $illrq->backend_create({test => 1}),
631 stage => 'commit', method => 'create', permitted => 1,
632 template => "/tmp/Mock/intra-includes/create.inc",
633 opac_template => "/tmp/Mock/opac-includes/create.inc",
635 "Backend create: commit stage, permitted, ILLModuleUnmediated disabled."
639 $backend->set_series('renew', { stage => 'bar', method => 'renew' });
640 is_deeply($illrq->backend_renew({test => 1}),
642 stage => 'bar', method => 'renew',
643 template => "/tmp/Mock/intra-includes/renew.inc",
644 opac_template => "/tmp/Mock/opac-includes/renew.inc",
646 "Backend renew: arbitrary stage.");
649 $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
650 is_deeply($illrq->backend_cancel({test => 1}),
652 stage => 'bar', method => 'cancel',
653 template => "/tmp/Mock/intra-includes/cancel.inc",
654 opac_template => "/tmp/Mock/opac-includes/cancel.inc",
656 "Backend cancel: arbitrary stage.");
658 # backend_update_status
659 $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
660 is_deeply($illrq->backend_update_status({test => 1}),
662 stage => 'bar', method => 'update_status',
663 template => "/tmp/Mock/intra-includes/update_status.inc",
664 opac_template => "/tmp/Mock/opac-includes/update_status.inc",
666 "Backend update_status: arbitrary stage.");
669 $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
670 is_deeply($illrq->backend_confirm({test => 1}),
672 stage => 'bar', method => 'confirm',
673 template => "/tmp/Mock/intra-includes/confirm.inc",
674 opac_template => "/tmp/Mock/opac-includes/confirm.inc",
676 "Backend confirm: arbitrary stage.");
678 $config->set_always('partner_code', "ILLTSTLIB");
679 $backend->set_always('metadata', { Test => "Foobar" });
680 my $illbrn = $builder->build({
682 value => { branchemail => "", branchreplyto => "" }
684 my $partner1 = $builder->build({
685 source => 'Borrower',
686 value => { categorycode => "ILLTSTLIB" },
688 my $partner2 = $builder->build({
689 source => 'Borrower',
690 value => { categorycode => "ILLTSTLIB" },
692 my $gen_conf = $illrq->generic_confirm({
693 current_branchcode => $illbrn->{branchcode}
695 isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
696 "Generic confirm: draft contains metadata."
698 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
699 "Generic cofnirm: partner 1 is correct."
701 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
702 "Generic confirm: partner 2 is correct."
705 dies_ok { $illrq->generic_confirm({
706 current_branchcode => $illbrn->{branchcode},
709 "Generic confirm: missing to dies OK.";
711 $schema->storage->txn_rollback;
715 subtest 'Helpers' => sub {
719 $schema->storage->txn_begin;
721 # Build infrastructure
722 my $backend = Test::MockObject->new;
723 $backend->set_isa('Koha::Illbackends::Mock');
724 $backend->set_always('name', 'Mock');
728 my ( $self, $rq ) = @_;
736 my $config = Test::MockObject->new;
737 $config->set_always('backend_dir', "/tmp");
739 my $patron = $builder->build({
740 source => 'Borrower',
741 value => { categorycode => "A" }
743 # Create a mocked branch with no email addressed defined
744 my $illbrn = $builder->build({
749 branchillemail => "",
753 my $illrq = $builder->build({
754 source => 'Illrequest',
755 value => { branchcode => "HDE", borrowernumber => $patron->{borrowernumber} }
757 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
758 $illrq_obj->_config($config);
759 $illrq_obj->_backend($backend);
762 $config->set_series('getPrefixes',
763 { HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
764 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
765 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "HDE" }), "TEST",
766 "getPrefix: branch");
767 $config->set_series('getPrefixes',
768 { HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
769 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
770 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
771 "getPrefix: default");
772 $config->set_always('getPrefixes', {});
773 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
774 "getPrefix: the empty prefix");
777 $config->set_series('getPrefixes',
778 { HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
779 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
780 is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
781 $config->set_series('getPrefixes',
782 { HDET => "TEST", TSLT => "BAR", default => "DEFAULT" },
783 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
784 is($illrq_obj->id_prefix, "", "id_prefix: default");
786 # requires_moderation
787 $illrq_obj->status('NEW')->store;
788 is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
789 $illrq_obj->status('CANCREQ')->store;
790 is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
793 my $attr = Koha::MessageAttributes->find({ message_name => 'Ill_ready' });
794 C4::Members::Messaging::SetMessagingPreference({
795 borrowernumber => $patron->{borrowernumber},
796 message_attribute_id => $attr->message_attribute_id,
797 message_transport_types => ['email']
799 my $return_patron = $illrq_obj->send_patron_notice('ILL_PICKUP_READY');
800 my $notice = $schema->resultset('MessageQueue')->search({
801 letter_code => 'ILL_PICKUP_READY',
802 message_transport_type => 'email',
803 borrowernumber => $illrq_obj->borrowernumber
804 })->next()->letter_code;
807 { result => { success => ['email'], fail => [] } },
808 "Correct return when notice created"
810 is($notice, 'ILL_PICKUP_READY' ,"Notice is correctly created");
812 my $return_patron_fail = $illrq_obj->send_patron_notice();
815 { error => 'notice_no_type' },
816 "Correct error when missing type"
820 # Specify that no staff notices should be send
821 t::lib::Mocks::mock_preference('ILLSendStaffNotices', '');
822 my $return_staff_cancel_fail =
823 $illrq_obj->send_staff_notice('ILL_REQUEST_CANCEL');
825 $return_staff_cancel_fail,
826 { error => 'notice_not_enabled' },
827 "Does not send notices that are not enabled"
829 my $queue = $schema->resultset('MessageQueue')->search({
830 letter_code => 'ILL_REQUEST_CANCEL'
832 is($queue->count, 0, "Notice is not queued");
834 # Specify that the cancel notice can be sent
835 t::lib::Mocks::mock_preference('ILLSendStaffNotices', 'ILL_REQUEST_CANCEL');
836 my $return_staff_cancel = $illrq_obj->send_staff_notice(
840 $return_staff_cancel,
841 { success => 'notice_queued' },
842 "Correct return when staff notice created"
844 $queue = $schema->resultset('MessageQueue')->search({
845 letter_code => 'ILL_REQUEST_CANCEL'
847 is($queue->count, 1, "Notice queued as expected");
849 my $return_staff_fail = $illrq_obj->send_staff_notice();
852 { error => 'notice_no_type' },
853 "Correct error when missing type"
855 $queue = $schema->resultset('MessageQueue')->search({
856 letter_code => 'ILL_REQUEST_CANCEL'
858 is($queue->count, 1, "Notice is not queued");
861 my $not = $illrq_obj->get_notice({
862 notice_code => 'ILL_REQUEST_CANCEL',
866 # We test the properties of the hashref separately because the random
867 # hash ordering of the metadata means we can't test the entire thing
870 $not->{module} eq 'ill',
871 'Correct module return from get_notice'
874 $not->{name} eq 'ILL request cancelled',
875 'Correct name return from get_notice'
878 $not->{message_transport_type} eq 'email',
879 'Correct message_transport_type return from get_notice'
882 $not->{title} eq 'Interlibrary loan request cancelled',
883 'Correct title return from get_notice'
885 $not->{content} =~ s/\s//g;
888 $not->{content},"Thepatronforinterlibraryloansrequest" . $illrq_obj->id . ",withthefollowingdetails,hasrequestedcancellationofthisILLrequest:-author:myauthor-title:mytitle",
889 'Correct content returned from get_notice with metadata correctly ordered'
892 $schema->storage->txn_rollback;
896 subtest 'Censorship' => sub {
900 $schema->storage->txn_begin;
902 # Build infrastructure
903 my $backend = Test::MockObject->new;
904 $backend->set_isa('Koha::Illbackends::Mock');
905 $backend->set_always('name', 'Mock');
907 my $config = Test::MockObject->new;
908 $config->set_always('backend_dir', "/tmp");
910 my $illrq = $builder->build({source => 'Illrequest'});
911 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
912 $illrq_obj->_config($config);
913 $illrq_obj->_backend($backend);
915 $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
917 my $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564 });
918 is_deeply($censor_out, { foo => 'bar', baz => 564, display_reply_date => 1 },
919 "_censor: not OPAC, reply_date = 1");
921 $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564, opac => 1 });
922 is_deeply($censor_out, {
923 foo => 'bar', baz => 564, censor_notes_staff => 1,
924 display_reply_date => 1, opac => 1
925 }, "_censor: notes_staff = 0, reply_date = 0");
927 $schema->storage->txn_rollback;
930 subtest 'Checking out' => sub {
934 $schema->storage->txn_begin;
936 my $itemtype = $builder->build_object({
937 class => 'Koha::ItemTypes',
942 my $library = $builder->build_object({ class => 'Koha::Libraries' });
943 my $biblio = $builder->build_sample_biblio();
944 my $patron = $builder->build_object({
945 class => 'Koha::Patrons',
946 value => { category_type => 'x' }
948 my $request = $builder->build_object({
949 class => 'Koha::Illrequests',
951 borrowernumber => $patron->borrowernumber,
952 biblio_id => $biblio->biblionumber
956 # First test that calling check_out without a stage param returns
957 # what's required to build the form
958 my $no_stage = $request->check_out();
959 is($no_stage->{method}, 'check_out');
960 is($no_stage->{stage}, 'form');
961 isa_ok($no_stage->{value}, 'HASH');
962 isa_ok($no_stage->{value}->{itemtypes}, 'Koha::ItemTypes');
963 isa_ok($no_stage->{value}->{libraries}, 'Koha::Libraries');
964 isa_ok($no_stage->{value}->{statistical}, 'Koha::Patrons');
965 isa_ok($no_stage->{value}->{biblio}, 'Koha::Biblio');
967 # Now test that form validation works when we supply a 'form' stage
970 my $form_stage_missing_params = $request->check_out({
973 is_deeply($form_stage_missing_params->{value}->{errors}, {
976 # inhouse passed but not a valid patron
977 my $form_stage_bad_patron = $request->check_out({
979 item_type => $itemtype->itemtype,
980 inhouse => 'I_DONT_EXIST'
982 is_deeply($form_stage_bad_patron->{value}->{errors}, {
985 # Too many items attached to biblio
986 my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
987 my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
988 my $form_stage_two_items = $request->check_out({
990 item_type => $itemtype->itemtype,
992 is_deeply($form_stage_two_items->{value}->{errors}, {
996 # Delete the items we created, so we can test that we can create one
1000 # We need to mock the user environment for AddIssue
1001 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1004 # First we pass bad parameters to the item creation to test we're
1005 # catching the failure of item creation
1006 my $form_stage_bad_branchcode;
1008 $form_stage_bad_branchcode = $request->check_out({
1010 item_type => $itemtype->itemtype,
1013 } qr/DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/,
1014 "Item creation fails on bad parameters";
1016 is_deeply($form_stage_bad_branchcode->{value}->{errors}, {
1018 },"We get expected failure of item creation");
1020 # Now create a proper item
1021 my $form_stage_good_branchcode = $request->check_out({
1023 item_type => $itemtype->itemtype,
1024 branchcode => $library->branchcode
1026 # By default, this item should not be loanable, so check that we're
1027 # informed of that fact
1029 $form_stage_good_branchcode->{value}->{check_out_errors},
1033 itemtype_notforloan => $itemtype->itemtype
1036 "We get expected error on notforloan of item"
1038 # Delete the item that was created
1039 $biblio->items->delete;
1040 # Now create an itemtype that is loanable
1041 my $itemtype_loanable = $builder->build_object({
1042 class => 'Koha::ItemTypes',
1047 # We need to mock the user environment for AddIssue
1048 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1049 my $form_stage_loanable = $request->check_out({
1051 item_type => $itemtype_loanable->itemtype,
1052 branchcode => $library->branchcode
1054 is($form_stage_loanable->{stage}, 'done_check_out');
1055 isa_ok($patron->checkouts, 'Koha::Checkouts');
1056 is($patron->checkouts->count, 1);
1057 is($request->status, 'CHK');
1059 $schema->storage->txn_rollback;
1062 subtest 'Checking Limits' => sub {
1066 $schema->storage->txn_begin;
1068 # Build infrastructure
1069 my $backend = Test::MockObject->new;
1070 $backend->set_isa('Koha::Illbackends::Mock');
1071 $backend->set_always('name', 'Mock');
1073 my $config = Test::MockObject->new;
1074 $config->set_always('backend_dir', "/tmp");
1076 my $illrq = $builder->build({source => 'Illrequest'});
1077 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
1078 $illrq_obj->_config($config);
1079 $illrq_obj->_backend($backend);
1082 $config->set_series('getLimitRules',
1083 { CPL => { count => 1, method => 'test' } },
1084 { default => { count => 0, method => 'active' } });
1085 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
1086 { count => 1, method => 'test' },
1087 "getLimits: by value.");
1088 is_deeply($illrq_obj->getLimits({ type => 'branch' }),
1089 { count => 0, method => 'active' },
1090 "getLimits: by default.");
1091 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
1092 { count => -1, method => 'active' },
1093 "getLimits: by hard-coded.");
1096 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
1097 1, "_limit_counter: Initial branch annual count.");
1098 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
1099 1, "_limit_counter: Initial branch active count.");
1100 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
1101 1, "_limit_counter: Initial patron annual count.");
1102 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
1103 1, "_limit_counter: Initial patron active count.");
1105 source => 'Illrequest',
1107 branchcode => $illrq_obj->branchcode,
1108 borrowernumber => $illrq_obj->borrowernumber,
1111 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
1112 2, "_limit_counter: Add a qualifying request for branch annual count.");
1113 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
1114 2, "_limit_counter: Add a qualifying request for branch active count.");
1115 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
1116 2, "_limit_counter: Add a qualifying request for patron annual count.");
1117 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
1118 2, "_limit_counter: Add a qualifying request for patron active count.");
1120 source => 'Illrequest',
1122 branchcode => $illrq_obj->branchcode,
1123 borrowernumber => $illrq_obj->borrowernumber,
1124 placed => "2005-05-31",
1127 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
1128 2, "_limit_counter: Add an out-of-date branch request.");
1129 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
1130 3, "_limit_counter: Add a qualifying request for branch active count.");
1131 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
1132 2, "_limit_counter: Add an out-of-date patron request.");
1133 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
1134 3, "_limit_counter: Add a qualifying request for patron active count.");
1136 source => 'Illrequest',
1138 branchcode => $illrq_obj->branchcode,
1139 borrowernumber => $illrq_obj->borrowernumber,
1143 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
1144 3, "_limit_counter: Add a qualifying request for branch annual count.");
1145 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
1146 3, "_limit_counter: Add a completed request for branch active count.");
1147 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
1148 3, "_limit_counter: Add a qualifying request for patron annual count.");
1149 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
1150 3, "_limit_counter: Add a completed request for patron active count.");
1154 # We've tested _limit_counter, so all we need to test here is whether the
1155 # current counts of 3 for each work as they should against different
1156 # configuration declarations.
1159 $config->set_always('getLimitRules', undef);
1160 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1161 librarycode => $illrq_obj->branchcode}),
1162 1, "check_limits: no configuration => no limits.");
1165 $config->set_always('getLimitRules',
1166 { $illrq_obj->branchcode => { count => 1, method => 'active' } });
1167 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1168 librarycode => $illrq_obj->branchcode}),
1169 0, "check_limits: branch active limit exceeded.");
1170 $config->set_always('getLimitRules',
1171 { $illrq_obj->branchcode => { count => 1, method => 'annual' } });
1172 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1173 librarycode => $illrq_obj->branchcode}),
1174 0, "check_limits: branch annual limit exceeded.");
1175 $config->set_always('getLimitRules',
1176 { $illrq_obj->branchcode => { count => 4, method => 'active' } });
1177 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1178 librarycode => $illrq_obj->branchcode}),
1179 1, "check_limits: branch active limit OK.");
1180 $config->set_always('getLimitRules',
1181 { $illrq_obj->branchcode => { count => 4, method => 'annual' } });
1182 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1183 librarycode => $illrq_obj->branchcode}),
1184 1, "check_limits: branch annual limit OK.");
1187 $config->set_always('getLimitRules',
1188 { $illrq_obj->patron->categorycode => { count => 1, method => 'active' } });
1189 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1190 librarycode => $illrq_obj->branchcode}),
1191 0, "check_limits: patron category active limit exceeded.");
1192 $config->set_always('getLimitRules',
1193 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
1194 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1195 librarycode => $illrq_obj->branchcode}),
1196 0, "check_limits: patron category annual limit exceeded.");
1197 $config->set_always('getLimitRules',
1198 { $illrq_obj->patron->categorycode => { count => 4, method => 'active' } });
1199 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1200 librarycode => $illrq_obj->branchcode}),
1201 1, "check_limits: patron category active limit OK.");
1202 $config->set_always('getLimitRules',
1203 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
1204 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1205 librarycode => $illrq_obj->branchcode}),
1206 1, "check_limits: patron category annual limit OK.");
1208 # One rule cancels the other
1209 $config->set_series('getLimitRules',
1210 # Branch rules allow request
1211 { $illrq_obj->branchcode => { count => 4, method => 'active' } },
1212 # Patron rule forbids it
1213 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
1214 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1215 librarycode => $illrq_obj->branchcode}),
1216 0, "check_limits: patron category veto overrides branch OK.");
1217 $config->set_series('getLimitRules',
1218 # Branch rules allow request
1219 { $illrq_obj->branchcode => { count => 1, method => 'active' } },
1220 # Patron rule forbids it
1221 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
1222 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1223 librarycode => $illrq_obj->branchcode}),
1224 0, "check_limits: branch veto overrides patron category OK.");
1226 $schema->storage->txn_rollback;
1229 subtest 'Custom statuses' => sub {
1233 $schema->storage->txn_begin;
1235 my $cat = Koha::AuthorisedValueCategories->search(
1237 category_name => 'ILLSTATUS'
1241 if ($cat->count == 0) {
1242 $cat = $builder->build_object(
1244 class => 'Koha::AuthorisedValueCategory',
1246 category_name => 'ILLSTATUS'
1252 my $av = $builder->build_object(
1254 class => 'Koha::AuthorisedValues',
1256 category => 'ILLSTATUS'
1261 is($av->category, 'ILLSTATUS',
1262 "Successfully created authorised value for custom status");
1264 my $ill_req = $builder->build_object(
1266 class => 'Koha::Illrequests',
1268 status_alias => $av->authorised_value
1272 isa_ok($ill_req->statusalias, 'Koha::AuthorisedValue',
1273 "statusalias correctly returning Koha::AuthorisedValue object");
1275 $ill_req->status("COMP");
1276 is($ill_req->statusalias, undef,
1277 "Koha::Illrequest->status overloading resetting status_alias");
1279 $schema->storage->txn_rollback;
1282 subtest 'Checking in hook' => sub {
1286 $schema->storage->txn_begin;
1288 # Build infrastructure
1289 my $backend = Test::MockObject->new;
1290 $backend->set_isa('Koha::Illbackends::Mock');
1291 $backend->set_always('name', 'Mock');
1293 my $config = Test::MockObject->new;
1294 $config->set_always('backend_dir', "/tmp");
1296 my $item = $builder->build_sample_item();
1297 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1299 t::lib::Mocks::mock_userenv(
1302 branchcode => $patron->branchcode
1306 my $illrq = $builder->build_object(
1308 class => 'Koha::Illrequests',
1310 biblio_id => $item->biblio->biblionumber,
1316 $illrq->_config($config);
1317 $illrq->_backend($backend);
1319 t::lib::Mocks::mock_preference('CirculateILL', 1);
1322 AddIssue( $patron->unblessed, $item->barcode );
1323 # Make the item withdrawn so checking-in is rejected
1324 t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems', 1);
1325 $item->set({ withdrawn => 1 })->store;
1326 AddReturn( $item->barcode, $patron->branchcode );
1328 $illrq->discard_changes;
1329 isnt( $illrq->status, 'RET' );
1331 # allow the check-in
1332 $item->set({ withdrawn => 0 })->store;
1333 AddReturn( $item->barcode, $patron->branchcode );
1335 $illrq->discard_changes;
1336 is( $illrq->status, 'RET' );
1338 $schema->storage->txn_rollback;