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::Item::Transfer::Limits;
26 use Koha::Library::Groups;
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
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 ) {
81 $biblio->biblionumber,
83 my $priority = C4::Reserves::CalculatePriority( $biblio->biblionumber ),
88 my $checkitem = $itemnumber,
93 my $holds = $biblio->holds;
94 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
95 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
96 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
97 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
98 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
99 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
101 my $item = Koha::Items->find( $itemnumber );
102 $holds = $item->current_holds;
103 my $first_hold = $holds->next;
104 my $reservedate = $first_hold->reservedate;
105 my $borrowernumber = $first_hold->borrowernumber;
106 my $branch_1code = $first_hold->branchcode;
107 my $reserve_id = $first_hold->reserve_id;
108 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
109 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
110 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
111 ok($reserve_id, "Test holds_placed_today()");
113 my $hold = Koha::Holds->find( $reserve_id );
114 ok( $hold, "Koha::Holds found the hold" );
115 my $hold_biblio = $hold->biblio();
116 ok( $hold_biblio, "Got biblio using biblio() method" );
117 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
118 my $hold_item = $hold->item();
119 ok( $hold_item, "Got item using item() method" );
120 ok( $hold_item == $hold->item(), "item method returns stashed item" );
121 my $hold_branch = $hold->branch();
122 ok( $hold_branch, "Got branch using branch() method" );
123 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
124 my $hold_found = $hold->found();
125 $hold->set({ found => 'W'})->store();
126 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
127 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
129 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
130 $holds = $patron->holds;
131 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
134 $holds = $item->current_holds;
135 $first_hold = $holds->next;
136 $borrowernumber = $first_hold->borrowernumber;
137 $branch_1code = $first_hold->branchcode;
138 $reserve_id = $first_hold->reserve_id;
141 reserve_id => $reserve_id,
143 branchcode => $branch_1,
144 itemnumber => $itemnumber,
145 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
148 $hold = Koha::Holds->find( $reserve_id );
149 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
150 ok( $hold->suspend, "Test ModReserve, suspend hold" );
151 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
153 ModReserve({ # call without reserve_id
155 biblionumber => $item_bibnum,
156 itemnumber => $itemnumber,
157 borrowernumber => $borrowernumber,
159 $hold = Koha::Holds->find( $reserve_id );
160 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
162 ToggleSuspend( $reserve_id );
163 $hold = Koha::Holds->find( $reserve_id );
164 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
166 ToggleSuspend( $reserve_id, '2012-01-01' );
167 $hold = Koha::Holds->find( $reserve_id );
168 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
170 AutoUnsuspendReserves();
171 $hold = Koha::Holds->find( $reserve_id );
172 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
175 borrowernumber => $borrowernumber,
176 biblionumber => $biblio->biblionumber,
178 suspend_until => '2012-01-01',
180 $hold = Koha::Holds->find( $reserve_id );
181 is( $hold->suspend, 1, "Test SuspendAll()" );
182 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
185 borrowernumber => $borrowernumber,
186 biblionumber => $biblio->biblionumber,
189 $hold = Koha::Holds->find( $reserve_id );
190 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
191 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
193 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
197 $biblio->biblionumber,
207 $patron = Koha::Patrons->find( $borrowernumber );
208 $holds = $patron->holds;
209 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
210 ModReserveMinusPriority( $itemnumber, $reserveid );
211 $holds = $patron->holds;
212 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
214 $holds = $biblio->holds;
215 $hold = $holds->next;
216 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
217 $hold = Koha::Holds->find( $reserveid );
218 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
220 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
221 $hold = Koha::Holds->find( $reserveid );
222 is( $hold->priority, '2', "Test AlterPriority(), move down" );
224 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
225 $hold = Koha::Holds->find( $reserveid );
226 is( $hold->priority, '1', "Test AlterPriority(), move up" );
228 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
229 $hold = Koha::Holds->find( $reserveid );
230 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
232 # Regression test for bug 2394
234 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
235 # a patron is not permittedo to request an item whose homebranch (i.e.,
236 # owner of the item) is different from the patron's own library.
237 # However, if canreservefromotherbranches is turned ON, the patron can
238 # create such hold requests.
240 # Note that canreservefromotherbranches has no effect if
241 # IndependentBranches is OFF.
243 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
244 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
245 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber);
246 Koha::CirculationRules->set_rules(
248 categorycode => undef,
252 reservesallowed => 25,
253 holds_per_record => 99,
257 Koha::CirculationRules->set_rules(
259 categorycode => undef,
261 itemtype => 'CANNOT',
263 reservesallowed => 0,
264 holds_per_record => 99,
269 # make sure some basic sysprefs are set
270 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
271 t::lib::Mocks::mock_preference('item-level_itypes', 1);
273 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
274 t::lib::Mocks::mock_preference('IndependentBranches', 0);
277 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
278 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
281 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
282 t::lib::Mocks::mock_preference('IndependentBranches', 1);
283 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
285 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
286 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
289 # ... unless canreservefromotherbranches is ON
290 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
292 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
293 '... unless canreservefromotherbranches is ON (bug 2394)'
297 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
298 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
299 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
300 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1);
301 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
302 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $biblio->biblionumber, '', 2);
303 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
304 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $biblio->biblionumber, '', 3);
305 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
306 my $hold3 = Koha::Holds->find( $reserveid3 );
307 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
308 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
309 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
310 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
313 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
314 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
315 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
316 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
318 $hold = Koha::Hold->new(
320 borrowernumber => $borrowernumbers[0],
321 itemnumber => $itemnumber,
322 biblionumber => $item_bibnum,
325 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
327 "Patron cannot place a second item level hold for a given item" );
330 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
331 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
332 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
334 # Regression test for bug 9532
335 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
336 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
340 $biblio->biblionumber,
345 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
346 "cannot request item if policy that matches on item-level item type forbids it"
348 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
350 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
351 "can request item if policy that matches on item type allows it"
354 t::lib::Mocks::mock_preference('item-level_itypes', 0);
355 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
357 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
358 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
362 # Test branch item rules
364 $dbh->do('DELETE FROM circulation_rules');
365 Koha::CirculationRules->set_rules(
367 categorycode => undef,
371 reservesallowed => 25,
372 holds_per_record => 99,
376 Koha::CirculationRules->set_rules(
378 branchcode => $branch_1,
379 itemtype => 'CANNOT',
382 returnbranch => 'homebranch',
386 Koha::CirculationRules->set_rules(
388 branchcode => $branch_1,
392 returnbranch => 'homebranch',
396 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
397 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
398 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
399 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
400 "CanItemBeReserved should return 'notReservable'");
402 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
403 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
404 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
405 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
406 'cannotReserveFromOtherBranches',
407 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
408 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
409 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
411 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
413 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
414 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
415 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
416 "CanItemBeReserved should return 'OK'");
419 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
420 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
422 $dbh->do('DELETE FROM reserves');
423 $dbh->do('DELETE FROM issues');
424 $dbh->do('DELETE FROM items');
425 $dbh->do('DELETE FROM biblio');
427 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
428 ( $item_bibnum, $item_bibitemnum, $itemnumber )
429 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
431 Koha::CirculationRules->set_rules(
433 categorycode => undef,
437 reservesallowed => 1,
438 holds_per_record => 99,
442 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
443 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
445 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
447 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
448 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
450 #results should be the same for both ReservesControlBranch settings
451 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
452 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
453 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
454 #reset for further tests
455 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
457 subtest 'Test max_holds per library/patron category' => sub {
460 $dbh->do('DELETE FROM reserves');
461 $dbh->do('DELETE FROM circulation_rules');
463 $biblio = $builder->build_sample_biblio;
464 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
465 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
466 $biblio->biblionumber );
467 Koha::CirculationRules->set_rules(
469 categorycode => undef,
471 itemtype => $biblio->itemtype,
473 reservesallowed => 99,
474 holds_per_record => 99,
478 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
479 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
480 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
483 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
484 is( $count, 3, 'Patron now has 3 holds' );
486 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
487 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
489 my $rule_all = Koha::CirculationRules->set_rule(
491 categorycode => $category->{categorycode},
493 rule_name => 'max_holds',
498 my $rule_branch = Koha::CirculationRules->set_rule(
500 branchcode => $branch_1,
501 categorycode => $category->{categorycode},
502 rule_name => 'max_holds',
507 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
508 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
510 $rule_branch->delete();
512 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
513 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
516 $rule_branch->rule_value(3);
517 $rule_branch->store();
519 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
520 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
522 $rule_branch->rule_value(5);
523 $rule_branch->update();
524 $rule_branch->rule_value(5);
525 $rule_branch->store();
527 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
528 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
531 subtest 'Pickup location availability tests' => sub {
534 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
535 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
536 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
537 #Add a default rule to allow some holds
539 Koha::CirculationRules->set_rules(
542 categorycode => undef,
545 reservesallowed => 25,
546 holds_per_record => 99,
550 my $item = Koha::Items->find($itemnumber);
551 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
552 my $library = Koha::Libraries->find($branch_to);
553 $library->pickup_location('1')->store;
554 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
556 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
557 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
559 $library->pickup_location('1')->store;
560 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
561 'OK', 'Library is a pickup location');
563 my $limit = Koha::Item::Transfer::Limit->new({
564 fromBranch => $item->holdingbranch,
565 toBranch => $branch_to,
566 itemtype => $item->effective_itemtype,
568 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
569 'cannotBeTransferred', 'Item cannot be transferred');
572 $library->pickup_location('0')->store;
573 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
574 'libraryNotPickupLocation', 'Library is not a pickup location');
575 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
576 'libraryNotFound', 'Cannot set unknown library as pickup location');
579 $schema->storage->txn_rollback;
581 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
585 $schema->storage->txn_begin;
587 Koha::Holds->search->delete;
588 $dbh->do('DELETE FROM issues');
589 $dbh->do('DELETE FROM issuingrules');
590 $dbh->do('DELETE FROM circulation_rules');
591 Koha::Items->search->delete;
592 Koha::Biblios->search->delete;
594 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
595 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
596 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
598 # Create 3 biblios with items
599 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
600 my ( undef, undef, $itemnumber_1 ) = AddItem(
601 { homebranch => $library->branchcode,
602 holdingbranch => $library->branchcode
604 $biblio_1->biblionumber
606 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
607 my ( undef, undef, $itemnumber_2 ) = AddItem(
608 { homebranch => $library->branchcode,
609 holdingbranch => $library->branchcode
611 $biblio_2->biblionumber
613 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
614 my ( undef, undef, $itemnumber_3 ) = AddItem(
615 { homebranch => $library->branchcode,
616 holdingbranch => $library->branchcode
618 $biblio_3->biblionumber
621 Koha::CirculationRules->search->delete;
622 Koha::CirculationRules->set_rules(
626 itemtype => $itemtype->itemtype,
628 reservesallowed => 1,
629 holds_per_record => 99,
636 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
638 'Patron can reserve item with hold limit of 1, no holds placed'
641 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
644 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
645 { status => 'tooManyReserves', limit => 1 },
646 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
649 # Raise reservesallowed to avoid tooManyReserves from it
650 Koha::CirculationRules->set_rule(
655 itemtype => $itemtype->itemtype,
656 rule_name => 'reservesallowed',
662 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
664 'Patron can reserve item with 2 reserves daily cap'
667 # Add a second reserve
668 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
670 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
671 { status => 'tooManyReservesToday', limit => 2 },
672 'Patron cannot a third item with 2 reserves daily cap'
675 # Update last hold so reservedate is in the past, so 2 holds, but different day
676 $hold = Koha::Holds->find($res_id);
677 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
678 $hold->reservedate($yesterday)->store;
681 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
683 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
686 # Set holds_per_day to 0
687 Koha::CirculationRules->set_rule(
692 itemtype => $itemtype->itemtype,
693 rule_name => 'holds_per_day',
699 # Delete existing holds
700 Koha::Holds->search->delete;
702 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
703 { status => 'tooManyReservesToday', limit => 0 },
704 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
707 Koha::CirculationRules->set_rule(
712 itemtype => $itemtype->itemtype,
713 rule_name => 'holds_per_day',
718 Koha::Holds->search->delete;
720 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
722 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
724 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
725 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
727 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
729 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
731 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
733 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
734 { status => 'tooManyReserves', limit => 3 },
735 'Unlimited daily holds, but reached reservesallowed'
737 #results should be the same for both ReservesControlBranch settings
738 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
740 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
741 { status => 'tooManyReserves', limit => 3 },
742 'Unlimited daily holds, but reached reservesallowed'
745 $schema->storage->txn_rollback;
748 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
751 $schema->storage->txn_begin;
754 Koha::Holds->search->delete;
755 $dbh->do('DELETE FROM issues');
756 $dbh->do('DELETE FROM issuingrules');
758 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
759 VALUES (?, ?, ?, ?)},
763 $dbh->do('DELETE FROM circulation_rules');
765 Koha::Items->search->delete;
766 Koha::Biblios->search->delete;
769 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
770 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
773 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
774 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
775 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
777 # Create library groups hierarchy
778 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
779 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
780 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
783 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
784 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
786 # Create 3 biblios with items
787 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
788 my ( undef, undef, $itemnumber_1 ) = AddItem(
789 { homebranch => $library1->branchcode,
790 holdingbranch => $library1->branchcode
792 $biblio_1->biblionumber
794 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
795 my ( undef, undef, $itemnumber_2 ) = AddItem(
796 { homebranch => $library2->branchcode,
797 holdingbranch => $library2->branchcode
799 $biblio_2->biblionumber
801 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
802 my ( undef, undef, $itemnumber_3 ) = AddItem(
803 { homebranch => $library1->branchcode,
804 holdingbranch => $library1->branchcode
806 $biblio_3->biblionumber
809 # Test 1: Patron 3 can place hold
811 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
813 'Patron can place hold if no circ_rules where defined'
816 # Insert default circ rule of holds allowed only from local hold group for all libraries
817 Koha::CirculationRules->set_rules(
821 categorycode => undef,
824 hold_fulfillment_policy => 'any',
825 returnbranch => 'any'
830 # Test 2: Patron 1 can place hold
832 CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
834 'Patron can place hold because patron\'s home library is part of hold group'
837 # Test 3: Patron 3 cannot place hold
839 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
840 { status => 'branchNotInHoldGroup' },
841 'Patron cannot place hold because patron\'s home library is not part of hold group'
844 # Cleanup default_cirt_rules
845 $dbh->do('DELETE FROM circulation_rules');
847 # Insert default circ rule to "any" for library 2
848 Koha::CirculationRules->set_rules(
850 branchcode => $library2->branchcode,
852 categorycode => undef,
855 hold_fulfillment_policy => 'any',
856 returnbranch => 'any'
861 # Test 4: Patron 3 can place hold
863 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
865 'Patron can place hold if holdallowed is set to "any" for library 2'
868 # Update default circ rule to "hold group" for library 2
869 Koha::CirculationRules->set_rules(
871 branchcode => $library2->branchcode,
873 categorycode => undef,
876 hold_fulfillment_policy => 'any',
877 returnbranch => 'any'
882 # Test 5: Patron 3 cannot place hold
884 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
885 { status => 'branchNotInHoldGroup' },
886 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
889 # Cleanup default_branch_cirt_rules
890 $dbh->do('DELETE FROM circulation_rules');
892 # Insert default item rule to "any" for itemtype 2
893 Koha::CirculationRules->set_rules(
896 itemtype => $itemtype2->itemtype,
897 categorycode => undef,
900 hold_fulfillment_policy => 'any',
901 returnbranch => 'any'
906 # Test 6: Patron 3 can place hold
908 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
910 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
913 # Update default item rule to "hold group" for itemtype 2
914 Koha::CirculationRules->set_rules(
917 itemtype => $itemtype2->itemtype,
918 categorycode => undef,
921 hold_fulfillment_policy => 'any',
922 returnbranch => 'any'
927 # Test 7: Patron 3 cannot place hold
929 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
930 { status => 'branchNotInHoldGroup' },
931 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
934 # Cleanup default_branch_item_rules
935 $dbh->do('DELETE FROM circulation_rules');
937 # Insert branch item rule to "any" for itemtype 2 and library 2
938 Koha::CirculationRules->set_rules(
940 branchcode => $library2->branchcode,
941 itemtype => $itemtype2->itemtype,
942 categorycode => undef,
945 hold_fulfillment_policy => 'any',
946 returnbranch => 'any'
951 # Test 8: Patron 3 can place hold
953 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
955 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
958 # Update branch item rule to "hold group" for itemtype 2 and library 2
959 Koha::CirculationRules->set_rules(
961 branchcode => $library2->branchcode,
962 itemtype => $itemtype2->itemtype,
963 categorycode => undef,
966 hold_fulfillment_policy => 'any',
967 returnbranch => 'any'
972 # Test 9: Patron 3 cannot place hold
974 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
975 { status => 'branchNotInHoldGroup' },
976 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
979 $schema->storage->txn_rollback;
983 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
986 $schema->storage->txn_begin;
989 Koha::Holds->search->delete;
990 $dbh->do('DELETE FROM issues');
991 $dbh->do('DELETE FROM issuingrules');
993 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
994 VALUES (?, ?, ?, ?)},
998 $dbh->do('DELETE FROM circulation_rules');
1000 Koha::Items->search->delete;
1001 Koha::Biblios->search->delete;
1004 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1005 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1008 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1009 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1010 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
1012 # Create library groups hierarchy
1013 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1014 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1015 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1018 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1019 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1021 # Create 3 biblios with items
1022 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1023 my ( undef, undef, $itemnumber_1 ) = AddItem(
1024 { homebranch => $library1->branchcode,
1025 holdingbranch => $library1->branchcode
1027 $biblio_1->biblionumber
1029 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1030 my ( undef, undef, $itemnumber_2 ) = AddItem(
1031 { homebranch => $library2->branchcode,
1032 holdingbranch => $library2->branchcode
1034 $biblio_2->biblionumber
1036 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1037 my ( undef, undef, $itemnumber_3 ) = AddItem(
1038 { homebranch => $library1->branchcode,
1039 holdingbranch => $library1->branchcode
1041 $biblio_3->biblionumber
1044 # Test 1: Patron 3 can place hold
1046 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1048 'Patron can place hold if no circ_rules where defined'
1051 # Insert default circ rule of holds allowed only from local hold group for all libraries
1052 Koha::CirculationRules->set_rules(
1054 branchcode => undef,
1056 categorycode => undef,
1059 hold_fulfillment_policy => 'holdgroup',
1060 returnbranch => 'any'
1065 # Test 2: Patron 1 can place hold
1067 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1069 'Patron can place hold because pickup location is part of hold group'
1072 # Test 3: Patron 3 cannot place hold
1074 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1075 { status => 'pickupNotInHoldGroup' },
1076 'Patron cannot place hold because pickup location is not part of hold group'
1079 # Cleanup default_cirt_rules
1080 $dbh->do('DELETE FROM circulation_rules');
1082 # Insert default circ rule to "any" for library 2
1083 Koha::CirculationRules->set_rules(
1085 branchcode => $library2->branchcode,
1087 categorycode => undef,
1090 hold_fulfillment_policy => 'any',
1091 returnbranch => 'any'
1096 # Test 4: Patron 3 can place hold
1098 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1100 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1103 # Update default circ rule to "hold group" for library 2
1104 Koha::CirculationRules->set_rules(
1106 branchcode => $library2->branchcode,
1108 categorycode => undef,
1111 hold_fulfillment_policy => 'holdgroup',
1112 returnbranch => 'any'
1117 # Test 5: Patron 3 cannot place hold
1119 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1120 { status => 'pickupNotInHoldGroup' },
1121 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1124 # Cleanup default_branch_cirt_rules
1125 $dbh->do('DELETE FROM circulation_rules');
1127 # Insert default item rule to "any" for itemtype 2
1128 Koha::CirculationRules->set_rules(
1130 branchcode => undef,
1131 itemtype => $itemtype2->itemtype,
1132 categorycode => undef,
1135 hold_fulfillment_policy => 'any',
1136 returnbranch => 'any'
1141 # Test 6: Patron 3 can place hold
1143 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1145 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1148 # Update default item rule to "hold group" for itemtype 2
1149 Koha::CirculationRules->set_rules(
1151 branchcode => undef,
1152 itemtype => $itemtype2->itemtype,
1153 categorycode => undef,
1156 hold_fulfillment_policy => 'holdgroup',
1157 returnbranch => 'any'
1162 # Test 7: Patron 3 cannot place hold
1164 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1165 { status => 'pickupNotInHoldGroup' },
1166 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1169 # Cleanup default_branch_item_rules
1170 $dbh->do('DELETE FROM circulation_rules');
1172 # Insert branch item rule to "any" for itemtype 2 and library 2
1173 Koha::CirculationRules->set_rules(
1175 branchcode => $library2->branchcode,
1176 itemtype => $itemtype2->itemtype,
1177 categorycode => undef,
1180 hold_fulfillment_policy => 'any',
1181 returnbranch => 'any'
1186 # Test 8: Patron 3 can place hold
1188 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1190 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1193 # Update branch item rule to "hold group" for itemtype 2 and library 2
1194 Koha::CirculationRules->set_rules(
1196 branchcode => $library2->branchcode,
1197 itemtype => $itemtype2->itemtype,
1198 categorycode => undef,
1201 hold_fulfillment_policy => 'holdgroup',
1202 returnbranch => 'any'
1207 # Test 9: Patron 3 cannot place hold
1209 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1210 { status => 'pickupNotInHoldGroup' },
1211 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1214 $schema->storage->txn_rollback;