6 use t::lib::TestBuilder;
10 use Test::More tests => 57;
19 use Koha::CirculationRules;
21 use Koha::DateUtils qw( dt_from_string output_pref );
23 use Koha::IssuingRules;
24 use Koha::Item::Transfer::Limits;
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 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
51 $insert_sth->execute('CAN');
52 $insert_sth->execute('CANNOT');
53 $insert_sth->execute('DUMMY');
54 $insert_sth->execute('ONLY1');
56 # Setup Test------------------------
57 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
59 # Create item instance for testing.
60 my ($item_bibnum, $item_bibitemnum, $itemnumber)
61 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
63 # Create some borrowers
65 foreach (1..$borrowers_count) {
66 my $borrowernumber = Koha::Patron->new({
67 firstname => 'my firstname',
68 surname => 'my surname ' . $_,
69 categorycode => $category->{categorycode},
70 branchcode => $branch_1,
71 })->store->borrowernumber;
72 push @borrowernumbers, $borrowernumber;
75 # Create five item level holds
76 foreach my $borrowernumber ( @borrowernumbers ) {
80 $biblio->biblionumber,
82 my $priority = C4::Reserves::CalculatePriority( $biblio->biblionumber ),
87 my $checkitem = $itemnumber,
92 my $holds = $biblio->holds;
93 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
94 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
95 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
96 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
97 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
98 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
100 my $item = Koha::Items->find( $itemnumber );
101 $holds = $item->current_holds;
102 my $first_hold = $holds->next;
103 my $reservedate = $first_hold->reservedate;
104 my $borrowernumber = $first_hold->borrowernumber;
105 my $branch_1code = $first_hold->branchcode;
106 my $reserve_id = $first_hold->reserve_id;
107 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
108 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
109 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
110 ok($reserve_id, "Test holds_placed_today()");
112 my $hold = Koha::Holds->find( $reserve_id );
113 ok( $hold, "Koha::Holds found the hold" );
114 my $hold_biblio = $hold->biblio();
115 ok( $hold_biblio, "Got biblio using biblio() method" );
116 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
117 my $hold_item = $hold->item();
118 ok( $hold_item, "Got item using item() method" );
119 ok( $hold_item == $hold->item(), "item method returns stashed item" );
120 my $hold_branch = $hold->branch();
121 ok( $hold_branch, "Got branch using branch() method" );
122 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
123 my $hold_found = $hold->found();
124 $hold->set({ found => 'W'})->store();
125 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
126 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
128 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
129 $holds = $patron->holds;
130 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
133 $holds = $item->current_holds;
134 $first_hold = $holds->next;
135 $borrowernumber = $first_hold->borrowernumber;
136 $branch_1code = $first_hold->branchcode;
137 $reserve_id = $first_hold->reserve_id;
140 reserve_id => $reserve_id,
142 branchcode => $branch_1,
143 itemnumber => $itemnumber,
144 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
147 $hold = Koha::Holds->find( $reserve_id );
148 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
149 ok( $hold->suspend, "Test ModReserve, suspend hold" );
150 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
152 ModReserve({ # call without reserve_id
154 biblionumber => $item_bibnum,
155 itemnumber => $itemnumber,
156 borrowernumber => $borrowernumber,
158 $hold = Koha::Holds->find( $reserve_id );
159 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
161 ToggleSuspend( $reserve_id );
162 $hold = Koha::Holds->find( $reserve_id );
163 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
165 ToggleSuspend( $reserve_id, '2012-01-01' );
166 $hold = Koha::Holds->find( $reserve_id );
167 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
169 AutoUnsuspendReserves();
170 $hold = Koha::Holds->find( $reserve_id );
171 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
174 borrowernumber => $borrowernumber,
175 biblionumber => $biblio->biblionumber,
177 suspend_until => '2012-01-01',
179 $hold = Koha::Holds->find( $reserve_id );
180 is( $hold->suspend, 1, "Test SuspendAll()" );
181 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
184 borrowernumber => $borrowernumber,
185 biblionumber => $biblio->biblionumber,
188 $hold = Koha::Holds->find( $reserve_id );
189 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
190 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
192 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
196 $biblio->biblionumber,
206 $patron = Koha::Patrons->find( $borrowernumber );
207 $holds = $patron->holds;
208 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
209 ModReserveMinusPriority( $itemnumber, $reserveid );
210 $holds = $patron->holds;
211 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
213 $holds = $biblio->holds;
214 $hold = $holds->next;
215 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
216 $hold = Koha::Holds->find( $reserveid );
217 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
219 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '2', "Test AlterPriority(), move down" );
223 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
224 $hold = Koha::Holds->find( $reserveid );
225 is( $hold->priority, '1', "Test AlterPriority(), move up" );
227 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
228 $hold = Koha::Holds->find( $reserveid );
229 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
231 # Regression test for bug 2394
233 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
234 # a patron is not permittedo to request an item whose homebranch (i.e.,
235 # owner of the item) is different from the patron's own library.
236 # However, if canreservefromotherbranches is turned ON, the patron can
237 # create such hold requests.
239 # Note that canreservefromotherbranches has no effect if
240 # IndependentBranches is OFF.
242 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
243 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
244 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber);
245 $dbh->do('DELETE FROM issuingrules');
247 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
248 VALUES (?, ?, ?, ?, ?)},
250 '*', '*', '*', 25, 99
253 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
254 VALUES (?, ?, ?, ?, ?)},
256 '*', '*', 'CANNOT', 0, 99
259 # make sure some basic sysprefs are set
260 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
261 t::lib::Mocks::mock_preference('item-level_itypes', 1);
263 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
264 t::lib::Mocks::mock_preference('IndependentBranches', 0);
266 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
267 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
270 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
271 t::lib::Mocks::mock_preference('IndependentBranches', 1);
272 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
274 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
275 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
278 # ... unless canreservefromotherbranches is ON
279 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
281 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
282 '... unless canreservefromotherbranches is ON (bug 2394)'
286 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
287 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
288 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
289 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1);
290 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
291 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $biblio->biblionumber, '', 2);
292 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
293 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $biblio->biblionumber, '', 3);
294 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
295 my $hold3 = Koha::Holds->find( $reserveid3 );
296 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
297 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
298 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
299 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
302 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
303 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
304 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
305 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
307 $hold = Koha::Hold->new(
309 borrowernumber => $borrowernumbers[0],
310 itemnumber => $itemnumber,
311 biblionumber => $item_bibnum,
314 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
316 "Patron cannot place a second item level hold for a given item" );
319 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
320 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
321 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
323 # Regression test for bug 9532
324 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
325 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
329 $biblio->biblionumber,
334 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
335 "cannot request item if policy that matches on item-level item type forbids it"
337 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
339 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
340 "can request item if policy that matches on item type allows it"
343 t::lib::Mocks::mock_preference('item-level_itypes', 0);
344 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
346 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
347 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
351 # Test branch item rules
353 $dbh->do('DELETE FROM issuingrules');
355 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
356 VALUES (?, ?, ?, ?)},
360 $dbh->do('DELETE FROM branch_item_rules');
361 $dbh->do('DELETE FROM default_branch_circ_rules');
362 $dbh->do('DELETE FROM default_branch_item_rules');
363 $dbh->do('DELETE FROM default_circ_rules');
365 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
367 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
369 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
371 }, {}, $branch_1, 'CAN', 1, 'homebranch');
372 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
373 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
374 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
375 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
376 "CanItemBeReserved should return 'notReservable'");
378 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
379 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
380 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
381 'cannotReserveFromOtherBranches',
382 "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
384 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
385 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
386 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
387 "CanItemBeReserved should return 'OK'");
390 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
391 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
393 $dbh->do('DELETE FROM reserves');
394 $dbh->do('DELETE FROM issues');
395 $dbh->do('DELETE FROM items');
396 $dbh->do('DELETE FROM biblio');
398 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
399 ( $item_bibnum, $item_bibitemnum, $itemnumber )
400 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
403 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
404 VALUES (?, ?, ?, ?, ?)},
406 '*', '*', 'ONLY1', 1, 99
408 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
409 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
411 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
413 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
414 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
416 subtest 'Test max_holds per library/patron category' => sub {
419 $dbh->do('DELETE FROM reserves');
420 $dbh->do('DELETE FROM issuingrules');
421 $dbh->do('DELETE FROM circulation_rules');
423 $biblio = $builder->build_sample_biblio({ itemtype => 'TEST' });
424 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
425 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
426 $biblio->biblionumber );
429 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
430 VALUES (?, ?, ?, ?, ?)
433 '*', '*', 'TEST', 99, 99
435 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
436 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
437 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
440 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
441 is( $count, 3, 'Patron now has 3 holds' );
443 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
444 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
446 my $rule_all = Koha::CirculationRules->set_rule(
448 categorycode => $category->{categorycode},
451 rule_name => 'max_holds',
456 my $rule_branch = Koha::CirculationRules->set_rule(
458 branchcode => $branch_1,
459 categorycode => $category->{categorycode},
461 rule_name => 'max_holds',
466 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
467 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
469 $rule_branch->delete();
471 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
472 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
475 $rule_branch->rule_value(3);
476 $rule_branch->store();
478 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
479 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
481 $rule_branch->rule_value(5);
482 $rule_branch->update();
483 $rule_branch->rule_value(5);
484 $rule_branch->store();
486 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
487 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
490 subtest 'Pickup location availability tests' => sub {
493 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
494 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
495 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
496 #Add a default rule to allow some holds
498 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
499 VALUES (?, ?, ?, ?, ?)},
501 '*', '*', '*', 25, 99
503 my $item = Koha::Items->find($itemnumber);
504 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
505 my $library = Koha::Libraries->find($branch_to);
506 $library->pickup_location('1')->store;
507 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
509 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
510 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
512 $library->pickup_location('1')->store;
513 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
514 'OK', 'Library is a pickup location');
516 my $limit = Koha::Item::Transfer::Limit->new({
517 fromBranch => $item->holdingbranch,
518 toBranch => $branch_to,
519 itemtype => $item->effective_itemtype,
521 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to),
522 'cannotBeTransferred', 'Item cannot be transferred');
525 $library->pickup_location('0')->store;
526 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
527 'libraryNotPickupLocation', 'Library is not a pickup location');
528 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
529 'libraryNotFound', 'Cannot set unknown library as pickup location');
532 $schema->storage->txn_rollback;
534 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
538 $schema->storage->txn_begin;
540 Koha::Holds->search->delete;
541 $dbh->do('DELETE FROM issues');
542 Koha::Items->search->delete;
543 Koha::Biblios->search->delete;
545 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
546 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
547 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
549 # Create 3 biblios with items
550 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
551 my ( undef, undef, $itemnumber_1 ) = AddItem(
552 { homebranch => $library->branchcode,
553 holdingbranch => $library->branchcode
555 $biblio_1->biblionumber
557 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
558 my ( undef, undef, $itemnumber_2 ) = AddItem(
559 { homebranch => $library->branchcode,
560 holdingbranch => $library->branchcode
562 $biblio_2->biblionumber
564 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
565 my ( undef, undef, $itemnumber_3 ) = AddItem(
566 { homebranch => $library->branchcode,
567 holdingbranch => $library->branchcode
569 $biblio_3->biblionumber
572 Koha::IssuingRules->search->delete;
573 my $issuingrule = Koha::IssuingRule->new(
574 { categorycode => '*',
576 itemtype => $itemtype->itemtype,
577 reservesallowed => 1,
578 holds_per_record => 99,
584 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
586 'Patron can reserve item with hold limit of 1, no holds placed'
589 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
592 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
593 { status => 'tooManyReserves', limit => 1 },
594 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
597 # Raise reservesallowed to avoid tooManyReserves from it
598 $issuingrule->set( { reservesallowed => 3 } )->store;
601 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
603 'Patron can reserve item with 2 reserves daily cap'
606 # Add a second reserve
607 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
609 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
610 { status => 'tooManyReservesToday', limit => 2 },
611 'Patron cannot a third item with 2 reserves daily cap'
614 # Update last hold so reservedate is in the past, so 2 holds, but different day
615 $hold = Koha::Holds->find($res_id);
616 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
617 $hold->reservedate($yesterday)->store;
620 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
622 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
625 # Set holds_per_day to 0
626 $issuingrule->set( { holds_per_day => 0 } )->store;
628 # Delete existing holds
629 Koha::Holds->search->delete;
631 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
632 { status => 'tooManyReservesToday', limit => 0 },
633 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
636 $issuingrule->set( { holds_per_day => undef } )->store;
637 Koha::Holds->search->delete;
639 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
641 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
643 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
644 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
646 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
648 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
650 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
652 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
653 { status => 'tooManyReserves', limit => 3 },
654 'Unlimited daily holds, but reached reservesallowed'
657 $schema->storage->txn_rollback;