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 => 10;
23 use t::lib::TestBuilder;
36 use Koha::Biblioitems;
38 use Koha::CirculationRules;
40 my $schema = Koha::Database->new->schema;
41 my $builder = t::lib::TestBuilder->new();
43 $schema->storage->txn_begin;
45 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
47 my $t = Test::Mojo->new('Koha::REST::V1');
49 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
50 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
51 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
52 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
54 # Generic password for everyone
55 my $password = 'thePassword123';
57 # User without any permissions
58 my $nopermission = $builder->build_object({
59 class => 'Koha::Patrons',
61 branchcode => $branchcode,
62 categorycode => $categorycode,
66 $nopermission->set_password( { password => $password, skip_validation => 1 } );
67 my $nopermission_userid = $nopermission->userid;
69 my $patron_1 = $builder->build_object(
71 class => 'Koha::Patrons',
73 categorycode => $categorycode,
74 branchcode => $branchcode,
75 surname => 'Test Surname',
76 flags => 80, #borrowers and reserveforothers flags
80 $patron_1->set_password( { password => $password, skip_validation => 1 } );
81 my $userid_1 = $patron_1->userid;
83 my $patron_2 = $builder->build_object(
85 class => 'Koha::Patrons',
87 categorycode => $categorycode,
88 branchcode => $branchcode,
89 surname => 'Test Surname 2',
90 flags => 16, # borrowers flag
94 $patron_2->set_password( { password => $password, skip_validation => 1 } );
95 my $userid_2 = $patron_2->userid;
97 my $patron_3 = $builder->build_object(
99 class => 'Koha::Patrons',
101 categorycode => $categorycode,
102 branchcode => $branchcode,
103 surname => 'Test Surname 3',
104 flags => 64, # reserveforothers flag
108 $patron_3->set_password( { password => $password, skip_validation => 1 } );
109 my $userid_3 = $patron_3->userid;
111 my $biblio_1 = $builder->build_sample_biblio;
112 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber, itype => $itemtype });
114 my $biblio_2 = $builder->build_sample_biblio;
115 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber, itype => $itemtype });
117 my $dbh = C4::Context->dbh;
118 $dbh->do('DELETE FROM reserves');
119 Koha::CirculationRules->search()->delete();
120 Koha::CirculationRules->set_rules(
122 categorycode => undef,
126 reservesallowed => 1,
127 holds_per_record => 99
132 my $reserve_id = C4::Reserves::AddReserve(
134 branchcode => $branchcode,
135 borrowernumber => $patron_1->borrowernumber,
136 biblionumber => $biblio_1->biblionumber,
138 itemnumber => $item_1->itemnumber,
142 # Add another reserve to be able to change first reserve's rank
143 my $reserve_id2 = C4::Reserves::AddReserve(
145 branchcode => $branchcode,
146 borrowernumber => $patron_2->borrowernumber,
147 biblionumber => $biblio_1->biblionumber,
149 itemnumber => $item_1->itemnumber,
153 my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
154 my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
157 patron_id => int($patron_1->borrowernumber),
158 biblio_id => int($biblio_1->biblionumber),
159 item_id => int($item_1->itemnumber),
160 pickup_library_id => $branchcode,
161 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->put_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->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $put_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 # While suspended_until is date-time, it's always set to midnight.
217 my $expected_suspended_until = $suspended_until->strftime('%FT00:00:00%z');
218 substr($expected_suspended_until, -2, 0, ':');
220 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => $put_data )
222 ->json_is( '/hold_id', $reserve_id )
223 ->json_is( '/suspended_until', $expected_suspended_until )
224 ->json_is( '/priority', 2 )
225 ->json_is( '/pickup_library_id', $branchcode );
227 # Change only pickup library, everything else should remain
228 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => { pickup_library_id => $branchcode2 } )
230 ->json_is( '/hold_id', $reserve_id )
231 ->json_is( '/suspended_until', $expected_suspended_until )
232 ->json_is( '/priority', 2 )
233 ->json_is( '/pickup_library_id', $branchcode2 );
235 # Reset suspended_until, everything else should remain
236 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => { suspended_until => undef } )
238 ->json_is( '/hold_id', $reserve_id )
239 ->json_is( '/suspended_until', undef )
240 ->json_is( '/priority', 2 )
241 ->json_is( '/pickup_library_id', $branchcode2 );
243 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
244 ->status_is(204, 'SWAGGER3.2.4')
245 ->content_is('', 'SWAGGER3.3.4');
247 $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
249 ->json_has('/error');
251 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
253 ->json_has('/error');
255 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
259 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
260 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
264 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
265 ->status_is(204, 'SWAGGER3.2.4')
266 ->content_is('', 'SWAGGER3.3.4');
268 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
270 ->json_has('/hold_id');
271 # Get id from response
272 $reserve_id = $t->tx->res->json->{hold_id};
274 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
276 ->json_is('/0/hold_id', $reserve_id)
277 ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
278 ->json_is('/0/pickup_library_id', $branchcode);
280 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
282 ->json_like('/error', qr/itemAlreadyOnHold/);
284 $post_data->{biblionumber} = int($biblio_2->biblionumber);
285 $post_data->{itemnumber} = int($item_2->itemnumber);
287 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
289 ->json_like('/error', qr/itemAlreadyOnHold/);
291 my $to_delete_patron = $builder->build_object({ class => 'Koha::Patrons' });
292 my $deleted_patron_id = $to_delete_patron->borrowernumber;
293 $to_delete_patron->delete;
295 my $tmp_patron_id = $post_data->{patron_id};
296 $post_data->{patron_id} = $deleted_patron_id;
297 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
299 ->json_is( { error => 'patron_id not found' } );
301 # Restore the original patron_id as it is expected by the next subtest
302 # FIXME: this tests need to be rewritten from scratch
303 $post_data->{patron_id} = $tmp_patron_id;
306 subtest 'Reserves with itemtype' => sub {
310 patron_id => int($patron_1->borrowernumber),
311 biblio_id => int($biblio_1->biblionumber),
312 pickup_library_id => $branchcode,
313 item_type => $itemtype,
316 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
317 ->status_is(204, 'SWAGGER3.2.4')
318 ->content_is('', 'SWAGGER3.3.4');
320 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
322 ->json_has('/hold_id');
324 $reserve_id = $t->tx->res->json->{hold_id};
326 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
328 ->json_is('/0/hold_id', $reserve_id)
329 ->json_is('/0/item_type', $itemtype);
333 subtest 'test AllowHoldDateInFuture' => sub {
337 $dbh->do('DELETE FROM reserves');
339 my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
342 patron_id => int($patron_1->borrowernumber),
343 biblio_id => int($biblio_1->biblionumber),
344 item_id => int($item_1->itemnumber),
345 pickup_library_id => $branchcode,
346 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
347 hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
351 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
353 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
355 ->json_has('/error');
357 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
359 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
361 ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
364 subtest 'test AllowHoldPolicyOverride' => sub {
368 $dbh->do('DELETE FROM reserves');
370 Koha::CirculationRules->set_rules(
380 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
382 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
384 ->json_has('/error');
386 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
388 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
392 $schema->storage->txn_rollback;
394 subtest 'suspend and resume tests' => sub {
398 $schema->storage->txn_begin;
400 my $password = 'AbcdEFG123';
402 my $patron = $builder->build_object(
403 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
404 $patron->set_password({ password => $password, skip_validation => 1 });
405 my $userid = $patron->userid;
408 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
409 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
411 my $hold = $builder->build_object(
412 { class => 'Koha::Holds',
413 value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
417 ok( !$hold->is_suspended, 'Hold is not suspended' );
418 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
419 ->status_is( 201, 'Hold suspension created' );
421 $hold->discard_changes; # refresh object
423 ok( $hold->is_suspended, 'Hold is suspended' );
424 $t->json_is('/end_date', undef, 'Hold suspension has no end date');
426 my $end_date = output_pref({
427 dt => dt_from_string( undef ),
428 dateformat => 'rfc3339',
432 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" => json => { end_date => $end_date } );
434 $hold->discard_changes; # refresh object
436 ok( $hold->is_suspended, 'Hold is suspended' );
440 dt => dt_from_string( $hold->suspend_until ),
441 dateformat => 'rfc3339',
444 'Hold suspension has correct end date'
447 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
448 ->status_is(204, 'SWAGGER3.2.4')
449 ->content_is('', 'SWAGGER3.3.4');
451 # Pass a an expiration date for the suspension
452 my $date = dt_from_string()->add( days => 5 );
454 "//$userid:$password@/api/v1/holds/"
456 . "/suspension" => json => {
458 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
460 )->status_is( 201, 'Hold suspension created' )
461 ->json_is( '/end_date',
462 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
463 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
465 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
466 ->status_is(204, 'SWAGGER3.2.4')
467 ->content_is('', 'SWAGGER3.3.4');
469 $hold->set_waiting->discard_changes;
471 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
472 ->status_is( 400, 'Cannot suspend waiting hold' )
473 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
475 $hold->set_transfer->discard_changes;
477 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
478 ->status_is( 400, 'Cannot suspend hold on transfer' )
479 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
481 $schema->storage->txn_rollback;
484 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
488 $schema->storage->txn_begin;
490 my $password = 'AbcdEFG123';
492 my $library = $builder->build_object({ class => 'Koha::Libraries' });
493 my $patron_np = $builder->build_object(
494 { class => 'Koha::Patrons', value => { flags => 0 } } );
495 $patron_np->set_password( { password => $password, skip_validation => 1 } );
496 my $userid_np = $patron_np->userid;
498 my $patron = $builder->build_object(
499 { class => 'Koha::Patrons', value => { flags => 0 } } );
500 $patron->set_password( { password => $password, skip_validation => 1 } );
501 my $userid = $patron->userid;
504 source => 'UserPermission',
506 borrowernumber => $patron->borrowernumber,
508 code => 'modify_holds_priority',
514 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
515 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
517 my $biblio = $builder->build_sample_biblio;
518 my $patron_1 = $builder->build_object(
520 class => 'Koha::Patrons',
521 value => { branchcode => $library->branchcode }
524 my $patron_2 = $builder->build_object(
526 class => 'Koha::Patrons',
527 value => { branchcode => $library->branchcode }
530 my $patron_3 = $builder->build_object(
532 class => 'Koha::Patrons',
533 value => { branchcode => $library->branchcode }
537 my $hold_1 = Koha::Holds->find(
540 branchcode => $library->branchcode,
541 borrowernumber => $patron_1->borrowernumber,
542 biblionumber => $biblio->biblionumber,
547 my $hold_2 = Koha::Holds->find(
550 branchcode => $library->branchcode,
551 borrowernumber => $patron_2->borrowernumber,
552 biblionumber => $biblio->biblionumber,
557 my $hold_3 = Koha::Holds->find(
560 branchcode => $library->branchcode,
561 borrowernumber => $patron_3->borrowernumber,
562 biblionumber => $biblio->biblionumber,
568 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
570 . "/priority" => json => 1 )->status_is(403);
572 $t->put_ok( "//$userid:$password@/api/v1/holds/"
574 . "/priority" => json => 1 )->status_is(200)->json_is(1);
576 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
577 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
578 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
580 $t->put_ok( "//$userid:$password@/api/v1/holds/"
582 . "/priority" => json => 3 )->status_is(200)->json_is(3);
584 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
585 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
586 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
588 $schema->storage->txn_rollback;
591 subtest 'add() tests (maxreserves behaviour)' => sub {
595 $schema->storage->txn_begin;
597 $dbh->do('DELETE FROM reserves');
599 Koha::CirculationRules->new->delete;
601 my $password = 'AbcdEFG123';
603 my $patron = $builder->build_object(
604 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
605 $patron->set_password({ password => $password, skip_validation => 1 });
606 my $userid = $patron->userid;
608 Koha::CirculationRules->set_rules(
612 categorycode => undef,
619 Koha::CirculationRules->set_rules(
622 categorycode => $patron->categorycode,
629 my $biblio_1 = $builder->build_sample_biblio;
630 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
631 my $biblio_2 = $builder->build_sample_biblio;
632 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
633 my $biblio_3 = $builder->build_sample_biblio;
634 my $item_3 = $builder->build_sample_item({ biblionumber => $biblio_3->biblionumber });
637 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
638 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
639 t::lib::Mocks::mock_preference( 'maxreserves', 2 );
640 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
643 patron_id => $patron->borrowernumber,
644 biblio_id => $biblio_1->biblionumber,
645 pickup_library_id => $item_1->home_branch->branchcode,
646 item_type => $item_1->itype,
649 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
653 patron_id => $patron->borrowernumber,
654 biblio_id => $biblio_2->biblionumber,
655 pickup_library_id => $item_2->home_branch->branchcode,
656 item_id => $item_2->itemnumber
659 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
663 patron_id => $patron->borrowernumber,
664 biblio_id => $biblio_3->biblionumber,
665 pickup_library_id => $item_1->home_branch->branchcode,
666 item_id => $item_3->itemnumber
669 $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
671 ->json_is( { error => 'Hold cannot be placed. Reason: tooManyReserves' } );
673 $schema->storage->txn_rollback;
676 subtest 'pickup_locations() tests' => sub {
680 $schema->storage->txn_begin;
682 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A' } });
683 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B' } });
684 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C' } });
686 my $patron = $builder->build_object(
688 class => 'Koha::Patrons',
689 value => { userid => 'tomasito', flags => 1 }
692 $patron->set_password( { password => $password, skip_validation => 1 } );
693 my $userid = $patron->userid;
695 my $item_class = Test::MockModule->new('Koha::Item');
699 my ( $self, $params ) = @_;
700 my $mock_patron = $params->{patron};
701 is( $mock_patron->borrowernumber,
702 $patron->borrowernumber, 'Patron passed correctly' );
703 return Koha::Libraries->search(
707 $library_1->branchcode,
708 $library_2->branchcode
712 { # we make sure no surprises in the order of the result
713 order_by => { '-asc' => 'marcorgcode' }
719 my $biblio_class = Test::MockModule->new('Koha::Biblio');
723 my ( $self, $params ) = @_;
724 my $mock_patron = $params->{patron};
725 is( $mock_patron->borrowernumber,
726 $patron->borrowernumber, 'Patron passed correctly' );
727 return Koha::Libraries->search(
731 $library_2->branchcode,
732 $library_3->branchcode
736 { # we make sure no surprises in the order of the result
737 order_by => { '-asc' => 'marcorgcode' }
743 my $item = $builder->build_sample_item;
746 my $hold_1 = $builder->build_object(
748 class => 'Koha::Holds',
751 biblionumber => $item->biblionumber,
752 borrowernumber => $patron->borrowernumber
757 my $hold_2 = $builder->build_object(
759 class => 'Koha::Holds',
761 itemnumber => $item->itemnumber,
762 biblionumber => $item->biblionumber,
763 borrowernumber => $patron->borrowernumber
768 $t->get_ok( "//$userid:$password@/api/v1/holds/"
770 . "/pickup_locations" )
771 ->json_is( [ $library_2->to_api, $library_3->to_api ] );
773 $t->get_ok( "//$userid:$password@/api/v1/holds/"
775 . "/pickup_locations" )
776 ->json_is( [ $library_1->to_api, $library_2->to_api ] );
778 $schema->storage->txn_rollback;