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 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
63 # Create some borrowers
65 foreach (1..$borrowers_count) {
66 my $borrowernumber = Koha::Patron->new({
67 firstname => 'my firstname',
68 surname => 'my surname ' . $_,
69 categorycode => $category->{categorycode},
70 branchcode => $branch_1,
71 })->store->borrowernumber;
72 push @borrowernumbers, $borrowernumber;
75 # Create five item level holds
76 foreach my $borrowernumber ( @borrowernumbers ) {
79 branchcode => $branch_1,
80 borrowernumber => $borrowernumber,
81 biblionumber => $biblio->biblionumber,
82 priority => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
83 itemnumber => $itemnumber,
88 my $holds = $biblio->holds;
89 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
90 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
91 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
92 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
93 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
94 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
96 my $item = Koha::Items->find( $itemnumber );
97 $holds = $item->current_holds;
98 my $first_hold = $holds->next;
99 my $reservedate = $first_hold->reservedate;
100 my $borrowernumber = $first_hold->borrowernumber;
101 my $branch_1code = $first_hold->branchcode;
102 my $reserve_id = $first_hold->reserve_id;
103 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
104 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
105 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
106 ok($reserve_id, "Test holds_placed_today()");
108 my $hold = Koha::Holds->find( $reserve_id );
109 ok( $hold, "Koha::Holds found the hold" );
110 my $hold_biblio = $hold->biblio();
111 ok( $hold_biblio, "Got biblio using biblio() method" );
112 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
113 my $hold_item = $hold->item();
114 ok( $hold_item, "Got item using item() method" );
115 ok( $hold_item == $hold->item(), "item method returns stashed item" );
116 my $hold_branch = $hold->branch();
117 ok( $hold_branch, "Got branch using branch() method" );
118 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
119 my $hold_found = $hold->found();
120 $hold->set({ found => 'W'})->store();
121 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
122 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
124 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
125 $holds = $patron->holds;
126 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
129 $holds = $item->current_holds;
130 $first_hold = $holds->next;
131 $borrowernumber = $first_hold->borrowernumber;
132 $branch_1code = $first_hold->branchcode;
133 $reserve_id = $first_hold->reserve_id;
136 reserve_id => $reserve_id,
138 branchcode => $branch_1,
139 itemnumber => $itemnumber,
140 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
143 $hold = Koha::Holds->find( $reserve_id );
144 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
145 ok( $hold->suspend, "Test ModReserve, suspend hold" );
146 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
148 ModReserve({ # call without reserve_id
150 biblionumber => $biblio->biblionumber,
151 itemnumber => $itemnumber,
152 borrowernumber => $borrowernumber,
154 $hold = Koha::Holds->find( $reserve_id );
155 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
157 ToggleSuspend( $reserve_id );
158 $hold = Koha::Holds->find( $reserve_id );
159 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
161 ToggleSuspend( $reserve_id, '2012-01-01' );
162 $hold = Koha::Holds->find( $reserve_id );
163 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
165 AutoUnsuspendReserves();
166 $hold = Koha::Holds->find( $reserve_id );
167 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
170 borrowernumber => $borrowernumber,
171 biblionumber => $biblio->biblionumber,
173 suspend_until => '2012-01-01',
175 $hold = Koha::Holds->find( $reserve_id );
176 is( $hold->suspend, 1, "Test SuspendAll()" );
177 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
180 borrowernumber => $borrowernumber,
181 biblionumber => $biblio->biblionumber,
184 $hold = Koha::Holds->find( $reserve_id );
185 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
186 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
188 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
191 branchcode => $branch_1,
192 borrowernumber => $borrowernumbers[0],
193 biblionumber => $biblio->biblionumber,
197 $patron = Koha::Patrons->find( $borrowernumber );
198 $holds = $patron->holds;
199 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
200 ModReserveMinusPriority( $itemnumber, $reserveid );
201 $holds = $patron->holds;
202 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
204 $holds = $biblio->holds;
205 $hold = $holds->next;
206 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
207 $hold = Koha::Holds->find( $reserveid );
208 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
210 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
211 $hold = Koha::Holds->find( $reserveid );
212 is( $hold->priority, '2', "Test AlterPriority(), move down" );
214 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
215 $hold = Koha::Holds->find( $reserveid );
216 is( $hold->priority, '1', "Test AlterPriority(), move up" );
218 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
219 $hold = Koha::Holds->find( $reserveid );
220 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
222 # Regression test for bug 2394
224 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
225 # a patron is not permittedo to request an item whose homebranch (i.e.,
226 # owner of the item) is different from the patron's own library.
227 # However, if canreservefromotherbranches is turned ON, the patron can
228 # create such hold requests.
230 # Note that canreservefromotherbranches has no effect if
231 # IndependentBranches is OFF.
233 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
234 my $foreign_itemnumber = $builder->build_sample_item({ library => $branch_2, biblionumber => $foreign_biblio->biblionumber })->itemnumber;
235 Koha::CirculationRules->set_rules(
237 categorycode => undef,
241 reservesallowed => 25,
242 holds_per_record => 99,
246 Koha::CirculationRules->set_rules(
248 categorycode => undef,
250 itemtype => 'CANNOT',
252 reservesallowed => 0,
253 holds_per_record => 99,
258 # make sure some basic sysprefs are set
259 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
260 t::lib::Mocks::mock_preference('item-level_itypes', 1);
262 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
263 t::lib::Mocks::mock_preference('IndependentBranches', 0);
266 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
267 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
270 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
271 t::lib::Mocks::mock_preference('IndependentBranches', 1);
272 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
274 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
275 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
278 # ... unless canreservefromotherbranches is ON
279 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
281 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
282 '... unless canreservefromotherbranches is ON (bug 2394)'
286 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
287 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
288 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
289 my $reserveid1 = AddReserve(
291 branchcode => $branch_1,
292 borrowernumber => $borrowernumbers[0],
293 biblionumber => $biblio->biblionumber,
298 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
299 my $reserveid2 = AddReserve(
301 branchcode => $branch_1,
302 borrowernumber => $borrowernumbers[1],
303 biblionumber => $biblio->biblionumber,
308 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
309 my $reserveid3 = AddReserve(
311 branchcode => $branch_1,
312 borrowernumber => $borrowernumbers[2],
313 biblionumber => $biblio->biblionumber,
318 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
319 my $hold3 = Koha::Holds->find( $reserveid3 );
320 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
321 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
322 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
323 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
326 Koha::Items->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
327 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
328 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
329 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
331 $hold = Koha::Hold->new(
333 borrowernumber => $borrowernumbers[0],
334 itemnumber => $itemnumber,
335 biblionumber => $biblio->biblionumber,
338 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
340 "Patron cannot place a second item level hold for a given item" );
343 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
344 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
345 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
347 # Regression test for bug 9532
348 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
349 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
352 branchcode => $branch_1,
353 borrowernumber => $borrowernumbers[0],
354 biblionumber => $biblio->biblionumber,
359 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber)->{status}, 'tooManyReserves',
360 "cannot request item if policy that matches on item-level item type forbids it"
363 $item->itype('CAN')->store;
365 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber)->{status} eq 'OK',
366 "can request item if policy that matches on item type allows it"
369 t::lib::Mocks::mock_preference('item-level_itypes', 0);
370 $item->itype(undef)->store;
372 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber)->{status} eq 'tooManyReserves',
373 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
377 # Test branch item rules
379 $dbh->do('DELETE FROM circulation_rules');
380 Koha::CirculationRules->set_rules(
382 categorycode => undef,
386 reservesallowed => 25,
387 holds_per_record => 99,
391 Koha::CirculationRules->set_rules(
393 branchcode => $branch_1,
394 itemtype => 'CANNOT',
397 returnbranch => 'homebranch',
401 Koha::CirculationRules->set_rules(
403 branchcode => $branch_1,
407 returnbranch => 'homebranch',
411 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
412 $itemnumber = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber})->itemnumber;
413 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
414 "CanItemBeReserved should return 'notReservable'");
416 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
417 $itemnumber = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber})->itemnumber;
418 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
419 'cannotReserveFromOtherBranches',
420 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
421 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
422 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
424 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
426 $itemnumber = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber})->itemnumber;
427 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
428 "CanItemBeReserved should return 'OK'");
431 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
432 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
434 $dbh->do('DELETE FROM reserves');
435 $dbh->do('DELETE FROM issues');
436 $dbh->do('DELETE FROM items');
437 $dbh->do('DELETE FROM biblio');
439 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
440 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
442 Koha::CirculationRules->set_rules(
444 categorycode => undef,
448 reservesallowed => 1,
449 holds_per_record => 99,
453 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
454 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
456 my $res_id = AddReserve(
458 branchcode => $branch_1,
459 borrowernumber => $borrowernumbers[0],
460 biblionumber => $biblio->biblionumber,
465 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
466 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
468 #results should be the same for both ReservesControlBranch settings
469 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
470 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
471 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
472 #reset for further tests
473 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
475 subtest 'Test max_holds per library/patron category' => sub {
478 $dbh->do('DELETE FROM reserves');
480 $biblio = $builder->build_sample_biblio;
481 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
482 Koha::CirculationRules->set_rules(
484 categorycode => undef,
486 itemtype => $biblio->itemtype,
488 reservesallowed => 99,
489 holds_per_record => 99,
497 branchcode => $branch_1,
498 borrowernumber => $borrowernumbers[0],
499 biblionumber => $biblio->biblionumber,
506 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
507 is( $count, 3, 'Patron now has 3 holds' );
509 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
510 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
512 my $rule_all = Koha::CirculationRules->set_rule(
514 categorycode => $category->{categorycode},
516 rule_name => 'max_holds',
521 my $rule_branch = Koha::CirculationRules->set_rule(
523 branchcode => $branch_1,
524 categorycode => $category->{categorycode},
525 rule_name => 'max_holds',
530 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
531 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
533 $rule_branch->delete();
535 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
536 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
539 $rule_branch->rule_value(3);
540 $rule_branch->store();
542 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
543 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
545 $rule_branch->rule_value(5);
546 $rule_branch->update();
547 $rule_branch->rule_value(5);
548 $rule_branch->store();
550 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
551 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
554 subtest 'Pickup location availability tests' => sub {
557 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
558 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
559 #Add a default rule to allow some holds
561 Koha::CirculationRules->set_rules(
564 categorycode => undef,
567 reservesallowed => 25,
568 holds_per_record => 99,
572 my $item = Koha::Items->find($itemnumber);
573 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
574 my $library = Koha::Libraries->find($branch_to);
575 $library->pickup_location('1')->store;
576 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
578 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
579 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
581 $library->pickup_location('1')->store;
582 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
583 'OK', 'Library is a pickup location');
585 my $limit = Koha::Item::Transfer::Limit->new({
586 fromBranch => $item->holdingbranch,
587 toBranch => $branch_to,
588 itemtype => $item->effective_itemtype,
590 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
591 'cannotBeTransferred', 'Item cannot be transferred');
594 $library->pickup_location('0')->store;
595 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
596 'libraryNotPickupLocation', 'Library is not a pickup location');
597 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
598 'libraryNotFound', 'Cannot set unknown library as pickup location');
601 $schema->storage->txn_rollback;
603 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
607 $schema->storage->txn_begin;
609 Koha::Holds->search->delete;
610 $dbh->do('DELETE FROM issues');
611 Koha::Items->search->delete;
612 Koha::Biblios->search->delete;
614 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
615 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
616 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
618 # Create 3 biblios with items
619 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
620 my $itemnumber_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber})->itemnumber;
621 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
622 my $itemnumber_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber})->itemnumber;
623 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
624 my $itemnumber_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber})->itemnumber;
626 Koha::CirculationRules->search->delete;
627 Koha::CirculationRules->set_rules(
631 itemtype => $itemtype->itemtype,
633 reservesallowed => 1,
634 holds_per_record => 99,
641 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
643 'Patron can reserve item with hold limit of 1, no holds placed'
648 branchcode => $library->branchcode,
649 borrowernumber => $patron->borrowernumber,
650 biblionumber => $biblio_1->biblionumber,
656 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
657 { status => 'tooManyReserves', limit => 1 },
658 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
661 # Raise reservesallowed to avoid tooManyReserves from it
662 Koha::CirculationRules->set_rule(
667 itemtype => $itemtype->itemtype,
668 rule_name => 'reservesallowed',
674 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
676 'Patron can reserve item with 2 reserves daily cap'
679 # Add a second reserve
680 my $res_id = AddReserve(
682 branchcode => $library->branchcode,
683 borrowernumber => $patron->borrowernumber,
684 biblionumber => $biblio_2->biblionumber,
689 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
690 { status => 'tooManyReservesToday', limit => 2 },
691 'Patron cannot a third item with 2 reserves daily cap'
694 # Update last hold so reservedate is in the past, so 2 holds, but different day
695 $hold = Koha::Holds->find($res_id);
696 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
697 $hold->reservedate($yesterday)->store;
700 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
702 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
705 # Set holds_per_day to 0
706 Koha::CirculationRules->set_rule(
711 itemtype => $itemtype->itemtype,
712 rule_name => 'holds_per_day',
718 # Delete existing holds
719 Koha::Holds->search->delete;
721 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
722 { status => 'tooManyReservesToday', limit => 0 },
723 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
726 Koha::CirculationRules->set_rule(
731 itemtype => $itemtype->itemtype,
732 rule_name => 'holds_per_day',
737 Koha::Holds->search->delete;
739 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
741 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
745 branchcode => $library->branchcode,
746 borrowernumber => $patron->borrowernumber,
747 biblionumber => $biblio_1->biblionumber,
753 branchcode => $library->branchcode,
754 borrowernumber => $patron->borrowernumber,
755 biblionumber => $biblio_2->biblionumber,
761 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
763 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
767 branchcode => $library->branchcode,
768 borrowernumber => $patron->borrowernumber,
769 biblionumber => $biblio_3->biblionumber,
774 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
775 { status => 'tooManyReserves', limit => 3 },
776 'Unlimited daily holds, but reached reservesallowed'
778 #results should be the same for both ReservesControlBranch settings
779 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
781 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
782 { status => 'tooManyReserves', limit => 3 },
783 'Unlimited daily holds, but reached reservesallowed'
786 $schema->storage->txn_rollback;
789 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
792 $schema->storage->txn_begin;
795 Koha::Holds->search->delete;
796 $dbh->do('DELETE FROM issues');
797 Koha::CirculationRules->set_rule(
800 categorycode => undef,
802 rule_name => 'reservesallowed',
807 Koha::Items->search->delete;
808 Koha::Biblios->search->delete;
811 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
812 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
815 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
816 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
817 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
819 # Create library groups hierarchy
820 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
821 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
822 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
825 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
826 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
828 # Create 3 biblios with items
829 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
830 my ( undef, undef, $itemnumber_1 ) = AddItem(
831 { homebranch => $library1->branchcode,
832 holdingbranch => $library1->branchcode
834 $biblio_1->biblionumber
836 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
837 my ( undef, undef, $itemnumber_2 ) = AddItem(
838 { homebranch => $library2->branchcode,
839 holdingbranch => $library2->branchcode
841 $biblio_2->biblionumber
843 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
844 my ( undef, undef, $itemnumber_3 ) = AddItem(
845 { homebranch => $library1->branchcode,
846 holdingbranch => $library1->branchcode
848 $biblio_3->biblionumber
851 # Test 1: Patron 3 can place hold
853 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
855 'Patron can place hold if no circ_rules where defined'
858 # Insert default circ rule of holds allowed only from local hold group for all libraries
859 Koha::CirculationRules->set_rules(
865 hold_fulfillment_policy => 'any',
866 returnbranch => 'any'
871 # Test 2: Patron 1 can place hold
873 CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
875 'Patron can place hold because patron\'s home library is part of hold group'
878 # Test 3: Patron 3 cannot place hold
880 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
881 { status => 'branchNotInHoldGroup' },
882 'Patron cannot place hold because patron\'s home library is not part of hold group'
885 # Insert default circ rule to "any" for library 2
886 Koha::CirculationRules->set_rules(
888 branchcode => $library2->branchcode,
892 hold_fulfillment_policy => 'any',
893 returnbranch => 'any'
898 # Test 4: Patron 3 can place hold
900 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
902 'Patron can place hold if holdallowed is set to "any" for library 2'
905 # Update default circ rule to "hold group" for library 2
906 Koha::CirculationRules->set_rules(
908 branchcode => $library2->branchcode,
912 hold_fulfillment_policy => 'any',
913 returnbranch => 'any'
918 # Test 5: Patron 3 cannot place hold
920 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
921 { status => 'branchNotInHoldGroup' },
922 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
925 # Insert default item rule to "any" for itemtype 2
926 Koha::CirculationRules->set_rules(
928 branchcode => $library2->branchcode,
929 itemtype => $itemtype2->itemtype,
932 hold_fulfillment_policy => 'any',
933 returnbranch => 'any'
938 # Test 6: Patron 3 can place hold
940 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
942 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
945 # Update default item rule to "hold group" for itemtype 2
946 Koha::CirculationRules->set_rules(
948 branchcode => $library2->branchcode,
949 itemtype => $itemtype2->itemtype,
952 hold_fulfillment_policy => 'any',
953 returnbranch => 'any'
958 # Test 7: Patron 3 cannot place hold
960 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
961 { status => 'branchNotInHoldGroup' },
962 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
965 # Insert branch item rule to "any" for itemtype 2 and library 2
966 Koha::CirculationRules->set_rules(
968 branchcode => $library2->branchcode,
969 itemtype => $itemtype2->itemtype,
972 hold_fulfillment_policy => 'any',
973 returnbranch => 'any'
978 # Test 8: Patron 3 can place hold
980 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
982 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
985 # Update branch item rule to "hold group" for itemtype 2 and library 2
986 Koha::CirculationRules->set_rules(
988 branchcode => $library2->branchcode,
989 itemtype => $itemtype2->itemtype,
992 hold_fulfillment_policy => 'any',
993 returnbranch => 'any'
998 # Test 9: Patron 3 cannot place hold
1000 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1001 { status => 'branchNotInHoldGroup' },
1002 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1005 $schema->storage->txn_rollback;
1009 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1012 $schema->storage->txn_begin;
1015 Koha::Holds->search->delete;
1016 $dbh->do('DELETE FROM issues');
1017 Koha::CirculationRules->set_rule(
1019 branchcode => undef,
1020 categorycode => undef,
1022 rule_name => 'reservesallowed',
1027 Koha::Items->search->delete;
1028 Koha::Biblios->search->delete;
1031 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1032 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1035 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1036 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1037 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1039 # Create library groups hierarchy
1040 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1041 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1042 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1045 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1046 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1048 # Create 3 biblios with items
1049 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1050 my ( undef, undef, $itemnumber_1 ) = AddItem(
1051 { homebranch => $library1->branchcode,
1052 holdingbranch => $library1->branchcode
1054 $biblio_1->biblionumber
1056 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1057 my ( undef, undef, $itemnumber_2 ) = AddItem(
1058 { homebranch => $library2->branchcode,
1059 holdingbranch => $library2->branchcode
1061 $biblio_2->biblionumber
1063 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1064 my ( undef, undef, $itemnumber_3 ) = AddItem(
1065 { homebranch => $library1->branchcode,
1066 holdingbranch => $library1->branchcode
1068 $biblio_3->biblionumber
1071 # Test 1: Patron 3 can place hold
1073 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1075 'Patron can place hold if no circ_rules where defined'
1078 # Insert default circ rule of holds allowed only from local hold group for all libraries
1079 Koha::CirculationRules->set_rules(
1081 branchcode => undef,
1085 hold_fulfillment_policy => 'holdgroup',
1086 returnbranch => 'any'
1091 # Test 2: Patron 1 can place hold
1093 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1095 'Patron can place hold because pickup location is part of hold group'
1098 # Test 3: Patron 3 cannot place hold
1100 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1101 { status => 'pickupNotInHoldGroup' },
1102 'Patron cannot place hold because pickup location is not part of hold group'
1105 # Insert default circ rule to "any" for library 2
1106 Koha::CirculationRules->set_rules(
1108 branchcode => $library2->branchcode,
1112 hold_fulfillment_policy => 'any',
1113 returnbranch => 'any'
1118 # Test 4: Patron 3 can place hold
1120 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1122 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1125 # Update default circ rule to "hold group" for library 2
1126 Koha::CirculationRules->set_rules(
1128 branchcode => $library2->branchcode,
1132 hold_fulfillment_policy => 'holdgroup',
1133 returnbranch => 'any'
1138 # Test 5: Patron 3 cannot place hold
1140 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1141 { status => 'pickupNotInHoldGroup' },
1142 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1145 # Insert default item rule to "any" for itemtype 2
1146 Koha::CirculationRules->set_rules(
1148 branchcode => $library2->branchcode,
1149 itemtype => $itemtype2->itemtype,
1152 hold_fulfillment_policy => 'any',
1153 returnbranch => 'any'
1158 # Test 6: Patron 3 can place hold
1160 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1162 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1165 # Update default item rule to "hold group" for itemtype 2
1166 Koha::CirculationRules->set_rules(
1168 branchcode => $library2->branchcode,
1169 itemtype => $itemtype2->itemtype,
1172 hold_fulfillment_policy => 'holdgroup',
1173 returnbranch => 'any'
1178 # Test 7: Patron 3 cannot place hold
1180 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1181 { status => 'pickupNotInHoldGroup' },
1182 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1185 # Insert branch item rule to "any" for itemtype 2 and library 2
1186 Koha::CirculationRules->set_rules(
1188 branchcode => $library2->branchcode,
1189 itemtype => $itemtype2->itemtype,
1192 hold_fulfillment_policy => 'any',
1193 returnbranch => 'any'
1198 # Test 8: Patron 3 can place hold
1200 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1202 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1205 # Update branch item rule to "hold group" for itemtype 2 and library 2
1206 Koha::CirculationRules->set_rules(
1208 branchcode => $library2->branchcode,
1209 itemtype => $itemtype2->itemtype,
1212 hold_fulfillment_policy => 'holdgroup',
1213 returnbranch => 'any'
1218 # Test 9: Patron 3 cannot place hold
1220 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1221 { status => 'pickupNotInHoldGroup' },
1222 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1225 $schema->storage->txn_rollback;