6 use t::lib::TestBuilder;
10 use Test::More tests => 72;
16 use C4::Reserves qw( AddReserve CalculatePriority ModReserve ToggleSuspend AutoUnsuspendReserves SuspendAll ModReserveMinusPriority AlterPriority CanItemBeReserved CheckReserves );
17 use C4::Circulation qw( CanBookBeRenewed );
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({ source => 'Category' });
48 my $borrowers_count = 5;
50 $dbh->do('DELETE FROM itemtypes');
51 $dbh->do('DELETE FROM reserves');
52 $dbh->do('DELETE FROM circulation_rules');
53 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
54 $insert_sth->execute('CAN');
55 $insert_sth->execute('CANNOT');
56 $insert_sth->execute('DUMMY');
57 $insert_sth->execute('ONLY1');
59 # Setup Test------------------------
60 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
62 # Create item instance for testing.
63 my $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
65 # Create some borrowers
68 foreach (1..$borrowers_count) {
69 my $patron = Koha::Patron->new({
70 firstname => 'my firstname',
71 surname => 'my surname ' . $_,
72 categorycode => $category->{categorycode},
73 branchcode => $branch_1,
75 push @patrons, $patron;
76 push @borrowernumbers, $patron->borrowernumber;
79 # Create five item level holds
80 foreach my $borrowernumber ( @borrowernumbers ) {
83 branchcode => $branch_1,
84 borrowernumber => $borrowernumber,
85 biblionumber => $biblio->biblionumber,
86 priority => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
87 itemnumber => $itemnumber,
92 my $holds = $biblio->holds;
93 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
94 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
95 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
96 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
97 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
98 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
100 my $item = Koha::Items->find( $itemnumber );
101 $holds = $item->current_holds;
102 my $first_hold = $holds->next;
103 my $reservedate = $first_hold->reservedate;
104 my $borrowernumber = $first_hold->borrowernumber;
105 my $branch_1code = $first_hold->branchcode;
106 my $reserve_id = $first_hold->reserve_id;
107 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
108 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
109 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
110 ok($reserve_id, "Test holds_placed_today()");
112 my $hold = Koha::Holds->find( $reserve_id );
113 ok( $hold, "Koha::Holds found the hold" );
114 my $hold_biblio = $hold->biblio();
115 ok( $hold_biblio, "Got biblio using biblio() method" );
116 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
117 my $hold_item = $hold->item();
118 ok( $hold_item, "Got item using item() method" );
119 ok( $hold_item == $hold->item(), "item method returns stashed item" );
120 my $hold_branch = $hold->branch();
121 ok( $hold_branch, "Got branch using branch() method" );
122 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
123 my $hold_found = $hold->found();
124 $hold->set({ found => 'W'})->store();
125 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
126 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
128 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
129 $holds = $patron->holds;
130 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
133 $holds = $item->current_holds;
134 $first_hold = $holds->next;
135 $borrowernumber = $first_hold->borrowernumber;
136 $branch_1code = $first_hold->branchcode;
137 $reserve_id = $first_hold->reserve_id;
140 reserve_id => $reserve_id,
142 branchcode => $branch_1,
143 itemnumber => $itemnumber,
144 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
147 $hold = Koha::Holds->find( $reserve_id );
148 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
149 ok( $hold->suspend, "Test ModReserve, suspend hold" );
150 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
152 ModReserve({ # call without reserve_id
154 biblionumber => $biblio->biblionumber,
155 itemnumber => $itemnumber,
156 borrowernumber => $borrowernumber,
158 $hold = Koha::Holds->find( $reserve_id );
159 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
161 ToggleSuspend( $reserve_id );
162 $hold = Koha::Holds->find( $reserve_id );
163 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
165 ToggleSuspend( $reserve_id, '2012-01-01' );
166 $hold = Koha::Holds->find( $reserve_id );
167 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
169 AutoUnsuspendReserves();
170 $hold = Koha::Holds->find( $reserve_id );
171 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
174 borrowernumber => $borrowernumber,
175 biblionumber => $biblio->biblionumber,
177 suspend_until => '2012-01-01',
179 $hold = Koha::Holds->find( $reserve_id );
180 is( $hold->suspend, 1, "Test SuspendAll()" );
181 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
184 borrowernumber => $borrowernumber,
185 biblionumber => $biblio->biblionumber,
188 $hold = Koha::Holds->find( $reserve_id );
189 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
190 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
192 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
195 branchcode => $branch_1,
196 borrowernumber => $borrowernumbers[0],
197 biblionumber => $biblio->biblionumber,
201 $patron = Koha::Patrons->find( $borrowernumber );
202 $holds = $patron->holds;
203 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
204 ModReserveMinusPriority( $itemnumber, $reserveid );
205 $holds = $patron->holds;
206 is( $holds->search({ itemnumber => $itemnumber })->count, 1, "Test ModReserveMinusPriority()" );
208 $holds = $biblio->holds;
209 $hold = $holds->next;
210 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
211 $hold = Koha::Holds->find( $reserveid );
212 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
214 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
215 $hold = Koha::Holds->find( $reserveid );
216 is( $hold->priority, '2', "Test AlterPriority(), move down" );
218 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
219 $hold = Koha::Holds->find( $reserveid );
220 is( $hold->priority, '1', "Test AlterPriority(), move up" );
222 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
223 $hold = Koha::Holds->find( $reserveid );
224 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
226 # Regression test for bug 2394
228 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
229 # a patron is not permittedo to request an item whose homebranch (i.e.,
230 # owner of the item) is different from the patron's own library.
231 # However, if canreservefromotherbranches is turned ON, the patron can
232 # create such hold requests.
234 # Note that canreservefromotherbranches has no effect if
235 # IndependentBranches is OFF.
237 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
238 my $foreign_item = $builder->build_sample_item({ library => $branch_2, biblionumber => $foreign_biblio->biblionumber });
239 Koha::CirculationRules->set_rules(
241 categorycode => undef,
245 reservesallowed => 25,
246 holds_per_record => 99,
250 Koha::CirculationRules->set_rules(
252 categorycode => undef,
254 itemtype => 'CANNOT',
256 reservesallowed => 0,
257 holds_per_record => 99,
262 # make sure some basic sysprefs are set
263 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
264 t::lib::Mocks::mock_preference('item-level_itypes', 1);
266 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
267 t::lib::Mocks::mock_preference('IndependentBranches', 0);
270 CanItemBeReserved($patrons[0], $foreign_item)->{status}, 'OK',
271 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
274 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
275 t::lib::Mocks::mock_preference('IndependentBranches', 1);
276 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
278 CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'cannotReserveFromOtherBranches',
279 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
282 # ... unless canreservefromotherbranches is ON
283 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
285 CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'OK',
286 '... unless canreservefromotherbranches is ON (bug 2394)'
290 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
291 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
292 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
293 my $reserveid1 = AddReserve(
295 branchcode => $branch_1,
296 borrowernumber => $borrowernumbers[0],
297 biblionumber => $biblio->biblionumber,
302 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
303 my $reserveid2 = AddReserve(
305 branchcode => $branch_1,
306 borrowernumber => $borrowernumbers[1],
307 biblionumber => $biblio->biblionumber,
312 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
313 my $reserveid3 = AddReserve(
315 branchcode => $branch_1,
316 borrowernumber => $borrowernumbers[2],
317 biblionumber => $biblio->biblionumber,
322 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
323 my $hold3 = Koha::Holds->find( $reserveid3 );
324 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
325 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
326 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
327 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
330 my $damaged_item = Koha::Items->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
331 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
332 is( CanItemBeReserved( $patrons[0], $damaged_item)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
333 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
335 $hold = Koha::Hold->new(
337 borrowernumber => $borrowernumbers[0],
338 itemnumber => $itemnumber,
339 biblionumber => $biblio->biblionumber,
342 is( CanItemBeReserved( $patrons[0], $damaged_item )->{status},
344 "Patron cannot place a second item level hold for a given item" );
347 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
348 ok( CanItemBeReserved( $patrons[0], $damaged_item)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
349 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
351 # Items that are not for loan, but holdable should not be trapped until they are available for loan
352 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 0 );
353 my $nfl_item = Koha::Items->find($itemnumber)->damaged(0)->notforloan(-1)->store;
354 Koha::Holds->search({ biblionumber => $biblio->id })->delete();
355 is( CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
356 $hold = Koha::Hold->new(
358 borrowernumber => $borrowernumbers[0],
359 itemnumber => $itemnumber,
360 biblionumber => $biblio->biblionumber,
363 reservedate => dt_from_string,
364 branchcode => $branch_1,
367 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
368 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 1 );
369 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold is trapped for item that is not for loan but holdable ( notforloan < 0 )" );
370 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1' );
371 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
372 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1|1' );
373 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
375 CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'itemAlreadyOnHold',
376 "cannot request item that you have already reservedd"
379 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'OK',
380 "can request item if we are not checking holds counts, but only if policy allows or forbids it"
384 # Regression test for bug 9532
385 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
386 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
389 branchcode => $branch_1,
390 borrowernumber => $borrowernumbers[0],
391 biblionumber => $biblio->biblionumber,
396 CanItemBeReserved( $patrons[0], $item)->{status}, 'noReservesAllowed',
397 "cannot request item if policy that matches on item-level item type forbids it"
400 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'noReservesAllowed',
401 "cannot request item if policy that matches on item-level item type forbids it even if ignoring counts"
404 subtest 'CanItemBeReserved' => sub {
407 my $itemtype_can = $builder->build({source => "Itemtype"})->{itemtype};
408 my $itemtype_cant = $builder->build({source => "Itemtype"})->{itemtype};
409 my $itemtype_cant_record = $builder->build({source => "Itemtype"})->{itemtype};
411 Koha::CirculationRules->set_rules(
413 categorycode => undef,
415 itemtype => $itemtype_cant,
417 reservesallowed => 0,
418 holds_per_record => 99,
422 Koha::CirculationRules->set_rules(
424 categorycode => undef,
426 itemtype => $itemtype_can,
428 reservesallowed => 2,
429 holds_per_record => 2,
433 Koha::CirculationRules->set_rules(
435 categorycode => undef,
437 itemtype => $itemtype_cant_record,
439 reservesallowed => 0,
440 holds_per_record => 0,
445 Koha::CirculationRules->set_rules(
447 branchcode => $branch_1,
448 itemtype => $itemtype_cant,
451 returnbranch => 'homebranch',
455 Koha::CirculationRules->set_rules(
457 branchcode => $branch_1,
458 itemtype => $itemtype_can,
461 returnbranch => 'homebranch',
466 subtest 'noReservesAllowed' => sub {
469 my $biblionumber_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant })->biblionumber;
470 my $biblionumber_can = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
471 my $biblionumber_record_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant_record })->biblionumber;
473 my $item_1_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_cannot });
474 my $item_1_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_cannot });
475 my $item_2_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_can });
476 my $item_2_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_can });
477 my $item_3_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant_record, biblionumber => $biblionumber_record_cannot });
479 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
481 t::lib::Mocks::mock_preference('item-level_itypes', 1);
483 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'noReservesAllowed',
484 "With item level set, rule from item must be picked (CANNOT)"
487 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'OK',
488 "With item level set, rule from item must be picked (CAN)"
490 t::lib::Mocks::mock_preference('item-level_itypes', 0);
492 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'noReservesAllowed',
493 "With biblio level set, rule from biblio must be picked (CANNOT)"
496 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'OK',
497 "With biblio level set, rule from biblio must be picked (CAN)"
500 CanItemBeReserved( $patrons[0], $item_3_cannot)->{status}, 'noReservesAllowed',
501 "When no holds allowed and no holds per record allowed should return noReservesAllowed"
505 subtest 'tooManyHoldsForThisRecord + tooManyReserves + itemAlreadyOnHold' => sub {
508 my $biblionumber_1 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
509 my $item_11 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
510 my $item_12 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
511 my $biblionumber_2 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
512 my $item_21 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
513 my $item_22 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
515 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
520 borrowernumber => $borrowernumbers[0],
521 biblionumber => $biblionumber_1,
523 for my $item_level ( 0..1 ) {
524 t::lib::Mocks::mock_preference('item-level_itypes', $item_level);
526 # FIXME This is not really correct, but CanItemBeReserved does not check if biblio-level holds already exist
527 CanItemBeReserved( $patrons[0], $item_11)->{status}, 'OK',
528 "A biblio-level hold already exists - another hold can be placed on a specific item item"
532 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
536 borrowernumber => $borrowernumbers[0],
537 biblionumber => $biblionumber_1,
538 itemnumber => $item_11->itemnumber,
541 $dbh->do('DELETE FROM circulation_rules');
542 Koha::CirculationRules->set_rules(
544 categorycode => undef,
548 reservesallowed => 5,
549 holds_per_record => 1,
554 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
555 "A item-level hold already exists and holds_per_record=1, another hold cannot be placed on this record"
557 Koha::CirculationRules->set_rules(
559 categorycode => undef,
563 reservesallowed => 1,
564 holds_per_record => 1,
569 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
570 "A item-level hold already exists and holds_per_record=1 - tooManyHoldsForThisRecord has priority over tooManyReserves"
572 Koha::CirculationRules->set_rules(
574 categorycode => undef,
578 reservesallowed => 5,
579 holds_per_record => 2,
584 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'OK',
585 "A item-level hold already exists but holds_per_record=2- another item-level hold can be placed on this record"
590 borrowernumber => $borrowernumbers[0],
591 biblionumber => $biblionumber_2,
592 itemnumber => $item_21->itemnumber
594 Koha::CirculationRules->set_rules(
596 categorycode => undef,
600 reservesallowed => 2,
601 holds_per_record => 2,
606 CanItemBeReserved( $patrons[0], $item_21)->{status}, 'itemAlreadyOnHold',
607 "A item-level holds already exists on this item, itemAlreadyOnHold should be raised"
610 CanItemBeReserved( $patrons[0], $item_22)->{status}, 'tooManyReserves',
611 "This patron has already placed reservesallowed holds, tooManyReserves should be raised"
617 # Test branch item rules
619 $dbh->do('DELETE FROM circulation_rules');
620 Koha::CirculationRules->set_rules(
622 categorycode => undef,
626 reservesallowed => 25,
627 holds_per_record => 99,
631 Koha::CirculationRules->set_rules(
633 branchcode => $branch_1,
634 itemtype => 'CANNOT',
636 holdallowed => 'not_allowed',
637 returnbranch => 'homebranch',
641 Koha::CirculationRules->set_rules(
643 branchcode => $branch_1,
646 holdallowed => 'from_home_library',
647 returnbranch => 'homebranch',
651 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
652 my $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
653 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'notReservable',
654 "CanItemBeReserved should return 'notReservable'");
656 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
657 $branch_rule_item = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber});
658 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
659 'cannotReserveFromOtherBranches',
660 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
661 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
662 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
664 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
666 $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber});
667 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'OK',
668 "CanItemBeReserved should return 'OK'");
671 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
672 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
674 $dbh->do('DELETE FROM reserves');
675 $dbh->do('DELETE FROM issues');
676 $dbh->do('DELETE FROM items');
677 $dbh->do('DELETE FROM biblio');
679 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
680 my $limit_count_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
682 Koha::CirculationRules->set_rules(
684 categorycode => undef,
688 reservesallowed => 1,
689 holds_per_record => 99,
693 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
694 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
696 my $res_id = AddReserve(
698 branchcode => $branch_1,
699 borrowernumber => $borrowernumbers[0],
700 biblionumber => $biblio->biblionumber,
705 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
706 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
707 is( CanItemBeReserved( $patrons[0], $limit_count_item, undef, { ignore_hold_counts => 1 } )->{status},
708 'OK', 'Patron can reserve item if checking policy but not counts' );
710 #results should be the same for both ReservesControlBranch settings
711 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
712 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
713 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
714 #reset for further tests
715 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
717 subtest 'Test max_holds per library/patron category' => sub {
720 $dbh->do('DELETE FROM reserves');
722 $biblio = $builder->build_sample_biblio;
723 my $max_holds_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
724 Koha::CirculationRules->set_rules(
726 categorycode => undef,
728 itemtype => $biblio->itemtype,
730 reservesallowed => 99,
731 holds_per_record => 99,
739 branchcode => $branch_1,
740 borrowernumber => $borrowernumbers[0],
741 biblionumber => $biblio->biblionumber,
748 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
749 is( $count, 3, 'Patron now has 3 holds' );
751 my $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
752 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
754 my $rule_all = Koha::CirculationRules->set_rule(
756 categorycode => $category->{categorycode},
758 rule_name => 'max_holds',
763 my $rule_branch = Koha::CirculationRules->set_rule(
765 branchcode => $branch_1,
766 categorycode => $category->{categorycode},
767 rule_name => 'max_holds',
772 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
773 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
775 $rule_branch->delete();
777 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
778 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
781 $rule_branch->rule_value(3);
782 $rule_branch->store();
784 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
785 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
787 $rule_branch->rule_value(5);
788 $rule_branch->update();
789 $rule_branch->rule_value(5);
790 $rule_branch->store();
792 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
793 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
796 subtest 'Pickup location availability tests' => sub {
799 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
800 my $pickup_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
801 #Add a default rule to allow some holds
803 Koha::CirculationRules->set_rules(
806 categorycode => undef,
809 reservesallowed => 25,
810 holds_per_record => 99,
814 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
815 my $library = Koha::Libraries->find($branch_to);
816 $library->pickup_location('1')->store;
817 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
819 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
820 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
822 $library->pickup_location('1')->store;
823 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
824 'OK', 'Library is a pickup location');
826 my $limit = Koha::Item::Transfer::Limit->new({
827 fromBranch => $pickup_item->holdingbranch,
828 toBranch => $branch_to,
829 itemtype => $pickup_item->effective_itemtype,
831 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
832 'cannotBeTransferred', 'Item cannot be transferred');
835 $library->pickup_location('0')->store;
836 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
837 'libraryNotPickupLocation', 'Library is not a pickup location');
838 is(CanItemBeReserved($patron, $pickup_item, 'nonexistent')->{status},
839 'libraryNotFound', 'Cannot set unknown library as pickup location');
842 $schema->storage->txn_rollback;
844 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
848 $schema->storage->txn_begin;
850 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
851 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
852 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
854 # Create 3 biblios with items
855 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
856 my $item_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber});
857 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
858 my $item_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber});
859 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
860 my $item_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber});
862 Koha::CirculationRules->set_rules(
866 itemtype => $itemtype->itemtype,
868 reservesallowed => 1,
869 holds_per_record => 99,
876 CanItemBeReserved( $patron, $item_1 ),
878 'Patron can reserve item with hold limit of 1, no holds placed'
883 branchcode => $library->branchcode,
884 borrowernumber => $patron->borrowernumber,
885 biblionumber => $biblio_1->biblionumber,
891 CanItemBeReserved( $patron, $item_1 ),
892 { status => 'tooManyReserves', limit => 1 },
893 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
896 # Raise reservesallowed to avoid tooManyReserves from it
897 Koha::CirculationRules->set_rule(
902 itemtype => $itemtype->itemtype,
903 rule_name => 'reservesallowed',
909 CanItemBeReserved( $patron, $item_2 ),
911 'Patron can reserve item with 2 reserves daily cap'
914 # Add a second reserve
915 my $res_id = AddReserve(
917 branchcode => $library->branchcode,
918 borrowernumber => $patron->borrowernumber,
919 biblionumber => $biblio_2->biblionumber,
924 CanItemBeReserved( $patron, $item_2 ),
925 { status => 'tooManyReservesToday', limit => 2 },
926 'Patron cannot a third item with 2 reserves daily cap'
929 # Update last hold so reservedate is in the past, so 2 holds, but different day
930 $hold = Koha::Holds->find($res_id);
931 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
932 $hold->reservedate($yesterday)->store;
935 CanItemBeReserved( $patron, $item_2 ),
937 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
940 # Set holds_per_day to 0
941 Koha::CirculationRules->set_rule(
946 itemtype => $itemtype->itemtype,
947 rule_name => 'holds_per_day',
953 # Delete existing holds
954 Koha::Holds->search->delete;
956 CanItemBeReserved( $patron, $item_2 ),
957 { status => 'tooManyReservesToday', limit => 0 },
958 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
961 Koha::CirculationRules->set_rule(
966 itemtype => $itemtype->itemtype,
967 rule_name => 'holds_per_day',
972 Koha::Holds->search->delete;
974 CanItemBeReserved( $patron, $item_2 ),
976 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
980 branchcode => $library->branchcode,
981 borrowernumber => $patron->borrowernumber,
982 biblionumber => $biblio_1->biblionumber,
988 branchcode => $library->branchcode,
989 borrowernumber => $patron->borrowernumber,
990 biblionumber => $biblio_2->biblionumber,
996 CanItemBeReserved( $patron, $item_3 ),
998 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
1002 branchcode => $library->branchcode,
1003 borrowernumber => $patron->borrowernumber,
1004 biblionumber => $biblio_3->biblionumber,
1009 CanItemBeReserved( $patron, $item_3 ),
1010 { status => 'tooManyReserves', limit => 3 },
1011 'Unlimited daily holds, but reached reservesallowed'
1013 #results should be the same for both ReservesControlBranch settings
1014 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1016 CanItemBeReserved( $patron, $item_3 ),
1017 { status => 'tooManyReserves', limit => 3 },
1018 'Unlimited daily holds, but reached reservesallowed'
1021 $schema->storage->txn_rollback;
1024 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
1027 $schema->storage->txn_begin;
1029 Koha::CirculationRules->set_rule(
1031 branchcode => undef,
1032 categorycode => undef,
1034 rule_name => 'reservesallowed',
1040 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1041 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1044 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1045 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1046 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
1048 # Create library groups hierarchy
1049 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1050 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1051 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1054 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1055 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1057 # Create 3 biblios with items
1058 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1059 my $item_1 = $builder->build_sample_item(
1061 biblionumber => $biblio_1->biblionumber,
1062 library => $library1->branchcode
1065 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1066 my $item_2 = $builder->build_sample_item(
1068 biblionumber => $biblio_2->biblionumber,
1069 library => $library2->branchcode
1072 my $itemnumber_2 = $item_2->itemnumber;
1073 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1074 my $item_3 = $builder->build_sample_item(
1076 biblionumber => $biblio_3->biblionumber,
1077 library => $library1->branchcode
1081 # Test 1: Patron 3 can place hold
1083 CanItemBeReserved( $patron3, $item_2 ),
1085 'Patron can place hold if no circ_rules where defined'
1088 # Insert default circ rule of holds allowed only from local hold group for all libraries
1089 Koha::CirculationRules->set_rules(
1091 branchcode => undef,
1094 holdallowed => 'from_local_hold_group',
1095 hold_fulfillment_policy => 'any',
1096 returnbranch => 'any'
1101 # Test 2: Patron 1 can place hold
1103 CanItemBeReserved( $patron1, $item_2 ),
1105 'Patron can place hold because patron\'s home library is part of hold group'
1108 # Test 3: Patron 3 cannot place hold
1110 CanItemBeReserved( $patron3, $item_2 ),
1111 { status => 'branchNotInHoldGroup' },
1112 'Patron cannot place hold because patron\'s home library is not part of hold group'
1115 # Insert default circ rule to "any" for library 2
1116 Koha::CirculationRules->set_rules(
1118 branchcode => $library2->branchcode,
1121 holdallowed => 'from_any_library',
1122 hold_fulfillment_policy => 'any',
1123 returnbranch => 'any'
1128 # Test 4: Patron 3 can place hold
1130 CanItemBeReserved( $patron3, $item_2 ),
1132 'Patron can place hold if holdallowed is set to "any" for library 2'
1135 # Update default circ rule to "hold group" for library 2
1136 Koha::CirculationRules->set_rules(
1138 branchcode => $library2->branchcode,
1141 holdallowed => 'from_local_hold_group',
1142 hold_fulfillment_policy => 'any',
1143 returnbranch => 'any'
1148 # Test 5: Patron 3 cannot place hold
1150 CanItemBeReserved( $patron3, $item_2 ),
1151 { status => 'branchNotInHoldGroup' },
1152 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
1155 # Insert default item rule to "any" for itemtype 2
1156 Koha::CirculationRules->set_rules(
1158 branchcode => $library2->branchcode,
1159 itemtype => $itemtype2->itemtype,
1161 holdallowed => 'from_any_library',
1162 hold_fulfillment_policy => 'any',
1163 returnbranch => 'any'
1168 # Test 6: Patron 3 can place hold
1170 CanItemBeReserved( $patron3, $item_2 ),
1172 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
1175 # Update default item rule to "hold group" for itemtype 2
1176 Koha::CirculationRules->set_rules(
1178 branchcode => $library2->branchcode,
1179 itemtype => $itemtype2->itemtype,
1181 holdallowed => 'from_local_hold_group',
1182 hold_fulfillment_policy => 'any',
1183 returnbranch => 'any'
1188 # Test 7: Patron 3 cannot place hold
1190 CanItemBeReserved( $patron3, $item_2 ),
1191 { status => 'branchNotInHoldGroup' },
1192 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
1195 # Insert branch item rule to "any" for itemtype 2 and library 2
1196 Koha::CirculationRules->set_rules(
1198 branchcode => $library2->branchcode,
1199 itemtype => $itemtype2->itemtype,
1201 holdallowed => 'from_any_library',
1202 hold_fulfillment_policy => 'any',
1203 returnbranch => 'any'
1208 # Test 8: Patron 3 can place hold
1210 CanItemBeReserved( $patron3, $item_2 ),
1212 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1215 # Update branch item rule to "hold group" for itemtype 2 and library 2
1216 Koha::CirculationRules->set_rules(
1218 branchcode => $library2->branchcode,
1219 itemtype => $itemtype2->itemtype,
1221 holdallowed => 'from_local_hold_group',
1222 hold_fulfillment_policy => 'any',
1223 returnbranch => 'any'
1228 # Test 9: Patron 3 cannot place hold
1230 CanItemBeReserved( $patron3, $item_2 ),
1231 { status => 'branchNotInHoldGroup' },
1232 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1235 $schema->storage->txn_rollback;
1239 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1242 $schema->storage->txn_begin;
1243 Koha::CirculationRules->set_rule(
1245 branchcode => undef,
1246 categorycode => undef,
1248 rule_name => 'reservesallowed',
1254 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1255 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1258 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1259 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1260 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1262 # Create library groups hierarchy
1263 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1264 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1265 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1268 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1269 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1271 # Create 3 biblios with items
1272 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1273 my $item_1 = $builder->build_sample_item(
1275 biblionumber => $biblio_1->biblionumber,
1276 library => $library1->branchcode
1279 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1280 my $item_2 = $builder->build_sample_item(
1282 biblionumber => $biblio_2->biblionumber,
1283 library => $library2->branchcode
1286 my $itemnumber_2 = $item_2->itemnumber;
1287 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1288 my $item_3 = $builder->build_sample_item(
1290 biblionumber => $biblio_3->biblionumber,
1291 library => $library1->branchcode
1295 # Test 1: Patron 3 can place hold
1297 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1299 'Patron can place hold if no circ_rules where defined'
1302 # Insert default circ rule of holds allowed only from local hold group for all libraries
1303 Koha::CirculationRules->set_rules(
1305 branchcode => undef,
1308 holdallowed => 'from_any_library',
1309 hold_fulfillment_policy => 'holdgroup',
1310 returnbranch => 'any'
1315 # Test 2: Patron 1 can place hold
1317 CanItemBeReserved( $patron3, $item_2, $library1->branchcode ),
1319 'Patron can place hold because pickup location is part of hold group'
1322 # Test 3: Patron 3 cannot place hold
1324 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1325 { status => 'pickupNotInHoldGroup' },
1326 'Patron cannot place hold because pickup location is not part of hold group'
1329 # Insert default circ rule to "any" for library 2
1330 Koha::CirculationRules->set_rules(
1332 branchcode => $library2->branchcode,
1335 holdallowed => 'from_any_library',
1336 hold_fulfillment_policy => 'any',
1337 returnbranch => 'any'
1342 # Test 4: Patron 3 can place hold
1344 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1346 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1349 # Update default circ rule to "hold group" for library 2
1350 Koha::CirculationRules->set_rules(
1352 branchcode => $library2->branchcode,
1355 holdallowed => 'from_any_library',
1356 hold_fulfillment_policy => 'holdgroup',
1357 returnbranch => 'any'
1362 # Test 5: Patron 3 cannot place hold
1364 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1365 { status => 'pickupNotInHoldGroup' },
1366 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1369 # Insert default item rule to "any" for itemtype 2
1370 Koha::CirculationRules->set_rules(
1372 branchcode => $library2->branchcode,
1373 itemtype => $itemtype2->itemtype,
1375 holdallowed => 'from_any_library',
1376 hold_fulfillment_policy => 'any',
1377 returnbranch => 'any'
1382 # Test 6: Patron 3 can place hold
1384 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1386 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1389 # Update default item rule to "hold group" for itemtype 2
1390 Koha::CirculationRules->set_rules(
1392 branchcode => $library2->branchcode,
1393 itemtype => $itemtype2->itemtype,
1395 holdallowed => 'from_any_library',
1396 hold_fulfillment_policy => 'holdgroup',
1397 returnbranch => 'any'
1402 # Test 7: Patron 3 cannot place hold
1404 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1405 { status => 'pickupNotInHoldGroup' },
1406 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1409 # Insert branch item rule to "any" for itemtype 2 and library 2
1410 Koha::CirculationRules->set_rules(
1412 branchcode => $library2->branchcode,
1413 itemtype => $itemtype2->itemtype,
1415 holdallowed => 'from_any_library',
1416 hold_fulfillment_policy => 'any',
1417 returnbranch => 'any'
1422 # Test 8: Patron 3 can place hold
1424 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1426 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1429 # Update branch item rule to "hold group" for itemtype 2 and library 2
1430 Koha::CirculationRules->set_rules(
1432 branchcode => $library2->branchcode,
1433 itemtype => $itemtype2->itemtype,
1435 holdallowed => 'from_any_library',
1436 hold_fulfillment_policy => 'holdgroup',
1437 returnbranch => 'any'
1442 # Test 9: Patron 3 cannot place hold
1444 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1445 { status => 'pickupNotInHoldGroup' },
1446 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1449 $schema->storage->txn_rollback;
1452 subtest 'non priority holds' => sub {
1456 $schema->storage->txn_begin;
1458 Koha::CirculationRules->set_rules(
1460 branchcode => undef,
1461 categorycode => undef,
1464 renewalsallowed => 5,
1465 reservesallowed => 5,
1470 my $item = $builder->build_sample_item;
1472 my $patron1 = $builder->build_object(
1474 class => 'Koha::Patrons',
1475 value => { branchcode => $item->homebranch }
1478 my $patron2 = $builder->build_object(
1480 class => 'Koha::Patrons',
1481 value => { branchcode => $item->homebranch }
1485 Koha::Checkout->new(
1487 borrowernumber => $patron1->borrowernumber,
1488 itemnumber => $item->itemnumber,
1489 branchcode => $item->homebranch
1493 my $hid = AddReserve(
1495 branchcode => $item->homebranch,
1496 borrowernumber => $patron2->borrowernumber,
1497 biblionumber => $item->biblionumber,
1499 itemnumber => $item->itemnumber,
1504 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1506 ok( !$ok, 'Cannot renew' );
1507 is( $err, 'on_reserve', 'Item is on hold' );
1509 my $hold = Koha::Holds->find($hid);
1510 $hold->non_priority(1)->store;
1513 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1515 ok( $ok, 'Can renew' );
1516 is( $err, undef, 'Item is on non priority hold' );
1518 my $patron3 = $builder->build_object(
1520 class => 'Koha::Patrons',
1521 value => { branchcode => $item->homebranch }
1525 # Add second hold with non_priority = 0
1528 branchcode => $item->homebranch,
1529 borrowernumber => $patron3->borrowernumber,
1530 biblionumber => $item->biblionumber,
1532 itemnumber => $item->itemnumber,
1537 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1539 ok( !$ok, 'Cannot renew' );
1540 is( $err, 'on_reserve', 'Item is on hold' );
1542 $schema->storage->txn_rollback;
1546 subtest 'CanItemBeReserved rule precedence tests' => sub {
1550 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1551 $schema->storage->txn_begin;
1552 my $library = $builder->build_object( { class => 'Koha::Libraries', value => {
1553 pickup_location => 1,
1555 my $item = $builder->build_sample_item({
1556 homebranch => $library->branchcode,
1557 holdingbranch => $library->branchcode
1559 my $item2 = $builder->build_sample_item({
1560 homebranch => $library->branchcode,
1561 holdingbranch => $library->branchcode,
1562 itype => $item->itype
1564 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
1565 branchcode => $library->branchcode
1567 Koha::CirculationRules->set_rules(
1569 branchcode => undef,
1570 categorycode => $patron->categorycode,
1571 itemtype => $item->itype,
1573 reservesallowed => 1,
1578 CanItemBeReserved( $patron, $item, $library->branchcode ),
1580 'Patron of specified category can place 1 hold on specified itemtype'
1582 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
1583 biblionumber => $item2->biblionumber,
1584 itemnumber => $item2->itemnumber,
1587 branchcode => $library->branchcode,
1588 borrowernumber => $patron->borrowernumber,
1591 CanItemBeReserved( $patron, $item, $library->branchcode ),
1592 { status => 'tooManyReserves', limit => 1 },
1593 'Patron of specified category can place 1 hold on specified itemtype, cannot place a second'
1595 Koha::CirculationRules->set_rules(
1597 branchcode => $library->branchcode,
1598 categorycode => undef,
1601 reservesallowed => 2,
1606 CanItemBeReserved( $patron, $item, $library->branchcode ),
1608 'Patron of specified category can place 1 hold on specified itemtype if library rule for all types and categories set to 2'
1611 $schema->storage->txn_rollback;
1615 subtest 'ModReserve can only update expirationdate for found holds' => sub {
1618 $schema->storage->txn_begin;
1620 my $category = $builder->build({ source => 'Category' });
1621 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
1622 my $biblio = $builder->build_sample_biblio( { itemtype => 'DUMMY' } );
1623 my $itemnumber = $builder->build_sample_item(
1624 { library => $branch, biblionumber => $biblio->biblionumber } )
1627 my $borrowernumber = Koha::Patron->new(
1629 firstname => 'my firstname',
1630 surname => 'whatever surname',
1631 categorycode => $category->{categorycode},
1632 branchcode => $branch,
1634 )->store->borrowernumber;
1636 my $reserve_id = AddReserve(
1638 branchcode => $branch,
1639 borrowernumber => $borrowernumber,
1640 biblionumber => $biblio->biblionumber,
1642 C4::Reserves::CalculatePriority( $biblio->biblionumber ),
1643 itemnumber => $itemnumber,
1647 my $hold = Koha::Holds->find($reserve_id);
1649 $hold->set( { priority => 0, found => 'W' } )->store();
1653 reserve_id => $hold->id,
1654 expirationdate => '1981-06-10',
1660 $hold = Koha::Holds->find($reserve_id);
1662 is( $hold->expirationdate, '1981-06-10',
1663 'Found hold expiration date updated correctly' );
1664 is( $hold->priority, '0', 'Found hold priority was not updated' );
1666 $schema->storage->txn_rollback;