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 my $renegade_library = $builder->build_object({ class => 'Koha::Libraries' });
378 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
380 # Make sure pickup location checks doesn't get in the middle
381 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
382 $mock_biblio->mock( 'pickup_locations',
383 sub { return Koha::Libraries->search({ branchcode => { '!=' => $renegade_library->branchcode } }); } );
384 my $mock_item = Test::MockModule->new('Koha::Item');
385 $mock_item->mock( 'pickup_locations',
386 sub { return Koha::Libraries->search({ branchcode => { '!=' => $renegade_library->branchcode } }) } );
388 my $can_item_be_reserved_result;
389 my $mock_reserves = Test::MockModule->new('C4::Reserves');
390 $mock_reserves->mock(
393 return $can_item_be_reserved_result;
397 my $item = $builder->build_sample_item;
400 item_id => $item->id,
401 biblio_id => $item->biblionumber,
402 patron_id => $patron->id,
403 pickup_library_id => $patron->branchcode,
406 $can_item_be_reserved_result = { status => 'ageRestricted' };
408 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
410 ->json_is( '/error' => "Hold cannot be placed. Reason: ageRestricted" );
412 # x-koha-override doesn't override if AllowHoldPolicyOverride not set
413 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
414 { 'x-koha-override' => 'any' } => json => $post_data )
417 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
419 $can_item_be_reserved_result = { status => 'pickupNotInHoldGroup' };
421 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
424 '/error' => "Hold cannot be placed. Reason: pickupNotInHoldGroup" );
426 # x-koha-override overrides the status
427 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
428 { 'x-koha-override' => 'any' } => json => $post_data )
431 $can_item_be_reserved_result = { status => 'OK' };
433 # x-koha-override works when status not need override
434 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
435 { 'x-koha-override' => 'any' } => json => $post_data )
438 # Test pickup locations can be overridden
439 $post_data->{pickup_library_id} = $renegade_library->branchcode;
441 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
444 $t->post_ok( "//$userid:$password@/api/v1/holds" =>
445 { 'x-koha-override' => 'any' } => json => $post_data )
448 $schema->storage->txn_rollback;
451 subtest 'suspend and resume tests' => sub {
455 $schema->storage->txn_begin;
457 my $password = 'AbcdEFG123';
459 my $patron = $builder->build_object(
460 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
461 $patron->set_password({ password => $password, skip_validation => 1 });
462 my $userid = $patron->userid;
465 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
466 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
468 my $hold = $builder->build_object(
469 { class => 'Koha::Holds',
470 value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
474 ok( !$hold->is_suspended, 'Hold is not suspended' );
475 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
476 ->status_is( 201, 'Hold suspension created' );
478 $hold->discard_changes; # refresh object
480 ok( $hold->is_suspended, 'Hold is suspended' );
481 $t->json_is('/end_date', undef, 'Hold suspension has no end date');
483 my $end_date = output_pref({
484 dt => dt_from_string( undef ),
485 dateformat => 'rfc3339',
489 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" => json => { end_date => $end_date } );
491 $hold->discard_changes; # refresh object
493 ok( $hold->is_suspended, 'Hold is suspended' );
497 dt => dt_from_string( $hold->suspend_until ),
498 dateformat => 'rfc3339',
501 'Hold suspension has correct end date'
504 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
505 ->status_is(204, 'SWAGGER3.2.4')
506 ->content_is('', 'SWAGGER3.3.4');
508 # Pass a an expiration date for the suspension
509 my $date = dt_from_string()->add( days => 5 );
511 "//$userid:$password@/api/v1/holds/"
513 . "/suspension" => json => {
515 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
517 )->status_is( 201, 'Hold suspension created' )
518 ->json_is( '/end_date',
519 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
520 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
522 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
523 ->status_is(204, 'SWAGGER3.2.4')
524 ->content_is('', 'SWAGGER3.3.4');
526 $hold->set_waiting->discard_changes;
528 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
529 ->status_is( 400, 'Cannot suspend waiting hold' )
530 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
532 $hold->set_transfer->discard_changes;
534 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
535 ->status_is( 400, 'Cannot suspend hold on transfer' )
536 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
538 $schema->storage->txn_rollback;
541 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
545 $schema->storage->txn_begin;
547 my $password = 'AbcdEFG123';
549 my $library = $builder->build_object({ class => 'Koha::Libraries' });
550 my $patron_np = $builder->build_object(
551 { class => 'Koha::Patrons', value => { flags => 0 } } );
552 $patron_np->set_password( { password => $password, skip_validation => 1 } );
553 my $userid_np = $patron_np->userid;
555 my $patron = $builder->build_object(
556 { class => 'Koha::Patrons', value => { flags => 0 } } );
557 $patron->set_password( { password => $password, skip_validation => 1 } );
558 my $userid = $patron->userid;
561 source => 'UserPermission',
563 borrowernumber => $patron->borrowernumber,
565 code => 'modify_holds_priority',
571 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
572 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
574 my $biblio = $builder->build_sample_biblio;
575 my $patron_1 = $builder->build_object(
577 class => 'Koha::Patrons',
578 value => { branchcode => $library->branchcode }
581 my $patron_2 = $builder->build_object(
583 class => 'Koha::Patrons',
584 value => { branchcode => $library->branchcode }
587 my $patron_3 = $builder->build_object(
589 class => 'Koha::Patrons',
590 value => { branchcode => $library->branchcode }
594 my $hold_1 = Koha::Holds->find(
597 branchcode => $library->branchcode,
598 borrowernumber => $patron_1->borrowernumber,
599 biblionumber => $biblio->biblionumber,
604 my $hold_2 = Koha::Holds->find(
607 branchcode => $library->branchcode,
608 borrowernumber => $patron_2->borrowernumber,
609 biblionumber => $biblio->biblionumber,
614 my $hold_3 = Koha::Holds->find(
617 branchcode => $library->branchcode,
618 borrowernumber => $patron_3->borrowernumber,
619 biblionumber => $biblio->biblionumber,
625 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
627 . "/priority" => json => 1 )->status_is(403);
629 $t->put_ok( "//$userid:$password@/api/v1/holds/"
631 . "/priority" => json => 1 )->status_is(200)->json_is(1);
633 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
634 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
635 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
637 $t->put_ok( "//$userid:$password@/api/v1/holds/"
639 . "/priority" => json => 3 )->status_is(200)->json_is(3);
641 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
642 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
643 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
645 $schema->storage->txn_rollback;
648 subtest 'add() tests (maxreserves behaviour)' => sub {
652 $schema->storage->txn_begin;
654 $dbh->do('DELETE FROM reserves');
656 Koha::CirculationRules->new->delete;
658 my $password = 'AbcdEFG123';
660 my $patron = $builder->build_object(
661 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
662 $patron->set_password({ password => $password, skip_validation => 1 });
663 my $userid = $patron->userid;
665 Koha::CirculationRules->set_rules(
669 categorycode => undef,
676 Koha::CirculationRules->set_rules(
679 categorycode => $patron->categorycode,
686 my $biblio_1 = $builder->build_sample_biblio;
687 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
688 my $biblio_2 = $builder->build_sample_biblio;
689 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
690 my $biblio_3 = $builder->build_sample_biblio;
691 my $item_3 = $builder->build_sample_item({ biblionumber => $biblio_3->biblionumber });
693 # Make sure pickup location checks doesn't get in the middle
694 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
695 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; });
696 my $mock_item = Test::MockModule->new('Koha::Item');
697 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search });
700 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
701 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
702 t::lib::Mocks::mock_preference( 'maxreserves', 2 );
703 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
706 patron_id => $patron->borrowernumber,
707 biblio_id => $biblio_1->biblionumber,
708 pickup_library_id => $item_1->home_branch->branchcode,
709 item_type => $item_1->itype,
712 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
716 patron_id => $patron->borrowernumber,
717 biblio_id => $biblio_2->biblionumber,
718 pickup_library_id => $item_2->home_branch->branchcode,
719 item_id => $item_2->itemnumber
722 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
726 patron_id => $patron->borrowernumber,
727 biblio_id => $biblio_3->biblionumber,
728 pickup_library_id => $item_1->home_branch->branchcode,
729 item_id => $item_3->itemnumber
732 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
734 ->json_is( { error => 'Hold cannot be placed. Reason: tooManyReserves' } );
736 $schema->storage->txn_rollback;
739 subtest 'pickup_locations() tests' => sub {
743 $schema->storage->txn_begin;
745 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
747 # Small trick to ease testing
748 Koha::Libraries->search->update({ pickup_location => 0 });
750 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
751 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
752 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
754 my $library_1_api = $library_1->to_api();
755 my $library_2_api = $library_2->to_api();
756 my $library_3_api = $library_3->to_api();
758 $library_1_api->{needs_override} = Mojo::JSON->false;
759 $library_2_api->{needs_override} = Mojo::JSON->false;
760 $library_3_api->{needs_override} = Mojo::JSON->false;
762 my $patron = $builder->build_object(
764 class => 'Koha::Patrons',
765 value => { userid => 'tomasito', flags => 0 }
768 $patron->set_password( { password => $password, skip_validation => 1 } );
769 my $userid = $patron->userid;
772 source => 'UserPermission',
774 borrowernumber => $patron->borrowernumber,
776 code => 'place_holds',
781 my $item_class = Test::MockModule->new('Koha::Item');
785 my ( $self, $params ) = @_;
786 my $mock_patron = $params->{patron};
787 is( $mock_patron->borrowernumber,
788 $patron->borrowernumber, 'Patron passed correctly' );
789 return Koha::Libraries->search(
793 $library_1->branchcode,
794 $library_2->branchcode
798 { # we make sure no surprises in the order of the result
799 order_by => { '-asc' => 'marcorgcode' }
805 my $biblio_class = Test::MockModule->new('Koha::Biblio');
809 my ( $self, $params ) = @_;
810 my $mock_patron = $params->{patron};
811 is( $mock_patron->borrowernumber,
812 $patron->borrowernumber, 'Patron passed correctly' );
813 return Koha::Libraries->search(
817 $library_2->branchcode,
818 $library_3->branchcode
822 { # we make sure no surprises in the order of the result
823 order_by => { '-asc' => 'marcorgcode' }
829 my $item = $builder->build_sample_item;
832 my $hold_1 = $builder->build_object(
834 class => 'Koha::Holds',
837 biblionumber => $item->biblionumber,
838 borrowernumber => $patron->borrowernumber
843 my $hold_2 = $builder->build_object(
845 class => 'Koha::Holds',
847 itemnumber => $item->itemnumber,
848 biblionumber => $item->biblionumber,
849 borrowernumber => $patron->borrowernumber
854 $t->get_ok( "//$userid:$password@/api/v1/holds/"
856 . "/pickup_locations" )
857 ->json_is( [ $library_2_api, $library_3_api ] );
859 $t->get_ok( "//$userid:$password@/api/v1/holds/"
861 . "/pickup_locations" )
862 ->json_is( [ $library_1_api, $library_2_api ] );
865 $t->get_ok( "//$userid:$password@/api/v1/holds/"
867 . '/pickup_locations?q={"marc_org_code": { "-like": "A%" }}' )
868 ->json_is( [ $library_1_api ] );
870 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
872 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
873 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
875 my $library_5_api = $library_5->to_api();
876 $library_5_api->{needs_override} = Mojo::JSON->true;
878 # bibli-level mock doesn't include library_1 as valid pickup location
879 $library_1_api->{needs_override} = Mojo::JSON->true;
881 $t->get_ok( "//$userid:$password@/api/v1/holds/"
883 . "/pickup_locations?_order_by=marc_org_code" )
884 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
886 $schema->storage->txn_rollback;
889 subtest 'edit() tests' => sub {
893 $schema->storage->txn_begin;
895 my $password = 'AbcdEFG123';
897 my $library = $builder->build_object({ class => 'Koha::Libraries' });
898 my $patron = $builder->build_object(
899 { class => 'Koha::Patrons', value => { flags => 1 } } );
900 $patron->set_password( { password => $password, skip_validation => 1 } );
901 my $userid = $patron->userid;
904 source => 'UserPermission',
906 borrowernumber => $patron->borrowernumber,
908 code => 'modify_holds_priority',
914 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
915 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
916 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
918 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
919 my $mock_item = Test::MockModule->new('Koha::Item');
921 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
922 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
923 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
925 # let's control what Koha::Biblio->pickup_locations returns, for testing
926 $mock_biblio->mock( 'pickup_locations', sub {
927 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
929 # let's mock what Koha::Item->pickup_locations returns, for testing
930 $mock_item->mock( 'pickup_locations', sub {
931 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
934 my $biblio = $builder->build_sample_biblio;
935 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
937 # Test biblio-level holds
938 my $biblio_hold = $builder->build_object(
940 class => "Koha::Holds",
942 biblionumber => $biblio->biblionumber,
943 branchcode => $library_3->branchcode,
950 my $biblio_hold_data = $biblio_hold->to_api;
951 $biblio_hold_data->{pickup_library_id} = $library_1->branchcode;
953 $t->put_ok( "//$userid:$password@/api/v1/holds/"
955 => json => $biblio_hold_data )
957 ->json_is({ error => 'The supplied pickup location is not valid' });
959 $biblio_hold->discard_changes;
960 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
962 $t->put_ok( "//$userid:$password@/api/v1/holds/" . $biblio_hold->id
963 => { 'x-koha-override' => 'any' }
964 => json => $biblio_hold_data )
966 ->json_has( '/pickup_library_id' => $library_1->id );
968 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
969 $t->put_ok( "//$userid:$password@/api/v1/holds/"
971 => json => $biblio_hold_data )
974 $biblio_hold->discard_changes;
975 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
977 # Test item-level holds
978 my $item_hold = $builder->build_object(
980 class => "Koha::Holds",
982 biblionumber => $biblio->biblionumber,
983 branchcode => $library_3->branchcode,
984 itemnumber => $item->itemnumber,
990 my $item_hold_data = $item_hold->to_api;
991 $item_hold_data->{pickup_library_id} = $library_1->branchcode;
993 $t->put_ok( "//$userid:$password@/api/v1/holds/"
995 => json => $item_hold_data )
997 ->json_is({ error => 'The supplied pickup location is not valid' });
999 $item_hold->discard_changes;
1000 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
1002 $t->put_ok( "//$userid:$password@/api/v1/holds/" . $item_hold->id
1003 => { 'x-koha-override' => 'any' }
1004 => json => $item_hold_data )
1006 ->json_has( '/pickup_library_id' => $library_1->id );
1008 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1009 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1011 => json => $item_hold_data )
1014 $item_hold->discard_changes;
1015 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
1017 $schema->storage->txn_rollback;
1020 subtest 'add() tests' => sub {
1024 $schema->storage->txn_begin;
1026 my $password = 'AbcdEFG123';
1028 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1029 my $patron = $builder->build_object(
1030 { class => 'Koha::Patrons', value => { flags => 1 } } );
1031 $patron->set_password( { password => $password, skip_validation => 1 } );
1032 my $userid = $patron->userid;
1035 source => 'UserPermission',
1037 borrowernumber => $patron->borrowernumber,
1039 code => 'modify_holds_priority',
1045 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1046 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1048 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
1049 my $mock_item = Test::MockModule->new('Koha::Item');
1051 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
1052 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
1053 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
1055 # let's control what Koha::Biblio->pickup_locations returns, for testing
1056 $mock_biblio->mock( 'pickup_locations', sub {
1057 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1059 # let's mock what Koha::Item->pickup_locations returns, for testing
1060 $mock_item->mock( 'pickup_locations', sub {
1061 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
1064 my $can_be_reserved = 'OK';
1065 my $mock_reserves = Test::MockModule->new('C4::Reserves');
1066 $mock_reserves->mock( 'CanItemBeReserved', sub
1068 return { status => $can_be_reserved }
1072 $mock_reserves->mock( 'CanBookBeReserved', sub
1074 return { status => $can_be_reserved }
1079 my $biblio = $builder->build_sample_biblio;
1080 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1082 # Test biblio-level holds
1083 my $biblio_hold = $builder->build_object(
1085 class => "Koha::Holds",
1087 biblionumber => $biblio->biblionumber,
1088 branchcode => $library_3->branchcode,
1089 itemnumber => undef,
1095 my $biblio_hold_data = $biblio_hold->to_api;
1096 $biblio_hold->delete;
1097 $biblio_hold_data->{pickup_library_id} = $library_1->branchcode;
1098 delete $biblio_hold_data->{hold_id};
1100 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1102 ->json_is({ error => 'The supplied pickup location is not valid' });
1104 $biblio_hold_data->{pickup_library_id} = $library_2->branchcode;
1105 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $biblio_hold_data )
1108 # Test item-level holds
1109 my $item_hold = $builder->build_object(
1111 class => "Koha::Holds",
1113 biblionumber => $biblio->biblionumber,
1114 branchcode => $library_3->branchcode,
1115 itemnumber => $item->itemnumber,
1121 my $item_hold_data = $item_hold->to_api;
1123 $item_hold_data->{pickup_library_id} = $library_1->branchcode;
1124 delete $item_hold->{hold_id};
1126 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1128 ->json_is({ error => 'The supplied pickup location is not valid' });
1130 $item_hold_data->{pickup_library_id} = $library_2->branchcode;
1131 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $item_hold_data )
1134 $schema->storage->txn_rollback;
1137 subtest 'PUT /holds/{hold_id}/pickup_location tests' => sub {
1141 $schema->storage->txn_begin;
1143 my $password = 'AbcdEFG123';
1145 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
1146 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
1147 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
1149 my $patron = $builder->build_object(
1150 { class => 'Koha::Patrons', value => { flags => 0 } } );
1151 $patron->set_password( { password => $password, skip_validation => 1 } );
1152 my $userid = $patron->userid;
1155 source => 'UserPermission',
1157 borrowernumber => $patron->borrowernumber,
1159 code => 'place_holds',
1165 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
1166 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
1168 my $libraries_query = { branchcode => [ $library_1->branchcode, $library_2->branchcode ] };
1170 my $mocked_biblio = Test::MockModule->new('Koha::Biblio');
1171 $mocked_biblio->mock( 'pickup_locations', sub {
1172 return Koha::Libraries->search($libraries_query);
1175 my $mocked_item = Test::MockModule->new('Koha::Item');
1176 $mocked_item->mock( 'pickup_locations', sub {
1177 return Koha::Libraries->search($libraries_query);
1180 my $biblio = $builder->build_sample_biblio;
1181 my $item = $builder->build_sample_item(
1183 biblionumber => $biblio->biblionumber,
1184 library => $library_1->branchcode
1189 my $hold = Koha::Holds->find(
1192 branchcode => $library_1->branchcode,
1193 borrowernumber => $patron->borrowernumber,
1194 biblionumber => $biblio->biblionumber,
1196 itemnumber => undef,
1201 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1203 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1205 ->json_is({ pickup_library_id => $library_2->branchcode });
1207 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1209 $libraries_query = { branchcode => $library_1->branchcode };
1211 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1213 . "/pickup_location" => json => { pickup_library_id => $library_3->branchcode } )
1215 ->json_is({ error => '[The supplied pickup location is not valid]' });
1217 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library unchanged' );
1220 $hold = Koha::Holds->find(
1223 branchcode => $library_1->branchcode,
1224 borrowernumber => $patron->borrowernumber,
1225 biblionumber => $biblio->biblionumber,
1227 itemnumber => $item->itemnumber,
1232 $libraries_query = { branchcode => $library_1->branchcode };
1234 # Attempt to use an invalid pickup locations ends in 400
1235 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1237 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1239 ->json_is({ error => '[The supplied pickup location is not valid]' });
1241 is( $hold->discard_changes->branchcode->branchcode, $library_1->branchcode, 'pickup library unchanged' );
1243 $libraries_query = {
1245 '-in' => [ $library_1->branchcode, $library_2->branchcode ]
1249 $t->put_ok( "//$userid:$password@/api/v1/holds/"
1251 . "/pickup_location" => json => { pickup_library_id => $library_2->branchcode } )
1253 ->json_is({ pickup_library_id => $library_2->branchcode });
1255 is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
1257 $schema->storage->txn_rollback;