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 => int($patron_1->borrowernumber),
159 biblio_id => int($biblio_1->biblionumber),
160 item_id => int($item_1->itemnumber),
161 pickup_library_id => $branchcode,
162 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
167 suspended_until => output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }),
170 subtest "Test endpoints without authentication" => sub {
172 $t->get_ok('/api/v1/holds')
174 $t->post_ok('/api/v1/holds')
176 $t->put_ok('/api/v1/holds/0')
178 $t->delete_ok('/api/v1/holds/0')
182 subtest "Test endpoints without permission" => sub {
186 $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
189 $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
192 $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json => $post_data )
195 $t->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $put_data )
198 $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
202 subtest "Test endpoints with permission" => sub {
206 $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
212 $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
214 ->json_is('/0/patron_id', $patron_2->borrowernumber)
217 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
218 ->status_is(204, 'SWAGGER3.2.4')
219 ->content_is('', 'SWAGGER3.3.4');
221 $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
223 ->json_has('/error');
225 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
227 ->json_has('/error');
229 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
233 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
234 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
238 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
239 ->status_is(204, 'SWAGGER3.2.4')
240 ->content_is('', 'SWAGGER3.3.4');
242 # Make sure pickup location checks doesn't get in the middle
243 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
244 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
245 my $mock_item = Test::MockModule->new('Koha::Item');
246 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
248 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
250 ->json_has('/hold_id');
252 # Get id from response
253 $reserve_id = $t->tx->res->json->{hold_id};
255 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
257 ->json_is('/0/hold_id', $reserve_id)
258 ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
259 ->json_is('/0/pickup_library_id', $branchcode);
261 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
263 ->json_like('/error', qr/itemAlreadyOnHold/);
265 $post_data->{biblionumber} = int($biblio_2->biblionumber);
266 $post_data->{itemnumber} = int($item_2->itemnumber);
268 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
270 ->json_like('/error', qr/itemAlreadyOnHold/);
272 my $to_delete_patron = $builder->build_object({ class => 'Koha::Patrons' });
273 my $deleted_patron_id = $to_delete_patron->borrowernumber;
274 $to_delete_patron->delete;
276 my $tmp_patron_id = $post_data->{patron_id};
277 $post_data->{patron_id} = $deleted_patron_id;
278 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
280 ->json_is( { error => 'patron_id not found' } );
282 # Restore the original patron_id as it is expected by the next subtest
283 # FIXME: this tests need to be rewritten from scratch
284 $post_data->{patron_id} = $tmp_patron_id;
287 subtest 'Reserves with itemtype' => sub {
291 patron_id => int($patron_1->borrowernumber),
292 biblio_id => int($biblio_1->biblionumber),
293 pickup_library_id => $branchcode,
294 item_type => $itemtype,
297 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
298 ->status_is(204, 'SWAGGER3.2.4')
299 ->content_is('', 'SWAGGER3.3.4');
301 # Make sure pickup location checks doesn't get in the middle
302 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
303 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
304 my $mock_item = Test::MockModule->new('Koha::Item');
305 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
307 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
309 ->json_has('/hold_id');
311 $reserve_id = $t->tx->res->json->{hold_id};
313 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
315 ->json_is('/0/hold_id', $reserve_id)
316 ->json_is('/0/item_type', $itemtype);
320 subtest 'test AllowHoldDateInFuture' => sub {
324 $dbh->do('DELETE FROM reserves');
326 my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
329 patron_id => int($patron_1->borrowernumber),
330 biblio_id => int($biblio_1->biblionumber),
331 item_id => int($item_1->itemnumber),
332 pickup_library_id => $branchcode,
333 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
334 hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
338 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
340 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
342 ->json_has('/error');
344 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
346 # Make sure pickup location checks doesn't get in the middle
347 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
348 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
349 my $mock_item = Test::MockModule->new('Koha::Item');
350 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
352 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
354 ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
357 $schema->storage->txn_rollback;
359 subtest 'x-koha-override and AllowHoldPolicyOverride tests' => sub {
363 $schema->storage->txn_begin;
365 my $patron = $builder->build_object(
367 class => 'Koha::Patrons',
368 value => { flags => 1 }
371 my $password = 'thePassword123';
372 $patron->set_password( { password => $password, skip_validation => 1 } );
373 $patron->discard_changes;
374 my $userid = $patron->userid;
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; } );
382 my $mock_item = Test::MockModule->new('Koha::Item');
383 $mock_item->mock( 'pickup_locations',
384 sub { return Koha::Libraries->search } );
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 $schema->storage->txn_rollback;
439 subtest 'suspend and resume tests' => sub {
443 $schema->storage->txn_begin;
445 my $password = 'AbcdEFG123';
447 my $patron = $builder->build_object(
448 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
449 $patron->set_password({ password => $password, skip_validation => 1 });
450 my $userid = $patron->userid;
453 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
454 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
456 my $hold = $builder->build_object(
457 { class => 'Koha::Holds',
458 value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
462 ok( !$hold->is_suspended, 'Hold is not suspended' );
463 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
464 ->status_is( 201, 'Hold suspension created' );
466 $hold->discard_changes; # refresh object
468 ok( $hold->is_suspended, 'Hold is suspended' );
469 $t->json_is('/end_date', undef, 'Hold suspension has no end date');
471 my $end_date = output_pref({
472 dt => dt_from_string( undef ),
473 dateformat => 'rfc3339',
477 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" => json => { end_date => $end_date } );
479 $hold->discard_changes; # refresh object
481 ok( $hold->is_suspended, 'Hold is suspended' );
485 dt => dt_from_string( $hold->suspend_until ),
486 dateformat => 'rfc3339',
489 'Hold suspension has correct end date'
492 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
493 ->status_is(204, 'SWAGGER3.2.4')
494 ->content_is('', 'SWAGGER3.3.4');
496 # Pass a an expiration date for the suspension
497 my $date = dt_from_string()->add( days => 5 );
499 "//$userid:$password@/api/v1/holds/"
501 . "/suspension" => json => {
503 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
505 )->status_is( 201, 'Hold suspension created' )
506 ->json_is( '/end_date',
507 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
508 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
510 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
511 ->status_is(204, 'SWAGGER3.2.4')
512 ->content_is('', 'SWAGGER3.3.4');
514 $hold->set_waiting->discard_changes;
516 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
517 ->status_is( 400, 'Cannot suspend waiting hold' )
518 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
520 $hold->set_transfer->discard_changes;
522 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
523 ->status_is( 400, 'Cannot suspend hold on transfer' )
524 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
526 $schema->storage->txn_rollback;
529 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
533 $schema->storage->txn_begin;
535 my $password = 'AbcdEFG123';
537 my $library = $builder->build_object({ class => 'Koha::Libraries' });
538 my $patron_np = $builder->build_object(
539 { class => 'Koha::Patrons', value => { flags => 0 } } );
540 $patron_np->set_password( { password => $password, skip_validation => 1 } );
541 my $userid_np = $patron_np->userid;
543 my $patron = $builder->build_object(
544 { class => 'Koha::Patrons', value => { flags => 0 } } );
545 $patron->set_password( { password => $password, skip_validation => 1 } );
546 my $userid = $patron->userid;
549 source => 'UserPermission',
551 borrowernumber => $patron->borrowernumber,
553 code => 'modify_holds_priority',
559 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
560 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
562 my $biblio = $builder->build_sample_biblio;
563 my $patron_1 = $builder->build_object(
565 class => 'Koha::Patrons',
566 value => { branchcode => $library->branchcode }
569 my $patron_2 = $builder->build_object(
571 class => 'Koha::Patrons',
572 value => { branchcode => $library->branchcode }
575 my $patron_3 = $builder->build_object(
577 class => 'Koha::Patrons',
578 value => { branchcode => $library->branchcode }
582 my $hold_1 = Koha::Holds->find(
585 branchcode => $library->branchcode,
586 borrowernumber => $patron_1->borrowernumber,
587 biblionumber => $biblio->biblionumber,
592 my $hold_2 = Koha::Holds->find(
595 branchcode => $library->branchcode,
596 borrowernumber => $patron_2->borrowernumber,
597 biblionumber => $biblio->biblionumber,
602 my $hold_3 = Koha::Holds->find(
605 branchcode => $library->branchcode,
606 borrowernumber => $patron_3->borrowernumber,
607 biblionumber => $biblio->biblionumber,
613 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
615 . "/priority" => json => 1 )->status_is(403);
617 $t->put_ok( "//$userid:$password@/api/v1/holds/"
619 . "/priority" => json => 1 )->status_is(200)->json_is(1);
621 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
622 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
623 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
625 $t->put_ok( "//$userid:$password@/api/v1/holds/"
627 . "/priority" => json => 3 )->status_is(200)->json_is(3);
629 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
630 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
631 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
633 $schema->storage->txn_rollback;
636 subtest 'add() tests (maxreserves behaviour)' => sub {
640 $schema->storage->txn_begin;
642 $dbh->do('DELETE FROM reserves');
644 Koha::CirculationRules->new->delete;
646 my $password = 'AbcdEFG123';
648 my $patron = $builder->build_object(
649 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
650 $patron->set_password({ password => $password, skip_validation => 1 });
651 my $userid = $patron->userid;
653 Koha::CirculationRules->set_rules(
657 categorycode => undef,
664 Koha::CirculationRules->set_rules(
667 categorycode => $patron->categorycode,
674 my $biblio_1 = $builder->build_sample_biblio;
675 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
676 my $biblio_2 = $builder->build_sample_biblio;
677 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
678 my $biblio_3 = $builder->build_sample_biblio;
679 my $item_3 = $builder->build_sample_item({ biblionumber => $biblio_3->biblionumber });
681 # Make sure pickup location checks doesn't get in the middle
682 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
683 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
684 my $mock_item = Test::MockModule->new('Koha::Item');
685 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
688 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
689 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
690 t::lib::Mocks::mock_preference( 'maxreserves', 2 );
691 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
694 patron_id => $patron->borrowernumber,
695 biblio_id => $biblio_1->biblionumber,
696 pickup_library_id => $item_1->home_branch->branchcode,
697 item_type => $item_1->itype,
700 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
704 patron_id => $patron->borrowernumber,
705 biblio_id => $biblio_2->biblionumber,
706 pickup_library_id => $item_2->home_branch->branchcode,
707 item_id => $item_2->itemnumber
710 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
714 patron_id => $patron->borrowernumber,
715 biblio_id => $biblio_3->biblionumber,
716 pickup_library_id => $item_1->home_branch->branchcode,
717 item_id => $item_3->itemnumber
720 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
722 ->json_is( { error => 'Hold cannot be placed. Reason: tooManyReserves' } );
724 $schema->storage->txn_rollback;
727 subtest 'pickup_locations() tests' => sub {
731 $schema->storage->txn_begin;
733 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
735 # Small trick to ease testing
736 Koha::Libraries->search->update({ pickup_location => 0 });
738 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
739 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
740 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
742 my $library_1_api = $library_1->to_api();
743 my $library_2_api = $library_2->to_api();
744 my $library_3_api = $library_3->to_api();
746 $library_1_api->{needs_override} = Mojo::JSON->false;
747 $library_2_api->{needs_override} = Mojo::JSON->false;
748 $library_3_api->{needs_override} = Mojo::JSON->false;
750 my $patron = $builder->build_object(
752 class => 'Koha::Patrons',
753 value => { userid => 'tomasito', flags => 0 }
756 $patron->set_password( { password => $password, skip_validation => 1 } );
757 my $userid = $patron->userid;
760 source => 'UserPermission',
762 borrowernumber => $patron->borrowernumber,
764 code => 'place_holds',
769 my $item_class = Test::MockModule->new('Koha::Item');
773 my ( $self, $params ) = @_;
774 my $mock_patron = $params->{patron};
775 is( $mock_patron->borrowernumber,
776 $patron->borrowernumber, 'Patron passed correctly' );
777 return Koha::Libraries->search(
781 $library_1->branchcode,
782 $library_2->branchcode
786 { # we make sure no surprises in the order of the result
787 order_by => { '-asc' => 'marcorgcode' }
793 my $biblio_class = Test::MockModule->new('Koha::Biblio');
797 my ( $self, $params ) = @_;
798 my $mock_patron = $params->{patron};
799 is( $mock_patron->borrowernumber,
800 $patron->borrowernumber, 'Patron passed correctly' );
801 return Koha::Libraries->search(
805 $library_2->branchcode,
806 $library_3->branchcode
810 { # we make sure no surprises in the order of the result
811 order_by => { '-asc' => 'marcorgcode' }
817 my $item = $builder->build_sample_item;
820 my $hold_1 = $builder->build_object(
822 class => 'Koha::Holds',
825 biblionumber => $item->biblionumber,
826 borrowernumber => $patron->borrowernumber
831 my $hold_2 = $builder->build_object(
833 class => 'Koha::Holds',
835 itemnumber => $item->itemnumber,
836 biblionumber => $item->biblionumber,
837 borrowernumber => $patron->borrowernumber
842 $t->get_ok( "//$userid:$password@/api/v1/holds/"
844 . "/pickup_locations" )
845 ->json_is( [ $library_2_api, $library_3_api ] );
847 $t->get_ok( "//$userid:$password@/api/v1/holds/"
849 . "/pickup_locations" )
850 ->json_is( [ $library_1_api, $library_2_api ] );
853 $t->get_ok( "//$userid:$password@/api/v1/holds/"
855 . '/pickup_locations?q={"marc_org_code": { "-like": "A%" }}' )
856 ->json_is( [ $library_1_api ] );
858 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
860 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
861 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
863 my $library_5_api = $library_5->to_api();
864 $library_5_api->{needs_override} = Mojo::JSON->true;
866 # bibli-level mock doesn't include library_1 as valid pickup location
867 $library_1_api->{needs_override} = Mojo::JSON->true;
869 $t->get_ok( "//$userid:$password@/api/v1/holds/"
871 . "/pickup_locations?_order_by=marc_org_code" )
872 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
874 $schema->storage->txn_rollback;
877 subtest 'edit() tests' => sub {
881 $schema->storage->txn_begin;
883 my $password = 'AbcdEFG123';
885 my $library = $builder->build_object({ class => 'Koha::Libraries' });
886 my $patron = $builder->build_object(
887 { class => 'Koha::Patrons', value => { flags => 1 } } );
888 $patron->set_password( { password => $password, skip_validation => 1 } );
889 my $userid = $patron->userid;
892 source => 'UserPermission',
894 borrowernumber => $patron->borrowernumber,
896 code => 'modify_holds_priority',
902 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
903 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
905 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
906 my $mock_item = Test::MockModule->new('Koha::Item');
908 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
909 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
910 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
912 # let's control what Koha::Biblio->pickup_locations returns, for testing
913 $mock_biblio->mock( 'pickup_locations', sub {
914 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
916 # let's mock what Koha::Item->pickup_locations returns, for testing
917 $mock_item->mock( 'pickup_locations', sub {
918 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
921 my $biblio = $builder->build_sample_biblio;
922 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
924 # Test biblio-level holds
925 my $biblio_hold = $builder->build_object(
927 class => "Koha::Holds",
929 biblionumber => $biblio->biblionumber,
930 branchcode => $library_3->branchcode,
937 my $biblio_hold_data = $biblio_hold->to_api;
938 $biblio_hold_data->{pickup_library_id} = $library_1->branchcode;
940 $t->put_ok( "//$userid:$password@/api/v1/holds/"
942 => json => $biblio_hold_data )
944 ->json_is({ error => 'The supplied pickup location is not valid' });
946 $biblio_hold->discard_changes;
947 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
949 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
950 $t->put_ok( "//$userid:$password@/api/v1/holds/"
952 => json => $biblio_hold_data )
955 $biblio_hold->discard_changes;
956 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
958 # Test item-level holds
959 my $item_hold = $builder->build_object(
961 class => "Koha::Holds",
963 biblionumber => $biblio->biblionumber,
964 branchcode => $library_3->branchcode,
965 itemnumber => $item->itemnumber,
971 my $item_hold_data = $item_hold->to_api;
972 $item_hold_data->{pickup_library_id} = $library_1->branchcode;
974 $t->put_ok( "//$userid:$password@/api/v1/holds/"
976 => json => $item_hold_data )
978 ->json_is({ error => 'The supplied pickup location is not valid' });
980 $item_hold->discard_changes;
981 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
983 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
984 $t->put_ok( "//$userid:$password@/api/v1/holds/"
986 => json => $item_hold_data )
989 $item_hold->discard_changes;
990 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
992 $schema->storage->txn_rollback;
995 subtest 'add() tests' => sub {
999 $schema->storage->txn_begin;
1001 my $password = 'AbcdEFG123';
1003 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1004 my $patron = $builder->build_object(
1005 { class => 'Koha::Patrons', value => { flags => 1 } } );
1006 $patron->set_password( { password => $password, skip_validation => 1 } );
1007 my $userid = $patron->userid;
1010 source => 'UserPermission',
1012 borrowernumber => $patron->borrowernumber,
1014 code => 'modify_holds_priority',
1020 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1021 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1023 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
1024 my $mock_item = Test::MockModule->new('Koha::Item');
1026 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
1027 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
1028 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
1030 # let's control what Koha::Biblio->pickup_locations returns, for testing
1031 $mock_biblio->mock( 'pickup_locations', sub {
1032 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1034 # let's mock what Koha::Item->pickup_locations returns, for testing
1035 $mock_item->mock( 'pickup_locations', sub {
1036 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1039 my $can_be_reserved = 'OK';
1040 my $mock_reserves = Test::MockModule->new('C4::Reserves');
1041 $mock_reserves->mock( 'CanItemBeReserved', sub
1043 return { status => $can_be_reserved }
1047 $mock_reserves->mock( 'CanBookBeReserved', sub
1049 return { status => $can_be_reserved }
1054 my $biblio = $builder->build_sample_biblio;
1055 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1057 # Test biblio-level holds
1058 my $biblio_hold = $builder->build_object(
1060 class => "Koha::Holds",
1062 biblionumber => $biblio->biblionumber,
1063 branchcode => $library_3->branchcode,
1064 itemnumber => undef,
1070 my $biblio_hold_data = $biblio_hold->to_api;
1071 $biblio_hold->delete;
1072 $biblio_hold_data->{pickup_library_id} = $library_1->branchcode;
1073 delete $biblio_hold_data->{hold_id};
1075 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1077 ->json_is({ error => 'The supplied pickup location is not valid' });
1079 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
1080 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1083 # Test item-level holds
1084 my $item_hold = $builder->build_object(
1086 class => "Koha::Holds",
1088 biblionumber => $biblio->biblionumber,
1089 branchcode => $library_3->branchcode,
1090 itemnumber => $item->itemnumber,
1096 my $item_hold_data = $item_hold->to_api;
1098 $item_hold_data->{pickup_library_id} = $library_1->branchcode;
1099 delete $item_hold->{hold_id};
1101 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1103 ->json_is({ error => 'The supplied pickup location is not valid' });
1105 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1106 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1109 $schema->storage->txn_rollback;
1112 subtest 'PUT /holds/{hold_id}/pickup_location tests' => sub {
1116 $schema->storage->txn_begin;
1118 my $password = 'AbcdEFG123';
1120 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
1121 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
1122 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
1124 my $patron = $builder->build_object(
1125 { class => 'Koha::Patrons', value => { flags => 0 } } );
1126 $patron->set_password( { password => $password, skip_validation => 1 } );
1127 my $userid = $patron->userid;
1130 source => 'UserPermission',
1132 borrowernumber => $patron->borrowernumber,
1134 code => 'place_holds',
1140 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1141 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1143 my $libraries_query = { branchcode => [ $library_1->branchcode, $library_2->branchcode ] };
1145 my $mocked_biblio = Test::MockModule->new('Koha::Biblio');
1146 $mocked_biblio->mock( 'pickup_locations', sub {
1147 return Koha::Libraries->search($libraries_query);
1150 my $mocked_item = Test::MockModule->new('Koha::Item');
1151 $mocked_item->mock( 'pickup_locations', sub {
1152 return Koha::Libraries->search($libraries_query);
1155 my $biblio = $builder->build_sample_biblio;
1156 my $item = $builder->build_sample_item(
1158 biblionumber => $biblio->biblionumber,
1159 library => $library_1->branchcode
1164 my $hold = Koha::Holds->find(
1167 branchcode => $library_1->branchcode,
1168 borrowernumber => $patron->borrowernumber,
1169 biblionumber => $biblio->biblionumber,
1171 itemnumber => undef,
1176 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1178 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1180 ->json_is({ pickup_library_id => $library_2->branchcode });
1182 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1184 $libraries_query = { branchcode => $library_1->branchcode };
1186 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1188 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1190 ->json_is({ error => '[The supplied pickup location is not valid]' });
1192 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library unchanged' );
1195 $hold = Koha::Holds->find(
1198 branchcode => $library_1->branchcode,
1199 borrowernumber => $patron->borrowernumber,
1200 biblionumber => $biblio->biblionumber,
1202 itemnumber => $item->itemnumber,
1207 $libraries_query = { branchcode => $library_1->branchcode };
1209 # Attempt to use an invalid pickup locations ends in 400
1210 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1212 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1214 ->json_is({ error => '[The supplied pickup location is not valid]' });
1216 is( $hold->discard_changes->branchcode->branchcode, $library_1->branchcode, 'pickup library unchanged' );
1218 $libraries_query = {
1220 '-in' => [ $library_1->branchcode, $library_2->branchcode ]
1224 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1226 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1228 ->json_is({ pickup_library_id => $library_2->branchcode });
1230 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1232 $schema->storage->txn_rollback;