6 use t::lib::TestBuilder;
10 use Test::More tests => 76;
18 use C4::Reserves qw( AddReserve CalculatePriority ModReserve ToggleSuspend AutoUnsuspendReserves SuspendAll ModReserveMinusPriority AlterPriority CanItemBeReserved CheckReserves );
19 use C4::Circulation qw( CanBookBeRenewed );
22 use Koha::CirculationRules;
24 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::Holds qw( search );
27 use Koha::Item::Transfer::Limits;
30 use Koha::Library::Groups;
32 use Koha::Hold qw( get_items_that_can_fill );
33 use Koha::Item::Transfers;
37 use lib $FindBin::Bin;
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
43 my $builder = t::lib::TestBuilder->new();
44 my $dbh = C4::Context->dbh;
46 # Create two random branches
47 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
48 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
50 my $category = $builder->build({ source => 'Category' });
52 my $borrowers_count = 5;
54 $dbh->do('DELETE FROM itemtypes');
55 $dbh->do('DELETE FROM reserves');
56 $dbh->do('DELETE FROM circulation_rules');
57 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
58 $insert_sth->execute('CAN');
59 $insert_sth->execute('CANNOT');
60 $insert_sth->execute('DUMMY');
61 $insert_sth->execute('ONLY1');
63 # Setup Test------------------------
64 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
66 # Create item instance for testing.
67 my $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
69 # Create some borrowers
72 foreach (1..$borrowers_count) {
73 my $patron = Koha::Patron->new({
74 firstname => 'my firstname',
75 surname => 'my surname ' . $_,
76 categorycode => $category->{categorycode},
77 branchcode => $branch_1,
79 push @patrons, $patron;
80 push @borrowernumbers, $patron->borrowernumber;
83 # Create five item level holds
84 foreach my $borrowernumber ( @borrowernumbers ) {
87 branchcode => $branch_1,
88 borrowernumber => $borrowernumber,
89 biblionumber => $biblio->biblionumber,
90 priority => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
91 itemnumber => $itemnumber,
96 my $holds = $biblio->holds;
97 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
98 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
99 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
100 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
101 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
102 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
104 my $item = Koha::Items->find( $itemnumber );
105 $holds = $item->current_holds;
106 my $first_hold = $holds->next;
107 my $reservedate = $first_hold->reservedate;
108 my $borrowernumber = $first_hold->borrowernumber;
109 my $branch_1code = $first_hold->branchcode;
110 my $reserve_id = $first_hold->reserve_id;
111 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
112 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
113 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
114 ok($reserve_id, "Test holds_placed_today()");
116 my $hold = Koha::Holds->find( $reserve_id );
117 ok( $hold, "Koha::Holds found the hold" );
118 my $hold_biblio = $hold->biblio();
119 ok( $hold_biblio, "Got biblio using biblio() method" );
120 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
121 my $hold_item = $hold->item();
122 ok( $hold_item, "Got item using item() method" );
123 ok( $hold_item == $hold->item(), "item method returns stashed item" );
124 my $hold_branch = $hold->branch();
125 ok( $hold_branch, "Got branch using branch() method" );
126 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
127 my $hold_found = $hold->found();
128 $hold->set({ found => 'W'})->store();
129 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
130 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
132 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
133 $holds = $patron->holds;
134 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
137 $holds = $item->current_holds;
138 $first_hold = $holds->next;
139 $borrowernumber = $first_hold->borrowernumber;
140 $branch_1code = $first_hold->branchcode;
141 $reserve_id = $first_hold->reserve_id;
144 reserve_id => $reserve_id,
146 branchcode => $branch_1,
147 itemnumber => $itemnumber,
148 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
151 $hold = Koha::Holds->find( $reserve_id );
152 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
153 ok( $hold->suspend, "Test ModReserve, suspend hold" );
154 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
156 ModReserve({ # call without reserve_id
158 branchcode => $branch_1,
159 biblionumber => $biblio->biblionumber,
160 itemnumber => $itemnumber,
161 borrowernumber => $borrowernumber,
163 $hold = Koha::Holds->find( $reserve_id );
164 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
166 ToggleSuspend( $reserve_id );
167 $hold = Koha::Holds->find( $reserve_id );
168 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
170 ToggleSuspend( $reserve_id, '2012-01-01' );
171 $hold = Koha::Holds->find( $reserve_id );
172 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
174 AutoUnsuspendReserves();
175 $hold = Koha::Holds->find( $reserve_id );
176 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
179 borrowernumber => $borrowernumber,
180 biblionumber => $biblio->biblionumber,
182 suspend_until => '2012-01-01',
184 $hold = Koha::Holds->find( $reserve_id );
185 is( $hold->suspend, 1, "Test SuspendAll()" );
186 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
189 borrowernumber => $borrowernumber,
190 biblionumber => $biblio->biblionumber,
193 $hold = Koha::Holds->find( $reserve_id );
194 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
195 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
197 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
200 branchcode => $branch_1,
201 borrowernumber => $borrowernumbers[0],
202 biblionumber => $biblio->biblionumber,
206 $patron = Koha::Patrons->find( $borrowernumber );
207 $holds = $patron->holds;
208 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
209 ModReserveMinusPriority( $itemnumber, $reserveid );
210 $holds = $patron->holds;
211 is( $holds->search({ itemnumber => $itemnumber })->count, 1, "Test ModReserveMinusPriority()" );
213 $holds = $biblio->holds;
214 $hold = $holds->next;
215 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
216 $hold = Koha::Holds->find( $reserveid );
217 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
219 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '2', "Test AlterPriority(), move down" );
223 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
224 $hold = Koha::Holds->find( $reserveid );
225 is( $hold->priority, '1', "Test AlterPriority(), move up" );
227 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
228 $hold = Koha::Holds->find( $reserveid );
229 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
234 { C4::Reserves::ModReserve({ reserve_id => $hold->reserve_id }) }
235 'Koha::Exceptions::ObjectNotFound',
236 'No hold with id ' . $hold->reserve_id;
238 # Regression test for bug 2394
240 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
241 # a patron is not permittedo to request an item whose homebranch (i.e.,
242 # owner of the item) is different from the patron's own library.
243 # However, if canreservefromotherbranches is turned ON, the patron can
244 # create such hold requests.
246 # Note that canreservefromotherbranches has no effect if
247 # IndependentBranches is OFF.
249 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
250 my $foreign_item = $builder->build_sample_item({ library => $branch_2, biblionumber => $foreign_biblio->biblionumber });
251 Koha::CirculationRules->set_rules(
253 categorycode => undef,
257 reservesallowed => 25,
258 holds_per_record => 99,
262 Koha::CirculationRules->set_rules(
264 categorycode => undef,
266 itemtype => 'CANNOT',
268 reservesallowed => 0,
269 holds_per_record => 99,
274 # make sure some basic sysprefs are set
275 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
276 t::lib::Mocks::mock_preference('item-level_itypes', 1);
278 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
279 t::lib::Mocks::mock_preference('IndependentBranches', 0);
282 CanItemBeReserved($patrons[0], $foreign_item)->{status}, 'OK',
283 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
286 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
287 t::lib::Mocks::mock_preference('IndependentBranches', 1);
288 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
290 CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'cannotReserveFromOtherBranches',
291 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
294 # ... unless canreservefromotherbranches is ON
295 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
297 CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'OK',
298 '... unless canreservefromotherbranches is ON (bug 2394)'
302 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
303 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
304 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
305 my $reserveid1 = AddReserve(
307 branchcode => $branch_1,
308 borrowernumber => $borrowernumbers[0],
309 biblionumber => $biblio->biblionumber,
314 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
315 my $reserveid2 = AddReserve(
317 branchcode => $branch_1,
318 borrowernumber => $borrowernumbers[1],
319 biblionumber => $biblio->biblionumber,
324 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
325 my $reserveid3 = AddReserve(
327 branchcode => $branch_1,
328 borrowernumber => $borrowernumbers[2],
329 biblionumber => $biblio->biblionumber,
334 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
335 my $hold3 = Koha::Holds->find( $reserveid3 );
336 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
337 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
338 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
339 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
342 my $damaged_item = Koha::Items->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
343 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
344 is( CanItemBeReserved( $patrons[0], $damaged_item)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
345 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
347 $hold = Koha::Hold->new(
349 borrowernumber => $borrowernumbers[0],
350 itemnumber => $itemnumber,
351 biblionumber => $biblio->biblionumber,
352 branchcode => $branch_1,
355 is( CanItemBeReserved( $patrons[0], $damaged_item )->{status},
357 "Patron cannot place a second item level hold for a given item" );
360 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
361 ok( CanItemBeReserved( $patrons[0], $damaged_item)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
362 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
364 # Items that are not for loan, but holdable should not be trapped until they are available for loan
365 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 0 );
366 my $nfl_item = Koha::Items->find($itemnumber)->damaged(0)->notforloan(-1)->store;
367 Koha::Holds->search({ biblionumber => $biblio->id })->delete();
368 is( CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
369 $hold = Koha::Hold->new(
371 borrowernumber => $borrowernumbers[0],
372 itemnumber => $itemnumber,
373 biblionumber => $biblio->biblionumber,
376 reservedate => dt_from_string,
377 branchcode => $branch_1,
380 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
381 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 1 );
382 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold is trapped for item that is not for loan but holdable ( notforloan < 0 )" );
383 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1' );
384 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
385 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1|1' );
386 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
387 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '' );
388 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
389 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
390 $item_group_1->add_item({ item_id => $itemnumber });
391 $hold->item_group_id( $item_group_2->id )->update;
392 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with non-matching item group" );
394 CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'itemAlreadyOnHold',
395 "cannot request item that you have already reservedd"
398 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'OK',
399 "can request item if we are not checking holds counts, but only if policy allows or forbids it"
403 # Regression test for bug 9532
404 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
405 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
408 branchcode => $branch_1,
409 borrowernumber => $borrowernumbers[0],
410 biblionumber => $biblio->biblionumber,
415 CanItemBeReserved( $patrons[0], $item)->{status}, 'noReservesAllowed',
416 "cannot request item if policy that matches on item-level item type forbids it"
419 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'noReservesAllowed',
420 "cannot request item if policy that matches on item-level item type forbids it even if ignoring counts"
423 subtest 'CanItemBeReserved' => sub {
426 my $itemtype_can = $builder->build({source => "Itemtype"})->{itemtype};
427 my $itemtype_cant = $builder->build({source => "Itemtype"})->{itemtype};
428 my $itemtype_cant_record = $builder->build({source => "Itemtype"})->{itemtype};
430 Koha::CirculationRules->set_rules(
432 categorycode => undef,
434 itemtype => $itemtype_cant,
436 reservesallowed => 0,
437 holds_per_record => 99,
441 Koha::CirculationRules->set_rules(
443 categorycode => undef,
445 itemtype => $itemtype_can,
447 reservesallowed => 2,
448 holds_per_record => 2,
452 Koha::CirculationRules->set_rules(
454 categorycode => undef,
456 itemtype => $itemtype_cant_record,
458 reservesallowed => 0,
459 holds_per_record => 0,
464 Koha::CirculationRules->set_rules(
466 branchcode => $branch_1,
467 itemtype => $itemtype_cant,
470 returnbranch => 'homebranch',
474 Koha::CirculationRules->set_rules(
476 branchcode => $branch_1,
477 itemtype => $itemtype_can,
480 returnbranch => 'homebranch',
485 subtest 'noReservesAllowed' => sub {
488 my $biblionumber_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant })->biblionumber;
489 my $biblionumber_can = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
490 my $biblionumber_record_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant_record })->biblionumber;
492 my $item_1_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_cannot });
493 my $item_1_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_cannot });
494 my $item_2_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_can });
495 my $item_2_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_can });
496 my $item_3_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant_record, biblionumber => $biblionumber_record_cannot });
498 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
500 t::lib::Mocks::mock_preference('item-level_itypes', 1);
502 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'noReservesAllowed',
503 "With item level set, rule from item must be picked (CANNOT)"
506 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'OK',
507 "With item level set, rule from item must be picked (CAN)"
509 t::lib::Mocks::mock_preference('item-level_itypes', 0);
511 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'noReservesAllowed',
512 "With biblio level set, rule from biblio must be picked (CANNOT)"
515 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'OK',
516 "With biblio level set, rule from biblio must be picked (CAN)"
519 CanItemBeReserved( $patrons[0], $item_3_cannot)->{status}, 'noReservesAllowed',
520 "When no holds allowed and no holds per record allowed should return noReservesAllowed"
524 subtest 'tooManyHoldsForThisRecord + tooManyReserves + itemAlreadyOnHold' => sub {
527 my $biblionumber_1 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
528 my $item_11 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
529 my $item_12 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
530 my $biblionumber_2 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
531 my $item_21 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
532 my $item_22 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
534 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
538 branchcode => $branch_1,
539 borrowernumber => $borrowernumbers[0],
540 biblionumber => $biblionumber_1,
542 for my $item_level ( 0..1 ) {
543 t::lib::Mocks::mock_preference('item-level_itypes', $item_level);
545 # FIXME This is not really correct, but CanItemBeReserved does not check if biblio-level holds already exist
546 CanItemBeReserved( $patrons[0], $item_11)->{status}, 'OK',
547 "A biblio-level hold already exists - another hold can be placed on a specific item item"
551 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
554 branchcode => $branch_1,
555 borrowernumber => $borrowernumbers[0],
556 biblionumber => $biblionumber_1,
557 itemnumber => $item_11->itemnumber,
560 $dbh->do('DELETE FROM circulation_rules');
561 Koha::CirculationRules->set_rules(
563 categorycode => undef,
567 reservesallowed => 5,
568 holds_per_record => 1,
573 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
574 "A item-level hold already exists and holds_per_record=1, another hold cannot be placed on this record"
576 Koha::CirculationRules->set_rules(
578 categorycode => undef,
582 reservesallowed => 1,
583 holds_per_record => 1,
588 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
589 "A item-level hold already exists and holds_per_record=1 - tooManyHoldsForThisRecord has priority over tooManyReserves"
591 Koha::CirculationRules->set_rules(
593 categorycode => undef,
597 reservesallowed => 5,
598 holds_per_record => 2,
603 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'OK',
604 "A item-level hold already exists but holds_per_record=2- another item-level hold can be placed on this record"
608 branchcode => $branch_1,
609 borrowernumber => $borrowernumbers[0],
610 biblionumber => $biblionumber_2,
611 itemnumber => $item_21->itemnumber
613 Koha::CirculationRules->set_rules(
615 categorycode => undef,
619 reservesallowed => 2,
620 holds_per_record => 2,
625 CanItemBeReserved( $patrons[0], $item_21)->{status}, 'itemAlreadyOnHold',
626 "A item-level holds already exists on this item, itemAlreadyOnHold should be raised"
629 CanItemBeReserved( $patrons[0], $item_22)->{status}, 'tooManyReserves',
630 "This patron has already placed reservesallowed holds, tooManyReserves should be raised"
636 # Test branch item rules
638 $dbh->do('DELETE FROM circulation_rules');
639 Koha::CirculationRules->set_rules(
641 categorycode => undef,
645 reservesallowed => 25,
646 holds_per_record => 99,
650 Koha::CirculationRules->set_rules(
652 branchcode => $branch_1,
653 itemtype => 'CANNOT',
655 holdallowed => 'not_allowed',
656 returnbranch => 'homebranch',
660 Koha::CirculationRules->set_rules(
662 branchcode => $branch_1,
665 holdallowed => 'from_home_library',
666 returnbranch => 'homebranch',
670 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
671 my $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
672 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'notReservable',
673 "CanItemBeReserved should return 'notReservable'");
675 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
676 $branch_rule_item = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber});
677 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
678 'cannotReserveFromOtherBranches',
679 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
680 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
681 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
683 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
685 $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber});
686 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'OK',
687 "CanItemBeReserved should return 'OK'");
690 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
691 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
693 $dbh->do('DELETE FROM reserves');
694 $dbh->do('DELETE FROM issues');
695 $dbh->do('DELETE FROM items');
696 $dbh->do('DELETE FROM biblio');
698 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
699 my $limit_count_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
701 Koha::CirculationRules->set_rules(
703 categorycode => undef,
707 reservesallowed => 1,
708 holds_per_record => 99,
712 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
713 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
715 my $res_id = AddReserve(
717 branchcode => $branch_1,
718 borrowernumber => $borrowernumbers[0],
719 biblionumber => $biblio->biblionumber,
724 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
725 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
726 is( CanItemBeReserved( $patrons[0], $limit_count_item, undef, { ignore_hold_counts => 1 } )->{status},
727 'OK', 'Patron can reserve item if checking policy but not counts' );
729 #results should be the same for both ReservesControlBranch settings
730 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
731 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
732 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
733 #reset for further tests
734 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
736 subtest 'Test max_holds per library/patron category' => sub {
739 $dbh->do('DELETE FROM reserves');
741 $biblio = $builder->build_sample_biblio;
742 my $max_holds_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
743 Koha::CirculationRules->set_rules(
745 categorycode => undef,
747 itemtype => $biblio->itemtype,
749 reservesallowed => 99,
750 holds_per_record => 99,
758 branchcode => $branch_1,
759 borrowernumber => $borrowernumbers[0],
760 biblionumber => $biblio->biblionumber,
767 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
768 is( $count, 3, 'Patron now has 3 holds' );
770 my $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
771 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
773 my $rule_all = Koha::CirculationRules->set_rule(
775 categorycode => $category->{categorycode},
777 rule_name => 'max_holds',
782 my $rule_branch = Koha::CirculationRules->set_rule(
784 branchcode => $branch_1,
785 categorycode => $category->{categorycode},
786 rule_name => 'max_holds',
791 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
792 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
794 $rule_branch->delete();
796 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
797 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
800 $rule_branch->rule_value(3);
801 $rule_branch->store();
803 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
804 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
806 $rule_branch->rule_value(5);
807 $rule_branch->update();
808 $rule_branch->rule_value(5);
809 $rule_branch->store();
811 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
812 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
815 subtest 'Pickup location availability tests' => sub {
818 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
819 my $pickup_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
820 #Add a default rule to allow some holds
822 Koha::CirculationRules->set_rules(
825 categorycode => undef,
828 reservesallowed => 25,
829 holds_per_record => 99,
833 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
834 my $library = Koha::Libraries->find($branch_to);
835 $library->pickup_location('1')->store;
836 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
838 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
839 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
841 $library->pickup_location('1')->store;
842 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
843 'OK', 'Library is a pickup location');
845 my $limit = Koha::Item::Transfer::Limit->new({
846 fromBranch => $pickup_item->holdingbranch,
847 toBranch => $branch_to,
848 itemtype => $pickup_item->effective_itemtype,
850 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
851 'cannotBeTransferred', 'Item cannot be transferred');
854 $library->pickup_location('0')->store;
855 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
856 'libraryNotPickupLocation', 'Library is not a pickup location');
857 is(CanItemBeReserved($patron, $pickup_item, 'nonexistent')->{status},
858 'libraryNotFound', 'Cannot set unknown library as pickup location');
861 $schema->storage->txn_rollback;
863 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
867 $schema->storage->txn_begin;
869 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
870 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
871 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
873 # Create 3 biblios with items
874 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
875 my $item_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber});
876 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
877 my $item_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber});
878 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
879 my $item_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber});
881 Koha::CirculationRules->set_rules(
885 itemtype => $itemtype->itemtype,
887 reservesallowed => 1,
888 holds_per_record => 99,
895 CanItemBeReserved( $patron, $item_1 ),
897 'Patron can reserve item with hold limit of 1, no holds placed'
902 branchcode => $library->branchcode,
903 borrowernumber => $patron->borrowernumber,
904 biblionumber => $biblio_1->biblionumber,
910 CanItemBeReserved( $patron, $item_1 ),
911 { status => 'tooManyReserves', limit => 1 },
912 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
915 # Raise reservesallowed to avoid tooManyReserves from it
916 Koha::CirculationRules->set_rule(
921 itemtype => $itemtype->itemtype,
922 rule_name => 'reservesallowed',
928 CanItemBeReserved( $patron, $item_2 ),
930 'Patron can reserve item with 2 reserves daily cap'
933 # Add a second reserve
934 my $res_id = AddReserve(
936 branchcode => $library->branchcode,
937 borrowernumber => $patron->borrowernumber,
938 biblionumber => $biblio_2->biblionumber,
943 CanItemBeReserved( $patron, $item_2 ),
944 { status => 'tooManyReservesToday', limit => 2 },
945 'Patron cannot a third item with 2 reserves daily cap'
948 # Update last hold so reservedate is in the past, so 2 holds, but different day
949 $hold = Koha::Holds->find($res_id);
950 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
951 $hold->reservedate($yesterday)->store;
954 CanItemBeReserved( $patron, $item_2 ),
956 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
959 # Set holds_per_day to 0
960 Koha::CirculationRules->set_rule(
965 itemtype => $itemtype->itemtype,
966 rule_name => 'holds_per_day',
972 # Delete existing holds
973 Koha::Holds->search->delete;
975 CanItemBeReserved( $patron, $item_2 ),
976 { status => 'tooManyReservesToday', limit => 0 },
977 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
980 Koha::CirculationRules->set_rule(
985 itemtype => $itemtype->itemtype,
986 rule_name => 'holds_per_day',
991 Koha::Holds->search->delete;
993 CanItemBeReserved( $patron, $item_2 ),
995 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
999 branchcode => $library->branchcode,
1000 borrowernumber => $patron->borrowernumber,
1001 biblionumber => $biblio_1->biblionumber,
1007 branchcode => $library->branchcode,
1008 borrowernumber => $patron->borrowernumber,
1009 biblionumber => $biblio_2->biblionumber,
1015 CanItemBeReserved( $patron, $item_3 ),
1017 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
1021 branchcode => $library->branchcode,
1022 borrowernumber => $patron->borrowernumber,
1023 biblionumber => $biblio_3->biblionumber,
1028 CanItemBeReserved( $patron, $item_3 ),
1029 { status => 'tooManyReserves', limit => 3 },
1030 'Unlimited daily holds, but reached reservesallowed'
1032 #results should be the same for both ReservesControlBranch settings
1033 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1035 CanItemBeReserved( $patron, $item_3 ),
1036 { status => 'tooManyReserves', limit => 3 },
1037 'Unlimited daily holds, but reached reservesallowed'
1040 $schema->storage->txn_rollback;
1043 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
1046 $schema->storage->txn_begin;
1048 Koha::CirculationRules->set_rule(
1050 branchcode => undef,
1051 categorycode => undef,
1053 rule_name => 'reservesallowed',
1059 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1060 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1063 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1064 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1065 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
1067 # Create library groups hierarchy
1068 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1069 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1070 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1073 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1074 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1076 # Create 3 biblios with items
1077 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1078 my $item_1 = $builder->build_sample_item(
1080 biblionumber => $biblio_1->biblionumber,
1081 library => $library1->branchcode
1084 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1085 my $item_2 = $builder->build_sample_item(
1087 biblionumber => $biblio_2->biblionumber,
1088 library => $library2->branchcode
1091 my $itemnumber_2 = $item_2->itemnumber;
1092 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1093 my $item_3 = $builder->build_sample_item(
1095 biblionumber => $biblio_3->biblionumber,
1096 library => $library1->branchcode
1100 # Test 1: Patron 3 can place hold
1102 CanItemBeReserved( $patron3, $item_2 ),
1104 'Patron can place hold if no circ_rules where defined'
1107 # Insert default circ rule of holds allowed only from local hold group for all libraries
1108 Koha::CirculationRules->set_rules(
1110 branchcode => undef,
1113 holdallowed => 'from_local_hold_group',
1114 hold_fulfillment_policy => 'any',
1115 returnbranch => 'any'
1120 # Test 2: Patron 1 can place hold
1122 CanItemBeReserved( $patron1, $item_2 ),
1124 'Patron can place hold because patron\'s home library is part of hold group'
1127 # Test 3: Patron 3 cannot place hold
1129 CanItemBeReserved( $patron3, $item_2 ),
1130 { status => 'branchNotInHoldGroup' },
1131 'Patron cannot place hold because patron\'s home library is not part of hold group'
1134 # Insert default circ rule to "any" for library 2
1135 Koha::CirculationRules->set_rules(
1137 branchcode => $library2->branchcode,
1140 holdallowed => 'from_any_library',
1141 hold_fulfillment_policy => 'any',
1142 returnbranch => 'any'
1147 # Test 4: Patron 3 can place hold
1149 CanItemBeReserved( $patron3, $item_2 ),
1151 'Patron can place hold if holdallowed is set to "any" for library 2'
1154 # Update default circ rule to "hold group" for library 2
1155 Koha::CirculationRules->set_rules(
1157 branchcode => $library2->branchcode,
1160 holdallowed => 'from_local_hold_group',
1161 hold_fulfillment_policy => 'any',
1162 returnbranch => 'any'
1167 # Test 5: Patron 3 cannot place hold
1169 CanItemBeReserved( $patron3, $item_2 ),
1170 { status => 'branchNotInHoldGroup' },
1171 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
1174 # Insert default item rule to "any" for itemtype 2
1175 Koha::CirculationRules->set_rules(
1177 branchcode => $library2->branchcode,
1178 itemtype => $itemtype2->itemtype,
1180 holdallowed => 'from_any_library',
1181 hold_fulfillment_policy => 'any',
1182 returnbranch => 'any'
1187 # Test 6: Patron 3 can place hold
1189 CanItemBeReserved( $patron3, $item_2 ),
1191 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
1194 # Update default item rule to "hold group" for itemtype 2
1195 Koha::CirculationRules->set_rules(
1197 branchcode => $library2->branchcode,
1198 itemtype => $itemtype2->itemtype,
1200 holdallowed => 'from_local_hold_group',
1201 hold_fulfillment_policy => 'any',
1202 returnbranch => 'any'
1207 # Test 7: Patron 3 cannot place hold
1209 CanItemBeReserved( $patron3, $item_2 ),
1210 { status => 'branchNotInHoldGroup' },
1211 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
1214 # Insert branch item rule to "any" for itemtype 2 and library 2
1215 Koha::CirculationRules->set_rules(
1217 branchcode => $library2->branchcode,
1218 itemtype => $itemtype2->itemtype,
1220 holdallowed => 'from_any_library',
1221 hold_fulfillment_policy => 'any',
1222 returnbranch => 'any'
1227 # Test 8: Patron 3 can place hold
1229 CanItemBeReserved( $patron3, $item_2 ),
1231 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1234 # Update branch item rule to "hold group" for itemtype 2 and library 2
1235 Koha::CirculationRules->set_rules(
1237 branchcode => $library2->branchcode,
1238 itemtype => $itemtype2->itemtype,
1240 holdallowed => 'from_local_hold_group',
1241 hold_fulfillment_policy => 'any',
1242 returnbranch => 'any'
1247 # Test 9: Patron 3 cannot place hold
1249 CanItemBeReserved( $patron3, $item_2 ),
1250 { status => 'branchNotInHoldGroup' },
1251 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1254 $schema->storage->txn_rollback;
1258 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1261 $schema->storage->txn_begin;
1262 Koha::CirculationRules->set_rule(
1264 branchcode => undef,
1265 categorycode => undef,
1267 rule_name => 'reservesallowed',
1273 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1274 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1277 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1278 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1279 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1281 # Create library groups hierarchy
1282 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1283 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1284 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1287 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1288 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1290 # Create 3 biblios with items
1291 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1292 my $item_1 = $builder->build_sample_item(
1294 biblionumber => $biblio_1->biblionumber,
1295 library => $library1->branchcode
1298 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1299 my $item_2 = $builder->build_sample_item(
1301 biblionumber => $biblio_2->biblionumber,
1302 library => $library2->branchcode
1305 my $itemnumber_2 = $item_2->itemnumber;
1306 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1307 my $item_3 = $builder->build_sample_item(
1309 biblionumber => $biblio_3->biblionumber,
1310 library => $library1->branchcode
1314 # Test 1: Patron 3 can place hold
1316 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1318 'Patron can place hold if no circ_rules where defined'
1321 # Insert default circ rule of holds allowed only from local hold group for all libraries
1322 Koha::CirculationRules->set_rules(
1324 branchcode => undef,
1327 holdallowed => 'from_any_library',
1328 hold_fulfillment_policy => 'holdgroup',
1329 returnbranch => 'any'
1334 # Test 2: Patron 1 can place hold
1336 CanItemBeReserved( $patron3, $item_2, $library1->branchcode ),
1338 'Patron can place hold because pickup location is part of hold group'
1341 # Test 3: Patron 3 cannot place hold
1343 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1344 { status => 'pickupNotInHoldGroup' },
1345 'Patron cannot place hold because pickup location is not part of hold group'
1348 # Insert default circ rule to "any" for library 2
1349 Koha::CirculationRules->set_rules(
1351 branchcode => $library2->branchcode,
1354 holdallowed => 'from_any_library',
1355 hold_fulfillment_policy => 'any',
1356 returnbranch => 'any'
1361 # Test 4: Patron 3 can place hold
1363 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1365 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1368 # Update default circ rule to "hold group" for library 2
1369 Koha::CirculationRules->set_rules(
1371 branchcode => $library2->branchcode,
1374 holdallowed => 'from_any_library',
1375 hold_fulfillment_policy => 'holdgroup',
1376 returnbranch => 'any'
1381 # Test 5: Patron 3 cannot place hold
1383 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1384 { status => 'pickupNotInHoldGroup' },
1385 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1388 # Insert default item rule to "any" for itemtype 2
1389 Koha::CirculationRules->set_rules(
1391 branchcode => $library2->branchcode,
1392 itemtype => $itemtype2->itemtype,
1394 holdallowed => 'from_any_library',
1395 hold_fulfillment_policy => 'any',
1396 returnbranch => 'any'
1401 # Test 6: Patron 3 can place hold
1403 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1405 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1408 # Update default item rule to "hold group" for itemtype 2
1409 Koha::CirculationRules->set_rules(
1411 branchcode => $library2->branchcode,
1412 itemtype => $itemtype2->itemtype,
1414 holdallowed => 'from_any_library',
1415 hold_fulfillment_policy => 'holdgroup',
1416 returnbranch => 'any'
1421 # Test 7: Patron 3 cannot place hold
1423 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1424 { status => 'pickupNotInHoldGroup' },
1425 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1428 # Insert branch item rule to "any" for itemtype 2 and library 2
1429 Koha::CirculationRules->set_rules(
1431 branchcode => $library2->branchcode,
1432 itemtype => $itemtype2->itemtype,
1434 holdallowed => 'from_any_library',
1435 hold_fulfillment_policy => 'any',
1436 returnbranch => 'any'
1441 # Test 8: Patron 3 can place hold
1443 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1445 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1448 # Update branch item rule to "hold group" for itemtype 2 and library 2
1449 Koha::CirculationRules->set_rules(
1451 branchcode => $library2->branchcode,
1452 itemtype => $itemtype2->itemtype,
1454 holdallowed => 'from_any_library',
1455 hold_fulfillment_policy => 'holdgroup',
1456 returnbranch => 'any'
1461 # Test 9: Patron 3 cannot place hold
1463 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1464 { status => 'pickupNotInHoldGroup' },
1465 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1468 $schema->storage->txn_rollback;
1471 subtest 'non priority holds' => sub {
1475 $schema->storage->txn_begin;
1477 Koha::CirculationRules->set_rules(
1479 branchcode => undef,
1480 categorycode => undef,
1483 renewalsallowed => 5,
1484 reservesallowed => 5,
1489 my $item = $builder->build_sample_item;
1491 my $patron1 = $builder->build_object(
1493 class => 'Koha::Patrons',
1494 value => { branchcode => $item->homebranch }
1497 my $patron2 = $builder->build_object(
1499 class => 'Koha::Patrons',
1500 value => { branchcode => $item->homebranch }
1504 Koha::Checkout->new(
1506 borrowernumber => $patron1->borrowernumber,
1507 itemnumber => $item->itemnumber,
1508 branchcode => $item->homebranch
1512 my $hid = AddReserve(
1514 branchcode => $item->homebranch,
1515 borrowernumber => $patron2->borrowernumber,
1516 biblionumber => $item->biblionumber,
1518 itemnumber => $item->itemnumber,
1523 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1525 ok( !$ok, 'Cannot renew' );
1526 is( $err, 'on_reserve', 'Item is on hold' );
1528 my $hold = Koha::Holds->find($hid);
1529 $hold->non_priority(1)->store;
1532 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1534 ok( $ok, 'Can renew' );
1535 is( $err, undef, 'Item is on non priority hold' );
1537 my $patron3 = $builder->build_object(
1539 class => 'Koha::Patrons',
1540 value => { branchcode => $item->homebranch }
1544 # Add second hold with non_priority = 0
1547 branchcode => $item->homebranch,
1548 borrowernumber => $patron3->borrowernumber,
1549 biblionumber => $item->biblionumber,
1551 itemnumber => $item->itemnumber,
1556 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1558 ok( !$ok, 'Cannot renew' );
1559 is( $err, 'on_reserve', 'Item is on hold' );
1561 $schema->storage->txn_rollback;
1564 subtest 'CanItemBeReserved / recall' => sub {
1567 $schema->storage->txn_begin;
1569 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1570 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1571 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1572 my $biblio1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1573 my $item1 = $builder->build_sample_item(
1575 biblionumber => $biblio1->biblionumber,
1576 library => $library1->branchcode
1580 patron_id => $patron1->borrowernumber,
1581 biblio_id => $biblio1->biblionumber,
1582 pickup_library_id => $library1->branchcode,
1583 item_id => $item1->itemnumber,
1584 created_date => '2020-05-04 10:10:10',
1587 is( CanItemBeReserved( $patron1, $item1, $library1->branchcode )->{status}, 'recall', "Can't reserve an item that they have already recalled" );
1589 $schema->storage->txn_rollback;
1592 subtest 'CanItemBeReserved rule precedence tests' => sub {
1596 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1597 $schema->storage->txn_begin;
1598 my $library = $builder->build_object( { class => 'Koha::Libraries', value => {
1599 pickup_location => 1,
1601 my $item = $builder->build_sample_item({
1602 homebranch => $library->branchcode,
1603 holdingbranch => $library->branchcode
1605 my $item2 = $builder->build_sample_item({
1606 homebranch => $library->branchcode,
1607 holdingbranch => $library->branchcode,
1608 itype => $item->itype
1610 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
1611 branchcode => $library->branchcode
1613 Koha::CirculationRules->set_rules(
1615 branchcode => undef,
1616 categorycode => $patron->categorycode,
1617 itemtype => $item->itype,
1619 reservesallowed => 1,
1624 CanItemBeReserved( $patron, $item, $library->branchcode ),
1626 'Patron of specified category can place 1 hold on specified itemtype'
1628 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
1629 biblionumber => $item2->biblionumber,
1630 itemnumber => $item2->itemnumber,
1633 branchcode => $library->branchcode,
1634 borrowernumber => $patron->borrowernumber,
1637 CanItemBeReserved( $patron, $item, $library->branchcode ),
1638 { status => 'tooManyReserves', limit => 1 },
1639 'Patron of specified category can place 1 hold on specified itemtype, cannot place a second'
1641 Koha::CirculationRules->set_rules(
1643 branchcode => $library->branchcode,
1644 categorycode => undef,
1647 reservesallowed => 2,
1652 CanItemBeReserved( $patron, $item, $library->branchcode ),
1654 'Patron of specified category can place 1 hold on specified itemtype if library rule for all types and categories set to 2'
1657 $schema->storage->txn_rollback;
1661 subtest 'ModReserve can only update expirationdate for found holds' => sub {
1664 $schema->storage->txn_begin;
1666 my $category = $builder->build({ source => 'Category' });
1667 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
1668 my $biblio = $builder->build_sample_biblio( { itemtype => 'DUMMY' } );
1669 my $itemnumber = $builder->build_sample_item(
1670 { library => $branch, biblionumber => $biblio->biblionumber } )
1673 my $borrowernumber = Koha::Patron->new(
1675 firstname => 'my firstname',
1676 surname => 'whatever surname',
1677 categorycode => $category->{categorycode},
1678 branchcode => $branch,
1680 )->store->borrowernumber;
1682 my $reserve_id = AddReserve(
1684 branchcode => $branch,
1685 borrowernumber => $borrowernumber,
1686 biblionumber => $biblio->biblionumber,
1688 C4::Reserves::CalculatePriority( $biblio->biblionumber ),
1689 itemnumber => $itemnumber,
1693 my $hold = Koha::Holds->find($reserve_id);
1695 $hold->set( { priority => 0, found => 'W' } )->store();
1699 reserve_id => $hold->id,
1700 expirationdate => '1981-06-10',
1706 $hold = Koha::Holds->find($reserve_id);
1708 is( $hold->expirationdate, '1981-06-10',
1709 'Found hold expiration date updated correctly' );
1710 is( $hold->priority, '0', 'Found hold priority was not updated' );
1712 $schema->storage->txn_rollback;
1716 subtest 'Koha::Holds->get_items_that_can_fill returns items with datecancelled or (inclusive) datearrived' => sub {
1719 $schema->storage->txn_begin;
1721 # biblio item with date arrived and date cancelled
1722 my $biblio1 = $builder->build_sample_biblio();
1723 my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1725 my $transfer1 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1726 datecancelled => '2022-06-12',
1727 itemnumber => $item1->itemnumber
1730 my $hold1 = $builder->build_object({ class => 'Koha::Holds', value => {
1731 biblionumber => $biblio1->biblionumber,
1732 itemnumber => undef,
1737 # biblio item with date arrived and NO date cancelled
1738 my $biblio2 = $builder->build_sample_biblio();
1739 my $item2 = $builder->build_sample_item({ biblionumber => $biblio2->biblionumber });
1741 my $transfer2 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1742 datecancelled => undef,
1743 itemnumber => $item2->itemnumber
1746 my $hold2 = $builder->build_object({ class => 'Koha::Holds', value => {
1747 biblionumber => $biblio2->biblionumber,
1748 itemnumber => undef,
1753 # biblio item with NO date arrived and date cancelled
1754 my $biblio3 = $builder->build_sample_biblio();
1755 my $item3 = $builder->build_sample_item({ biblionumber => $biblio3->biblionumber });
1757 my $transfer3 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1758 datecancelled => '2022-06-12',
1759 itemnumber => $item3->itemnumber,
1760 datearrived => undef
1763 my $hold3 = $builder->build_object({ class => 'Koha::Holds', value => {
1764 biblionumber => $biblio3->biblionumber,
1765 itemnumber => undef,
1771 # biblio item with NO date arrived and NO date cancelled
1772 my $biblio4 = $builder->build_sample_biblio();
1773 my $item4 = $builder->build_sample_item({ biblionumber => $biblio4->biblionumber });
1775 my $transfer4 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1776 datecancelled => undef,
1777 itemnumber => $item4->itemnumber,
1778 datearrived => undef
1781 my $hold4 = $builder->build_object({ class => 'Koha::Holds', value => {
1782 biblionumber => $biblio4->biblionumber,
1783 itemnumber => undef,
1788 # create the holds which get_items_that_can_fill will be ran on
1789 my $holds1 = Koha::Holds->search({reserve_id => $hold1->id});
1790 my $holds2 = Koha::Holds->search({reserve_id => $hold2->id});
1791 my $holds3 = Koha::Holds->search({reserve_id => $hold3->id});
1792 my $holds4 = Koha::Holds->search({reserve_id => $hold4->id});
1794 my $items_that_can_fill1 = $holds1->get_items_that_can_fill;
1795 my $items_that_can_fill2 = $holds2->get_items_that_can_fill;
1796 my $items_that_can_fill3 = $holds3->get_items_that_can_fill;
1797 my $items_that_can_fill4 = $holds4->get_items_that_can_fill;
1799 is($items_that_can_fill1->next->id, $item1->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and datecancelled");
1800 is($items_that_can_fill1->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1801 is($items_that_can_fill2->next->id, $item2->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and undefined datecancelled");
1802 is($items_that_can_fill2->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1803 is($items_that_can_fill3->next->id, $item3->id, "Koha::Holds->get_items_that_can_fill returns item with undefined datearrived and defined datecancelled");
1804 is($items_that_can_fill3->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1805 is($items_that_can_fill4->next, undef, "Koha::Holds->get_items_that_can_fill doesn't return item with undefined datearrived and undefined datecancelled");
1806 is($items_that_can_fill4->count, 0, "Koha::Holds->get_items_that_can_fill returns 0 item");
1808 $schema->storage->txn_rollback;