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({ itemtype => 'TEST' });
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 => $testitemtype,
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 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
540 VALUES (?, ?, ?, ?, ?)},
542 '*', '*', '*', 25, 99
544 my $item = Koha::Items->find($itemnumber);
545 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
546 my $library = Koha::Libraries->find($branch_to);
547 $library->pickup_location('1')->store;
548 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
550 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
551 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
553 $library->pickup_location('1')->store;
554 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
555 'OK', 'Library is a pickup location');
557 my $limit = Koha::Item::Transfer::Limit->new({
558 fromBranch => $item->holdingbranch,
559 toBranch => $branch_to,
560 itemtype => $item->effective_itemtype,
562 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
563 'cannotBeTransferred', 'Item cannot be transferred');
566 $library->pickup_location('0')->store;
567 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
568 'libraryNotPickupLocation', 'Library is not a pickup location');
569 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
570 'libraryNotFound', 'Cannot set unknown library as pickup location');
573 $schema->storage->txn_rollback;
575 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
579 $schema->storage->txn_begin;
581 Koha::Holds->search->delete;
582 $dbh->do('DELETE FROM issues');
583 $dbh->do('DELETE FROM issuingrules');
584 $dbh->do('DELETE FROM circulation_rules');
585 Koha::Items->search->delete;
586 Koha::Biblios->search->delete;
588 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
589 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
590 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
592 # Create 3 biblios with items
593 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
594 my ( undef, undef, $itemnumber_1 ) = AddItem(
595 { homebranch => $library->branchcode,
596 holdingbranch => $library->branchcode
598 $biblio_1->biblionumber
600 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
601 my ( undef, undef, $itemnumber_2 ) = AddItem(
602 { homebranch => $library->branchcode,
603 holdingbranch => $library->branchcode
605 $biblio_2->biblionumber
607 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
608 my ( undef, undef, $itemnumber_3 ) = AddItem(
609 { homebranch => $library->branchcode,
610 holdingbranch => $library->branchcode
612 $biblio_3->biblionumber
615 Koha::CirculationRules->search->delete;
616 Koha::CirculationRules->set_rules(
620 itemtype => $itemtype->itemtype,
622 reservesallowed => 1,
623 holds_per_record => 99,
630 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
632 'Patron can reserve item with hold limit of 1, no holds placed'
635 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
638 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
639 { status => 'tooManyReserves', limit => 1 },
640 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
643 # Raise reservesallowed to avoid tooManyReserves from it
644 $issuingrule->set( { reservesallowed => 3 } )->store;
647 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
649 'Patron can reserve item with 2 reserves daily cap'
652 # Add a second reserve
653 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
655 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
656 { status => 'tooManyReservesToday', limit => 2 },
657 'Patron cannot a third item with 2 reserves daily cap'
660 # Update last hold so reservedate is in the past, so 2 holds, but different day
661 $hold = Koha::Holds->find($res_id);
662 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
663 $hold->reservedate($yesterday)->store;
666 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
668 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
671 # Set holds_per_day to 0
672 $issuingrule->set( { holds_per_day => 0 } )->store;
674 # Delete existing holds
675 Koha::Holds->search->delete;
677 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
678 { status => 'tooManyReservesToday', limit => 0 },
679 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
682 $issuingrule->set( { holds_per_day => undef } )->store;
683 Koha::Holds->search->delete;
685 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
687 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
689 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
690 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
692 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
694 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
696 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
698 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
699 { status => 'tooManyReserves', limit => 3 },
700 'Unlimited daily holds, but reached reservesallowed'
702 #results should be the same for both ReservesControlBranch settings
703 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
705 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
706 { status => 'tooManyReserves', limit => 3 },
707 'Unlimited daily holds, but reached reservesallowed'
710 $schema->storage->txn_rollback;
713 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
716 $schema->storage->txn_begin;
719 Koha::Holds->search->delete;
720 $dbh->do('DELETE FROM issues');
721 $dbh->do('DELETE FROM issuingrules');
723 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
724 VALUES (?, ?, ?, ?)},
728 $dbh->do('DELETE FROM circulation_rules');
730 Koha::Items->search->delete;
731 Koha::Biblios->search->delete;
734 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
735 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
738 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
739 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
740 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
742 # Create library groups hierarchy
743 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
744 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
745 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
748 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
749 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
751 # Create 3 biblios with items
752 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
753 my ( undef, undef, $itemnumber_1 ) = AddItem(
754 { homebranch => $library1->branchcode,
755 holdingbranch => $library1->branchcode
757 $biblio_1->biblionumber
759 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
760 my ( undef, undef, $itemnumber_2 ) = AddItem(
761 { homebranch => $library2->branchcode,
762 holdingbranch => $library2->branchcode
764 $biblio_2->biblionumber
766 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
767 my ( undef, undef, $itemnumber_3 ) = AddItem(
768 { homebranch => $library1->branchcode,
769 holdingbranch => $library1->branchcode
771 $biblio_3->biblionumber
774 # Test 1: Patron 3 can place hold
776 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
778 'Patron can place hold if no circ_rules where defined'
781 # Insert default circ rule of holds allowed only from local hold group for all libraries
782 Koha::CirculationRules->set_rules(
786 categorycode => undef,
789 hold_fulfillment_policy => 'any',
790 returnbranch => 'any'
795 # Test 2: Patron 1 can place hold
797 CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
799 'Patron can place hold because patron\'s home library is part of hold group'
802 # Test 3: Patron 3 cannot place hold
804 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
805 { status => 'branchNotInHoldGroup' },
806 'Patron cannot place hold because patron\'s home library is not part of hold group'
809 # Cleanup default_cirt_rules
810 $dbh->do('DELETE FROM circulation_rules');
812 # Insert default circ rule to "any" for library 2
813 Koha::CirculationRules->set_rules(
815 branchcode => $library2->branchcode,
817 categorycode => undef,
820 hold_fulfillment_policy => 'any',
821 returnbranch => 'any'
826 # Test 4: Patron 3 can place hold
828 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
830 'Patron can place hold if holdallowed is set to "any" for library 2'
833 # Update default circ rule to "hold group" for library 2
834 Koha::CirculationRules->set_rules(
836 branchcode => $library2->branchcode,
838 categorycode => undef,
841 hold_fulfillment_policy => 'any',
842 returnbranch => 'any'
847 # Test 5: Patron 3 cannot place hold
849 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
850 { status => 'branchNotInHoldGroup' },
851 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
854 # Cleanup default_branch_cirt_rules
855 $dbh->do('DELETE FROM circulation_rules');
857 # Insert default item rule to "any" for itemtype 2
858 Koha::CirculationRules->set_rules(
861 itemtype => $itemtype2->itemtype,
862 categorycode => undef,
865 hold_fulfillment_policy => 'any',
866 returnbranch => 'any'
871 # Test 6: Patron 3 can place hold
873 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
875 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
878 # Update default item rule to "hold group" for itemtype 2
879 Koha::CirculationRules->set_rules(
882 itemtype => $itemtype2->itemtype,
883 categorycode => undef,
886 hold_fulfillment_policy => 'any',
887 returnbranch => 'any'
892 # Test 7: Patron 3 cannot place hold
894 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
895 { status => 'branchNotInHoldGroup' },
896 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
899 # Cleanup default_branch_item_rules
900 $dbh->do('DELETE FROM circulation_rules');
902 # Insert branch item rule to "any" for itemtype 2 and library 2
903 Koha::CirculationRules->set_rules(
905 branchcode => $library2->branchcode,
906 itemtype => $itemtype2->itemtype,
907 categorycode => undef,
910 hold_fulfillment_policy => 'any',
911 returnbranch => 'any'
916 # Test 8: Patron 3 can place hold
918 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
920 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
923 # Update branch item rule to "hold group" for itemtype 2 and library 2
924 Koha::CirculationRules->set_rules(
926 branchcode => $library2->branchcode,
927 itemtype => $itemtype2->itemtype,
928 categorycode => undef,
931 hold_fulfillment_policy => 'any',
932 returnbranch => 'any'
937 # Test 9: Patron 3 cannot place hold
939 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
940 { status => 'branchNotInHoldGroup' },
941 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
944 $schema->storage->txn_rollback;
948 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
951 $schema->storage->txn_begin;
954 Koha::Holds->search->delete;
955 $dbh->do('DELETE FROM issues');
956 $dbh->do('DELETE FROM issuingrules');
958 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
959 VALUES (?, ?, ?, ?)},
963 $dbh->do('DELETE FROM circulation_rules');
965 Koha::Items->search->delete;
966 Koha::Biblios->search->delete;
969 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
970 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
973 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
974 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
975 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
977 # Create library groups hierarchy
978 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
979 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
980 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
983 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
984 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
986 # Create 3 biblios with items
987 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
988 my ( undef, undef, $itemnumber_1 ) = AddItem(
989 { homebranch => $library1->branchcode,
990 holdingbranch => $library1->branchcode
992 $biblio_1->biblionumber
994 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
995 my ( undef, undef, $itemnumber_2 ) = AddItem(
996 { homebranch => $library2->branchcode,
997 holdingbranch => $library2->branchcode
999 $biblio_2->biblionumber
1001 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1002 my ( undef, undef, $itemnumber_3 ) = AddItem(
1003 { homebranch => $library1->branchcode,
1004 holdingbranch => $library1->branchcode
1006 $biblio_3->biblionumber
1009 # Test 1: Patron 3 can place hold
1011 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1013 'Patron can place hold if no circ_rules where defined'
1016 # Insert default circ rule of holds allowed only from local hold group for all libraries
1017 Koha::CirculationRules->set_rules(
1019 branchcode => undef,
1021 categorycode => undef,
1024 hold_fulfillment_policy => 'holdgroup',
1025 returnbranch => 'any'
1030 # Test 2: Patron 1 can place hold
1032 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1034 'Patron can place hold because pickup location is part of hold group'
1037 # Test 3: Patron 3 cannot place hold
1039 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1040 { status => 'pickupNotInHoldGroup' },
1041 'Patron cannot place hold because pickup location is not part of hold group'
1044 # Cleanup default_cirt_rules
1045 $dbh->do('DELETE FROM circulation_rules');
1047 # Insert default circ rule to "any" for library 2
1048 Koha::CirculationRules->set_rules(
1050 branchcode => $library2->branchcode,
1052 categorycode => undef,
1055 hold_fulfillment_policy => 'any',
1056 returnbranch => 'any'
1061 # Test 4: Patron 3 can place hold
1063 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1065 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1068 # Update default circ rule to "hold group" for library 2
1069 Koha::CirculationRules->set_rules(
1071 branchcode => $library2->branchcode,
1073 categorycode => undef,
1076 hold_fulfillment_policy => 'holdgroup',
1077 returnbranch => 'any'
1082 # Test 5: Patron 3 cannot place hold
1084 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1085 { status => 'pickupNotInHoldGroup' },
1086 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1089 # Cleanup default_branch_cirt_rules
1090 $dbh->do('DELETE FROM circulation_rules');
1092 # Insert default item rule to "any" for itemtype 2
1093 Koha::CirculationRules->set_rules(
1095 branchcode => undef,
1096 itemtype => $itemtype2->itemtype,
1097 categorycode => undef,
1100 hold_fulfillment_policy => 'any',
1101 returnbranch => 'any'
1106 # Test 6: Patron 3 can place hold
1108 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1110 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1113 # Update default item rule to "hold group" for itemtype 2
1114 Koha::CirculationRules->set_rules(
1116 branchcode => undef,
1117 itemtype => $itemtype2->itemtype,
1118 categorycode => undef,
1121 hold_fulfillment_policy => 'holdgroup',
1122 returnbranch => 'any'
1127 # Test 7: Patron 3 cannot place hold
1129 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1130 { status => 'pickupNotInHoldGroup' },
1131 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1134 # Cleanup default_branch_item_rules
1135 $dbh->do('DELETE FROM circulation_rules');
1137 # Insert branch item rule to "any" for itemtype 2 and library 2
1138 Koha::CirculationRules->set_rules(
1140 branchcode => $library2->branchcode,
1141 itemtype => $itemtype2->itemtype,
1142 categorycode => undef,
1145 hold_fulfillment_policy => 'any',
1146 returnbranch => 'any'
1151 # Test 8: Patron 3 can place hold
1153 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1155 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1158 # Update branch item rule to "hold group" for itemtype 2 and library 2
1159 Koha::CirculationRules->set_rules(
1161 branchcode => $library2->branchcode,
1162 itemtype => $itemtype2->itemtype,
1163 categorycode => undef,
1166 hold_fulfillment_policy => 'holdgroup',
1167 returnbranch => 'any'
1172 # Test 9: Patron 3 cannot place hold
1174 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1175 { status => 'pickupNotInHoldGroup' },
1176 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1179 $schema->storage->txn_rollback;