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 # Create a biblio instance for testing
58 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
60 # Create item instance for testing.
61 my ($item_bibnum, $item_bibitemnum, $itemnumber)
62 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
64 # Create some borrowers
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 my $biblionumber = $bibnum;
78 # Create five item level holds
79 foreach my $borrowernumber ( @borrowernumbers ) {
85 my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
90 my $checkitem = $itemnumber,
95 my $biblio = Koha::Biblios->find( $biblionumber );
96 my $holds = $biblio->holds;
97 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
98 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
99 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
100 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
101 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
102 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
104 my $item = Koha::Items->find( $itemnumber );
105 $holds = $item->current_holds;
106 my $first_hold = $holds->next;
107 my $reservedate = $first_hold->reservedate;
108 my $borrowernumber = $first_hold->borrowernumber;
109 my $branch_1code = $first_hold->branchcode;
110 my $reserve_id = $first_hold->reserve_id;
111 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
112 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
113 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
114 ok($reserve_id, "Test holds_placed_today()");
116 my $hold = Koha::Holds->find( $reserve_id );
117 ok( $hold, "Koha::Holds found the hold" );
118 my $hold_biblio = $hold->biblio();
119 ok( $hold_biblio, "Got biblio using biblio() method" );
120 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
121 my $hold_item = $hold->item();
122 ok( $hold_item, "Got item using item() method" );
123 ok( $hold_item == $hold->item(), "item method returns stashed item" );
124 my $hold_branch = $hold->branch();
125 ok( $hold_branch, "Got branch using branch() method" );
126 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
127 my $hold_found = $hold->found();
128 $hold->set({ found => 'W'})->store();
129 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
130 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
132 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
133 $holds = $patron->holds;
134 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
137 $holds = $item->current_holds;
138 $first_hold = $holds->next;
139 $borrowernumber = $first_hold->borrowernumber;
140 $branch_1code = $first_hold->branchcode;
141 $reserve_id = $first_hold->reserve_id;
144 reserve_id => $reserve_id,
146 branchcode => $branch_1,
147 itemnumber => $itemnumber,
148 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
151 $hold = Koha::Holds->find( $reserve_id );
152 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
153 ok( $hold->suspend, "Test ModReserve, suspend hold" );
154 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
156 ModReserve({ # call without reserve_id
158 biblionumber => $item_bibnum,
159 itemnumber => $itemnumber,
160 borrowernumber => $borrowernumber,
162 $hold = Koha::Holds->find( $reserve_id );
163 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
165 ToggleSuspend( $reserve_id );
166 $hold = Koha::Holds->find( $reserve_id );
167 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
169 ToggleSuspend( $reserve_id, '2012-01-01' );
170 $hold = Koha::Holds->find( $reserve_id );
171 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
173 AutoUnsuspendReserves();
174 $hold = Koha::Holds->find( $reserve_id );
175 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
178 borrowernumber => $borrowernumber,
179 biblionumber => $biblionumber,
181 suspend_until => '2012-01-01',
183 $hold = Koha::Holds->find( $reserve_id );
184 is( $hold->suspend, 1, "Test SuspendAll()" );
185 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
188 borrowernumber => $borrowernumber,
189 biblionumber => $biblionumber,
192 $hold = Koha::Holds->find( $reserve_id );
193 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
194 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
196 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
210 $patron = Koha::Patrons->find( $borrowernumber );
211 $holds = $patron->holds;
212 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
213 ModReserveMinusPriority( $itemnumber, $reserveid );
214 $holds = $patron->holds;
215 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
217 $holds = $biblio->holds;
218 $hold = $holds->next;
219 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
223 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
224 $hold = Koha::Holds->find( $reserveid );
225 is( $hold->priority, '2', "Test AlterPriority(), move down" );
227 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
228 $hold = Koha::Holds->find( $reserveid );
229 is( $hold->priority, '1', "Test AlterPriority(), move up" );
231 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
232 $hold = Koha::Holds->find( $reserveid );
233 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
235 # Regression test for bug 2394
237 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
238 # a patron is not permittedo to request an item whose homebranch (i.e.,
239 # owner of the item) is different from the patron's own library.
240 # However, if canreservefromotherbranches is turned ON, the patron can
241 # create such hold requests.
243 # Note that canreservefromotherbranches has no effect if
244 # IndependentBranches is OFF.
246 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
247 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
248 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
249 $dbh->do('DELETE FROM issuingrules');
251 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
252 VALUES (?, ?, ?, ?, ?)},
254 '*', '*', '*', 25, 99
257 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
258 VALUES (?, ?, ?, ?, ?)},
260 '*', '*', 'CANNOT', 0, 99
263 # make sure some basic sysprefs are set
264 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
265 t::lib::Mocks::mock_preference('item-level_itypes', 1);
267 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
268 t::lib::Mocks::mock_preference('IndependentBranches', 0);
270 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
271 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
274 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
275 t::lib::Mocks::mock_preference('IndependentBranches', 1);
276 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
278 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
279 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
282 # ... unless canreservefromotherbranches is ON
283 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
285 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
286 '... unless canreservefromotherbranches is ON (bug 2394)'
290 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
291 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
292 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
293 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
294 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
295 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
296 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
297 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
298 my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
299 my $hold3 = Koha::Holds->find( $reserveid3 );
300 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
301 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
302 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
303 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
306 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
307 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
308 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
309 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
311 $hold = Koha::Hold->new(
313 borrowernumber => $borrowernumbers[0],
314 itemnumber => $itemnumber,
315 biblionumber => $item_bibnum,
318 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
320 "Patron cannot place a second item level hold for a given item" );
323 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
324 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
325 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
327 # Regression test for bug 9532
328 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
329 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
338 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
339 "cannot request item if policy that matches on item-level item type forbids it"
341 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
343 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
344 "can request item if policy that matches on item type allows it"
347 t::lib::Mocks::mock_preference('item-level_itypes', 0);
348 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
350 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
351 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
355 # Test branch item rules
357 $dbh->do('DELETE FROM issuingrules');
359 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
360 VALUES (?, ?, ?, ?)},
364 $dbh->do('DELETE FROM branch_item_rules');
365 $dbh->do('DELETE FROM default_branch_circ_rules');
366 $dbh->do('DELETE FROM default_branch_item_rules');
367 $dbh->do('DELETE FROM default_circ_rules');
369 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
371 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
373 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
375 }, {}, $branch_1, 'CAN', 1, 'homebranch');
376 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
377 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
378 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
379 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
380 "CanItemBeReserved should return 'notReservable'");
382 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
383 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
384 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
385 'cannotReserveFromOtherBranches',
386 "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
388 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
389 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
390 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
391 "CanItemBeReserved should return 'OK'");
394 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
395 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
397 $dbh->do('DELETE FROM reserves');
398 $dbh->do('DELETE FROM issues');
399 $dbh->do('DELETE FROM items');
400 $dbh->do('DELETE FROM biblio');
402 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
403 ( $item_bibnum, $item_bibitemnum, $itemnumber )
404 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
407 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
408 VALUES (?, ?, ?, ?, ?)},
410 '*', '*', 'ONLY1', 1, 99
412 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
413 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
415 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
417 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
418 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
420 subtest 'Test max_holds per library/patron category' => sub {
423 $dbh->do('DELETE FROM reserves');
424 $dbh->do('DELETE FROM issuingrules');
425 $dbh->do('DELETE FROM circulation_rules');
427 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('TEST');
428 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
429 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
433 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
434 VALUES (?, ?, ?, ?, ?)
437 '*', '*', 'TEST', 99, 99
439 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
440 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
441 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
444 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
445 is( $count, 3, 'Patron now has 3 holds' );
447 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
448 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
450 my $rule_all = Koha::CirculationRules->set_rule(
452 categorycode => $category->{categorycode},
455 rule_name => 'max_holds',
460 my $rule_branch = Koha::CirculationRules->set_rule(
462 branchcode => $branch_1,
463 categorycode => $category->{categorycode},
465 rule_name => 'max_holds',
470 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
471 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
473 $rule_branch->delete();
475 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
476 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
479 $rule_branch->rule_value(3);
480 $rule_branch->store();
482 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
483 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
485 $rule_branch->rule_value(5);
486 $rule_branch->update();
487 $rule_branch->rule_value(5);
488 $rule_branch->store();
490 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
491 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
494 subtest 'Pickup location availability tests' => sub {
497 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
498 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
499 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
500 #Add a default rule to allow some holds
502 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
503 VALUES (?, ?, ?, ?, ?)},
505 '*', '*', '*', 25, 99
507 my $item = Koha::Items->find($itemnumber);
508 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
509 my $library = Koha::Libraries->find($branch_to);
510 $library->pickup_location('1')->store;
511 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
513 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
514 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
516 $library->pickup_location('1')->store;
517 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
518 'OK', 'Library is a pickup location');
520 my $limit = Koha::Item::Transfer::Limit->new({
521 fromBranch => $item->holdingbranch,
522 toBranch => $branch_to,
523 itemtype => $item->effective_itemtype,
525 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to),
526 'cannotBeTransferred', 'Item cannot be transferred');
529 $library->pickup_location('0')->store;
530 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
531 'libraryNotPickupLocation', 'Library is not a pickup location');
532 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
533 'libraryNotFound', 'Cannot set unknown library as pickup location');
536 $schema->storage->txn_rollback;
538 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
542 $schema->storage->txn_begin;
544 Koha::Holds->search->delete;
545 $dbh->do('DELETE FROM issues');
546 Koha::Items->search->delete;
547 Koha::Biblios->search->delete;
549 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
550 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
551 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
553 # Create 3 biblios with items
554 my ($bibnum_1) = create_helper_biblio( $itemtype->itemtype );
555 my ( undef, undef, $itemnumber_1 ) = AddItem(
556 { homebranch => $library->branchcode,
557 holdingbranch => $library->branchcode
561 my ($bibnum_2) = create_helper_biblio( $itemtype->itemtype );
562 my ( undef, undef, $itemnumber_2 ) = AddItem(
563 { homebranch => $library->branchcode,
564 holdingbranch => $library->branchcode
568 my ($bibnum_3) = create_helper_biblio( $itemtype->itemtype );
569 my ( undef, undef, $itemnumber_3 ) = AddItem(
570 { homebranch => $library->branchcode,
571 holdingbranch => $library->branchcode
576 Koha::IssuingRules->search->delete;
577 my $issuingrule = Koha::IssuingRule->new(
578 { categorycode => '*',
580 itemtype => $itemtype->itemtype,
581 reservesallowed => 1,
582 holds_per_record => 99,
588 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
590 'Patron can reserve item with hold limit of 1, no holds placed'
593 AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
596 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
597 { status => 'tooManyReserves', limit => 1 },
598 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
601 # Raise reservesallowed to avoid tooManyReserves from it
602 $issuingrule->set( { reservesallowed => 3 } )->store;
605 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
607 'Patron can reserve item with 2 reserves daily cap'
610 # Add a second reserve
611 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
613 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
614 { status => 'tooManyReservesToday', limit => 2 },
615 'Patron cannot a third item with 2 reserves daily cap'
618 # Update last hold so reservedate is in the past, so 2 holds, but different day
619 $hold = Koha::Holds->find($res_id);
620 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
621 $hold->reservedate($yesterday)->store;
624 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
626 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
629 # Set holds_per_day to 0
630 $issuingrule->set( { holds_per_day => 0 } )->store;
632 # Delete existing holds
633 Koha::Holds->search->delete;
635 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
636 { status => 'tooManyReservesToday', limit => 0 },
637 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
640 $issuingrule->set( { holds_per_day => undef } )->store;
641 Koha::Holds->search->delete;
643 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
645 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
647 AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
648 AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
650 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
652 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
654 AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_3, '', 1, );
656 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
657 { status => 'tooManyReserves', limit => 3 },
658 'Unlimited daily holds, but reached reservesallowed'
661 $schema->storage->txn_rollback;
664 # Helper method to set up a Biblio.
665 sub create_helper_biblio {
666 my $itemtype = shift;
667 my $bib = MARC::Record->new();
668 my $title = 'Silence in the library';
670 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
671 MARC::Field->new('245', ' ', ' ', a => $title),
672 MARC::Field->new('942', ' ', ' ', c => $itemtype),
674 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');