6 use t::lib::TestBuilder;
10 use Test::More tests => 61;
19 use Koha::CirculationRules;
21 use Koha::DateUtils qw( dt_from_string output_pref );
23 use Koha::IssuingRules;
24 use Koha::Item::Transfer::Limits;
27 use Koha::Library::Groups;
32 use lib $FindBin::Bin;
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
38 my $builder = t::lib::TestBuilder->new();
39 my $dbh = C4::Context->dbh;
41 # Create two random branches
42 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
43 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
45 my $category = $builder->build({ source => 'Category' });
47 my $borrowers_count = 5;
49 $dbh->do('DELETE FROM itemtypes');
50 $dbh->do('DELETE FROM reserves');
51 $dbh->do('DELETE FROM circulation_rules');
52 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
53 $insert_sth->execute('CAN');
54 $insert_sth->execute('CANNOT');
55 $insert_sth->execute('DUMMY');
56 $insert_sth->execute('ONLY1');
58 # Setup Test------------------------
59 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
61 # Create item instance for testing.
62 my ($item_bibnum, $item_bibitemnum, $itemnumber)
63 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
65 # Create some borrowers
67 foreach (1..$borrowers_count) {
68 my $borrowernumber = Koha::Patron->new({
69 firstname => 'my firstname',
70 surname => 'my surname ' . $_,
71 categorycode => $category->{categorycode},
72 branchcode => $branch_1,
73 })->store->borrowernumber;
74 push @borrowernumbers, $borrowernumber;
77 # Create five item level holds
78 foreach my $borrowernumber ( @borrowernumbers ) {
82 $biblio->biblionumber,
84 my $priority = C4::Reserves::CalculatePriority( $biblio->biblionumber ),
89 my $checkitem = $itemnumber,
94 my $holds = $biblio->holds;
95 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
96 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
97 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
98 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
99 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
100 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
102 my $item = Koha::Items->find( $itemnumber );
103 $holds = $item->current_holds;
104 my $first_hold = $holds->next;
105 my $reservedate = $first_hold->reservedate;
106 my $borrowernumber = $first_hold->borrowernumber;
107 my $branch_1code = $first_hold->branchcode;
108 my $reserve_id = $first_hold->reserve_id;
109 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
110 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
111 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
112 ok($reserve_id, "Test holds_placed_today()");
114 my $hold = Koha::Holds->find( $reserve_id );
115 ok( $hold, "Koha::Holds found the hold" );
116 my $hold_biblio = $hold->biblio();
117 ok( $hold_biblio, "Got biblio using biblio() method" );
118 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
119 my $hold_item = $hold->item();
120 ok( $hold_item, "Got item using item() method" );
121 ok( $hold_item == $hold->item(), "item method returns stashed item" );
122 my $hold_branch = $hold->branch();
123 ok( $hold_branch, "Got branch using branch() method" );
124 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
125 my $hold_found = $hold->found();
126 $hold->set({ found => 'W'})->store();
127 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
128 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
130 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
131 $holds = $patron->holds;
132 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
135 $holds = $item->current_holds;
136 $first_hold = $holds->next;
137 $borrowernumber = $first_hold->borrowernumber;
138 $branch_1code = $first_hold->branchcode;
139 $reserve_id = $first_hold->reserve_id;
142 reserve_id => $reserve_id,
144 branchcode => $branch_1,
145 itemnumber => $itemnumber,
146 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
149 $hold = Koha::Holds->find( $reserve_id );
150 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
151 ok( $hold->suspend, "Test ModReserve, suspend hold" );
152 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
154 ModReserve({ # call without reserve_id
156 biblionumber => $item_bibnum,
157 itemnumber => $itemnumber,
158 borrowernumber => $borrowernumber,
160 $hold = Koha::Holds->find( $reserve_id );
161 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
163 ToggleSuspend( $reserve_id );
164 $hold = Koha::Holds->find( $reserve_id );
165 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
167 ToggleSuspend( $reserve_id, '2012-01-01' );
168 $hold = Koha::Holds->find( $reserve_id );
169 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
171 AutoUnsuspendReserves();
172 $hold = Koha::Holds->find( $reserve_id );
173 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
176 borrowernumber => $borrowernumber,
177 biblionumber => $biblio->biblionumber,
179 suspend_until => '2012-01-01',
181 $hold = Koha::Holds->find( $reserve_id );
182 is( $hold->suspend, 1, "Test SuspendAll()" );
183 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
186 borrowernumber => $borrowernumber,
187 biblionumber => $biblio->biblionumber,
190 $hold = Koha::Holds->find( $reserve_id );
191 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
192 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
194 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
198 $biblio->biblionumber,
208 $patron = Koha::Patrons->find( $borrowernumber );
209 $holds = $patron->holds;
210 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
211 ModReserveMinusPriority( $itemnumber, $reserveid );
212 $holds = $patron->holds;
213 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
215 $holds = $biblio->holds;
216 $hold = $holds->next;
217 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
218 $hold = Koha::Holds->find( $reserveid );
219 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
221 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
222 $hold = Koha::Holds->find( $reserveid );
223 is( $hold->priority, '2', "Test AlterPriority(), move down" );
225 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
226 $hold = Koha::Holds->find( $reserveid );
227 is( $hold->priority, '1', "Test AlterPriority(), move up" );
229 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
230 $hold = Koha::Holds->find( $reserveid );
231 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
233 # Regression test for bug 2394
235 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
236 # a patron is not permittedo to request an item whose homebranch (i.e.,
237 # owner of the item) is different from the patron's own library.
238 # However, if canreservefromotherbranches is turned ON, the patron can
239 # create such hold requests.
241 # Note that canreservefromotherbranches has no effect if
242 # IndependentBranches is OFF.
244 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
245 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
246 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber);
247 Koha::CirculationRules->set_rules(
253 reservesallowed => 25,
254 holds_per_record => 99,
258 Koha::CirculationRules->set_rules(
262 itemtype => 'CANNOT',
264 reservesallowed => 0,
265 holds_per_record => 99,
270 # make sure some basic sysprefs are set
271 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
272 t::lib::Mocks::mock_preference('item-level_itypes', 1);
274 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
275 t::lib::Mocks::mock_preference('IndependentBranches', 0);
278 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
279 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
282 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
283 t::lib::Mocks::mock_preference('IndependentBranches', 1);
284 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
286 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
287 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
290 # ... unless canreservefromotherbranches is ON
291 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
293 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
294 '... unless canreservefromotherbranches is ON (bug 2394)'
298 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
299 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
300 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
301 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1);
302 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
303 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $biblio->biblionumber, '', 2);
304 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
305 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $biblio->biblionumber, '', 3);
306 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
307 my $hold3 = Koha::Holds->find( $reserveid3 );
308 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
309 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
310 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
311 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
314 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
315 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
316 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
317 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
319 $hold = Koha::Hold->new(
321 borrowernumber => $borrowernumbers[0],
322 itemnumber => $itemnumber,
323 biblionumber => $item_bibnum,
326 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
328 "Patron cannot place a second item level hold for a given item" );
331 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
332 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
333 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
335 # Regression test for bug 9532
336 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
337 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
341 $biblio->biblionumber,
346 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
347 "cannot request item if policy that matches on item-level item type forbids it"
349 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
351 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
352 "can request item if policy that matches on item type allows it"
355 t::lib::Mocks::mock_preference('item-level_itypes', 0);
356 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
358 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
359 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
363 # Test branch item rules
365 $dbh->do('DELETE FROM circulation_rules');
366 Koha::CirculationRules->set_rules(
372 reservesallowed => 25,
373 holds_per_record => 99,
377 Koha::CirculationRules->set_rules(
379 branchcode => $branch_1,
380 itemtype => 'CANNOT',
381 categorycode => undef,
384 returnbranch => 'homebranch',
388 Koha::CirculationRules->set_rules(
390 branchcode => $branch_1,
392 categorycode => undef,
395 returnbranch => 'homebranch',
399 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
400 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
401 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
402 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
403 "CanItemBeReserved should return 'notReservable'");
405 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
406 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
407 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
408 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
409 'cannotReserveFromOtherBranches',
410 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
411 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
412 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
414 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
416 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
417 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
418 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
419 "CanItemBeReserved should return 'OK'");
422 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
423 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
425 $dbh->do('DELETE FROM reserves');
426 $dbh->do('DELETE FROM issues');
427 $dbh->do('DELETE FROM items');
428 $dbh->do('DELETE FROM biblio');
430 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
431 ( $item_bibnum, $item_bibitemnum, $itemnumber )
432 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
434 Koha::CirculationRules->set_rules(
440 reservesallowed => 1,
441 holds_per_record => 99,
445 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
446 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
448 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
450 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
451 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
453 #results should be the same for both ReservesControlBranch settings
454 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
455 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
456 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
457 #reset for further tests
458 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
460 subtest 'Test max_holds per library/patron category' => sub {
463 $dbh->do('DELETE FROM reserves');
464 $dbh->do('DELETE FROM circulation_rules');
466 $biblio = $builder->build_sample_biblio({ itemtype => 'TEST' });
467 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
468 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
469 $biblio->biblionumber );
470 Koha::CirculationRules->set_rules(
476 reservesallowed => 99,
477 holds_per_record => 99,
481 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
482 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
483 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
486 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
487 is( $count, 3, 'Patron now has 3 holds' );
489 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
490 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
492 my $rule_all = Koha::CirculationRules->set_rule(
494 categorycode => $category->{categorycode},
497 rule_name => 'max_holds',
502 my $rule_branch = Koha::CirculationRules->set_rule(
504 branchcode => $branch_1,
505 categorycode => $category->{categorycode},
507 rule_name => 'max_holds',
512 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
513 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
515 $rule_branch->delete();
517 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
518 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
521 $rule_branch->rule_value(3);
522 $rule_branch->store();
524 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
525 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
527 $rule_branch->rule_value(5);
528 $rule_branch->update();
529 $rule_branch->rule_value(5);
530 $rule_branch->store();
532 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
533 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
536 subtest 'Pickup location availability tests' => sub {
539 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
540 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
541 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
542 #Add a default rule to allow some holds
544 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
545 VALUES (?, ?, ?, ?, ?)},
547 '*', '*', '*', 25, 99
549 my $item = Koha::Items->find($itemnumber);
550 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
551 my $library = Koha::Libraries->find($branch_to);
552 $library->pickup_location('1')->store;
553 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
555 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
556 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
558 $library->pickup_location('1')->store;
559 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
560 'OK', 'Library is a pickup location');
562 my $limit = Koha::Item::Transfer::Limit->new({
563 fromBranch => $item->holdingbranch,
564 toBranch => $branch_to,
565 itemtype => $item->effective_itemtype,
567 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
568 'cannotBeTransferred', 'Item cannot be transferred');
571 $library->pickup_location('0')->store;
572 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
573 'libraryNotPickupLocation', 'Library is not a pickup location');
574 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
575 'libraryNotFound', 'Cannot set unknown library as pickup location');
578 $schema->storage->txn_rollback;
580 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
584 $schema->storage->txn_begin;
586 Koha::Holds->search->delete;
587 $dbh->do('DELETE FROM issues');
588 $dbh->do('DELETE FROM issuingrules');
589 $dbh->do('DELETE FROM circulation_rules');
590 Koha::Items->search->delete;
591 Koha::Biblios->search->delete;
593 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
594 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
595 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
597 # Create 3 biblios with items
598 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
599 my ( undef, undef, $itemnumber_1 ) = AddItem(
600 { homebranch => $library->branchcode,
601 holdingbranch => $library->branchcode
603 $biblio_1->biblionumber
605 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
606 my ( undef, undef, $itemnumber_2 ) = AddItem(
607 { homebranch => $library->branchcode,
608 holdingbranch => $library->branchcode
610 $biblio_2->biblionumber
612 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
613 my ( undef, undef, $itemnumber_3 ) = AddItem(
614 { homebranch => $library->branchcode,
615 holdingbranch => $library->branchcode
617 $biblio_3->biblionumber
620 Koha::IssuingRules->search->delete;
621 my $issuingrule = Koha::IssuingRule->new(
622 { categorycode => '*',
624 itemtype => $itemtype->itemtype,
625 reservesallowed => 1,
626 holds_per_record => 99,
632 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
634 'Patron can reserve item with hold limit of 1, no holds placed'
637 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
640 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
641 { status => 'tooManyReserves', limit => 1 },
642 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
645 # Raise reservesallowed to avoid tooManyReserves from it
646 $issuingrule->set( { reservesallowed => 3 } )->store;
649 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
651 'Patron can reserve item with 2 reserves daily cap'
654 # Add a second reserve
655 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
657 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
658 { status => 'tooManyReservesToday', limit => 2 },
659 'Patron cannot a third item with 2 reserves daily cap'
662 # Update last hold so reservedate is in the past, so 2 holds, but different day
663 $hold = Koha::Holds->find($res_id);
664 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
665 $hold->reservedate($yesterday)->store;
668 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
670 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
673 # Set holds_per_day to 0
674 $issuingrule->set( { holds_per_day => 0 } )->store;
676 # Delete existing holds
677 Koha::Holds->search->delete;
679 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
680 { status => 'tooManyReservesToday', limit => 0 },
681 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
684 $issuingrule->set( { holds_per_day => undef } )->store;
685 Koha::Holds->search->delete;
687 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
689 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
691 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
692 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
694 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
696 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
698 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
700 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
701 { status => 'tooManyReserves', limit => 3 },
702 'Unlimited daily holds, but reached reservesallowed'
704 #results should be the same for both ReservesControlBranch settings
705 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
707 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
708 { status => 'tooManyReserves', limit => 3 },
709 'Unlimited daily holds, but reached reservesallowed'
712 $schema->storage->txn_rollback;
715 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
718 $schema->storage->txn_begin;
721 Koha::Holds->search->delete;
722 $dbh->do('DELETE FROM issues');
723 $dbh->do('DELETE FROM issuingrules');
725 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
726 VALUES (?, ?, ?, ?)},
730 $dbh->do('DELETE FROM circulation_rules');
732 Koha::Items->search->delete;
733 Koha::Biblios->search->delete;
736 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
737 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
740 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
741 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
742 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
744 # Create library groups hierarchy
745 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
746 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
747 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
750 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
751 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
753 # Create 3 biblios with items
754 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
755 my ( undef, undef, $itemnumber_1 ) = AddItem(
756 { homebranch => $library1->branchcode,
757 holdingbranch => $library1->branchcode
759 $biblio_1->biblionumber
761 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
762 my ( undef, undef, $itemnumber_2 ) = AddItem(
763 { homebranch => $library2->branchcode,
764 holdingbranch => $library2->branchcode
766 $biblio_2->biblionumber
768 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
769 my ( undef, undef, $itemnumber_3 ) = AddItem(
770 { homebranch => $library1->branchcode,
771 holdingbranch => $library1->branchcode
773 $biblio_3->biblionumber
776 # Test 1: Patron 3 can place hold
778 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
780 'Patron can place hold if no circ_rules where defined'
783 # Insert default circ rule of holds allowed only from local hold group for all libraries
784 Koha::CirculationRules->set_rules(
788 categorycode => undef,
791 hold_fulfillment_policy => 'any',
792 returnbranch => 'any'
797 # Test 2: Patron 1 can place hold
799 CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
801 'Patron can place hold because patron\'s home library is part of hold group'
804 # Test 3: Patron 3 cannot place hold
806 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
807 { status => 'branchNotInHoldGroup' },
808 'Patron cannot place hold because patron\'s home library is not part of hold group'
811 # Cleanup default_cirt_rules
812 $dbh->do('DELETE FROM circulation_rules');
814 # Insert default circ rule to "any" for library 2
815 Koha::CirculationRules->set_rules(
817 branchcode => $library2->branchcode,
819 categorycode => undef,
822 hold_fulfillment_policy => 'any',
823 returnbranch => 'any'
828 # Test 4: Patron 3 can place hold
830 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
832 'Patron can place hold if holdallowed is set to "any" for library 2'
835 # Update default circ rule to "hold group" for library 2
836 Koha::CirculationRules->set_rules(
838 branchcode => $library2->branchcode,
840 categorycode => undef,
843 hold_fulfillment_policy => 'any',
844 returnbranch => 'any'
849 # Test 5: Patron 3 cannot place hold
851 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
852 { status => 'branchNotInHoldGroup' },
853 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
856 # Cleanup default_branch_cirt_rules
857 $dbh->do('DELETE FROM circulation_rules');
859 # Insert default item rule to "any" for itemtype 2
860 Koha::CirculationRules->set_rules(
863 itemtype => $itemtype2->itemtype,
864 categorycode => undef,
867 hold_fulfillment_policy => 'any',
868 returnbranch => 'any'
873 # Test 6: Patron 3 can place hold
875 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
877 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
880 # Update default item rule to "hold group" for itemtype 2
881 Koha::CirculationRules->set_rules(
884 itemtype => $itemtype2->itemtype,
885 categorycode => undef,
888 hold_fulfillment_policy => 'any',
889 returnbranch => 'any'
894 # Test 7: Patron 3 cannot place hold
896 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
897 { status => 'branchNotInHoldGroup' },
898 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
901 # Cleanup default_branch_item_rules
902 $dbh->do('DELETE FROM circulation_rules');
904 # Insert branch item rule to "any" for itemtype 2 and library 2
905 Koha::CirculationRules->set_rules(
907 branchcode => $library2->branchcode,
908 itemtype => $itemtype2->itemtype,
909 categorycode => undef,
912 hold_fulfillment_policy => 'any',
913 returnbranch => 'any'
918 # Test 8: Patron 3 can place hold
920 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
922 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
925 # Update branch item rule to "hold group" for itemtype 2 and library 2
926 Koha::CirculationRules->set_rules(
928 branchcode => $library2->branchcode,
929 itemtype => $itemtype2->itemtype,
930 categorycode => undef,
933 hold_fulfillment_policy => 'any',
934 returnbranch => 'any'
939 # Test 9: Patron 3 cannot place hold
941 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
942 { status => 'branchNotInHoldGroup' },
943 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
946 $schema->storage->txn_rollback;
950 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
953 $schema->storage->txn_begin;
956 Koha::Holds->search->delete;
957 $dbh->do('DELETE FROM issues');
958 $dbh->do('DELETE FROM issuingrules');
960 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
961 VALUES (?, ?, ?, ?)},
965 $dbh->do('DELETE FROM circulation_rules');
967 Koha::Items->search->delete;
968 Koha::Biblios->search->delete;
971 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
972 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
975 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
976 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
977 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
979 # Create library groups hierarchy
980 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
981 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
982 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
985 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
986 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
988 # Create 3 biblios with items
989 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
990 my ( undef, undef, $itemnumber_1 ) = AddItem(
991 { homebranch => $library1->branchcode,
992 holdingbranch => $library1->branchcode
994 $biblio_1->biblionumber
996 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
997 my ( undef, undef, $itemnumber_2 ) = AddItem(
998 { homebranch => $library2->branchcode,
999 holdingbranch => $library2->branchcode
1001 $biblio_2->biblionumber
1003 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1004 my ( undef, undef, $itemnumber_3 ) = AddItem(
1005 { homebranch => $library1->branchcode,
1006 holdingbranch => $library1->branchcode
1008 $biblio_3->biblionumber
1011 # Test 1: Patron 3 can place hold
1013 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1015 'Patron can place hold if no circ_rules where defined'
1018 # Insert default circ rule of holds allowed only from local hold group for all libraries
1019 Koha::CirculationRules->set_rules(
1021 branchcode => undef,
1023 categorycode => undef,
1026 hold_fulfillment_policy => 'holdgroup',
1027 returnbranch => 'any'
1032 # Test 2: Patron 1 can place hold
1034 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1036 'Patron can place hold because pickup location is part of hold group'
1039 # Test 3: Patron 3 cannot place hold
1041 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1042 { status => 'pickupNotInHoldGroup' },
1043 'Patron cannot place hold because pickup location is not part of hold group'
1046 # Cleanup default_cirt_rules
1047 $dbh->do('DELETE FROM circulation_rules');
1049 # Insert default circ rule to "any" for library 2
1050 Koha::CirculationRules->set_rules(
1052 branchcode => $library2->branchcode,
1054 categorycode => undef,
1057 hold_fulfillment_policy => 'any',
1058 returnbranch => 'any'
1063 # Test 4: Patron 3 can place hold
1065 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1067 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1070 # Update default circ rule to "hold group" for library 2
1071 Koha::CirculationRules->set_rules(
1073 branchcode => $library2->branchcode,
1075 categorycode => undef,
1078 hold_fulfillment_policy => 'holdgroup',
1079 returnbranch => 'any'
1084 # Test 5: Patron 3 cannot place hold
1086 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1087 { status => 'pickupNotInHoldGroup' },
1088 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1091 # Cleanup default_branch_cirt_rules
1092 $dbh->do('DELETE FROM circulation_rules');
1094 # Insert default item rule to "any" for itemtype 2
1095 Koha::CirculationRules->set_rules(
1097 branchcode => undef,
1098 itemtype => $itemtype2->itemtype,
1099 categorycode => undef,
1102 hold_fulfillment_policy => 'any',
1103 returnbranch => 'any'
1108 # Test 6: Patron 3 can place hold
1110 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1112 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1115 # Update default item rule to "hold group" for itemtype 2
1116 Koha::CirculationRules->set_rules(
1118 branchcode => undef,
1119 itemtype => $itemtype2->itemtype,
1120 categorycode => undef,
1123 hold_fulfillment_policy => 'holdgroup',
1124 returnbranch => 'any'
1129 # Test 7: Patron 3 cannot place hold
1131 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1132 { status => 'pickupNotInHoldGroup' },
1133 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1136 # Cleanup default_branch_item_rules
1137 $dbh->do('DELETE FROM circulation_rules');
1139 # Insert branch item rule to "any" for itemtype 2 and library 2
1140 Koha::CirculationRules->set_rules(
1142 branchcode => $library2->branchcode,
1143 itemtype => $itemtype2->itemtype,
1144 categorycode => undef,
1147 hold_fulfillment_policy => 'any',
1148 returnbranch => 'any'
1153 # Test 8: Patron 3 can place hold
1155 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1157 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1160 # Update branch item rule to "hold group" for itemtype 2 and library 2
1161 Koha::CirculationRules->set_rules(
1163 branchcode => $library2->branchcode,
1164 itemtype => $itemtype2->itemtype,
1165 categorycode => undef,
1168 hold_fulfillment_policy => 'holdgroup',
1169 returnbranch => 'any'
1174 # Test 9: Patron 3 cannot place hold
1176 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1177 { status => 'pickupNotInHoldGroup' },
1178 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1181 $schema->storage->txn_rollback;