Bug 18936: Convert issuingrules fields to circulation_rules
[koha.git] / t / db_dependent / api / v1 / holds.t
blob11fa62d34f2f4b3d63eb5b814033e6e03de9a86d
1 #!/usr/bin/env perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use Modern::Perl;
20 use Test::More tests => 8;
21 use Test::Mojo;
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
25 use DateTime;
27 use C4::Context;
28 use Koha::Patrons;
29 use C4::Reserves;
30 use C4::Items;
32 use Koha::Database;
33 use Koha::DateUtils;
34 use Koha::Biblios;
35 use Koha::Biblioitems;
36 use Koha::Items;
37 use Koha::CirculationRules;
39 my $schema = Koha::Database->new->schema;
40 my $builder = t::lib::TestBuilder->new();
42 $schema->storage->txn_begin;
44 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
46 my $t = Test::Mojo->new('Koha::REST::V1');
48 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
49 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
50 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
52 # Generic password for everyone
53 my $password = 'thePassword123';
55 # User without any permissions
56 my $nopermission = $builder->build_object({
57 class => 'Koha::Patrons',
58 value => {
59 branchcode => $branchcode,
60 categorycode => $categorycode,
61 flags => 0
63 });
64 $nopermission->set_password( { password => $password, skip_validation => 1 } );
65 my $nopermission_userid = $nopermission->userid;
67 my $patron_1 = $builder->build_object(
69 class => 'Koha::Patrons',
70 value => {
71 categorycode => $categorycode,
72 branchcode => $branchcode,
73 surname => 'Test Surname',
74 flags => 80, #borrowers and reserveforothers flags
78 $patron_1->set_password( { password => $password, skip_validation => 1 } );
79 my $userid_1 = $patron_1->userid;
81 my $patron_2 = $builder->build_object(
83 class => 'Koha::Patrons',
84 value => {
85 categorycode => $categorycode,
86 branchcode => $branchcode,
87 surname => 'Test Surname 2',
88 flags => 16, # borrowers flag
92 $patron_2->set_password( { password => $password, skip_validation => 1 } );
93 my $userid_2 = $patron_2->userid;
95 my $patron_3 = $builder->build_object(
97 class => 'Koha::Patrons',
98 value => {
99 categorycode => $categorycode,
100 branchcode => $branchcode,
101 surname => 'Test Surname 3',
102 flags => 64, # reserveforothers flag
106 $patron_3->set_password( { password => $password, skip_validation => 1 } );
107 my $userid_3 = $patron_3->userid;
109 my $biblio_1 = $builder->build_sample_biblio;
110 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber, itype => $itemtype });
112 my $biblio_2 = $builder->build_sample_biblio;
113 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber, itype => $itemtype });
115 my $dbh = C4::Context->dbh;
116 $dbh->do('DELETE FROM reserves');
117 $dbh->do('DELETE FROM circulation_rules');
118 Koha::CirculationRules->set_rules(
120 categorycode => '*',
121 branchcode => '*',
122 itemtype => '*',
123 rules => {
124 reservesallowed => 1
129 my $reserve_id = C4::Reserves::AddReserve($branchcode, $patron_1->borrowernumber,
130 $biblio_1->biblionumber, undef, 1, undef, undef, undef, '', $item_1->itemnumber);
132 # Add another reserve to be able to change first reserve's rank
133 my $reserve_id2 = C4::Reserves::AddReserve($branchcode, $patron_2->borrowernumber,
134 $biblio_1->biblionumber, undef, 2, undef, undef, undef, '', $item_1->itemnumber);
136 my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
137 my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
139 my $post_data = {
140 patron_id => int($patron_1->borrowernumber),
141 biblio_id => int($biblio_1->biblionumber),
142 item_id => int($item_1->itemnumber),
143 pickup_library_id => $branchcode,
144 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
145 priority => 2,
147 my $put_data = {
148 priority => 2,
149 suspended_until => output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }),
152 subtest "Test endpoints without authentication" => sub {
153 plan tests => 8;
154 $t->get_ok('/api/v1/holds')
155 ->status_is(401);
156 $t->post_ok('/api/v1/holds')
157 ->status_is(401);
158 $t->put_ok('/api/v1/holds/0')
159 ->status_is(401);
160 $t->delete_ok('/api/v1/holds/0')
161 ->status_is(401);
164 subtest "Test endpoints without permission" => sub {
166 plan tests => 10;
168 $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
169 ->status_is(403);
171 $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
172 ->status_is(403);
174 $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json => $post_data )
175 ->status_is(403);
177 $t->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $put_data )
178 ->status_is(403);
180 $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
181 ->status_is(403);
184 subtest "Test endpoints with permission" => sub {
186 plan tests => 44;
188 $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
189 ->status_is(200)
190 ->json_has('/0')
191 ->json_has('/1')
192 ->json_hasnt('/2');
194 $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
195 ->status_is(200)
196 ->json_is('/0/patron_id', $patron_2->borrowernumber)
197 ->json_hasnt('/1');
199 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => $put_data )
200 ->status_is(200)
201 ->json_is( '/hold_id', $reserve_id )
202 ->json_is( '/suspended_until', output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }) )
203 ->json_is( '/priority', 2 );
205 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
206 ->status_is(200);
208 $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
209 ->status_is(404)
210 ->json_has('/error');
212 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
213 ->status_is(404)
214 ->json_has('/error');
216 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
217 ->status_is(200)
218 ->json_is([]);
220 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
221 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
222 ->status_is(200)
223 ->json_is([]);
225 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
226 ->status_is(200);
228 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
229 ->status_is(201)
230 ->json_has('/hold_id');
231 # Get id from response
232 $reserve_id = $t->tx->res->json->{hold_id};
234 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
235 ->status_is(200)
236 ->json_is('/0/hold_id', $reserve_id)
237 ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
238 ->json_is('/0/pickup_library_id', $branchcode);
240 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
241 ->status_is(403)
242 ->json_like('/error', qr/itemAlreadyOnHold/);
244 $post_data->{biblionumber} = int($biblio_2->biblionumber);
245 $post_data->{itemnumber} = int($item_2->itemnumber);
247 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
248 ->status_is(403)
249 ->json_like('/error', qr/itemAlreadyOnHold/);
252 subtest 'Reserves with itemtype' => sub {
253 plan tests => 9;
255 my $post_data = {
256 patron_id => int($patron_1->borrowernumber),
257 biblio_id => int($biblio_1->biblionumber),
258 pickup_library_id => $branchcode,
259 item_type => $itemtype,
262 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
263 ->status_is(200);
265 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
266 ->status_is(201)
267 ->json_has('/hold_id');
269 $reserve_id = $t->tx->res->json->{hold_id};
271 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
272 ->status_is(200)
273 ->json_is('/0/hold_id', $reserve_id)
274 ->json_is('/0/item_type', $itemtype);
278 subtest 'test AllowHoldDateInFuture' => sub {
280 plan tests => 6;
282 $dbh->do('DELETE FROM reserves');
284 my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
286 my $post_data = {
287 patron_id => int($patron_1->borrowernumber),
288 biblio_id => int($biblio_1->biblionumber),
289 item_id => int($item_1->itemnumber),
290 pickup_library_id => $branchcode,
291 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
292 hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
293 priority => 2,
296 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
298 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
299 ->status_is(400)
300 ->json_has('/error');
302 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
304 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
305 ->status_is(201)
306 ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
309 subtest 'test AllowHoldPolicyOverride' => sub {
311 plan tests => 5;
313 $dbh->do('DELETE FROM reserves');
315 Koha::CirculationRules->set_rules(
317 categorycode => undef,
318 itemtype => undef,
319 branchcode => undef,
320 rules => {
321 holdallowed => 1
326 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
328 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
329 ->status_is(403)
330 ->json_has('/error');
332 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
334 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
335 ->status_is(201);
338 $schema->storage->txn_rollback;
340 subtest 'suspend and resume tests' => sub {
342 plan tests => 21;
344 $schema->storage->txn_begin;
346 my $password = 'AbcdEFG123';
348 my $patron = $builder->build_object(
349 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
350 $patron->set_password({ password => $password, skip_validation => 1 });
351 my $userid = $patron->userid;
353 # Disable logging
354 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
355 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
357 my $hold = $builder->build_object(
358 { class => 'Koha::Holds',
359 value => { suspend => 0, suspend_until => undef, waitingdate => undef }
363 ok( !$hold->is_suspended, 'Hold is not suspended' );
364 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
365 ->status_is( 201, 'Hold suspension created' );
367 $hold->discard_changes; # refresh object
369 ok( $hold->is_suspended, 'Hold is suspended' );
370 $t->json_is(
371 '/end_date',
372 output_pref(
373 { dt => dt_from_string( $hold->suspend_until ),
374 dateformat => 'rfc3339',
375 dateonly => 1
380 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
381 ->status_is( 204, "Correct status when deleting a resource" )
382 ->json_is( undef );
384 # Pass a an expiration date for the suspension
385 my $date = dt_from_string()->add( days => 5 );
386 $t->post_ok(
387 "//$userid:$password@/api/v1/holds/"
388 . $hold->id
389 . "/suspension" => json => {
390 end_date =>
391 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
393 )->status_is( 201, 'Hold suspension created' )
394 ->json_is( '/end_date',
395 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
396 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
398 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
399 ->status_is( 204, "Correct status when deleting a resource" )
400 ->json_is( undef );
402 $hold->set_waiting->discard_changes;
404 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
405 ->status_is( 400, 'Cannot suspend waiting hold' )
406 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
408 $hold->set_waiting(1)->discard_changes;
410 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
411 ->status_is( 400, 'Cannot suspend waiting hold' )
412 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
414 $schema->storage->txn_rollback;
417 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
419 plan tests => 14;
421 $schema->storage->txn_begin;
423 my $password = 'AbcdEFG123';
425 my $library = $builder->build_object({ class => 'Koha::Libraries' });
426 my $patron_np = $builder->build_object(
427 { class => 'Koha::Patrons', value => { flags => 0 } } );
428 $patron_np->set_password( { password => $password, skip_validation => 1 } );
429 my $userid_np = $patron_np->userid;
431 my $patron = $builder->build_object(
432 { class => 'Koha::Patrons', value => { flags => 0 } } );
433 $patron->set_password( { password => $password, skip_validation => 1 } );
434 my $userid = $patron->userid;
435 $builder->build(
437 source => 'UserPermission',
438 value => {
439 borrowernumber => $patron->borrowernumber,
440 module_bit => 6,
441 code => 'modify_holds_priority',
446 # Disable logging
447 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
448 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
450 my $biblio = $builder->build_sample_biblio;
451 my $patron_1 = $builder->build_object(
453 class => 'Koha::Patrons',
454 value => { branchcode => $library->branchcode }
457 my $patron_2 = $builder->build_object(
459 class => 'Koha::Patrons',
460 value => { branchcode => $library->branchcode }
463 my $patron_3 = $builder->build_object(
465 class => 'Koha::Patrons',
466 value => { branchcode => $library->branchcode }
470 my $hold_1 = Koha::Holds->find(
471 AddReserve(
472 $library->branchcode, $patron_1->borrowernumber,
473 $biblio->biblionumber, undef,
477 my $hold_2 = Koha::Holds->find(
478 AddReserve(
479 $library->branchcode, $patron_2->borrowernumber,
480 $biblio->biblionumber, undef,
484 my $hold_3 = Koha::Holds->find(
485 AddReserve(
486 $library->branchcode, $patron_3->borrowernumber,
487 $biblio->biblionumber, undef,
492 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
493 . $hold_3->id
494 . "/priority" => json => 1 )->status_is(403);
496 $t->put_ok( "//$userid:$password@/api/v1/holds/"
497 . $hold_3->id
498 . "/priority" => json => 1 )->status_is(200)->json_is(1);
500 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
501 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
502 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
504 $t->put_ok( "//$userid:$password@/api/v1/holds/"
505 . $hold_3->id
506 . "/priority" => json => 3 )->status_is(200)->json_is(3);
508 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
509 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
510 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
512 $schema->storage->txn_rollback;