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 => 13;
23 use t::lib::TestBuilder;
27 use Mojo::JSON qw(encode_json);
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 => 'rfc3339', 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 => 'rfc3339', 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 => 'rfc3339', dateonly => 1 } ),
333 hold_date => output_pref( { dt => $future_hold_date, dateformat => 'rfc3339', 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 => 'rfc3339', 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 => 1 } } );
465 $patron->set_password({ password => $password, skip_validation => 1 });
466 my $userid = $patron->userid;
469 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
470 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
472 my $hold = $builder->build_object(
473 { class => 'Koha::Holds',
474 value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
478 ok( !$hold->is_suspended, 'Hold is not suspended' );
479 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
480 ->status_is( 201, 'Hold suspension created' );
482 $hold->discard_changes; # refresh object
484 ok( $hold->is_suspended, 'Hold is suspended' );
485 $t->json_is('/end_date', undef, 'Hold suspension has no end date');
487 my $end_date = output_pref({
488 dt => dt_from_string( undef ),
489 dateformat => 'rfc3339',
493 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" => json => { end_date => $end_date } );
495 $hold->discard_changes; # refresh object
497 ok( $hold->is_suspended, 'Hold is suspended' );
501 dt => dt_from_string( $hold->suspend_until ),
502 dateformat => 'rfc3339',
505 'Hold suspension has correct end date'
508 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
509 ->status_is(204, 'SWAGGER3.2.4')
510 ->content_is('', 'SWAGGER3.3.4');
512 # Pass a an expiration date for the suspension
513 my $date = dt_from_string()->add( days => 5 );
515 "//$userid:$password@/api/v1/holds/"
517 . "/suspension" => json => {
519 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
521 )->status_is( 201, 'Hold suspension created' )
522 ->json_is( '/end_date',
523 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
524 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
526 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
527 ->status_is(204, 'SWAGGER3.2.4')
528 ->content_is('', 'SWAGGER3.3.4');
530 $hold->set_waiting->discard_changes;
532 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
533 ->status_is( 400, 'Cannot suspend waiting hold' )
534 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
536 $hold->set_transfer->discard_changes;
538 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
539 ->status_is( 400, 'Cannot suspend hold on transfer' )
540 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
542 $schema->storage->txn_rollback;
545 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
549 $schema->storage->txn_begin;
551 my $password = 'AbcdEFG123';
553 my $library = $builder->build_object({ class => 'Koha::Libraries' });
554 my $patron_np = $builder->build_object(
555 { class => 'Koha::Patrons', value => { flags => 0 } } );
556 $patron_np->set_password( { password => $password, skip_validation => 1 } );
557 my $userid_np = $patron_np->userid;
559 my $patron = $builder->build_object(
560 { class => 'Koha::Patrons', value => { flags => 0 } } );
561 $patron->set_password( { password => $password, skip_validation => 1 } );
562 my $userid = $patron->userid;
565 source => 'UserPermission',
567 borrowernumber => $patron->borrowernumber,
569 code => 'modify_holds_priority',
575 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
576 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
578 my $biblio = $builder->build_sample_biblio;
579 my $patron_1 = $builder->build_object(
581 class => 'Koha::Patrons',
582 value => { branchcode => $library->branchcode }
585 my $patron_2 = $builder->build_object(
587 class => 'Koha::Patrons',
588 value => { branchcode => $library->branchcode }
591 my $patron_3 = $builder->build_object(
593 class => 'Koha::Patrons',
594 value => { branchcode => $library->branchcode }
598 my $hold_1 = Koha::Holds->find(
601 branchcode => $library->branchcode,
602 borrowernumber => $patron_1->borrowernumber,
603 biblionumber => $biblio->biblionumber,
608 my $hold_2 = Koha::Holds->find(
611 branchcode => $library->branchcode,
612 borrowernumber => $patron_2->borrowernumber,
613 biblionumber => $biblio->biblionumber,
618 my $hold_3 = Koha::Holds->find(
621 branchcode => $library->branchcode,
622 borrowernumber => $patron_3->borrowernumber,
623 biblionumber => $biblio->biblionumber,
629 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
631 . "/priority" => json => 1 )->status_is(403);
633 $t->put_ok( "//$userid:$password@/api/v1/holds/"
635 . "/priority" => json => 1 )->status_is(200)->json_is(1);
637 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
638 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
639 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
641 $t->put_ok( "//$userid:$password@/api/v1/holds/"
643 . "/priority" => json => 3 )->status_is(200)->json_is(3);
645 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
646 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
647 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
649 $schema->storage->txn_rollback;
652 subtest 'add() tests (maxreserves behaviour)' => sub {
656 $schema->storage->txn_begin;
658 $dbh->do('DELETE FROM reserves');
660 Koha::CirculationRules->new->delete;
662 my $password = 'AbcdEFG123';
664 my $patron = $builder->build_object(
665 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
666 $patron->set_password({ password => $password, skip_validation => 1 });
667 my $userid = $patron->userid;
669 Koha::CirculationRules->set_rules(
673 categorycode => undef,
680 Koha::CirculationRules->set_rules(
683 categorycode => $patron->categorycode,
690 my $biblio_1 = $builder->build_sample_biblio;
691 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
692 my $biblio_2 = $builder->build_sample_biblio;
693 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
694 my $biblio_3 = $builder->build_sample_biblio;
695 my $item_3 = $builder->build_sample_item({ biblionumber => $biblio_3->biblionumber });
697 # Make sure pickup location checks doesn't get in the middle
698 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
699 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
700 my $mock_item = Test::MockModule->new('Koha::Item');
701 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
704 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
705 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
706 t::lib::Mocks::mock_preference( 'maxreserves', 2 );
707 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
710 patron_id => $patron->borrowernumber,
711 biblio_id => $biblio_1->biblionumber,
712 pickup_library_id => $item_1->home_branch->branchcode,
713 item_type => $item_1->itype,
716 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
720 patron_id => $patron->borrowernumber,
721 biblio_id => $biblio_2->biblionumber,
722 pickup_library_id => $item_2->home_branch->branchcode,
723 item_id => $item_2->itemnumber
726 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
730 patron_id => $patron->borrowernumber,
731 biblio_id => $biblio_3->biblionumber,
732 pickup_library_id => $item_1->home_branch->branchcode,
733 item_id => $item_3->itemnumber
736 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
738 ->json_is( { error => 'Hold cannot be placed. Reason: tooManyReserves' } );
740 $schema->storage->txn_rollback;
743 subtest 'pickup_locations() tests' => sub {
747 $schema->storage->txn_begin;
749 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
751 # Small trick to ease testing
752 Koha::Libraries->search->update({ pickup_location => 0 });
754 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
755 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
756 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
758 my $library_1_api = $library_1->to_api();
759 my $library_2_api = $library_2->to_api();
760 my $library_3_api = $library_3->to_api();
762 $library_1_api->{needs_override} = Mojo::JSON->false;
763 $library_2_api->{needs_override} = Mojo::JSON->false;
764 $library_3_api->{needs_override} = Mojo::JSON->false;
766 my $patron = $builder->build_object(
768 class => 'Koha::Patrons',
769 value => { userid => 'tomasito', flags => 0 }
772 $patron->set_password( { password => $password, skip_validation => 1 } );
773 my $userid = $patron->userid;
776 source => 'UserPermission',
778 borrowernumber => $patron->borrowernumber,
780 code => 'place_holds',
785 my $item_class = Test::MockModule->new('Koha::Item');
789 my ( $self, $params ) = @_;
790 my $mock_patron = $params->{patron};
791 is( $mock_patron->borrowernumber,
792 $patron->borrowernumber, 'Patron passed correctly' );
793 return Koha::Libraries->search(
797 $library_1->branchcode,
798 $library_2->branchcode
802 { # we make sure no surprises in the order of the result
803 order_by => { '-asc' => 'marcorgcode' }
809 my $biblio_class = Test::MockModule->new('Koha::Biblio');
813 my ( $self, $params ) = @_;
814 my $mock_patron = $params->{patron};
815 is( $mock_patron->borrowernumber,
816 $patron->borrowernumber, 'Patron passed correctly' );
817 return Koha::Libraries->search(
821 $library_2->branchcode,
822 $library_3->branchcode
826 { # we make sure no surprises in the order of the result
827 order_by => { '-asc' => 'marcorgcode' }
833 my $item = $builder->build_sample_item;
836 my $hold_1 = $builder->build_object(
838 class => 'Koha::Holds',
841 biblionumber => $item->biblionumber,
842 borrowernumber => $patron->borrowernumber
847 my $hold_2 = $builder->build_object(
849 class => 'Koha::Holds',
851 itemnumber => $item->itemnumber,
852 biblionumber => $item->biblionumber,
853 borrowernumber => $patron->borrowernumber
858 $t->get_ok( "//$userid:$password@/api/v1/holds/"
860 . "/pickup_locations" )
861 ->json_is( [ $library_2_api, $library_3_api ] );
863 $t->get_ok( "//$userid:$password@/api/v1/holds/"
865 . "/pickup_locations" )
866 ->json_is( [ $library_1_api, $library_2_api ] );
869 $t->get_ok( "//$userid:$password@/api/v1/holds/"
871 . '/pickup_locations?q={"marc_org_code": { "-like": "A%" }}' )
872 ->json_is( [ $library_1_api ] );
874 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
876 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
877 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
879 my $library_5_api = $library_5->to_api();
880 $library_5_api->{needs_override} = Mojo::JSON->true;
882 # bibli-level mock doesn't include library_1 as valid pickup location
883 $library_1_api->{needs_override} = Mojo::JSON->true;
885 $t->get_ok( "//$userid:$password@/api/v1/holds/"
887 . "/pickup_locations?_order_by=marc_org_code" )
888 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
890 $schema->storage->txn_rollback;
893 subtest 'edit() tests' => sub {
897 $schema->storage->txn_begin;
899 my $password = 'AbcdEFG123';
901 my $library = $builder->build_object({ class => 'Koha::Libraries' });
902 my $patron = $builder->build_object(
903 { class => 'Koha::Patrons', value => { flags => 1 } } );
904 $patron->set_password( { password => $password, skip_validation => 1 } );
905 my $userid = $patron->userid;
908 source => 'UserPermission',
910 borrowernumber => $patron->borrowernumber,
912 code => 'modify_holds_priority',
918 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
919 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
920 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
922 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
923 my $mock_item = Test::MockModule->new('Koha::Item');
925 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
926 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
927 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
929 # let's control what Koha::Biblio->pickup_locations returns, for testing
930 $mock_biblio->mock( 'pickup_locations', sub {
931 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
933 # let's mock what Koha::Item->pickup_locations returns, for testing
934 $mock_item->mock( 'pickup_locations', sub {
935 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
938 my $biblio = $builder->build_sample_biblio;
939 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
941 # Test biblio-level holds
942 my $biblio_hold = $builder->build_object(
944 class => "Koha::Holds",
946 biblionumber => $biblio->biblionumber,
947 branchcode => $library_3->branchcode,
954 my $biblio_hold_api_data = $biblio_hold->to_api;
955 my $biblio_hold_data = {
956 pickup_library_id => $library_1->branchcode,
957 priority => $biblio_hold_api_data->{priority}
960 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
962 => json => $biblio_hold_data )
964 ->json_is({ error => 'The supplied pickup location is not valid' });
966 $t->put_ok( "//$userid:$password@/api/v1/holds/"
968 => json => $biblio_hold_data )
970 ->json_is({ error => 'The supplied pickup location is not valid' });
972 $biblio_hold->discard_changes;
973 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
975 $t->patch_ok( "//$userid:$password@/api/v1/holds/" . $biblio_hold->id
976 => { 'x-koha-override' => 'any' }
977 => json => $biblio_hold_data )
979 ->json_has( '/pickup_library_id' => $library_1->id );
981 $t->put_ok( "//$userid:$password@/api/v1/holds/" . $biblio_hold->id
982 => { 'x-koha-override' => 'any' }
983 => json => $biblio_hold_data )
985 ->json_has( '/pickup_library_id' => $library_1->id );
987 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
988 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
990 => json => $biblio_hold_data )
993 $biblio_hold->discard_changes;
994 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
996 $t->put_ok( "//$userid:$password@/api/v1/holds/"
998 => json => $biblio_hold_data )
1001 $biblio_hold->discard_changes;
1002 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
1004 # Test item-level holds
1005 my $item_hold = $builder->build_object(
1007 class => "Koha::Holds",
1009 biblionumber => $biblio->biblionumber,
1010 branchcode => $library_3->branchcode,
1011 itemnumber => $item->itemnumber,
1017 my $item_hold_api_data = $item_hold->to_api;
1018 my $item_hold_data = {
1019 pickup_library_id => $library_1->branchcode,
1020 priority => $item_hold_api_data->{priority}
1023 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
1025 => json => $item_hold_data )
1027 ->json_is({ error => 'The supplied pickup location is not valid' });
1029 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1031 => json => $item_hold_data )
1033 ->json_is({ error => 'The supplied pickup location is not valid' });
1035 $item_hold->discard_changes;
1036 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
1038 $t->patch_ok( "//$userid:$password@/api/v1/holds/" . $item_hold->id
1039 => { 'x-koha-override' => 'any' }
1040 => json => $item_hold_data )
1042 ->json_has( '/pickup_library_id' => $library_1->id );
1044 $t->put_ok( "//$userid:$password@/api/v1/holds/" . $item_hold->id
1045 => { 'x-koha-override' => 'any' }
1046 => json => $item_hold_data )
1048 ->json_has( '/pickup_library_id' => $library_1->id );
1050 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1051 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
1053 => json => $item_hold_data )
1056 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1058 => json => $item_hold_data )
1061 $item_hold->discard_changes;
1062 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
1064 $schema->storage->txn_rollback;
1067 subtest 'add() tests' => sub {
1071 $schema->storage->txn_begin;
1073 my $password = 'AbcdEFG123';
1075 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1076 my $patron = $builder->build_object(
1077 { class => 'Koha::Patrons', value => { flags => 1 } } );
1078 $patron->set_password( { password => $password, skip_validation => 1 } );
1079 my $userid = $patron->userid;
1082 source => 'UserPermission',
1084 borrowernumber => $patron->borrowernumber,
1086 code => 'modify_holds_priority',
1092 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1093 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1095 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
1096 my $mock_item = Test::MockModule->new('Koha::Item');
1098 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
1099 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
1100 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
1102 # let's control what Koha::Biblio->pickup_locations returns, for testing
1103 $mock_biblio->mock( 'pickup_locations', sub {
1104 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1106 # let's mock what Koha::Item->pickup_locations returns, for testing
1107 $mock_item->mock( 'pickup_locations', sub {
1108 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1111 my $can_be_reserved = 'OK';
1112 my $mock_reserves = Test::MockModule->new('C4::Reserves');
1113 $mock_reserves->mock( 'CanItemBeReserved', sub
1115 return { status => $can_be_reserved }
1119 $mock_reserves->mock( 'CanBookBeReserved', sub
1121 return { status => $can_be_reserved }
1126 my $biblio = $builder->build_sample_biblio;
1127 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1129 # Test biblio-level holds
1130 my $biblio_hold = $builder->build_object(
1132 class => "Koha::Holds",
1134 biblionumber => $biblio->biblionumber,
1135 branchcode => $library_3->branchcode,
1136 itemnumber => undef,
1142 my $biblio_hold_api_data = $biblio_hold->to_api;
1143 $biblio_hold->delete;
1144 my $biblio_hold_data = {
1145 biblio_id => $biblio_hold_api_data->{biblio_id},
1146 patron_id => $biblio_hold_api_data->{patron_id},
1147 pickup_library_id => $library_1->branchcode,
1150 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1152 ->json_is({ error => 'The supplied pickup location is not valid' });
1154 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
1155 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1158 # Test item-level holds
1159 my $item_hold = $builder->build_object(
1161 class => "Koha::Holds",
1163 biblionumber => $biblio->biblionumber,
1164 branchcode => $library_3->branchcode,
1165 itemnumber => $item->itemnumber,
1171 my $item_hold_api_data = $item_hold->to_api;
1173 my $item_hold_data = {
1174 biblio_id => $item_hold_api_data->{biblio_id},
1175 item_id => $item_hold_api_data->{item_id},
1176 patron_id => $item_hold_api_data->{patron_id},
1177 pickup_library_id => $library_1->branchcode,
1180 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1182 ->json_is({ error => 'The supplied pickup location is not valid' });
1184 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1185 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1188 $schema->storage->txn_rollback;
1191 subtest 'PUT /holds/{hold_id}/pickup_location tests' => sub {
1195 $schema->storage->txn_begin;
1197 my $password = 'AbcdEFG123';
1199 # library_1 and library_2 are available pickup locations, not library_3
1200 my $library_1 = $builder->build_object(
1201 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1202 my $library_2 = $builder->build_object(
1203 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1204 my $library_3 = $builder->build_object(
1205 { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
1207 my $patron = $builder->build_object(
1208 { class => 'Koha::Patrons', value => { flags => 0 } } );
1209 $patron->set_password( { password => $password, skip_validation => 1 } );
1210 my $userid = $patron->userid;
1213 source => 'UserPermission',
1215 borrowernumber => $patron->borrowernumber,
1217 code => 'place_holds',
1223 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1224 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1226 my $biblio = $builder->build_sample_biblio;
1227 my $item = $builder->build_sample_item(
1229 biblionumber => $biblio->biblionumber,
1230 library => $library_1->branchcode
1235 my $hold = Koha::Holds->find(
1238 branchcode => $library_1->branchcode,
1239 borrowernumber => $patron->borrowernumber,
1240 biblionumber => $biblio->biblionumber,
1242 itemnumber => undef,
1247 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1249 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1251 ->json_is({ pickup_library_id => $library_2->branchcode });
1253 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1255 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1257 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1259 ->json_is({ error => '[The supplied pickup location is not valid]' });
1261 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library unchanged' );
1264 $hold = Koha::Holds->find(
1267 branchcode => $library_1->branchcode,
1268 borrowernumber => $patron->borrowernumber,
1269 biblionumber => $biblio->biblionumber,
1271 itemnumber => $item->itemnumber,
1276 # Attempt to use an invalid pickup locations ends in 400
1277 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1279 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1281 ->json_is({ error => '[The supplied pickup location is not valid]' });
1283 is( $hold->discard_changes->branchcode->branchcode, $library_1->branchcode, 'pickup library unchanged' );
1285 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
1287 # Attempt to use an invalid pickup locations with override succeeds
1288 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1290 . "/pickup_location"
1291 => { 'x-koha-override' => 'any' }
1292 => json => { pickup_library_id => $library_2->branchcode } )
1294 ->json_is({ pickup_library_id => $library_2->branchcode });
1296 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library changed' );
1298 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
1300 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1302 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1304 ->json_is({ pickup_library_id => $library_2->branchcode });
1306 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1308 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1310 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1312 ->json_is({ error => '[The supplied pickup location is not valid]' });
1314 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'invalid pickup library not used' );
1316 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1318 . "/pickup_location"
1319 => { 'x-koha-override' => 'any' }
1320 => json => { pickup_library_id => $library_3->branchcode } )
1322 ->json_is({ error => '[The supplied pickup location is not valid]' });
1324 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'invalid pickup library not used, even if x-koha-override is passed' );
1326 $schema->storage->txn_rollback;