6 use t::lib::TestBuilder;
10 use Test::More tests => 68;
20 use Koha::CirculationRules;
22 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::Item::Transfer::Limits;
28 use Koha::Library::Groups;
33 use lib $FindBin::Bin;
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
39 my $builder = t::lib::TestBuilder->new();
40 my $dbh = C4::Context->dbh;
42 # Create two random branches
43 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
44 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
46 my $category = $builder->build({
49 BlockExpiredPatronOpacActions => -1,
53 my $borrowers_count = 5;
55 $dbh->do('DELETE FROM itemtypes');
56 $dbh->do('DELETE FROM reserves');
57 $dbh->do('DELETE FROM circulation_rules');
58 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
59 $insert_sth->execute('CAN');
60 $insert_sth->execute('CANNOT');
61 $insert_sth->execute('DUMMY');
62 $insert_sth->execute('ONLY1');
64 # Setup Test------------------------
65 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
67 # Create item instance for testing.
68 my $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
70 # Create some borrowers
72 foreach (1..$borrowers_count) {
73 my $borrowernumber = Koha::Patron->new({
74 firstname => 'my firstname',
75 surname => 'my surname ' . $_,
76 categorycode => $category->{categorycode},
77 branchcode => $branch_1,
78 })->store->borrowernumber;
79 push @borrowernumbers, $borrowernumber;
82 # Create five item level holds
83 foreach my $borrowernumber ( @borrowernumbers ) {
86 branchcode => $branch_1,
87 borrowernumber => $borrowernumber,
88 biblionumber => $biblio->biblionumber,
89 priority => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
90 itemnumber => $itemnumber,
95 my $holds = $biblio->holds;
96 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
97 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
98 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
99 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
100 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
101 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
103 my $item = Koha::Items->find( $itemnumber );
104 $holds = $item->current_holds;
105 my $first_hold = $holds->next;
106 my $reservedate = $first_hold->reservedate;
107 my $borrowernumber = $first_hold->borrowernumber;
108 my $branch_1code = $first_hold->branchcode;
109 my $reserve_id = $first_hold->reserve_id;
110 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
111 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
112 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
113 ok($reserve_id, "Test holds_placed_today()");
115 my $hold = Koha::Holds->find( $reserve_id );
116 ok( $hold, "Koha::Holds found the hold" );
117 my $hold_biblio = $hold->biblio();
118 ok( $hold_biblio, "Got biblio using biblio() method" );
119 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
120 my $hold_item = $hold->item();
121 ok( $hold_item, "Got item using item() method" );
122 ok( $hold_item == $hold->item(), "item method returns stashed item" );
123 my $hold_branch = $hold->branch();
124 ok( $hold_branch, "Got branch using branch() method" );
125 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
126 my $hold_found = $hold->found();
127 $hold->set({ found => 'W'})->store();
128 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
129 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
131 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
132 $holds = $patron->holds;
133 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
135 my $expired_borrowernumber = Koha::Patron->new({
136 firstname => 'Expired',
138 categorycode => $category->{categorycode},
139 branchcode => $branch_1,
140 dateexpiry => '2000-01-01',
141 })->store->borrowernumber;
143 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
145 CanItemBeReserved($expired_borrowernumber, $itemnumber)->{status} eq 'patronExpired',
146 'Expired patron cannot reserve'
149 $holds = $item->current_holds;
150 $first_hold = $holds->next;
151 $borrowernumber = $first_hold->borrowernumber;
152 $branch_1code = $first_hold->branchcode;
153 $reserve_id = $first_hold->reserve_id;
156 reserve_id => $reserve_id,
158 branchcode => $branch_1,
159 itemnumber => $itemnumber,
160 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
163 $hold = Koha::Holds->find( $reserve_id );
164 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
165 ok( $hold->suspend, "Test ModReserve, suspend hold" );
166 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
168 ModReserve({ # call without reserve_id
170 biblionumber => $biblio->biblionumber,
171 itemnumber => $itemnumber,
172 borrowernumber => $borrowernumber,
174 $hold = Koha::Holds->find( $reserve_id );
175 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
177 ToggleSuspend( $reserve_id );
178 $hold = Koha::Holds->find( $reserve_id );
179 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
181 ToggleSuspend( $reserve_id, '2012-01-01' );
182 $hold = Koha::Holds->find( $reserve_id );
183 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
185 AutoUnsuspendReserves();
186 $hold = Koha::Holds->find( $reserve_id );
187 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
190 borrowernumber => $borrowernumber,
191 biblionumber => $biblio->biblionumber,
193 suspend_until => '2012-01-01',
195 $hold = Koha::Holds->find( $reserve_id );
196 is( $hold->suspend, 1, "Test SuspendAll()" );
197 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
200 borrowernumber => $borrowernumber,
201 biblionumber => $biblio->biblionumber,
204 $hold = Koha::Holds->find( $reserve_id );
205 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
206 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
208 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
211 branchcode => $branch_1,
212 borrowernumber => $borrowernumbers[0],
213 biblionumber => $biblio->biblionumber,
217 $patron = Koha::Patrons->find( $borrowernumber );
218 $holds = $patron->holds;
219 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
220 ModReserveMinusPriority( $itemnumber, $reserveid );
221 $holds = $patron->holds;
222 is( $holds->search({ itemnumber => $itemnumber })->count, 1, "Test ModReserveMinusPriority()" );
224 $holds = $biblio->holds;
225 $hold = $holds->next;
226 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
227 $hold = Koha::Holds->find( $reserveid );
228 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
230 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
231 $hold = Koha::Holds->find( $reserveid );
232 is( $hold->priority, '2', "Test AlterPriority(), move down" );
234 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
235 $hold = Koha::Holds->find( $reserveid );
236 is( $hold->priority, '1', "Test AlterPriority(), move up" );
238 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
239 $hold = Koha::Holds->find( $reserveid );
240 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
242 # Regression test for bug 2394
244 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
245 # a patron is not permittedo to request an item whose homebranch (i.e.,
246 # owner of the item) is different from the patron's own library.
247 # However, if canreservefromotherbranches is turned ON, the patron can
248 # create such hold requests.
250 # Note that canreservefromotherbranches has no effect if
251 # IndependentBranches is OFF.
253 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
254 my $foreign_itemnumber = $builder->build_sample_item({ library => $branch_2, biblionumber => $foreign_biblio->biblionumber })->itemnumber;
255 Koha::CirculationRules->set_rules(
257 categorycode => undef,
261 reservesallowed => 25,
262 holds_per_record => 99,
266 Koha::CirculationRules->set_rules(
268 categorycode => undef,
270 itemtype => 'CANNOT',
272 reservesallowed => 0,
273 holds_per_record => 99,
278 # make sure some basic sysprefs are set
279 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
280 t::lib::Mocks::mock_preference('item-level_itypes', 1);
282 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
283 t::lib::Mocks::mock_preference('IndependentBranches', 0);
286 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
287 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
290 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
291 t::lib::Mocks::mock_preference('IndependentBranches', 1);
292 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
294 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
295 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
298 # ... unless canreservefromotherbranches is ON
299 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
301 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
302 '... unless canreservefromotherbranches is ON (bug 2394)'
306 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
307 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
308 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
309 my $reserveid1 = AddReserve(
311 branchcode => $branch_1,
312 borrowernumber => $borrowernumbers[0],
313 biblionumber => $biblio->biblionumber,
318 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
319 my $reserveid2 = AddReserve(
321 branchcode => $branch_1,
322 borrowernumber => $borrowernumbers[1],
323 biblionumber => $biblio->biblionumber,
328 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
329 my $reserveid3 = AddReserve(
331 branchcode => $branch_1,
332 borrowernumber => $borrowernumbers[2],
333 biblionumber => $biblio->biblionumber,
338 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
339 my $hold3 = Koha::Holds->find( $reserveid3 );
340 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
341 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
342 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
343 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
346 Koha::Items->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
347 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
348 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
349 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
351 $hold = Koha::Hold->new(
353 borrowernumber => $borrowernumbers[0],
354 itemnumber => $itemnumber,
355 biblionumber => $biblio->biblionumber,
358 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
360 "Patron cannot place a second item level hold for a given item" );
363 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
364 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
365 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
367 # Items that are not for loan, but holdable should not be trapped until they are available for loan
368 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 0 );
369 Koha::Items->find($itemnumber)->damaged(0)->notforloan(-1)->store;
370 Koha::Holds->search({ biblionumber => $biblio->id })->delete();
371 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
372 $hold = Koha::Hold->new(
374 borrowernumber => $borrowernumbers[0],
375 itemnumber => $itemnumber,
376 biblionumber => $biblio->biblionumber,
379 reservedate => dt_from_string,
380 branchcode => $branch_1,
383 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
384 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 1 );
385 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold is trapped for item that is not for loan but holdable ( notforloan < 0 )" );
386 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1' );
387 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
388 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1|1' );
389 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
392 # Regression test for bug 9532
393 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
394 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
397 branchcode => $branch_1,
398 borrowernumber => $borrowernumbers[0],
399 biblionumber => $biblio->biblionumber,
404 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber)->{status}, 'tooManyReserves',
405 "cannot request item if policy that matches on item-level item type forbids it"
408 $item->itype('CAN')->store;
410 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber)->{status} eq 'OK',
411 "can request item if policy that matches on item type allows it"
414 t::lib::Mocks::mock_preference('item-level_itypes', 0);
415 $item->itype(undef)->store;
417 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber)->{status} eq 'tooManyReserves',
418 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
422 # Test branch item rules
424 $dbh->do('DELETE FROM circulation_rules');
425 Koha::CirculationRules->set_rules(
427 categorycode => undef,
431 reservesallowed => 25,
432 holds_per_record => 99,
436 Koha::CirculationRules->set_rules(
438 branchcode => $branch_1,
439 itemtype => 'CANNOT',
442 returnbranch => 'homebranch',
446 Koha::CirculationRules->set_rules(
448 branchcode => $branch_1,
452 returnbranch => 'homebranch',
456 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
457 $itemnumber = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber})->itemnumber;
458 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
459 "CanItemBeReserved should return 'notReservable'");
461 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
462 $itemnumber = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber})->itemnumber;
463 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
464 'cannotReserveFromOtherBranches',
465 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
466 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
467 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
469 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
471 $itemnumber = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber})->itemnumber;
472 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
473 "CanItemBeReserved should return 'OK'");
476 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
477 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
479 $dbh->do('DELETE FROM reserves');
480 $dbh->do('DELETE FROM issues');
481 $dbh->do('DELETE FROM items');
482 $dbh->do('DELETE FROM biblio');
484 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
485 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
487 Koha::CirculationRules->set_rules(
489 categorycode => undef,
493 reservesallowed => 1,
494 holds_per_record => 99,
498 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
499 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
501 my $res_id = AddReserve(
503 branchcode => $branch_1,
504 borrowernumber => $borrowernumbers[0],
505 biblionumber => $biblio->biblionumber,
510 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
511 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
513 #results should be the same for both ReservesControlBranch settings
514 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
515 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
516 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
517 #reset for further tests
518 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
520 subtest 'Test max_holds per library/patron category' => sub {
523 $dbh->do('DELETE FROM reserves');
525 $biblio = $builder->build_sample_biblio;
526 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
527 Koha::CirculationRules->set_rules(
529 categorycode => undef,
531 itemtype => $biblio->itemtype,
533 reservesallowed => 99,
534 holds_per_record => 99,
542 branchcode => $branch_1,
543 borrowernumber => $borrowernumbers[0],
544 biblionumber => $biblio->biblionumber,
551 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
552 is( $count, 3, 'Patron now has 3 holds' );
554 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
555 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
557 my $rule_all = Koha::CirculationRules->set_rule(
559 categorycode => $category->{categorycode},
561 rule_name => 'max_holds',
566 my $rule_branch = Koha::CirculationRules->set_rule(
568 branchcode => $branch_1,
569 categorycode => $category->{categorycode},
570 rule_name => 'max_holds',
575 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
576 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
578 $rule_branch->delete();
580 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
581 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
584 $rule_branch->rule_value(3);
585 $rule_branch->store();
587 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
588 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
590 $rule_branch->rule_value(5);
591 $rule_branch->update();
592 $rule_branch->rule_value(5);
593 $rule_branch->store();
595 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
596 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
599 subtest 'Pickup location availability tests' => sub {
602 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
603 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
604 #Add a default rule to allow some holds
606 Koha::CirculationRules->set_rules(
609 categorycode => undef,
612 reservesallowed => 25,
613 holds_per_record => 99,
617 my $item = Koha::Items->find($itemnumber);
618 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
619 my $library = Koha::Libraries->find($branch_to);
620 $library->pickup_location('1')->store;
621 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
623 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
624 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
626 $library->pickup_location('1')->store;
627 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
628 'OK', 'Library is a pickup location');
630 my $limit = Koha::Item::Transfer::Limit->new({
631 fromBranch => $item->holdingbranch,
632 toBranch => $branch_to,
633 itemtype => $item->effective_itemtype,
635 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
636 'cannotBeTransferred', 'Item cannot be transferred');
639 $library->pickup_location('0')->store;
640 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
641 'libraryNotPickupLocation', 'Library is not a pickup location');
642 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
643 'libraryNotFound', 'Cannot set unknown library as pickup location');
646 $schema->storage->txn_rollback;
648 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
652 $schema->storage->txn_begin;
654 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
655 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
656 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
658 # Create 3 biblios with items
659 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
660 my $itemnumber_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber})->itemnumber;
661 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
662 my $itemnumber_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber})->itemnumber;
663 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
664 my $itemnumber_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber})->itemnumber;
666 Koha::CirculationRules->set_rules(
670 itemtype => $itemtype->itemtype,
672 reservesallowed => 1,
673 holds_per_record => 99,
680 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
682 'Patron can reserve item with hold limit of 1, no holds placed'
687 branchcode => $library->branchcode,
688 borrowernumber => $patron->borrowernumber,
689 biblionumber => $biblio_1->biblionumber,
695 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
696 { status => 'tooManyReserves', limit => 1 },
697 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
700 # Raise reservesallowed to avoid tooManyReserves from it
701 Koha::CirculationRules->set_rule(
706 itemtype => $itemtype->itemtype,
707 rule_name => 'reservesallowed',
713 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
715 'Patron can reserve item with 2 reserves daily cap'
718 # Add a second reserve
719 my $res_id = AddReserve(
721 branchcode => $library->branchcode,
722 borrowernumber => $patron->borrowernumber,
723 biblionumber => $biblio_2->biblionumber,
728 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
729 { status => 'tooManyReservesToday', limit => 2 },
730 'Patron cannot a third item with 2 reserves daily cap'
733 # Update last hold so reservedate is in the past, so 2 holds, but different day
734 $hold = Koha::Holds->find($res_id);
735 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
736 $hold->reservedate($yesterday)->store;
739 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
741 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
744 # Set holds_per_day to 0
745 Koha::CirculationRules->set_rule(
750 itemtype => $itemtype->itemtype,
751 rule_name => 'holds_per_day',
757 # Delete existing holds
758 Koha::Holds->search->delete;
760 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
761 { status => 'tooManyReservesToday', limit => 0 },
762 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
765 Koha::CirculationRules->set_rule(
770 itemtype => $itemtype->itemtype,
771 rule_name => 'holds_per_day',
776 Koha::Holds->search->delete;
778 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
780 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
784 branchcode => $library->branchcode,
785 borrowernumber => $patron->borrowernumber,
786 biblionumber => $biblio_1->biblionumber,
792 branchcode => $library->branchcode,
793 borrowernumber => $patron->borrowernumber,
794 biblionumber => $biblio_2->biblionumber,
800 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
802 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
806 branchcode => $library->branchcode,
807 borrowernumber => $patron->borrowernumber,
808 biblionumber => $biblio_3->biblionumber,
813 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
814 { status => 'tooManyReserves', limit => 3 },
815 'Unlimited daily holds, but reached reservesallowed'
817 #results should be the same for both ReservesControlBranch settings
818 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
820 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
821 { status => 'tooManyReserves', limit => 3 },
822 'Unlimited daily holds, but reached reservesallowed'
825 $schema->storage->txn_rollback;
828 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
831 $schema->storage->txn_begin;
833 Koha::CirculationRules->set_rule(
836 categorycode => undef,
838 rule_name => 'reservesallowed',
844 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
845 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
848 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
849 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
850 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
852 # Create library groups hierarchy
853 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
854 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
855 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
858 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
859 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
861 # Create 3 biblios with items
862 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
863 my $item_1 = $builder->build_sample_item(
865 biblionumber => $biblio_1->biblionumber,
866 library => $library1->branchcode
869 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
870 my $item_2 = $builder->build_sample_item(
872 biblionumber => $biblio_2->biblionumber,
873 library => $library2->branchcode
876 my $itemnumber_2 = $item_2->itemnumber;
877 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
878 my $item_3 = $builder->build_sample_item(
880 biblionumber => $biblio_3->biblionumber,
881 library => $library1->branchcode
885 # Test 1: Patron 3 can place hold
887 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
889 'Patron can place hold if no circ_rules where defined'
892 # Insert default circ rule of holds allowed only from local hold group for all libraries
893 Koha::CirculationRules->set_rules(
899 hold_fulfillment_policy => 'any',
900 returnbranch => 'any'
905 # Test 2: Patron 1 can place hold
907 CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
909 'Patron can place hold because patron\'s home library is part of hold group'
912 # Test 3: Patron 3 cannot place hold
914 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
915 { status => 'branchNotInHoldGroup' },
916 'Patron cannot place hold because patron\'s home library is not part of hold group'
919 # Insert default circ rule to "any" for library 2
920 Koha::CirculationRules->set_rules(
922 branchcode => $library2->branchcode,
926 hold_fulfillment_policy => 'any',
927 returnbranch => 'any'
932 # Test 4: Patron 3 can place hold
934 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
936 'Patron can place hold if holdallowed is set to "any" for library 2'
939 # Update default circ rule to "hold group" for library 2
940 Koha::CirculationRules->set_rules(
942 branchcode => $library2->branchcode,
946 hold_fulfillment_policy => 'any',
947 returnbranch => 'any'
952 # Test 5: Patron 3 cannot place hold
954 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
955 { status => 'branchNotInHoldGroup' },
956 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
959 # Insert default item rule to "any" for itemtype 2
960 Koha::CirculationRules->set_rules(
962 branchcode => $library2->branchcode,
963 itemtype => $itemtype2->itemtype,
966 hold_fulfillment_policy => 'any',
967 returnbranch => 'any'
972 # Test 6: Patron 3 can place hold
974 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
976 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
979 # Update default item rule to "hold group" for itemtype 2
980 Koha::CirculationRules->set_rules(
982 branchcode => $library2->branchcode,
983 itemtype => $itemtype2->itemtype,
986 hold_fulfillment_policy => 'any',
987 returnbranch => 'any'
992 # Test 7: Patron 3 cannot place hold
994 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
995 { status => 'branchNotInHoldGroup' },
996 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
999 # Insert branch item rule to "any" for itemtype 2 and library 2
1000 Koha::CirculationRules->set_rules(
1002 branchcode => $library2->branchcode,
1003 itemtype => $itemtype2->itemtype,
1006 hold_fulfillment_policy => 'any',
1007 returnbranch => 'any'
1012 # Test 8: Patron 3 can place hold
1014 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1016 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1019 # Update branch item rule to "hold group" for itemtype 2 and library 2
1020 Koha::CirculationRules->set_rules(
1022 branchcode => $library2->branchcode,
1023 itemtype => $itemtype2->itemtype,
1026 hold_fulfillment_policy => 'any',
1027 returnbranch => 'any'
1032 # Test 9: Patron 3 cannot place hold
1034 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1035 { status => 'branchNotInHoldGroup' },
1036 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1039 $schema->storage->txn_rollback;
1043 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1046 $schema->storage->txn_begin;
1047 Koha::CirculationRules->set_rule(
1049 branchcode => undef,
1050 categorycode => undef,
1052 rule_name => 'reservesallowed',
1058 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1059 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1062 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1063 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1064 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1066 # Create library groups hierarchy
1067 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1068 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1069 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1072 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1073 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1075 # Create 3 biblios with items
1076 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1077 my $item_1 = $builder->build_sample_item(
1079 biblionumber => $biblio_1->biblionumber,
1080 library => $library1->branchcode
1083 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1084 my $item_2 = $builder->build_sample_item(
1086 biblionumber => $biblio_2->biblionumber,
1087 library => $library2->branchcode
1090 my $itemnumber_2 = $item_2->itemnumber;
1091 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1092 my $item_3 = $builder->build_sample_item(
1094 biblionumber => $biblio_3->biblionumber,
1095 library => $library1->branchcode
1099 # Test 1: Patron 3 can place hold
1101 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1103 'Patron can place hold if no circ_rules where defined'
1106 # Insert default circ rule of holds allowed only from local hold group for all libraries
1107 Koha::CirculationRules->set_rules(
1109 branchcode => undef,
1113 hold_fulfillment_policy => 'holdgroup',
1114 returnbranch => 'any'
1119 # Test 2: Patron 1 can place hold
1121 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1123 'Patron can place hold because pickup location is part of hold group'
1126 # Test 3: Patron 3 cannot place hold
1128 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1129 { status => 'pickupNotInHoldGroup' },
1130 'Patron cannot place hold because pickup location is not part of hold group'
1133 # Insert default circ rule to "any" for library 2
1134 Koha::CirculationRules->set_rules(
1136 branchcode => $library2->branchcode,
1140 hold_fulfillment_policy => 'any',
1141 returnbranch => 'any'
1146 # Test 4: Patron 3 can place hold
1148 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1150 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1153 # Update default circ rule to "hold group" for library 2
1154 Koha::CirculationRules->set_rules(
1156 branchcode => $library2->branchcode,
1160 hold_fulfillment_policy => 'holdgroup',
1161 returnbranch => 'any'
1166 # Test 5: Patron 3 cannot place hold
1168 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1169 { status => 'pickupNotInHoldGroup' },
1170 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1173 # Insert default item rule to "any" for itemtype 2
1174 Koha::CirculationRules->set_rules(
1176 branchcode => $library2->branchcode,
1177 itemtype => $itemtype2->itemtype,
1180 hold_fulfillment_policy => 'any',
1181 returnbranch => 'any'
1186 # Test 6: 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'
1193 # Update default item rule to "hold group" for itemtype 2
1194 Koha::CirculationRules->set_rules(
1196 branchcode => $library2->branchcode,
1197 itemtype => $itemtype2->itemtype,
1200 hold_fulfillment_policy => 'holdgroup',
1201 returnbranch => 'any'
1206 # Test 7: Patron 3 cannot place hold
1208 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1209 { status => 'pickupNotInHoldGroup' },
1210 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1213 # Insert branch item rule to "any" for itemtype 2 and library 2
1214 Koha::CirculationRules->set_rules(
1216 branchcode => $library2->branchcode,
1217 itemtype => $itemtype2->itemtype,
1220 hold_fulfillment_policy => 'any',
1221 returnbranch => 'any'
1226 # Test 8: Patron 3 can place hold
1228 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1230 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1233 # Update branch item rule to "hold group" for itemtype 2 and library 2
1234 Koha::CirculationRules->set_rules(
1236 branchcode => $library2->branchcode,
1237 itemtype => $itemtype2->itemtype,
1240 hold_fulfillment_policy => 'holdgroup',
1241 returnbranch => 'any'
1246 # Test 9: Patron 3 cannot place hold
1248 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1249 { status => 'pickupNotInHoldGroup' },
1250 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1253 $schema->storage->txn_rollback;
1256 subtest 'non priority holds' => sub {
1260 $schema->storage->txn_begin;
1262 Koha::CirculationRules->set_rules(
1264 branchcode => undef,
1265 categorycode => undef,
1268 renewalsallowed => 5,
1269 reservesallowed => 5,
1274 my $item = $builder->build_sample_item;
1276 my $patron1 = $builder->build_object(
1278 class => 'Koha::Patrons',
1279 value => { branchcode => $item->homebranch }
1282 my $patron2 = $builder->build_object(
1284 class => 'Koha::Patrons',
1285 value => { branchcode => $item->homebranch }
1289 Koha::Checkout->new(
1291 borrowernumber => $patron1->borrowernumber,
1292 itemnumber => $item->itemnumber,
1293 branchcode => $item->homebranch
1297 my $hid = AddReserve(
1299 branchcode => $item->homebranch,
1300 borrowernumber => $patron2->borrowernumber,
1301 biblionumber => $item->biblionumber,
1303 itemnumber => $item->itemnumber,
1308 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1310 ok( !$ok, 'Cannot renew' );
1311 is( $err, 'on_reserve', 'Item is on hold' );
1313 my $hold = Koha::Holds->find($hid);
1314 $hold->non_priority(1)->store;
1317 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1319 ok( $ok, 'Can renew' );
1320 is( $err, undef, 'Item is on non priority hold' );
1322 my $patron3 = $builder->build_object(
1324 class => 'Koha::Patrons',
1325 value => { branchcode => $item->homebranch }
1329 # Add second hold with non_priority = 0
1332 branchcode => $item->homebranch,
1333 borrowernumber => $patron3->borrowernumber,
1334 biblionumber => $item->biblionumber,
1336 itemnumber => $item->itemnumber,
1341 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1343 ok( !$ok, 'Cannot renew' );
1344 is( $err, 'on_reserve', 'Item is on hold' );
1346 $schema->storage->txn_rollback;
1350 subtest 'CanItemBeReserved rule precedence tests' => sub {
1354 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1355 $schema->storage->txn_begin;
1356 my $library = $builder->build_object( { class => 'Koha::Libraries', value => {
1357 pickup_location => 1,
1359 my $item = $builder->build_sample_item({
1360 homebranch => $library->branchcode,
1361 holdingbranch => $library->branchcode
1363 my $item2 = $builder->build_sample_item({
1364 homebranch => $library->branchcode,
1365 holdingbranch => $library->branchcode,
1366 itype => $item->itype
1368 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
1369 branchcode => $library->branchcode
1371 Koha::CirculationRules->set_rules(
1373 branchcode => undef,
1374 categorycode => $patron->categorycode,
1375 itemtype => $item->itype,
1377 reservesallowed => 1,
1382 CanItemBeReserved( $patron->borrowernumber, $item->itemnumber, $library->branchcode ),
1384 'Patron of specified category can place 1 hold on specified itemtype'
1386 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
1387 biblionumber => $item2->biblionumber,
1388 itemnumber => $item2->itemnumber,
1391 branchcode => $library->branchcode,
1392 borrowernumber => $patron->borrowernumber,
1395 CanItemBeReserved( $patron->borrowernumber, $item->itemnumber, $library->branchcode ),
1396 { status => 'tooManyReserves', limit => 1 },
1397 'Patron of specified category can place 1 hold on specified itemtype, cannot place a second'
1399 Koha::CirculationRules->set_rules(
1401 branchcode => $library->branchcode,
1402 categorycode => undef,
1405 reservesallowed => 2,
1410 CanItemBeReserved( $patron->borrowernumber, $item->itemnumber, $library->branchcode ),
1412 'Patron of specified category can place 1 hold on specified itemtype if library rule for all types and categories set to 2'
1415 $schema->storage->txn_rollback;