Bug 14711: Change prototype for AddReserve - pass a hashref
[koha.git] / t / db_dependent / Holds.t
bloba84eba8a0e8b5f4454d9b74712c5acb5a4a9b513
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
8 use C4::Context;
10 use Test::More tests => 61;
11 use MARC::Record;
13 use C4::Biblio;
14 use C4::Calendar;
15 use C4::Items;
16 use C4::Reserves;
18 use Koha::Biblios;
19 use Koha::CirculationRules;
20 use Koha::Database;
21 use Koha::DateUtils qw( dt_from_string output_pref );
22 use Koha::Holds;
23 use Koha::Item::Transfer::Limits;
24 use Koha::Items;
25 use Koha::Libraries;
26 use Koha::Library::Groups;
27 use Koha::Patrons;
29 BEGIN {
30 use FindBin;
31 use lib $FindBin::Bin;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new();
38 my $dbh = C4::Context->dbh;
40 # Create two random branches
41 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
42 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
44 my $category = $builder->build({ source => 'Category' });
46 my $borrowers_count = 5;
48 $dbh->do('DELETE FROM itemtypes');
49 $dbh->do('DELETE FROM reserves');
50 $dbh->do('DELETE FROM circulation_rules');
51 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
52 $insert_sth->execute('CAN');
53 $insert_sth->execute('CANNOT');
54 $insert_sth->execute('DUMMY');
55 $insert_sth->execute('ONLY1');
57 # Setup Test------------------------
58 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
60 # Create item instance for testing.
61 my ($item_bibnum, $item_bibitemnum, $itemnumber)
62 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
64 # Create some borrowers
65 my @borrowernumbers;
66 foreach (1..$borrowers_count) {
67 my $borrowernumber = Koha::Patron->new({
68 firstname => 'my firstname',
69 surname => 'my surname ' . $_,
70 categorycode => $category->{categorycode},
71 branchcode => $branch_1,
72 })->store->borrowernumber;
73 push @borrowernumbers, $borrowernumber;
76 # Create five item level holds
77 foreach my $borrowernumber ( @borrowernumbers ) {
78 AddReserve(
80 branchcode => $branch_1,
81 borrowernumber => $borrowernumber,
82 biblionumber => $biblio->biblionumber,
83 priority => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
84 itemnumber => $itemnumber,
89 my $holds = $biblio->holds;
90 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
91 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
92 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
93 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
94 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
95 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
97 my $item = Koha::Items->find( $itemnumber );
98 $holds = $item->current_holds;
99 my $first_hold = $holds->next;
100 my $reservedate = $first_hold->reservedate;
101 my $borrowernumber = $first_hold->borrowernumber;
102 my $branch_1code = $first_hold->branchcode;
103 my $reserve_id = $first_hold->reserve_id;
104 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
105 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
106 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
107 ok($reserve_id, "Test holds_placed_today()");
109 my $hold = Koha::Holds->find( $reserve_id );
110 ok( $hold, "Koha::Holds found the hold" );
111 my $hold_biblio = $hold->biblio();
112 ok( $hold_biblio, "Got biblio using biblio() method" );
113 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
114 my $hold_item = $hold->item();
115 ok( $hold_item, "Got item using item() method" );
116 ok( $hold_item == $hold->item(), "item method returns stashed item" );
117 my $hold_branch = $hold->branch();
118 ok( $hold_branch, "Got branch using branch() method" );
119 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
120 my $hold_found = $hold->found();
121 $hold->set({ found => 'W'})->store();
122 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
123 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
125 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
126 $holds = $patron->holds;
127 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
130 $holds = $item->current_holds;
131 $first_hold = $holds->next;
132 $borrowernumber = $first_hold->borrowernumber;
133 $branch_1code = $first_hold->branchcode;
134 $reserve_id = $first_hold->reserve_id;
136 ModReserve({
137 reserve_id => $reserve_id,
138 rank => '4',
139 branchcode => $branch_1,
140 itemnumber => $itemnumber,
141 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
144 $hold = Koha::Holds->find( $reserve_id );
145 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
146 ok( $hold->suspend, "Test ModReserve, suspend hold" );
147 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
149 ModReserve({ # call without reserve_id
150 rank => '3',
151 biblionumber => $item_bibnum,
152 itemnumber => $itemnumber,
153 borrowernumber => $borrowernumber,
155 $hold = Koha::Holds->find( $reserve_id );
156 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
158 ToggleSuspend( $reserve_id );
159 $hold = Koha::Holds->find( $reserve_id );
160 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
162 ToggleSuspend( $reserve_id, '2012-01-01' );
163 $hold = Koha::Holds->find( $reserve_id );
164 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
166 AutoUnsuspendReserves();
167 $hold = Koha::Holds->find( $reserve_id );
168 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
170 SuspendAll(
171 borrowernumber => $borrowernumber,
172 biblionumber => $biblio->biblionumber,
173 suspend => 1,
174 suspend_until => '2012-01-01',
176 $hold = Koha::Holds->find( $reserve_id );
177 is( $hold->suspend, 1, "Test SuspendAll()" );
178 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
180 SuspendAll(
181 borrowernumber => $borrowernumber,
182 biblionumber => $biblio->biblionumber,
183 suspend => 0,
185 $hold = Koha::Holds->find( $reserve_id );
186 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
187 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
189 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
190 AddReserve(
192 branchcode => $branch_1,
193 borrowernumber => $borrowernumbers[0],
194 biblionumber => $biblio->biblionumber,
198 $patron = Koha::Patrons->find( $borrowernumber );
199 $holds = $patron->holds;
200 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
201 ModReserveMinusPriority( $itemnumber, $reserveid );
202 $holds = $patron->holds;
203 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
205 $holds = $biblio->holds;
206 $hold = $holds->next;
207 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
208 $hold = Koha::Holds->find( $reserveid );
209 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
211 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
212 $hold = Koha::Holds->find( $reserveid );
213 is( $hold->priority, '2', "Test AlterPriority(), move down" );
215 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
216 $hold = Koha::Holds->find( $reserveid );
217 is( $hold->priority, '1', "Test AlterPriority(), move up" );
219 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
223 # Regression test for bug 2394
225 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
226 # a patron is not permittedo to request an item whose homebranch (i.e.,
227 # owner of the item) is different from the patron's own library.
228 # However, if canreservefromotherbranches is turned ON, the patron can
229 # create such hold requests.
231 # Note that canreservefromotherbranches has no effect if
232 # IndependentBranches is OFF.
234 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
235 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
236 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber);
237 Koha::CirculationRules->set_rules(
239 categorycode => undef,
240 branchcode => undef,
241 itemtype => undef,
242 rules => {
243 reservesallowed => 25,
244 holds_per_record => 99,
248 Koha::CirculationRules->set_rules(
250 categorycode => undef,
251 branchcode => undef,
252 itemtype => 'CANNOT',
253 rules => {
254 reservesallowed => 0,
255 holds_per_record => 99,
260 # make sure some basic sysprefs are set
261 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
262 t::lib::Mocks::mock_preference('item-level_itypes', 1);
264 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
265 t::lib::Mocks::mock_preference('IndependentBranches', 0);
268 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
269 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
272 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
273 t::lib::Mocks::mock_preference('IndependentBranches', 1);
274 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
276 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
277 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
280 # ... unless canreservefromotherbranches is ON
281 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
283 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
284 '... unless canreservefromotherbranches is ON (bug 2394)'
288 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
289 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
290 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
291 my $reserveid1 = AddReserve(
293 branchcode => $branch_1,
294 borrowernumber => $borrowernumbers[0],
295 biblionumber => $biblio->biblionumber,
296 priority => 1
300 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
301 my $reserveid2 = AddReserve(
303 branchcode => $branch_1,
304 borrowernumber => $borrowernumbers[1],
305 biblionumber => $biblio->biblionumber,
306 priority => 2
310 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
311 my $reserveid3 = AddReserve(
313 branchcode => $branch_1,
314 borrowernumber => $borrowernumbers[2],
315 biblionumber => $biblio->biblionumber,
316 priority => 3
320 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
321 my $hold3 = Koha::Holds->find( $reserveid3 );
322 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
323 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
324 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
325 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
328 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
329 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
330 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
331 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
333 $hold = Koha::Hold->new(
335 borrowernumber => $borrowernumbers[0],
336 itemnumber => $itemnumber,
337 biblionumber => $item_bibnum,
339 )->store();
340 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
341 'itemAlreadyOnHold',
342 "Patron cannot place a second item level hold for a given item" );
343 $hold->delete();
345 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
346 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
347 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
349 # Regression test for bug 9532
350 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
351 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
352 AddReserve(
354 branchcode => $branch_1,
355 borrowernumber => $borrowernumbers[0],
356 biblionumber => $biblio->biblionumber,
357 priority => 1,
361 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
362 "cannot request item if policy that matches on item-level item type forbids it"
364 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
366 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
367 "can request item if policy that matches on item type allows it"
370 t::lib::Mocks::mock_preference('item-level_itypes', 0);
371 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
373 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
374 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
378 # Test branch item rules
380 $dbh->do('DELETE FROM circulation_rules');
381 Koha::CirculationRules->set_rules(
383 categorycode => undef,
384 branchcode => undef,
385 itemtype => undef,
386 rules => {
387 reservesallowed => 25,
388 holds_per_record => 99,
392 Koha::CirculationRules->set_rules(
394 branchcode => $branch_1,
395 itemtype => 'CANNOT',
396 rules => {
397 holdallowed => 0,
398 returnbranch => 'homebranch',
402 Koha::CirculationRules->set_rules(
404 branchcode => $branch_1,
405 itemtype => 'CAN',
406 rules => {
407 holdallowed => 1,
408 returnbranch => 'homebranch',
412 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
413 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
414 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
415 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
416 "CanItemBeReserved should return 'notReservable'");
418 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
419 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
420 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
421 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
422 'cannotReserveFromOtherBranches',
423 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
424 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
425 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
426 'OK',
427 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
429 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
430 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
431 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
432 "CanItemBeReserved should return 'OK'");
434 # Bug 12632
435 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
436 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
438 $dbh->do('DELETE FROM reserves');
439 $dbh->do('DELETE FROM issues');
440 $dbh->do('DELETE FROM items');
441 $dbh->do('DELETE FROM biblio');
443 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
444 ( $item_bibnum, $item_bibitemnum, $itemnumber )
445 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
447 Koha::CirculationRules->set_rules(
449 categorycode => undef,
450 branchcode => undef,
451 itemtype => 'ONLY1',
452 rules => {
453 reservesallowed => 1,
454 holds_per_record => 99,
458 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
459 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
461 my $res_id = AddReserve(
463 branchcode => $branch_1,
464 borrowernumber => $borrowernumbers[0],
465 biblionumber => $biblio->biblionumber,
466 priority => 1,
470 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
471 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
473 #results should be the same for both ReservesControlBranch settings
474 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
475 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
476 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
477 #reset for further tests
478 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
480 subtest 'Test max_holds per library/patron category' => sub {
481 plan tests => 6;
483 $dbh->do('DELETE FROM reserves');
485 $biblio = $builder->build_sample_biblio;
486 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
487 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
488 $biblio->biblionumber );
489 Koha::CirculationRules->set_rules(
491 categorycode => undef,
492 branchcode => undef,
493 itemtype => $biblio->itemtype,
494 rules => {
495 reservesallowed => 99,
496 holds_per_record => 99,
501 for ( 1 .. 3 ) {
502 AddReserve(
504 branchcode => $branch_1,
505 borrowernumber => $borrowernumbers[0],
506 biblionumber => $biblio->biblionumber,
507 priority => 1,
512 my $count =
513 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
514 is( $count, 3, 'Patron now has 3 holds' );
516 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
517 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
519 my $rule_all = Koha::CirculationRules->set_rule(
521 categorycode => $category->{categorycode},
522 branchcode => undef,
523 rule_name => 'max_holds',
524 rule_value => 3,
528 my $rule_branch = Koha::CirculationRules->set_rule(
530 branchcode => $branch_1,
531 categorycode => $category->{categorycode},
532 rule_name => 'max_holds',
533 rule_value => 5,
537 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
538 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
540 $rule_branch->delete();
542 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
543 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
545 $rule_all->delete();
546 $rule_branch->rule_value(3);
547 $rule_branch->store();
549 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
550 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
552 $rule_branch->rule_value(5);
553 $rule_branch->update();
554 $rule_branch->rule_value(5);
555 $rule_branch->store();
557 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
558 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
561 subtest 'Pickup location availability tests' => sub {
562 plan tests => 4;
564 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
565 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
566 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
567 #Add a default rule to allow some holds
569 Koha::CirculationRules->set_rules(
571 branchcode => undef,
572 categorycode => undef,
573 itemtype => undef,
574 rules => {
575 reservesallowed => 25,
576 holds_per_record => 99,
580 my $item = Koha::Items->find($itemnumber);
581 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
582 my $library = Koha::Libraries->find($branch_to);
583 $library->pickup_location('1')->store;
584 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
586 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
587 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
589 $library->pickup_location('1')->store;
590 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
591 'OK', 'Library is a pickup location');
593 my $limit = Koha::Item::Transfer::Limit->new({
594 fromBranch => $item->holdingbranch,
595 toBranch => $branch_to,
596 itemtype => $item->effective_itemtype,
597 })->store;
598 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
599 'cannotBeTransferred', 'Item cannot be transferred');
600 $limit->delete;
602 $library->pickup_location('0')->store;
603 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
604 'libraryNotPickupLocation', 'Library is not a pickup location');
605 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
606 'libraryNotFound', 'Cannot set unknown library as pickup location');
609 $schema->storage->txn_rollback;
611 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
613 plan tests => 10;
615 $schema->storage->txn_begin;
617 Koha::Holds->search->delete;
618 $dbh->do('DELETE FROM issues');
619 Koha::Items->search->delete;
620 Koha::Biblios->search->delete;
622 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
623 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
624 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
626 # Create 3 biblios with items
627 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
628 my ( undef, undef, $itemnumber_1 ) = AddItem(
629 { homebranch => $library->branchcode,
630 holdingbranch => $library->branchcode
632 $biblio_1->biblionumber
634 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
635 my ( undef, undef, $itemnumber_2 ) = AddItem(
636 { homebranch => $library->branchcode,
637 holdingbranch => $library->branchcode
639 $biblio_2->biblionumber
641 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
642 my ( undef, undef, $itemnumber_3 ) = AddItem(
643 { homebranch => $library->branchcode,
644 holdingbranch => $library->branchcode
646 $biblio_3->biblionumber
649 Koha::CirculationRules->search->delete;
650 Koha::CirculationRules->set_rules(
652 categorycode => '*',
653 branchcode => '*',
654 itemtype => $itemtype->itemtype,
655 rules => {
656 reservesallowed => 1,
657 holds_per_record => 99,
658 holds_per_day => 2
663 is_deeply(
664 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
665 { status => 'OK' },
666 'Patron can reserve item with hold limit of 1, no holds placed'
669 AddReserve(
671 branchcode => $library->branchcode,
672 borrowernumber => $patron->borrowernumber,
673 biblionumber => $biblio_1->biblionumber,
674 priority => 1,
678 is_deeply(
679 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
680 { status => 'tooManyReserves', limit => 1 },
681 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
684 # Raise reservesallowed to avoid tooManyReserves from it
685 Koha::CirculationRules->set_rule(
688 categorycode => '*',
689 branchcode => '*',
690 itemtype => $itemtype->itemtype,
691 rule_name => 'reservesallowed',
692 rule_value => 3,
696 is_deeply(
697 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
698 { status => 'OK' },
699 'Patron can reserve item with 2 reserves daily cap'
702 # Add a second reserve
703 my $res_id = AddReserve(
705 branchcode => $library->branchcode,
706 borrowernumber => $patron->borrowernumber,
707 biblionumber => $biblio_2->biblionumber,
708 priority => 1,
711 is_deeply(
712 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
713 { status => 'tooManyReservesToday', limit => 2 },
714 'Patron cannot a third item with 2 reserves daily cap'
717 # Update last hold so reservedate is in the past, so 2 holds, but different day
718 $hold = Koha::Holds->find($res_id);
719 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
720 $hold->reservedate($yesterday)->store;
722 is_deeply(
723 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
724 { status => 'OK' },
725 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
728 # Set holds_per_day to 0
729 Koha::CirculationRules->set_rule(
732 categorycode => '*',
733 branchcode => '*',
734 itemtype => $itemtype->itemtype,
735 rule_name => 'holds_per_day',
736 rule_value => 0,
741 # Delete existing holds
742 Koha::Holds->search->delete;
743 is_deeply(
744 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
745 { status => 'tooManyReservesToday', limit => 0 },
746 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
749 Koha::CirculationRules->set_rule(
752 categorycode => '*',
753 branchcode => '*',
754 itemtype => $itemtype->itemtype,
755 rule_name => 'holds_per_day',
756 rule_value => undef,
760 Koha::Holds->search->delete;
761 is_deeply(
762 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
763 { status => 'OK' },
764 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
766 AddReserve(
768 branchcode => $library->branchcode,
769 borrowernumber => $patron->borrowernumber,
770 biblionumber => $biblio_1->biblionumber,
771 priority => 1,
774 AddReserve(
776 branchcode => $library->branchcode,
777 borrowernumber => $patron->borrowernumber,
778 biblionumber => $biblio_2->biblionumber,
779 priority => 1,
783 is_deeply(
784 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
785 { status => 'OK' },
786 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
788 AddReserve(
790 branchcode => $library->branchcode,
791 borrowernumber => $patron->borrowernumber,
792 biblionumber => $biblio_3->biblionumber,
793 priority => 1,
796 is_deeply(
797 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
798 { status => 'tooManyReserves', limit => 3 },
799 'Unlimited daily holds, but reached reservesallowed'
801 #results should be the same for both ReservesControlBranch settings
802 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
803 is_deeply(
804 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
805 { status => 'tooManyReserves', limit => 3 },
806 'Unlimited daily holds, but reached reservesallowed'
809 $schema->storage->txn_rollback;
812 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
813 plan tests => 9;
815 $schema->storage->txn_begin;
817 # Cleanup database
818 Koha::Holds->search->delete;
819 $dbh->do('DELETE FROM issues');
820 Koha::CirculationRules->set_rule(
822 branchcode => undef,
823 categorycode => undef,
824 itemtype => undef,
825 rule_name => 'reservesallowed',
826 rule_value => 25,
830 Koha::Items->search->delete;
831 Koha::Biblios->search->delete;
833 # Create item types
834 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
835 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
837 # Create libraries
838 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
839 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
840 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
842 # Create library groups hierarchy
843 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
844 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
845 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
847 # Create 2 patrons
848 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
849 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
851 # Create 3 biblios with items
852 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
853 my ( undef, undef, $itemnumber_1 ) = AddItem(
854 { homebranch => $library1->branchcode,
855 holdingbranch => $library1->branchcode
857 $biblio_1->biblionumber
859 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
860 my ( undef, undef, $itemnumber_2 ) = AddItem(
861 { homebranch => $library2->branchcode,
862 holdingbranch => $library2->branchcode
864 $biblio_2->biblionumber
866 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
867 my ( undef, undef, $itemnumber_3 ) = AddItem(
868 { homebranch => $library1->branchcode,
869 holdingbranch => $library1->branchcode
871 $biblio_3->biblionumber
874 # Test 1: Patron 3 can place hold
875 is_deeply(
876 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
877 { status => 'OK' },
878 'Patron can place hold if no circ_rules where defined'
881 # Insert default circ rule of holds allowed only from local hold group for all libraries
882 Koha::CirculationRules->set_rules(
884 branchcode => undef,
885 itemtype => undef,
886 rules => {
887 holdallowed => 3,
888 hold_fulfillment_policy => 'any',
889 returnbranch => 'any'
894 # Test 2: Patron 1 can place hold
895 is_deeply(
896 CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
897 { status => 'OK' },
898 'Patron can place hold because patron\'s home library is part of hold group'
901 # Test 3: Patron 3 cannot place hold
902 is_deeply(
903 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
904 { status => 'branchNotInHoldGroup' },
905 'Patron cannot place hold because patron\'s home library is not part of hold group'
908 # Insert default circ rule to "any" for library 2
909 Koha::CirculationRules->set_rules(
911 branchcode => $library2->branchcode,
912 itemtype => undef,
913 rules => {
914 holdallowed => 2,
915 hold_fulfillment_policy => 'any',
916 returnbranch => 'any'
921 # Test 4: Patron 3 can place hold
922 is_deeply(
923 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
924 { status => 'OK' },
925 'Patron can place hold if holdallowed is set to "any" for library 2'
928 # Update default circ rule to "hold group" for library 2
929 Koha::CirculationRules->set_rules(
931 branchcode => $library2->branchcode,
932 itemtype => undef,
933 rules => {
934 holdallowed => 3,
935 hold_fulfillment_policy => 'any',
936 returnbranch => 'any'
941 # Test 5: Patron 3 cannot place hold
942 is_deeply(
943 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
944 { status => 'branchNotInHoldGroup' },
945 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
948 # Insert default item rule to "any" for itemtype 2
949 Koha::CirculationRules->set_rules(
951 branchcode => $library2->branchcode,
952 itemtype => $itemtype2->itemtype,
953 rules => {
954 holdallowed => 2,
955 hold_fulfillment_policy => 'any',
956 returnbranch => 'any'
961 # Test 6: Patron 3 can place hold
962 is_deeply(
963 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
964 { status => 'OK' },
965 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
968 # Update default item rule to "hold group" for itemtype 2
969 Koha::CirculationRules->set_rules(
971 branchcode => $library2->branchcode,
972 itemtype => $itemtype2->itemtype,
973 rules => {
974 holdallowed => 3,
975 hold_fulfillment_policy => 'any',
976 returnbranch => 'any'
981 # Test 7: Patron 3 cannot place hold
982 is_deeply(
983 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
984 { status => 'branchNotInHoldGroup' },
985 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
988 # Insert branch item rule to "any" for itemtype 2 and library 2
989 Koha::CirculationRules->set_rules(
991 branchcode => $library2->branchcode,
992 itemtype => $itemtype2->itemtype,
993 rules => {
994 holdallowed => 2,
995 hold_fulfillment_policy => 'any',
996 returnbranch => 'any'
1001 # Test 8: Patron 3 can place hold
1002 is_deeply(
1003 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1004 { status => 'OK' },
1005 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1008 # Update branch item rule to "hold group" for itemtype 2 and library 2
1009 Koha::CirculationRules->set_rules(
1011 branchcode => $library2->branchcode,
1012 itemtype => $itemtype2->itemtype,
1013 rules => {
1014 holdallowed => 3,
1015 hold_fulfillment_policy => 'any',
1016 returnbranch => 'any'
1021 # Test 9: Patron 3 cannot place hold
1022 is_deeply(
1023 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1024 { status => 'branchNotInHoldGroup' },
1025 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1028 $schema->storage->txn_rollback;
1032 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1033 plan tests => 9;
1035 $schema->storage->txn_begin;
1037 # Cleanup database
1038 Koha::Holds->search->delete;
1039 $dbh->do('DELETE FROM issues');
1040 Koha::CirculationRules->set_rule(
1042 branchcode => undef,
1043 categorycode => undef,
1044 itemtype => undef,
1045 rule_name => 'reservesallowed',
1046 rule_value => 25,
1050 Koha::Items->search->delete;
1051 Koha::Biblios->search->delete;
1053 # Create item types
1054 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1055 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1057 # Create libraries
1058 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1059 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1060 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
1062 # Create library groups hierarchy
1063 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1064 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1065 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1067 # Create 2 patrons
1068 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1069 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1071 # Create 3 biblios with items
1072 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1073 my ( undef, undef, $itemnumber_1 ) = AddItem(
1074 { homebranch => $library1->branchcode,
1075 holdingbranch => $library1->branchcode
1077 $biblio_1->biblionumber
1079 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1080 my ( undef, undef, $itemnumber_2 ) = AddItem(
1081 { homebranch => $library2->branchcode,
1082 holdingbranch => $library2->branchcode
1084 $biblio_2->biblionumber
1086 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1087 my ( undef, undef, $itemnumber_3 ) = AddItem(
1088 { homebranch => $library1->branchcode,
1089 holdingbranch => $library1->branchcode
1091 $biblio_3->biblionumber
1094 # Test 1: Patron 3 can place hold
1095 is_deeply(
1096 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1097 { status => 'OK' },
1098 'Patron can place hold if no circ_rules where defined'
1101 # Insert default circ rule of holds allowed only from local hold group for all libraries
1102 Koha::CirculationRules->set_rules(
1104 branchcode => undef,
1105 itemtype => undef,
1106 rules => {
1107 holdallowed => 2,
1108 hold_fulfillment_policy => 'holdgroup',
1109 returnbranch => 'any'
1114 # Test 2: Patron 1 can place hold
1115 is_deeply(
1116 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1117 { status => 'OK' },
1118 'Patron can place hold because pickup location is part of hold group'
1121 # Test 3: Patron 3 cannot place hold
1122 is_deeply(
1123 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1124 { status => 'pickupNotInHoldGroup' },
1125 'Patron cannot place hold because pickup location is not part of hold group'
1128 # Insert default circ rule to "any" for library 2
1129 Koha::CirculationRules->set_rules(
1131 branchcode => $library2->branchcode,
1132 itemtype => undef,
1133 rules => {
1134 holdallowed => 2,
1135 hold_fulfillment_policy => 'any',
1136 returnbranch => 'any'
1141 # Test 4: Patron 3 can place hold
1142 is_deeply(
1143 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1144 { status => 'OK' },
1145 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1148 # Update default circ rule to "hold group" for library 2
1149 Koha::CirculationRules->set_rules(
1151 branchcode => $library2->branchcode,
1152 itemtype => undef,
1153 rules => {
1154 holdallowed => 2,
1155 hold_fulfillment_policy => 'holdgroup',
1156 returnbranch => 'any'
1161 # Test 5: Patron 3 cannot place hold
1162 is_deeply(
1163 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1164 { status => 'pickupNotInHoldGroup' },
1165 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1168 # Insert default item rule to "any" for itemtype 2
1169 Koha::CirculationRules->set_rules(
1171 branchcode => $library2->branchcode,
1172 itemtype => $itemtype2->itemtype,
1173 rules => {
1174 holdallowed => 2,
1175 hold_fulfillment_policy => 'any',
1176 returnbranch => 'any'
1181 # Test 6: Patron 3 can place hold
1182 is_deeply(
1183 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1184 { status => 'OK' },
1185 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1188 # Update default item rule to "hold group" for itemtype 2
1189 Koha::CirculationRules->set_rules(
1191 branchcode => $library2->branchcode,
1192 itemtype => $itemtype2->itemtype,
1193 rules => {
1194 holdallowed => 2,
1195 hold_fulfillment_policy => 'holdgroup',
1196 returnbranch => 'any'
1201 # Test 7: Patron 3 cannot place hold
1202 is_deeply(
1203 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1204 { status => 'pickupNotInHoldGroup' },
1205 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1208 # Insert branch item rule to "any" for itemtype 2 and library 2
1209 Koha::CirculationRules->set_rules(
1211 branchcode => $library2->branchcode,
1212 itemtype => $itemtype2->itemtype,
1213 rules => {
1214 holdallowed => 2,
1215 hold_fulfillment_policy => 'any',
1216 returnbranch => 'any'
1221 # Test 8: Patron 3 can place hold
1222 is_deeply(
1223 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1224 { status => 'OK' },
1225 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1228 # Update branch item rule to "hold group" for itemtype 2 and library 2
1229 Koha::CirculationRules->set_rules(
1231 branchcode => $library2->branchcode,
1232 itemtype => $itemtype2->itemtype,
1233 rules => {
1234 holdallowed => 2,
1235 hold_fulfillment_policy => 'holdgroup',
1236 returnbranch => 'any'
1241 # Test 9: Patron 3 cannot place hold
1242 is_deeply(
1243 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1244 { status => 'pickupNotInHoldGroup' },
1245 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1248 $schema->storage->txn_rollback;