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;
29 use Koha::AuthorisedValueCategories;
30 use Koha::AuthorisedValues;
32 use t::lib::TestBuilder;
36 use Test::Deep qw/ cmp_deeply ignore /;
38 use Test::More tests => 12;
40 my $schema = Koha::Database->new->schema;
41 my $builder = t::lib::TestBuilder->new;
42 use_ok('Koha::Illrequest');
43 use_ok('Koha::Illrequests');
45 subtest 'Basic object tests' => sub {
49 $schema->storage->txn_begin;
51 Koha::Illrequests->search->delete;
52 my $illrq = $builder->build({ source => 'Illrequest' });
53 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
55 isa_ok($illrq_obj, 'Koha::Illrequest',
56 "Correctly create and load an illrequest object.");
57 isa_ok($illrq_obj->_config, 'Koha::Illrequest::Config',
58 "Created a config object as part of Illrequest creation.");
60 is($illrq_obj->illrequest_id, $illrq->{illrequest_id},
61 "Illrequest_id getter works.");
62 is($illrq_obj->borrowernumber, $illrq->{borrowernumber},
63 "Borrowernumber getter works.");
64 is($illrq_obj->biblio_id, $illrq->{biblio_id},
65 "Biblio_Id getter works.");
66 is($illrq_obj->branchcode, $illrq->{branchcode},
67 "Branchcode getter works.");
68 is($illrq_obj->status, $illrq->{status},
69 "Status getter works.");
70 is($illrq_obj->placed, $illrq->{placed},
71 "Placed getter works.");
72 is($illrq_obj->replied, $illrq->{replied},
73 "Replied getter works.");
74 is($illrq_obj->updated, $illrq->{updated},
75 "Updated getter works.");
76 is($illrq_obj->completed, $illrq->{completed},
77 "Completed getter works.");
78 is($illrq_obj->medium, $illrq->{medium},
79 "Medium getter works.");
80 is($illrq_obj->accessurl, $illrq->{accessurl},
81 "Accessurl getter works.");
82 is($illrq_obj->cost, $illrq->{cost},
83 "Cost getter works.");
84 is($illrq_obj->price_paid, $illrq->{price_paid},
85 "Price_paid getter works.");
86 is($illrq_obj->notesopac, $illrq->{notesopac},
87 "Notesopac getter works.");
88 is($illrq_obj->notesstaff, $illrq->{notesstaff},
89 "Notesstaff getter works.");
90 is($illrq_obj->orderid, $illrq->{orderid},
91 "Orderid getter works.");
92 is($illrq_obj->backend, $illrq->{backend},
93 "Backend getter works.");
95 is($illrq_obj->get_type, undef,
96 'get_type() returns undef if no type is set');
98 source => 'Illrequestattribute',
100 illrequest_id => $illrq_obj->illrequest_id,
105 is($illrq_obj->get_type, 'book',
106 'get_type() returns correct type if set');
108 isnt($illrq_obj->status, 'COMP',
109 "ILL is not currently marked complete.");
110 $illrq_obj->mark_completed;
111 is($illrq_obj->status, 'COMP',
112 "ILL is now marked complete.");
116 is(Koha::Illrequests->search->count, 0,
117 "No illrequest found after delete.");
119 $schema->storage->txn_rollback;
122 subtest 'Working with related objects' => sub {
126 $schema->storage->txn_begin;
128 Koha::Illrequests->search->delete;
130 my $patron = $builder->build({ source => 'Borrower' });
131 my $illrq = $builder->build({
132 source => 'Illrequest',
133 value => { borrowernumber => $patron->{borrowernumber} }
135 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
137 isa_ok($illrq_obj->patron, 'Koha::Patron',
138 "OK accessing related patron.");
141 source => 'Illrequestattribute',
142 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
145 source => 'Illrequestattribute',
146 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
149 source => 'Illrequestattribute',
150 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
153 is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
154 "Fetching expected number of Illrequestattributes for our request.");
156 my $illrq1 = $builder->build({ source => 'Illrequest' });
158 source => 'Illrequestattribute',
159 value => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
162 is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
163 "Fetching expected number of Illrequestattributes for our request.");
165 is($illrq_obj->biblio, undef, "->biblio returns undef if no biblio");
166 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
167 my $req_bib = $builder->build_object({
168 class => 'Koha::Illrequests',
170 biblio_id => $biblio->biblionumber
173 isa_ok($req_bib->biblio, 'Koha::Biblio', "OK accessing related biblio");
176 is(Koha::Illrequestattributes->search->count, 1,
177 "Correct number of illrequestattributes after delete.");
179 isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
180 "Borrower was not deleted after illrq delete.");
182 $schema->storage->txn_rollback;
185 subtest 'Status Graph tests' => sub {
189 $schema->storage->txn_begin;
191 my $illrq = $builder->build({source => 'Illrequest'});
192 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
194 # _core_status_graph tests: it's just a constant, so here we just make
195 # sure it returns a hashref.
196 is(ref $illrq_obj->_core_status_graph, "HASH",
197 "_core_status_graph returns a hash.");
199 # _status_graph_union: let's try different merge operations.
202 $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
203 $illrq_obj->_core_status_graph,
204 "core_status_graph + null = core_status_graph"
209 $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
210 $illrq_obj->_core_status_graph,
211 "null + core_status_graph = core_status_graph"
214 # Correct merge behaviour
216 $illrq_obj->_status_graph_union({
224 prev_actions => [ 'REQ' ],
226 next_actions => [ 'REQ' ],
231 prev_actions => [ 'QER' ],
233 next_actions => [ 'QER' ],
236 prev_actions => [ 'REQ' ],
238 next_actions => [ 'REQ' ],
241 "REQ atom + linking QER = cyclical status graph"
244 # Create a new node, with no prev_actions and no next_actions. This should
245 # protect us against regressions related to bug 22280.
253 # Add the new node to the core_status_grpah
254 my $new_graph = $illrq_obj->_status_graph_union( $new_node, $illrq_obj->_core_status_graph);
255 # Compare the updated graph to the expected graph
256 # The structure we compare against here is just a copy of the structure found
257 # in Koha::Illrequest::_core_status_graph() + the new node we created above
258 cmp_deeply( $new_graph,
266 prev_actions => [ ], # Actions containing buttons
267 # leading to this status
268 id => 'NEW', # ID of this status
269 name => 'New request', # UI name of this status
270 ui_method_name => 'New request', # UI name of method leading
272 method => 'create', # method to this status
273 next_actions => [ 'REQ', 'GENREQ', 'KILL' ], # buttons to add to all
274 # requests with this status
275 ui_method_icon => 'fa-plus', # UI Style class
278 prev_actions => [ 'NEW', 'REQREV', 'QUEUED', 'CANCREQ' ],
281 ui_method_name => 'Confirm request',
283 next_actions => [ 'REQREV', 'COMP', 'CHK' ],
284 ui_method_icon => 'fa-check',
287 prev_actions => [ 'NEW', 'REQREV' ],
289 name => 'Requested from partners',
290 ui_method_name => 'Place request with partners',
291 method => 'generic_confirm',
292 next_actions => [ 'COMP', 'CHK' ],
293 ui_method_icon => 'fa-send-o',
296 prev_actions => [ 'REQ' ],
298 name => 'Request reverted',
299 ui_method_name => 'Revert Request',
301 next_actions => [ 'REQ', 'GENREQ', 'KILL' ],
302 ui_method_icon => 'fa-times',
307 name => 'Queued request',
310 next_actions => [ 'REQ', 'KILL' ],
314 prev_actions => [ 'NEW' ],
316 name => 'Cancellation requested',
319 next_actions => [ 'KILL', 'REQ' ],
323 prev_actions => [ 'REQ' ],
326 ui_method_name => 'Mark completed',
327 method => 'mark_completed',
328 next_actions => [ 'CHK' ],
329 ui_method_icon => 'fa-check',
332 prev_actions => [ 'QUEUED', 'REQREV', 'NEW', 'CANCREQ' ],
335 ui_method_name => 'Delete request',
338 ui_method_icon => 'fa-trash',
341 prev_actions => [ 'REQ', 'GENREQ', 'COMP' ],
343 name => 'Checked out',
344 ui_method_name => 'Check out',
345 needs_prefs => [ 'CirculateILL' ],
346 needs_perms => [ 'user_circulate_circulate_remaining_permissions' ],
347 needs_all => ignore(),
348 method => 'check_out',
350 ui_method_icon => 'fa-upload',
353 prev_actions => [ 'CHK' ],
355 name => 'Returned to library',
356 ui_method_name => 'Check in',
357 method => 'check_in',
358 next_actions => [ 'COMP' ],
359 ui_method_icon => 'fa-download',
362 "new node + core_status_graph = bigger status graph"
363 ) || diag explain $new_graph;
365 $schema->storage->txn_rollback;
368 subtest 'Backend testing (mocks)' => sub {
372 $schema->storage->txn_begin;
374 # testing load_backend & available_backends requires that we have at least
375 # the Dummy plugin installed. load_backend & available_backends don't
376 # currently have tests as a result.
378 t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' } );
379 my $backend = Test::MockObject->new;
380 $backend->set_isa('Koha::Illbackends::Mock');
381 $backend->set_always('name', 'Mock');
383 my $patron = $builder->build({ source => 'Borrower' });
384 my $illrq = $builder->build_object({
385 class => 'Koha::Illrequests',
388 $illrq->_backend($backend);
390 isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
391 "OK accessing mocked backend.");
393 # _backend_capability tests:
394 # We need to test whether this optional feature of a mocked backend
395 # behaves as expected.
396 # 3 scenarios: feature not implemented, feature implemented, but requested
397 # capability is not provided by backend, & feature is implemented &
398 # capability exists. This method can be used to implement custom backend
399 # functionality, such as unmediated in the BLDSS backend (also see
401 $backend->set_always('capabilities', undef);
402 is($illrq->_backend_capability('Test'), 0,
403 "0 returned on Mock not implementing capabilities.");
405 $backend->set_always('capabilities', 0);
406 is($illrq->_backend_capability('Test'), 0,
407 "0 returned on Mock not implementing Test capability.");
409 $backend->set_always('capabilities', sub { return 'bar'; } );
410 is($illrq->_backend_capability('Test'), 'bar',
411 "'bar' returned on Mock implementing Test capability.");
413 # metadata test: we need to be sure that we return the arbitrary values
418 my ( $self, $rq ) = @_;
420 ID => $rq->illrequest_id,
421 Title => $rq->patron->borrowernumber
429 ID => $illrq->illrequest_id,
430 Title => $illrq->patron->borrowernumber
437 # No backend graph extension
438 $backend->set_always('status_graph', {});
439 is_deeply($illrq->capabilities('COMP'),
441 prev_actions => [ 'REQ' ],
444 ui_method_name => 'Mark completed',
445 method => 'mark_completed',
446 next_actions => [ 'CHK' ],
447 ui_method_icon => 'fa-check',
449 "Dummy status graph for COMP.");
450 is($illrq->capabilities('UNKNOWN'), undef,
451 "Dummy status graph for UNKNOWN.");
452 is_deeply($illrq->capabilities(),
453 $illrq->_core_status_graph,
454 "Dummy full status graph.");
455 # Simple backend graph extension
456 $backend->set_always('status_graph',
459 prev_actions => [ 'REQ' ],
461 next_actions => [ 'REQ' ],
464 is_deeply($illrq->capabilities('QER'),
466 prev_actions => [ 'REQ' ],
468 next_actions => [ 'REQ' ],
470 "Simple status graph for QER.");
471 is($illrq->capabilities('UNKNOWN'), undef,
472 "Simple status graph for UNKNOWN.");
473 is_deeply($illrq->capabilities(),
474 $illrq->_status_graph_union(
475 $illrq->_core_status_graph,
478 prev_actions => [ 'REQ' ],
480 next_actions => [ 'REQ' ],
484 "Simple full status graph.");
488 # No backend graph extension
489 $backend->set_always('status_graph', {});
490 is($illrq->custom_capability('unknown', {}), 0,
491 "Unknown candidate.");
493 # Simple backend graph extension
494 $backend->set_always('status_graph',
497 prev_actions => [ 'REQ' ],
499 method => 'identity',
500 next_actions => [ 'REQ' ],
503 $backend->mock('identity',
504 sub { my ( $self, $params ) = @_; return $params->{other}; });
505 is($illrq->custom_capability('identity', { test => 1, method => 'blah' })->{test}, 1,
506 "Resolve identity custom_capability");
508 $schema->storage->txn_rollback;
512 subtest 'Backend core methods' => sub {
516 $schema->storage->txn_begin;
518 # Build infrastructure
519 my $backend = Test::MockObject->new;
520 $backend->set_isa('Koha::Illbackends::Mock');
521 $backend->set_always('name', 'Mock');
522 $backend->mock('capabilities', sub { return 'Mock'; });
524 my $config = Test::MockObject->new;
525 $config->set_always('backend_dir', "/tmp");
526 $config->set_always('getLimitRules',
527 { default => { count => 0, method => 'active' } });
529 my $illrq = $builder->build_object({
530 class => 'Koha::Illrequests',
531 value => { backend => undef }
533 $illrq->_config($config);
535 # Test error conditions (no backend)
536 throws_ok { $illrq->load_backend; }
537 'Koha::Exceptions::Ill::InvalidBackendId',
538 'Exception raised correctly';
540 throws_ok { $illrq->load_backend(''); }
541 'Koha::Exceptions::Ill::InvalidBackendId',
542 'Exception raised correctly';
544 # Now load the mocked backend
545 $illrq->_backend($backend);
548 is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
552 template => "/tmp/Mock/intra-includes/bar.inc",
553 opac_template => "/tmp/Mock/opac-includes/bar.inc",
558 # we are testing simple cases.
559 $backend->set_series('create',
560 { stage => 'bar', method => 'create' },
561 { stage => 'commit', method => 'create' },
562 { stage => 'commit', method => 'create' },
563 { stage => 'commit', method => 'create' },
564 { stage => 'commit', method => 'create' });
566 is_deeply($illrq->backend_create({test => 1}),
568 stage => 'bar', method => 'create',
569 template => "/tmp/Mock/intra-includes/create.inc",
570 opac_template => "/tmp/Mock/opac-includes/create.inc",
572 "Backend create: arbitrary stage.");
574 is_deeply($illrq->backend_create({test => 1}),
576 stage => 'commit', method => 'create', permitted => 0,
577 template => "/tmp/Mock/intra-includes/create.inc",
578 opac_template => "/tmp/Mock/opac-includes/create.inc",
580 "Backend create: arbitrary stage, not permitted.");
581 is($illrq->status, "QUEUED", "Backend create: queued if restricted.");
582 $config->set_always('getLimitRules', {});
583 $illrq->status('NEW');
584 is_deeply($illrq->backend_create({test => 1}),
586 stage => 'commit', method => 'create', permitted => 1,
587 template => "/tmp/Mock/intra-includes/create.inc",
588 opac_template => "/tmp/Mock/opac-includes/create.inc",
590 "Backend create: arbitrary stage, permitted.");
591 is($illrq->status, "NEW", "Backend create: not-queued.");
593 # Test that enabling the unmediated workflow causes the backend's
594 # 'unmediated_ill' method to be called
595 t::lib::Mocks::mock_preference('ILLModuleUnmediated', '1');
599 my ($self, $name) = @_;
600 if ($name eq 'unmediated_ill') {
602 return { unmediated_ill => 1 };
607 $illrq->status('NEW');
609 $illrq->backend_create({test => 1}),
611 'opac_template' => '/tmp/Mock/opac-includes/.inc',
612 'template' => '/tmp/Mock/intra-includes/.inc',
613 'unmediated_ill' => 1
615 "Backend create: commit stage, permitted, ILLModuleUnmediated enabled."
618 # Test that disabling the unmediated workflow causes the backend's
619 # 'unmediated_ill' method to be NOT called
620 t::lib::Mocks::mock_preference('ILLModuleUnmediated', '0');
621 $illrq->status('NEW');
623 $illrq->backend_create({test => 1}),
625 stage => 'commit', method => 'create', permitted => 1,
626 template => "/tmp/Mock/intra-includes/create.inc",
627 opac_template => "/tmp/Mock/opac-includes/create.inc",
629 "Backend create: commit stage, permitted, ILLModuleUnmediated disabled."
633 $backend->set_series('renew', { stage => 'bar', method => 'renew' });
634 is_deeply($illrq->backend_renew({test => 1}),
636 stage => 'bar', method => 'renew',
637 template => "/tmp/Mock/intra-includes/renew.inc",
638 opac_template => "/tmp/Mock/opac-includes/renew.inc",
640 "Backend renew: arbitrary stage.");
643 $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
644 is_deeply($illrq->backend_cancel({test => 1}),
646 stage => 'bar', method => 'cancel',
647 template => "/tmp/Mock/intra-includes/cancel.inc",
648 opac_template => "/tmp/Mock/opac-includes/cancel.inc",
650 "Backend cancel: arbitrary stage.");
652 # backend_update_status
653 $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
654 is_deeply($illrq->backend_update_status({test => 1}),
656 stage => 'bar', method => 'update_status',
657 template => "/tmp/Mock/intra-includes/update_status.inc",
658 opac_template => "/tmp/Mock/opac-includes/update_status.inc",
660 "Backend update_status: arbitrary stage.");
663 $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
664 is_deeply($illrq->backend_confirm({test => 1}),
666 stage => 'bar', method => 'confirm',
667 template => "/tmp/Mock/intra-includes/confirm.inc",
668 opac_template => "/tmp/Mock/opac-includes/confirm.inc",
670 "Backend confirm: arbitrary stage.");
672 $config->set_always('partner_code', "ILLTSTLIB");
673 $backend->set_always('metadata', { Test => "Foobar" });
674 my $illbrn = $builder->build({
676 value => { branchemail => "", branchreplyto => "" }
678 my $partner1 = $builder->build({
679 source => 'Borrower',
680 value => { categorycode => "ILLTSTLIB" },
682 my $partner2 = $builder->build({
683 source => 'Borrower',
684 value => { categorycode => "ILLTSTLIB" },
686 my $gen_conf = $illrq->generic_confirm({
687 current_branchcode => $illbrn->{branchcode}
689 isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
690 "Generic confirm: draft contains metadata."
692 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
693 "Generic cofnirm: partner 1 is correct."
695 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
696 "Generic confirm: partner 2 is correct."
699 dies_ok { $illrq->generic_confirm({
700 current_branchcode => $illbrn->{branchcode},
703 "Generic confirm: missing to dies OK.";
705 dies_ok { $illrq->generic_confirm({
706 current_branchcode => $illbrn->{branchcode},
707 partners => $partner1->{email},
710 "Generic confirm: missing from dies OK.";
712 $schema->storage->txn_rollback;
716 subtest 'Helpers' => sub {
720 $schema->storage->txn_begin;
722 # Build infrastructure
723 my $backend = Test::MockObject->new;
724 $backend->set_isa('Koha::Illbackends::Mock');
725 $backend->set_always('name', 'Mock');
727 my $config = Test::MockObject->new;
728 $config->set_always('backend_dir', "/tmp");
730 my $patron = $builder->build({
731 source => 'Borrower',
732 value => { categorycode => "A" }
734 my $illrq = $builder->build({
735 source => 'Illrequest',
736 value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
738 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
739 $illrq_obj->_config($config);
740 $illrq_obj->_backend($backend);
743 $config->set_series('getPrefixes',
744 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
745 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
746 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
747 "getPrefix: branch");
748 $config->set_series('getPrefixes',
749 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
750 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
751 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
752 "getPrefix: default");
753 $config->set_always('getPrefixes', {});
754 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
755 "getPrefix: the empty prefix");
758 $config->set_series('getPrefixes',
759 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
760 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
761 is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
762 $config->set_series('getPrefixes',
763 { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
764 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
765 is($illrq_obj->id_prefix, "", "id_prefix: default");
767 # requires_moderation
768 $illrq_obj->status('NEW')->store;
769 is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
770 $illrq_obj->status('CANCREQ')->store;
771 is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
773 $schema->storage->txn_rollback;
777 subtest 'Censorship' => sub {
781 $schema->storage->txn_begin;
783 # Build infrastructure
784 my $backend = Test::MockObject->new;
785 $backend->set_isa('Koha::Illbackends::Mock');
786 $backend->set_always('name', 'Mock');
788 my $config = Test::MockObject->new;
789 $config->set_always('backend_dir', "/tmp");
791 my $illrq = $builder->build({source => 'Illrequest'});
792 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
793 $illrq_obj->_config($config);
794 $illrq_obj->_backend($backend);
796 $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
798 my $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564 });
799 is_deeply($censor_out, { foo => 'bar', baz => 564, display_reply_date => 1 },
800 "_censor: not OPAC, reply_date = 1");
802 $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564, opac => 1 });
803 is_deeply($censor_out, {
804 foo => 'bar', baz => 564, censor_notes_staff => 1,
805 display_reply_date => 1, opac => 1
806 }, "_censor: notes_staff = 0, reply_date = 0");
808 $schema->storage->txn_rollback;
811 subtest 'Checking out' => sub {
815 $schema->storage->txn_begin;
817 my $itemtype = $builder->build_object({
818 class => 'Koha::ItemTypes',
823 my $library = $builder->build_object({ class => 'Koha::Libraries' });
824 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
825 my $patron = $builder->build_object({
826 class => 'Koha::Patrons',
827 value => { category_type => 'x' }
829 my $request = $builder->build_object({
830 class => 'Koha::Illrequests',
832 borrowernumber => $patron->borrowernumber,
833 biblio_id => $biblio->biblionumber
837 # First test that calling check_out without a stage param returns
838 # what's required to build the form
839 my $no_stage = $request->check_out();
840 is($no_stage->{method}, 'check_out');
841 is($no_stage->{stage}, 'form');
842 isa_ok($no_stage->{value}, 'HASH');
843 isa_ok($no_stage->{value}->{itemtypes}, 'Koha::ItemTypes');
844 isa_ok($no_stage->{value}->{libraries}, 'Koha::Libraries');
845 isa_ok($no_stage->{value}->{statistical}, 'Koha::Patrons');
846 isa_ok($no_stage->{value}->{biblio}, 'Koha::Biblio');
848 # Now test that form validation works when we supply a 'form' stage
851 my $form_stage_missing_params = $request->check_out({
854 is_deeply($form_stage_missing_params->{value}->{errors}, {
857 # inhouse passed but not a valid patron
858 my $form_stage_bad_patron = $request->check_out({
860 item_type => $itemtype->itemtype,
861 inhouse => 'I_DONT_EXIST'
863 is_deeply($form_stage_bad_patron->{value}->{errors}, {
866 # Too many items attached to biblio
867 my $item1 = $builder->build_object({
868 class => 'Koha::Items',
870 biblionumber => $biblio->biblionumber,
871 biblioitemnumber => 1
874 my $item2 = $builder->build_object({
875 class => 'Koha::Items',
877 biblionumber => $biblio->biblionumber,
878 biblioitemnumber => 2
881 my $form_stage_two_items = $request->check_out({
883 item_type => $itemtype->itemtype,
885 is_deeply($form_stage_two_items->{value}->{errors}, {
891 # We need to mock the user environment for AddIssue
892 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
894 # Delete the items we created, so we can test that we can create one
895 Koha::Items->find({ itemnumber => $item1->itemnumber })->delete;
896 Koha::Items->find({ itemnumber => $item2->itemnumber })->delete;
897 # Create a biblioitem
898 my $biblioitem = $builder->build_object({
899 class => 'Koha::Biblioitems',
901 biblionumber => $biblio->biblionumber
904 # First we pass bad parameters to the item creation to test we're
905 # catching the failure of item creation
906 # Note: This will generate a DBD::mysql error when running this test!
907 my $form_stage_bad_branchcode = $request->check_out({
909 item_type => $itemtype->itemtype,
912 is_deeply($form_stage_bad_branchcode->{value}->{errors}, {
915 # Now create a proper item
916 my $form_stage_good_branchcode = $request->check_out({
918 item_type => $itemtype->itemtype,
919 branchcode => $library->branchcode
921 # By default, this item should not be loanable, so check that we're
922 # informed of that fact
924 $form_stage_good_branchcode->{value}->{check_out_errors},
928 itemtype_notforloan => $itemtype->itemtype
932 # Delete the item that was created
933 $biblio->items->delete;
934 # Now create an itemtype that is loanable
935 my $itemtype_loanable = $builder->build_object({
936 class => 'Koha::ItemTypes',
941 # We need to mock the user environment for AddIssue
942 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
943 my $form_stage_loanable = $request->check_out({
945 item_type => $itemtype_loanable->itemtype,
946 branchcode => $library->branchcode
948 is($form_stage_loanable->{stage}, 'done_check_out');
949 isa_ok($patron->checkouts, 'Koha::Checkouts');
950 is($patron->checkouts->count, 1);
951 is($request->status, 'CHK');
953 $schema->storage->txn_rollback;
956 subtest 'Checking Limits' => sub {
960 $schema->storage->txn_begin;
962 # Build infrastructure
963 my $backend = Test::MockObject->new;
964 $backend->set_isa('Koha::Illbackends::Mock');
965 $backend->set_always('name', 'Mock');
967 my $config = Test::MockObject->new;
968 $config->set_always('backend_dir', "/tmp");
970 my $illrq = $builder->build({source => 'Illrequest'});
971 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
972 $illrq_obj->_config($config);
973 $illrq_obj->_backend($backend);
976 $config->set_series('getLimitRules',
977 { CPL => { count => 1, method => 'test' } },
978 { default => { count => 0, method => 'active' } });
979 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
980 { count => 1, method => 'test' },
981 "getLimits: by value.");
982 is_deeply($illrq_obj->getLimits({ type => 'branch' }),
983 { count => 0, method => 'active' },
984 "getLimits: by default.");
985 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
986 { count => -1, method => 'active' },
987 "getLimits: by hard-coded.");
990 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
991 1, "_limit_counter: Initial branch annual count.");
992 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
993 1, "_limit_counter: Initial branch active count.");
994 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
995 1, "_limit_counter: Initial patron annual count.");
996 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
997 1, "_limit_counter: Initial patron active count.");
999 source => 'Illrequest',
1001 branchcode => $illrq_obj->branchcode,
1002 borrowernumber => $illrq_obj->borrowernumber,
1005 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
1006 2, "_limit_counter: Add a qualifying request for branch annual count.");
1007 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
1008 2, "_limit_counter: Add a qualifying request for branch active count.");
1009 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
1010 2, "_limit_counter: Add a qualifying request for patron annual count.");
1011 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
1012 2, "_limit_counter: Add a qualifying request for patron active count.");
1014 source => 'Illrequest',
1016 branchcode => $illrq_obj->branchcode,
1017 borrowernumber => $illrq_obj->borrowernumber,
1018 placed => "2005-05-31",
1021 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
1022 2, "_limit_counter: Add an out-of-date branch request.");
1023 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
1024 3, "_limit_counter: Add a qualifying request for branch active count.");
1025 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
1026 2, "_limit_counter: Add an out-of-date patron request.");
1027 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
1028 3, "_limit_counter: Add a qualifying request for patron active count.");
1030 source => 'Illrequest',
1032 branchcode => $illrq_obj->branchcode,
1033 borrowernumber => $illrq_obj->borrowernumber,
1037 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
1038 3, "_limit_counter: Add a qualifying request for branch annual count.");
1039 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
1040 3, "_limit_counter: Add a completed request for branch active count.");
1041 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
1042 3, "_limit_counter: Add a qualifying request for patron annual count.");
1043 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
1044 3, "_limit_counter: Add a completed request for patron active count.");
1048 # We've tested _limit_counter, so all we need to test here is whether the
1049 # current counts of 3 for each work as they should against different
1050 # configuration declarations.
1053 $config->set_always('getLimitRules', undef);
1054 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1055 librarycode => $illrq_obj->branchcode}),
1056 1, "check_limits: no configuration => no limits.");
1059 $config->set_always('getLimitRules',
1060 { $illrq_obj->branchcode => { count => 1, method => 'active' } });
1061 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1062 librarycode => $illrq_obj->branchcode}),
1063 0, "check_limits: branch active limit exceeded.");
1064 $config->set_always('getLimitRules',
1065 { $illrq_obj->branchcode => { count => 1, method => 'annual' } });
1066 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1067 librarycode => $illrq_obj->branchcode}),
1068 0, "check_limits: branch annual limit exceeded.");
1069 $config->set_always('getLimitRules',
1070 { $illrq_obj->branchcode => { count => 4, method => 'active' } });
1071 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1072 librarycode => $illrq_obj->branchcode}),
1073 1, "check_limits: branch active limit OK.");
1074 $config->set_always('getLimitRules',
1075 { $illrq_obj->branchcode => { count => 4, method => 'annual' } });
1076 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1077 librarycode => $illrq_obj->branchcode}),
1078 1, "check_limits: branch annual limit OK.");
1081 $config->set_always('getLimitRules',
1082 { $illrq_obj->patron->categorycode => { count => 1, method => 'active' } });
1083 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1084 librarycode => $illrq_obj->branchcode}),
1085 0, "check_limits: patron category active limit exceeded.");
1086 $config->set_always('getLimitRules',
1087 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
1088 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1089 librarycode => $illrq_obj->branchcode}),
1090 0, "check_limits: patron category annual limit exceeded.");
1091 $config->set_always('getLimitRules',
1092 { $illrq_obj->patron->categorycode => { count => 4, method => 'active' } });
1093 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1094 librarycode => $illrq_obj->branchcode}),
1095 1, "check_limits: patron category active limit OK.");
1096 $config->set_always('getLimitRules',
1097 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
1098 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1099 librarycode => $illrq_obj->branchcode}),
1100 1, "check_limits: patron category annual limit OK.");
1102 # One rule cancels the other
1103 $config->set_series('getLimitRules',
1104 # Branch rules allow request
1105 { $illrq_obj->branchcode => { count => 4, method => 'active' } },
1106 # Patron rule forbids it
1107 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
1108 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1109 librarycode => $illrq_obj->branchcode}),
1110 0, "check_limits: patron category veto overrides branch OK.");
1111 $config->set_series('getLimitRules',
1112 # Branch rules allow request
1113 { $illrq_obj->branchcode => { count => 1, method => 'active' } },
1114 # Patron rule forbids it
1115 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
1116 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
1117 librarycode => $illrq_obj->branchcode}),
1118 0, "check_limits: branch veto overrides patron category OK.");
1120 $schema->storage->txn_rollback;
1123 subtest 'Custom statuses' => sub {
1127 $schema->storage->txn_begin;
1129 my $cat = Koha::AuthorisedValueCategories->search(
1131 category_name => 'ILLSTATUS'
1135 if ($cat->count == 0) {
1136 $cat = $builder->build_object(
1138 class => 'Koha::AuthorisedValueCategory',
1140 category_name => 'ILLSTATUS'
1146 my $av = $builder->build_object(
1148 class => 'Koha::AuthorisedValues',
1150 category => 'ILLSTATUS'
1155 is($av->category, 'ILLSTATUS',
1156 "Successfully created authorised value for custom status");
1158 my $ill_req = $builder->build_object(
1160 class => 'Koha::Illrequests',
1162 status_alias => $av->authorised_value
1166 isa_ok($ill_req->statusalias, 'Koha::AuthorisedValue',
1167 "statusalias correctly returning Koha::AuthorisedValue object");
1169 $ill_req->status("COMP");
1170 is($ill_req->statusalias, undef,
1171 "Koha::Illrequest->status overloading resetting status_alias");
1173 $schema->storage->txn_rollback;