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 Test::More tests => 14;
23 use t::lib::TestBuilder;
27 use Mojo::JSON qw(encode_json);
31 use C4::Reserves qw( AddReserve CanItemBeReserved CanBookBeReserved );
35 use Koha::DateUtils qw( dt_from_string output_pref );
37 use Koha::Biblioitems;
39 use Koha::CirculationRules;
41 my $schema = Koha::Database->new->schema;
42 my $builder = t::lib::TestBuilder->new();
44 $schema->storage->txn_begin;
46 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
48 my $t = Test::Mojo->new('Koha::REST::V1');
50 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
51 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
52 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
53 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
55 # Generic password for everyone
56 my $password = 'thePassword123';
58 # User without any permissions
59 my $nopermission = $builder->build_object({
60 class => 'Koha::Patrons',
62 branchcode => $branchcode,
63 categorycode => $categorycode,
67 $nopermission->set_password( { password => $password, skip_validation => 1 } );
68 my $nopermission_userid = $nopermission->userid;
70 my $patron_1 = $builder->build_object(
72 class => 'Koha::Patrons',
74 categorycode => $categorycode,
75 branchcode => $branchcode,
76 surname => 'Test Surname',
77 flags => 80, #borrowers and reserveforothers flags
81 $patron_1->set_password( { password => $password, skip_validation => 1 } );
82 my $userid_1 = $patron_1->userid;
84 my $patron_2 = $builder->build_object(
86 class => 'Koha::Patrons',
88 categorycode => $categorycode,
89 branchcode => $branchcode,
90 surname => 'Test Surname 2',
91 flags => 16, # borrowers flag
95 $patron_2->set_password( { password => $password, skip_validation => 1 } );
96 my $userid_2 = $patron_2->userid;
98 my $patron_3 = $builder->build_object(
100 class => 'Koha::Patrons',
102 categorycode => $categorycode,
103 branchcode => $branchcode,
104 surname => 'Test Surname 3',
105 flags => 64, # reserveforothers flag
109 $patron_3->set_password( { password => $password, skip_validation => 1 } );
110 my $userid_3 = $patron_3->userid;
112 my $biblio_1 = $builder->build_sample_biblio;
113 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber, itype => $itemtype });
115 my $biblio_2 = $builder->build_sample_biblio;
116 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber, itype => $itemtype });
118 my $dbh = C4::Context->dbh;
119 $dbh->do('DELETE FROM reserves');
120 Koha::CirculationRules->search()->delete();
121 Koha::CirculationRules->set_rules(
123 categorycode => undef,
127 reservesallowed => 1,
128 holds_per_record => 99
133 my $reserve_id = C4::Reserves::AddReserve(
135 branchcode => $branchcode,
136 borrowernumber => $patron_1->borrowernumber,
137 biblionumber => $biblio_1->biblionumber,
139 itemnumber => $item_1->itemnumber,
143 # Add another reserve to be able to change first reserve's rank
144 my $reserve_id2 = C4::Reserves::AddReserve(
146 branchcode => $branchcode,
147 borrowernumber => $patron_2->borrowernumber,
148 biblionumber => $biblio_1->biblionumber,
150 itemnumber => $item_1->itemnumber,
154 my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
155 my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
158 patron_id => $patron_1->borrowernumber,
159 biblio_id => $biblio_1->biblionumber,
160 item_id => $item_1->itemnumber,
161 pickup_library_id => $branchcode,
162 expiration_date => output_pref( { dt => $expiration_date, dateformat => 'iso', dateonly => 1 } ),
166 suspended_until => output_pref( { dt => $suspended_until, dateformat => 'rfc3339' } ),
169 subtest "Test endpoints without authentication" => sub {
171 $t->get_ok('/api/v1/holds')
173 $t->post_ok('/api/v1/holds')
175 $t->patch_ok('/api/v1/holds/0')
177 $t->delete_ok('/api/v1/holds/0')
181 subtest "Test endpoints without permission" => sub {
185 $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
188 $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
191 $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json => $post_data )
194 $t->patch_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $patch_data )
197 $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
201 subtest "Test endpoints with permission" => sub {
205 $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
211 $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
213 ->json_is('/0/patron_id', $patron_2->borrowernumber)
216 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
217 ->status_is(204, 'SWAGGER3.2.4')
218 ->content_is('', 'SWAGGER3.3.4');
220 $t->patch_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $patch_data )
222 ->json_has('/error');
224 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
226 ->json_has('/error');
228 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
232 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
233 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
237 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
238 ->status_is(204, 'SWAGGER3.2.4')
239 ->content_is('', 'SWAGGER3.3.4');
241 # Make sure pickup location checks doesn't get in the middle
242 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
243 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
244 my $mock_item = Test::MockModule->new('Koha::Item');
245 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
247 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
249 ->json_has('/hold_id');
251 # Get id from response
252 $reserve_id = $t->tx->res->json->{hold_id};
254 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
256 ->json_is('/0/hold_id', $reserve_id)
257 ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'iso', dateonly => 1 }))
258 ->json_is('/0/pickup_library_id', $branchcode);
260 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
262 ->json_like('/error', qr/itemAlreadyOnHold/);
264 $post_data->{biblio_id} = $biblio_2->biblionumber;
265 $post_data->{item_id} = $item_2->itemnumber;
267 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
269 ->json_like('/error', qr/Hold cannot be placed. Reason: tooManyReserves/);
271 my $to_delete_patron = $builder->build_object({ class => 'Koha::Patrons' });
272 my $deleted_patron_id = $to_delete_patron->borrowernumber;
273 $to_delete_patron->delete;
275 my $tmp_patron_id = $post_data->{patron_id};
276 $post_data->{patron_id} = $deleted_patron_id;
277 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
279 ->json_is( { error => 'patron_id not found' } );
281 # Restore the original patron_id as it is expected by the next subtest
282 # FIXME: this tests need to be rewritten from scratch
283 $post_data->{patron_id} = $tmp_patron_id;
286 subtest 'Reserves with itemtype' => sub {
290 patron_id => int($patron_1->borrowernumber),
291 biblio_id => int($biblio_1->biblionumber),
292 pickup_library_id => $branchcode,
293 item_type => $itemtype,
296 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
297 ->status_is(204, 'SWAGGER3.2.4')
298 ->content_is('', 'SWAGGER3.3.4');
300 # Make sure pickup location checks doesn't get in the middle
301 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
302 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
303 my $mock_item = Test::MockModule->new('Koha::Item');
304 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
306 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
308 ->json_has('/hold_id');
310 $reserve_id = $t->tx->res->json->{hold_id};
312 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
314 ->json_is('/0/hold_id', $reserve_id)
315 ->json_is('/0/item_type', $itemtype);
319 subtest 'test AllowHoldDateInFuture' => sub {
323 $dbh->do('DELETE FROM reserves');
325 my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
328 patron_id => $patron_1->borrowernumber,
329 biblio_id => $biblio_1->biblionumber,
330 item_id => $item_1->itemnumber,
331 pickup_library_id => $branchcode,
332 expiration_date => output_pref( { dt => $expiration_date, dateformat => 'iso', dateonly => 1 } ),
333 hold_date => output_pref( { dt => $future_hold_date, dateformat => 'iso', dateonly => 1 } ),
336 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
338 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
340 ->json_has('/error');
342 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
344 # Make sure pickup location checks doesn't get in the middle
345 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
346 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
347 my $mock_item = Test::MockModule->new('Koha::Item');
348 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
350 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
352 ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'iso', dateonly => 1 }));
355 $schema->storage->txn_rollback;
357 subtest 'x-koha-override and AllowHoldPolicyOverride tests' => sub {
361 $schema->storage->txn_begin;
363 my $patron = $builder->build_object(
365 class => 'Koha::Patrons',
366 value => { flags => 1 }
369 my $password = 'thePassword123';
370 $patron->set_password( { password => $password, skip_validation => 1 } );
371 $patron->discard_changes;
372 my $userid = $patron->userid;
374 my $renegade_library = $builder->build_object({ class => 'Koha::Libraries' });
376 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
378 # Make sure pickup location checks doesn't get in the middle
379 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
380 $mock_biblio->mock( 'pickup_locations',
381 sub { return Koha::Libraries->search({ branchcode => { '!=' => $renegade_library->branchcode } }); } );
382 my $mock_item = Test::MockModule->new('Koha::Item');
383 $mock_item->mock( 'pickup_locations',
384 sub { return Koha::Libraries->search({ branchcode => { '!=' => $renegade_library->branchcode } }) } );
386 my $can_item_be_reserved_result;
387 my $mock_reserves = Test::MockModule->new('C4::Reserves');
388 $mock_reserves->mock(
391 return $can_item_be_reserved_result;
395 my $item = $builder->build_sample_item;
398 item_id => $item->id,
399 biblio_id => $item->biblionumber,
400 patron_id => $patron->id,
401 pickup_library_id => $patron->branchcode,
404 $can_item_be_reserved_result = { status => 'ageRestricted' };
406 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
408 ->json_is( '/error' => "Hold cannot be placed. Reason: ageRestricted" );
410 # x-koha-override doesn't override if AllowHoldPolicyOverride not set
411 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
412 { 'x-koha-override' => 'any' } => json => $post_data )
415 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
417 $can_item_be_reserved_result = { status => 'pickupNotInHoldGroup' };
419 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
422 '/error' => "Hold cannot be placed. Reason: pickupNotInHoldGroup" );
424 # x-koha-override overrides the status
425 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
426 { 'x-koha-override' => 'any' } => json => $post_data )
429 $can_item_be_reserved_result = { status => 'OK' };
431 # x-koha-override works when status not need override
432 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
433 { 'x-koha-override' => 'any' } => json => $post_data )
436 # Test pickup locations can be overridden
437 $post_data->{pickup_library_id} = $renegade_library->branchcode;
439 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
442 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
443 { 'x-koha-override' => 'any' } => json => $post_data )
446 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0);
448 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
449 { 'x-koha-override' => 'any' } => json => $post_data )
452 $schema->storage->txn_rollback;
455 subtest 'suspend and resume tests' => sub {
459 $schema->storage->txn_begin;
461 my $password = 'AbcdEFG123';
463 my $patron = $builder->build_object(
464 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 0 } } );
467 source => 'UserPermission',
469 borrowernumber => $patron->borrowernumber,
471 code => 'place_holds',
476 $patron->set_password({ password => $password, skip_validation => 1 });
477 my $userid = $patron->userid;
480 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
481 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
483 my $hold = $builder->build_object(
484 { class => 'Koha::Holds',
485 value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
489 ok( !$hold->is_suspended, 'Hold is not suspended' );
490 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
491 ->status_is( 201, 'Hold suspension created' );
493 $hold->discard_changes; # refresh object
495 ok( $hold->is_suspended, 'Hold is suspended' );
496 $t->json_is('/end_date', undef, 'Hold suspension has no end date');
498 my $end_date = output_pref({
499 dt => dt_from_string( undef ),
504 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" => json => { end_date => $end_date } );
506 $hold->discard_changes; # refresh object
508 ok( $hold->is_suspended, 'Hold is suspended' );
512 dt => dt_from_string( $hold->suspend_until ),
516 'Hold suspension has correct end date'
519 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
520 ->status_is(204, 'SWAGGER3.2.4')
521 ->content_is('', 'SWAGGER3.3.4');
523 # Pass a an expiration date for the suspension
524 my $date = dt_from_string()->add( days => 5 );
526 "//$userid:$password@/api/v1/holds/"
528 . "/suspension" => json => {
530 output_pref( { dt => $date, dateformat => 'iso', dateonly => 1 } )
532 )->status_is( 201, 'Hold suspension created' )
533 ->json_is( '/end_date',
534 output_pref( { dt => $date, dateformat => 'iso', dateonly => 1 } ) )
535 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
537 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
538 ->status_is(204, 'SWAGGER3.2.4')
539 ->content_is('', 'SWAGGER3.3.4');
541 $hold->set_waiting->discard_changes;
543 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
544 ->status_is( 400, 'Cannot suspend waiting hold' )
545 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
547 $hold->set_transfer->discard_changes;
549 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
550 ->status_is( 400, 'Cannot suspend hold on transfer' )
551 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
553 $schema->storage->txn_rollback;
556 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
560 $schema->storage->txn_begin;
562 my $password = 'AbcdEFG123';
564 my $library = $builder->build_object({ class => 'Koha::Libraries' });
565 my $patron_np = $builder->build_object(
566 { class => 'Koha::Patrons', value => { flags => 0 } } );
567 $patron_np->set_password( { password => $password, skip_validation => 1 } );
568 my $userid_np = $patron_np->userid;
570 my $patron = $builder->build_object(
571 { class => 'Koha::Patrons', value => { flags => 0 } } );
572 $patron->set_password( { password => $password, skip_validation => 1 } );
573 my $userid = $patron->userid;
576 source => 'UserPermission',
578 borrowernumber => $patron->borrowernumber,
580 code => 'modify_holds_priority',
586 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
587 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
589 my $biblio = $builder->build_sample_biblio;
590 my $patron_1 = $builder->build_object(
592 class => 'Koha::Patrons',
593 value => { branchcode => $library->branchcode }
596 my $patron_2 = $builder->build_object(
598 class => 'Koha::Patrons',
599 value => { branchcode => $library->branchcode }
602 my $patron_3 = $builder->build_object(
604 class => 'Koha::Patrons',
605 value => { branchcode => $library->branchcode }
609 my $hold_1 = Koha::Holds->find(
612 branchcode => $library->branchcode,
613 borrowernumber => $patron_1->borrowernumber,
614 biblionumber => $biblio->biblionumber,
619 my $hold_2 = Koha::Holds->find(
622 branchcode => $library->branchcode,
623 borrowernumber => $patron_2->borrowernumber,
624 biblionumber => $biblio->biblionumber,
629 my $hold_3 = Koha::Holds->find(
632 branchcode => $library->branchcode,
633 borrowernumber => $patron_3->borrowernumber,
634 biblionumber => $biblio->biblionumber,
640 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
642 . "/priority" => json => 1 )->status_is(403);
644 $t->put_ok( "//$userid:$password@/api/v1/holds/"
646 . "/priority" => json => 1 )->status_is(200)->json_is(1);
648 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
649 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
650 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
652 $t->put_ok( "//$userid:$password@/api/v1/holds/"
654 . "/priority" => json => 3 )->status_is(200)->json_is(3);
656 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
657 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
658 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
660 $schema->storage->txn_rollback;
663 subtest 'add() tests (maxreserves behaviour)' => sub {
667 $schema->storage->txn_begin;
669 $dbh->do('DELETE FROM reserves');
671 Koha::CirculationRules->new->delete;
673 my $password = 'AbcdEFG123';
675 my $patron = $builder->build_object(
676 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
677 $patron->set_password({ password => $password, skip_validation => 1 });
678 my $userid = $patron->userid;
680 Koha::CirculationRules->set_rules(
684 categorycode => undef,
691 Koha::CirculationRules->set_rules(
694 categorycode => $patron->categorycode,
701 my $biblio_1 = $builder->build_sample_biblio;
702 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
703 my $biblio_2 = $builder->build_sample_biblio;
704 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
705 my $biblio_3 = $builder->build_sample_biblio;
706 my $item_3 = $builder->build_sample_item({ biblionumber => $biblio_3->biblionumber });
708 # Make sure pickup location checks doesn't get in the middle
709 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
710 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
711 my $mock_item = Test::MockModule->new('Koha::Item');
712 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
715 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
716 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
717 t::lib::Mocks::mock_preference( 'maxreserves', 2 );
718 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
721 patron_id => $patron->borrowernumber,
722 biblio_id => $biblio_1->biblionumber,
723 pickup_library_id => $item_1->home_branch->branchcode,
724 item_type => $item_1->itype,
727 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
731 patron_id => $patron->borrowernumber,
732 biblio_id => $biblio_2->biblionumber,
733 pickup_library_id => $item_2->home_branch->branchcode,
734 item_id => $item_2->itemnumber
737 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
741 patron_id => $patron->borrowernumber,
742 biblio_id => $biblio_3->biblionumber,
743 pickup_library_id => $item_1->home_branch->branchcode,
744 item_id => $item_3->itemnumber
747 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
749 ->json_is( { error => 'Hold cannot be placed. Reason: tooManyReserves' } );
751 t::lib::Mocks::mock_preference( 'maxreserves', 0 );
753 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
754 ->status_is(201, 'maxreserves == 0 => no limit');
756 # cleanup for the next tests
757 my $hold_id = $t->tx->res->json->{hold_id};
758 Koha::Holds->find( $hold_id )->delete;
760 t::lib::Mocks::mock_preference( 'maxreserves', undef );
762 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
763 ->status_is(201, 'maxreserves == undef => no limit');
765 $schema->storage->txn_rollback;
768 subtest 'pickup_locations() tests' => sub {
772 $schema->storage->txn_begin;
774 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
776 # Small trick to ease testing
777 Koha::Libraries->search->update({ pickup_location => 0 });
779 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
780 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
781 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
783 my $library_1_api = $library_1->to_api();
784 my $library_2_api = $library_2->to_api();
785 my $library_3_api = $library_3->to_api();
787 $library_1_api->{needs_override} = Mojo::JSON->false;
788 $library_2_api->{needs_override} = Mojo::JSON->false;
789 $library_3_api->{needs_override} = Mojo::JSON->false;
791 my $patron = $builder->build_object(
793 class => 'Koha::Patrons',
794 value => { userid => 'tomasito', flags => 0 }
797 $patron->set_password( { password => $password, skip_validation => 1 } );
798 my $userid = $patron->userid;
801 source => 'UserPermission',
803 borrowernumber => $patron->borrowernumber,
805 code => 'place_holds',
810 my $item_class = Test::MockModule->new('Koha::Item');
814 my ( $self, $params ) = @_;
815 my $mock_patron = $params->{patron};
816 is( $mock_patron->borrowernumber,
817 $patron->borrowernumber, 'Patron passed correctly' );
818 return Koha::Libraries->search(
822 $library_1->branchcode,
823 $library_2->branchcode
827 { # we make sure no surprises in the order of the result
828 order_by => { '-asc' => 'marcorgcode' }
834 my $biblio_class = Test::MockModule->new('Koha::Biblio');
838 my ( $self, $params ) = @_;
839 my $mock_patron = $params->{patron};
840 is( $mock_patron->borrowernumber,
841 $patron->borrowernumber, 'Patron passed correctly' );
842 return Koha::Libraries->search(
846 $library_2->branchcode,
847 $library_3->branchcode
851 { # we make sure no surprises in the order of the result
852 order_by => { '-asc' => 'marcorgcode' }
858 my $item = $builder->build_sample_item;
861 my $hold_1 = $builder->build_object(
863 class => 'Koha::Holds',
866 biblionumber => $item->biblionumber,
867 borrowernumber => $patron->borrowernumber
872 my $hold_2 = $builder->build_object(
874 class => 'Koha::Holds',
876 itemnumber => $item->itemnumber,
877 biblionumber => $item->biblionumber,
878 borrowernumber => $patron->borrowernumber
883 $t->get_ok( "//$userid:$password@/api/v1/holds/"
885 . "/pickup_locations" )
886 ->json_is( [ $library_2_api, $library_3_api ] );
888 $t->get_ok( "//$userid:$password@/api/v1/holds/"
890 . "/pickup_locations" )
891 ->json_is( [ $library_1_api, $library_2_api ] );
894 $t->get_ok( "//$userid:$password@/api/v1/holds/"
896 . '/pickup_locations?q={"marc_org_code": { "-like": "A%" }}' )
897 ->json_is( [ $library_1_api ] );
899 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
901 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
902 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
904 my $library_5_api = $library_5->to_api();
905 $library_5_api->{needs_override} = Mojo::JSON->true;
907 # bibli-level mock doesn't include library_1 as valid pickup location
908 $library_1_api->{needs_override} = Mojo::JSON->true;
910 $t->get_ok( "//$userid:$password@/api/v1/holds/"
912 . "/pickup_locations?_order_by=marc_org_code" )
913 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
915 $schema->storage->txn_rollback;
918 subtest 'edit() tests' => sub {
922 $schema->storage->txn_begin;
924 my $password = 'AbcdEFG123';
926 my $library = $builder->build_object({ class => 'Koha::Libraries' });
927 my $patron = $builder->build_object(
928 { class => 'Koha::Patrons', value => { flags => 1 } } );
929 $patron->set_password( { password => $password, skip_validation => 1 } );
930 my $userid = $patron->userid;
933 source => 'UserPermission',
935 borrowernumber => $patron->borrowernumber,
937 code => 'modify_holds_priority',
943 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
944 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
945 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
947 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
948 my $mock_item = Test::MockModule->new('Koha::Item');
950 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
951 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
952 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
954 # let's control what Koha::Biblio->pickup_locations returns, for testing
955 $mock_biblio->mock( 'pickup_locations', sub {
956 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
958 # let's mock what Koha::Item->pickup_locations returns, for testing
959 $mock_item->mock( 'pickup_locations', sub {
960 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
963 my $biblio = $builder->build_sample_biblio;
964 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
966 # Test biblio-level holds
967 my $biblio_hold = $builder->build_object(
969 class => "Koha::Holds",
971 biblionumber => $biblio->biblionumber,
972 branchcode => $library_3->branchcode,
979 my $biblio_hold_api_data = $biblio_hold->to_api;
980 my $biblio_hold_data = {
981 pickup_library_id => $library_1->branchcode,
982 priority => $biblio_hold_api_data->{priority}
985 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
987 => json => $biblio_hold_data )
989 ->json_is({ error => 'The supplied pickup location is not valid' });
991 $t->put_ok( "//$userid:$password@/api/v1/holds/"
993 => json => $biblio_hold_data )
995 ->json_is({ error => 'The supplied pickup location is not valid' });
997 $biblio_hold->discard_changes;
998 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
1000 $t->patch_ok( "//$userid:$password@/api/v1/holds/" . $biblio_hold->id
1001 => { 'x-koha-override' => 'any' }
1002 => json => $biblio_hold_data )
1004 ->json_has( '/pickup_library_id' => $library_1->id );
1006 $t->put_ok( "//$userid:$password@/api/v1/holds/" . $biblio_hold->id
1007 => { 'x-koha-override' => 'any' }
1008 => json => $biblio_hold_data )
1010 ->json_has( '/pickup_library_id' => $library_1->id );
1012 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
1013 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
1015 => json => $biblio_hold_data )
1018 $biblio_hold->discard_changes;
1019 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
1021 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1023 => json => $biblio_hold_data )
1026 $biblio_hold->discard_changes;
1027 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
1029 # Test item-level holds
1030 my $item_hold = $builder->build_object(
1032 class => "Koha::Holds",
1034 biblionumber => $biblio->biblionumber,
1035 branchcode => $library_3->branchcode,
1036 itemnumber => $item->itemnumber,
1039 suspend_until => undef,
1044 my $item_hold_api_data = $item_hold->to_api;
1045 my $item_hold_data = {
1046 pickup_library_id => $library_1->branchcode,
1047 priority => $item_hold_api_data->{priority}
1050 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
1052 => json => $item_hold_data )
1054 ->json_is({ error => 'The supplied pickup location is not valid' });
1056 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1058 => json => $item_hold_data )
1060 ->json_is({ error => 'The supplied pickup location is not valid' });
1062 $item_hold->discard_changes;
1063 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
1065 $t->patch_ok( "//$userid:$password@/api/v1/holds/" . $item_hold->id
1066 => { 'x-koha-override' => 'any' }
1067 => json => $item_hold_data )
1069 ->json_has( '/pickup_library_id' => $library_1->id );
1071 $t->put_ok( "//$userid:$password@/api/v1/holds/" . $item_hold->id
1072 => { 'x-koha-override' => 'any' }
1073 => json => $item_hold_data )
1075 ->json_has( '/pickup_library_id' => $library_1->id );
1077 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1078 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
1080 => json => $item_hold_data )
1083 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1085 => json => $item_hold_data )
1088 $item_hold->discard_changes;
1089 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
1091 is( $item_hold->suspend, 0, 'Location change should not activate suspended status' );
1092 is( $item_hold->suspend_until, undef, 'Location change should keep suspended_until be undef' );
1094 $schema->storage->txn_rollback;
1098 subtest 'add() tests' => sub {
1102 $schema->storage->txn_begin;
1104 my $password = 'AbcdEFG123';
1106 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1107 my $patron = $builder->build_object(
1108 { class => 'Koha::Patrons', value => { flags => 1 } } );
1109 $patron->set_password( { password => $password, skip_validation => 1 } );
1110 my $userid = $patron->userid;
1113 source => 'UserPermission',
1115 borrowernumber => $patron->borrowernumber,
1117 code => 'modify_holds_priority',
1123 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1124 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1126 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
1127 my $mock_item = Test::MockModule->new('Koha::Item');
1129 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
1130 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
1131 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
1133 # let's control what Koha::Biblio->pickup_locations returns, for testing
1134 $mock_biblio->mock( 'pickup_locations', sub {
1135 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1137 # let's mock what Koha::Item->pickup_locations returns, for testing
1138 $mock_item->mock( 'pickup_locations', sub {
1139 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1142 my $can_be_reserved = 'OK';
1143 my $mock_reserves = Test::MockModule->new('C4::Reserves');
1144 $mock_reserves->mock( 'CanItemBeReserved', sub
1146 return { status => $can_be_reserved }
1150 $mock_reserves->mock( 'CanBookBeReserved', sub
1152 return { status => $can_be_reserved }
1157 my $biblio = $builder->build_sample_biblio;
1158 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1160 # Test biblio-level holds
1161 my $biblio_hold = $builder->build_object(
1163 class => "Koha::Holds",
1165 biblionumber => $biblio->biblionumber,
1166 branchcode => $library_3->branchcode,
1167 itemnumber => undef,
1173 my $biblio_hold_api_data = $biblio_hold->to_api;
1174 $biblio_hold->delete;
1175 my $biblio_hold_data = {
1176 biblio_id => $biblio_hold_api_data->{biblio_id},
1177 patron_id => $biblio_hold_api_data->{patron_id},
1178 pickup_library_id => $library_1->branchcode,
1181 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1183 ->json_is({ error => 'The supplied pickup location is not valid' });
1185 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
1186 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1189 # Test item-level holds
1190 my $item_hold = $builder->build_object(
1192 class => "Koha::Holds",
1194 biblionumber => $biblio->biblionumber,
1195 branchcode => $library_3->branchcode,
1196 itemnumber => $item->itemnumber,
1202 my $item_hold_api_data = $item_hold->to_api;
1204 my $item_hold_data = {
1205 biblio_id => $item_hold_api_data->{biblio_id},
1206 item_id => $item_hold_api_data->{item_id},
1207 patron_id => $item_hold_api_data->{patron_id},
1208 pickup_library_id => $library_1->branchcode,
1211 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1213 ->json_is({ error => 'The supplied pickup location is not valid' });
1215 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1216 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1220 $mock_biblio->mock( 'pickup_locations', sub {
1221 return Koha::Libraries->new->empty;
1224 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1226 ->json_is({ error => 'The supplied pickup location is not valid' });
1229 $mock_item->mock( 'pickup_locations', sub {
1230 return Koha::Libraries->new->empty;
1233 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1235 ->json_is({ error => 'The supplied pickup location is not valid' });
1237 $schema->storage->txn_rollback;
1240 subtest 'PUT /holds/{hold_id}/pickup_location tests' => sub {
1244 $schema->storage->txn_begin;
1246 my $password = 'AbcdEFG123';
1248 # library_1 and library_2 are available pickup locations, not library_3
1249 my $library_1 = $builder->build_object(
1250 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1251 my $library_2 = $builder->build_object(
1252 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1253 my $library_3 = $builder->build_object(
1254 { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
1256 my $patron = $builder->build_object(
1257 { class => 'Koha::Patrons', value => { flags => 0 } } );
1258 $patron->set_password( { password => $password, skip_validation => 1 } );
1259 my $userid = $patron->userid;
1262 source => 'UserPermission',
1264 borrowernumber => $patron->borrowernumber,
1266 code => 'place_holds',
1272 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1273 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1275 my $biblio = $builder->build_sample_biblio;
1276 my $item = $builder->build_sample_item(
1278 biblionumber => $biblio->biblionumber,
1279 library => $library_1->branchcode
1284 my $hold = Koha::Holds->find(
1287 branchcode => $library_1->branchcode,
1288 borrowernumber => $patron->borrowernumber,
1289 biblionumber => $biblio->biblionumber,
1291 itemnumber => undef,
1296 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1298 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1300 ->json_is({ pickup_library_id => $library_2->branchcode });
1302 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1304 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1306 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1308 ->json_is({ error => '[The supplied pickup location is not valid]' });
1310 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library unchanged' );
1313 $hold = Koha::Holds->find(
1316 branchcode => $library_1->branchcode,
1317 borrowernumber => $patron->borrowernumber,
1318 biblionumber => $biblio->biblionumber,
1320 itemnumber => $item->itemnumber,
1325 # Attempt to use an invalid pickup locations ends in 400
1326 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1328 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1330 ->json_is({ error => '[The supplied pickup location is not valid]' });
1332 is( $hold->discard_changes->branchcode->branchcode, $library_1->branchcode, 'pickup library unchanged' );
1334 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
1336 # Attempt to use an invalid pickup locations with override succeeds
1337 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1339 . "/pickup_location"
1340 => { 'x-koha-override' => 'any' }
1341 => json => { pickup_library_id => $library_2->branchcode } )
1343 ->json_is({ pickup_library_id => $library_2->branchcode });
1345 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library changed' );
1347 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
1349 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1351 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1353 ->json_is({ pickup_library_id => $library_2->branchcode });
1355 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1357 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1359 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1361 ->json_is({ error => '[The supplied pickup location is not valid]' });
1363 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'invalid pickup library not used' );
1365 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1367 . "/pickup_location"
1368 => { 'x-koha-override' => 'any' }
1369 => json => { pickup_library_id => $library_3->branchcode } )
1371 ->json_is({ error => '[The supplied pickup location is not valid]' });
1373 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'invalid pickup library not used, even if x-koha-override is passed' );
1375 $schema->storage->txn_rollback;
1378 subtest 'delete() tests' => sub {
1382 $schema->storage->txn_begin;
1384 my $password = 'AbcdEFG123';
1385 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
1386 $patron->set_password({ password => $password, skip_validation => 1 });
1387 my $userid = $patron->userid;
1389 # Only have 'place_holds' subpermission
1392 source => 'UserPermission',
1394 borrowernumber => $patron->borrowernumber,
1396 code => 'place_holds',
1402 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1403 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1405 my $biblio = $builder->build_sample_biblio;
1406 my $item = $builder->build_sample_item(
1408 biblionumber => $biblio->biblionumber,
1409 library => $patron->branchcode
1414 my $hold = Koha::Holds->find(
1417 branchcode => $patron->branchcode,
1418 borrowernumber => $patron->borrowernumber,
1419 biblionumber => $biblio->biblionumber,
1421 itemnumber => undef,
1426 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id )
1427 ->status_is(204, 'SWAGGER3.2.4')
1428 ->content_is('', 'SWAGGER3.3.4');
1430 $schema->storage->txn_rollback;