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 $biblio_hold->discard_changes;
967 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
969 $t->patch_ok( "//$userid:$password@/api/v1/holds/" . $biblio_hold->id
970 => { 'x-koha-override' => 'any' }
971 => json => $biblio_hold_data )
973 ->json_has( '/pickup_library_id' => $library_1->id );
975 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
976 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
978 => json => $biblio_hold_data )
981 $biblio_hold->discard_changes;
982 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
984 # Test item-level holds
985 my $item_hold = $builder->build_object(
987 class => "Koha::Holds",
989 biblionumber => $biblio->biblionumber,
990 branchcode => $library_3->branchcode,
991 itemnumber => $item->itemnumber,
997 my $item_hold_api_data = $item_hold->to_api;
998 my $item_hold_data = {
999 pickup_library_id => $library_1->branchcode,
1000 priority => $item_hold_api_data->{priority}
1003 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
1005 => json => $item_hold_data )
1007 ->json_is({ error => 'The supplied pickup location is not valid' });
1009 $item_hold->discard_changes;
1010 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
1012 $t->patch_ok( "//$userid:$password@/api/v1/holds/" . $item_hold->id
1013 => { 'x-koha-override' => 'any' }
1014 => json => $item_hold_data )
1016 ->json_has( '/pickup_library_id' => $library_1->id );
1018 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1019 $t->patch_ok( "//$userid:$password@/api/v1/holds/"
1021 => json => $item_hold_data )
1024 $item_hold->discard_changes;
1025 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
1027 $schema->storage->txn_rollback;
1030 subtest 'add() tests' => sub {
1034 $schema->storage->txn_begin;
1036 my $password = 'AbcdEFG123';
1038 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1039 my $patron = $builder->build_object(
1040 { class => 'Koha::Patrons', value => { flags => 1 } } );
1041 $patron->set_password( { password => $password, skip_validation => 1 } );
1042 my $userid = $patron->userid;
1045 source => 'UserPermission',
1047 borrowernumber => $patron->borrowernumber,
1049 code => 'modify_holds_priority',
1055 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1056 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1058 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
1059 my $mock_item = Test::MockModule->new('Koha::Item');
1061 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
1062 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
1063 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
1065 # let's control what Koha::Biblio->pickup_locations returns, for testing
1066 $mock_biblio->mock( 'pickup_locations', sub {
1067 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1069 # let's mock what Koha::Item->pickup_locations returns, for testing
1070 $mock_item->mock( 'pickup_locations', sub {
1071 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1074 my $can_be_reserved = 'OK';
1075 my $mock_reserves = Test::MockModule->new('C4::Reserves');
1076 $mock_reserves->mock( 'CanItemBeReserved', sub
1078 return { status => $can_be_reserved }
1082 $mock_reserves->mock( 'CanBookBeReserved', sub
1084 return { status => $can_be_reserved }
1089 my $biblio = $builder->build_sample_biblio;
1090 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1092 # Test biblio-level holds
1093 my $biblio_hold = $builder->build_object(
1095 class => "Koha::Holds",
1097 biblionumber => $biblio->biblionumber,
1098 branchcode => $library_3->branchcode,
1099 itemnumber => undef,
1105 my $biblio_hold_api_data = $biblio_hold->to_api;
1106 $biblio_hold->delete;
1107 my $biblio_hold_data = {
1108 biblio_id => $biblio_hold_api_data->{biblio_id},
1109 patron_id => $biblio_hold_api_data->{patron_id},
1110 pickup_library_id => $library_1->branchcode,
1113 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1115 ->json_is({ error => 'The supplied pickup location is not valid' });
1117 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
1118 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1121 # Test item-level holds
1122 my $item_hold = $builder->build_object(
1124 class => "Koha::Holds",
1126 biblionumber => $biblio->biblionumber,
1127 branchcode => $library_3->branchcode,
1128 itemnumber => $item->itemnumber,
1134 my $item_hold_api_data = $item_hold->to_api;
1136 my $item_hold_data = {
1137 biblio_id => $item_hold_api_data->{biblio_id},
1138 item_id => $item_hold_api_data->{item_id},
1139 patron_id => $item_hold_api_data->{patron_id},
1140 pickup_library_id => $library_1->branchcode,
1143 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1145 ->json_is({ error => 'The supplied pickup location is not valid' });
1147 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1148 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1151 $schema->storage->txn_rollback;
1154 subtest 'PUT /holds/{hold_id}/pickup_location tests' => sub {
1158 $schema->storage->txn_begin;
1160 my $password = 'AbcdEFG123';
1162 # library_1 and library_2 are available pickup locations, not library_3
1163 my $library_1 = $builder->build_object(
1164 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1165 my $library_2 = $builder->build_object(
1166 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1167 my $library_3 = $builder->build_object(
1168 { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
1170 my $patron = $builder->build_object(
1171 { class => 'Koha::Patrons', value => { flags => 0 } } );
1172 $patron->set_password( { password => $password, skip_validation => 1 } );
1173 my $userid = $patron->userid;
1176 source => 'UserPermission',
1178 borrowernumber => $patron->borrowernumber,
1180 code => 'place_holds',
1186 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1187 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1189 my $biblio = $builder->build_sample_biblio;
1190 my $item = $builder->build_sample_item(
1192 biblionumber => $biblio->biblionumber,
1193 library => $library_1->branchcode
1198 my $hold = Koha::Holds->find(
1201 branchcode => $library_1->branchcode,
1202 borrowernumber => $patron->borrowernumber,
1203 biblionumber => $biblio->biblionumber,
1205 itemnumber => undef,
1210 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1212 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1214 ->json_is({ pickup_library_id => $library_2->branchcode });
1216 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1218 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1220 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1222 ->json_is({ error => '[The supplied pickup location is not valid]' });
1224 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library unchanged' );
1227 $hold = Koha::Holds->find(
1230 branchcode => $library_1->branchcode,
1231 borrowernumber => $patron->borrowernumber,
1232 biblionumber => $biblio->biblionumber,
1234 itemnumber => $item->itemnumber,
1239 # Attempt to use an invalid pickup locations ends in 400
1240 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1242 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1244 ->json_is({ error => '[The supplied pickup location is not valid]' });
1246 is( $hold->discard_changes->branchcode->branchcode, $library_1->branchcode, 'pickup library unchanged' );
1248 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
1250 # Attempt to use an invalid pickup locations with override succeeds
1251 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1253 . "/pickup_location"
1254 => { 'x-koha-override' => 'any' }
1255 => json => { pickup_library_id => $library_2->branchcode } )
1257 ->json_is({ pickup_library_id => $library_2->branchcode });
1259 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library changed' );
1261 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
1263 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1265 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1267 ->json_is({ pickup_library_id => $library_2->branchcode });
1269 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1271 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1273 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1275 ->json_is({ error => '[The supplied pickup location is not valid]' });
1277 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'invalid pickup library not used' );
1279 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1281 . "/pickup_location"
1282 => { 'x-koha-override' => 'any' }
1283 => json => { pickup_library_id => $library_3->branchcode } )
1285 ->json_is({ error => '[The supplied pickup location is not valid]' });
1287 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'invalid pickup library not used, even if x-koha-override is passed' );
1289 $schema->storage->txn_rollback;