6 use t::lib::TestBuilder;
10 use Test::More tests => 75;
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" );
388 CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'itemAlreadyOnHold',
389 "cannot request item that you have already reservedd"
392 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'OK',
393 "can request item if we are not checking holds counts, but only if policy allows or forbids it"
397 # Regression test for bug 9532
398 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
399 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
402 branchcode => $branch_1,
403 borrowernumber => $borrowernumbers[0],
404 biblionumber => $biblio->biblionumber,
409 CanItemBeReserved( $patrons[0], $item)->{status}, 'noReservesAllowed',
410 "cannot request item if policy that matches on item-level item type forbids it"
413 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'noReservesAllowed',
414 "cannot request item if policy that matches on item-level item type forbids it even if ignoring counts"
417 subtest 'CanItemBeReserved' => sub {
420 my $itemtype_can = $builder->build({source => "Itemtype"})->{itemtype};
421 my $itemtype_cant = $builder->build({source => "Itemtype"})->{itemtype};
422 my $itemtype_cant_record = $builder->build({source => "Itemtype"})->{itemtype};
424 Koha::CirculationRules->set_rules(
426 categorycode => undef,
428 itemtype => $itemtype_cant,
430 reservesallowed => 0,
431 holds_per_record => 99,
435 Koha::CirculationRules->set_rules(
437 categorycode => undef,
439 itemtype => $itemtype_can,
441 reservesallowed => 2,
442 holds_per_record => 2,
446 Koha::CirculationRules->set_rules(
448 categorycode => undef,
450 itemtype => $itemtype_cant_record,
452 reservesallowed => 0,
453 holds_per_record => 0,
458 Koha::CirculationRules->set_rules(
460 branchcode => $branch_1,
461 itemtype => $itemtype_cant,
464 returnbranch => 'homebranch',
468 Koha::CirculationRules->set_rules(
470 branchcode => $branch_1,
471 itemtype => $itemtype_can,
474 returnbranch => 'homebranch',
479 subtest 'noReservesAllowed' => sub {
482 my $biblionumber_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant })->biblionumber;
483 my $biblionumber_can = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
484 my $biblionumber_record_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant_record })->biblionumber;
486 my $item_1_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_cannot });
487 my $item_1_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_cannot });
488 my $item_2_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_can });
489 my $item_2_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_can });
490 my $item_3_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant_record, biblionumber => $biblionumber_record_cannot });
492 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
494 t::lib::Mocks::mock_preference('item-level_itypes', 1);
496 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'noReservesAllowed',
497 "With item level set, rule from item must be picked (CANNOT)"
500 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'OK',
501 "With item level set, rule from item must be picked (CAN)"
503 t::lib::Mocks::mock_preference('item-level_itypes', 0);
505 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'noReservesAllowed',
506 "With biblio level set, rule from biblio must be picked (CANNOT)"
509 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'OK',
510 "With biblio level set, rule from biblio must be picked (CAN)"
513 CanItemBeReserved( $patrons[0], $item_3_cannot)->{status}, 'noReservesAllowed',
514 "When no holds allowed and no holds per record allowed should return noReservesAllowed"
518 subtest 'tooManyHoldsForThisRecord + tooManyReserves + itemAlreadyOnHold' => sub {
521 my $biblionumber_1 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
522 my $item_11 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
523 my $item_12 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
524 my $biblionumber_2 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
525 my $item_21 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
526 my $item_22 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
528 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
532 branchcode => $branch_1,
533 borrowernumber => $borrowernumbers[0],
534 biblionumber => $biblionumber_1,
536 for my $item_level ( 0..1 ) {
537 t::lib::Mocks::mock_preference('item-level_itypes', $item_level);
539 # FIXME This is not really correct, but CanItemBeReserved does not check if biblio-level holds already exist
540 CanItemBeReserved( $patrons[0], $item_11)->{status}, 'OK',
541 "A biblio-level hold already exists - another hold can be placed on a specific item item"
545 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
548 branchcode => $branch_1,
549 borrowernumber => $borrowernumbers[0],
550 biblionumber => $biblionumber_1,
551 itemnumber => $item_11->itemnumber,
554 $dbh->do('DELETE FROM circulation_rules');
555 Koha::CirculationRules->set_rules(
557 categorycode => undef,
561 reservesallowed => 5,
562 holds_per_record => 1,
567 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
568 "A item-level hold already exists and holds_per_record=1, another hold cannot be placed on this record"
570 Koha::CirculationRules->set_rules(
572 categorycode => undef,
576 reservesallowed => 1,
577 holds_per_record => 1,
582 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
583 "A item-level hold already exists and holds_per_record=1 - tooManyHoldsForThisRecord has priority over tooManyReserves"
585 Koha::CirculationRules->set_rules(
587 categorycode => undef,
591 reservesallowed => 5,
592 holds_per_record => 2,
597 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'OK',
598 "A item-level hold already exists but holds_per_record=2- another item-level hold can be placed on this record"
602 branchcode => $branch_1,
603 borrowernumber => $borrowernumbers[0],
604 biblionumber => $biblionumber_2,
605 itemnumber => $item_21->itemnumber
607 Koha::CirculationRules->set_rules(
609 categorycode => undef,
613 reservesallowed => 2,
614 holds_per_record => 2,
619 CanItemBeReserved( $patrons[0], $item_21)->{status}, 'itemAlreadyOnHold',
620 "A item-level holds already exists on this item, itemAlreadyOnHold should be raised"
623 CanItemBeReserved( $patrons[0], $item_22)->{status}, 'tooManyReserves',
624 "This patron has already placed reservesallowed holds, tooManyReserves should be raised"
630 # Test branch item rules
632 $dbh->do('DELETE FROM circulation_rules');
633 Koha::CirculationRules->set_rules(
635 categorycode => undef,
639 reservesallowed => 25,
640 holds_per_record => 99,
644 Koha::CirculationRules->set_rules(
646 branchcode => $branch_1,
647 itemtype => 'CANNOT',
649 holdallowed => 'not_allowed',
650 returnbranch => 'homebranch',
654 Koha::CirculationRules->set_rules(
656 branchcode => $branch_1,
659 holdallowed => 'from_home_library',
660 returnbranch => 'homebranch',
664 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
665 my $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
666 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'notReservable',
667 "CanItemBeReserved should return 'notReservable'");
669 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
670 $branch_rule_item = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber});
671 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
672 'cannotReserveFromOtherBranches',
673 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
674 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
675 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
677 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
679 $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber});
680 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'OK',
681 "CanItemBeReserved should return 'OK'");
684 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
685 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
687 $dbh->do('DELETE FROM reserves');
688 $dbh->do('DELETE FROM issues');
689 $dbh->do('DELETE FROM items');
690 $dbh->do('DELETE FROM biblio');
692 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
693 my $limit_count_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
695 Koha::CirculationRules->set_rules(
697 categorycode => undef,
701 reservesallowed => 1,
702 holds_per_record => 99,
706 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
707 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
709 my $res_id = AddReserve(
711 branchcode => $branch_1,
712 borrowernumber => $borrowernumbers[0],
713 biblionumber => $biblio->biblionumber,
718 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
719 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
720 is( CanItemBeReserved( $patrons[0], $limit_count_item, undef, { ignore_hold_counts => 1 } )->{status},
721 'OK', 'Patron can reserve item if checking policy but not counts' );
723 #results should be the same for both ReservesControlBranch settings
724 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
725 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
726 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
727 #reset for further tests
728 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
730 subtest 'Test max_holds per library/patron category' => sub {
733 $dbh->do('DELETE FROM reserves');
735 $biblio = $builder->build_sample_biblio;
736 my $max_holds_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
737 Koha::CirculationRules->set_rules(
739 categorycode => undef,
741 itemtype => $biblio->itemtype,
743 reservesallowed => 99,
744 holds_per_record => 99,
752 branchcode => $branch_1,
753 borrowernumber => $borrowernumbers[0],
754 biblionumber => $biblio->biblionumber,
761 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
762 is( $count, 3, 'Patron now has 3 holds' );
764 my $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
765 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
767 my $rule_all = Koha::CirculationRules->set_rule(
769 categorycode => $category->{categorycode},
771 rule_name => 'max_holds',
776 my $rule_branch = Koha::CirculationRules->set_rule(
778 branchcode => $branch_1,
779 categorycode => $category->{categorycode},
780 rule_name => 'max_holds',
785 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
786 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
788 $rule_branch->delete();
790 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
791 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
794 $rule_branch->rule_value(3);
795 $rule_branch->store();
797 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
798 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
800 $rule_branch->rule_value(5);
801 $rule_branch->update();
802 $rule_branch->rule_value(5);
803 $rule_branch->store();
805 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
806 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
809 subtest 'Pickup location availability tests' => sub {
812 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
813 my $pickup_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
814 #Add a default rule to allow some holds
816 Koha::CirculationRules->set_rules(
819 categorycode => undef,
822 reservesallowed => 25,
823 holds_per_record => 99,
827 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
828 my $library = Koha::Libraries->find($branch_to);
829 $library->pickup_location('1')->store;
830 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
832 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
833 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
835 $library->pickup_location('1')->store;
836 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
837 'OK', 'Library is a pickup location');
839 my $limit = Koha::Item::Transfer::Limit->new({
840 fromBranch => $pickup_item->holdingbranch,
841 toBranch => $branch_to,
842 itemtype => $pickup_item->effective_itemtype,
844 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
845 'cannotBeTransferred', 'Item cannot be transferred');
848 $library->pickup_location('0')->store;
849 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
850 'libraryNotPickupLocation', 'Library is not a pickup location');
851 is(CanItemBeReserved($patron, $pickup_item, 'nonexistent')->{status},
852 'libraryNotFound', 'Cannot set unknown library as pickup location');
855 $schema->storage->txn_rollback;
857 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
861 $schema->storage->txn_begin;
863 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
864 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
865 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
867 # Create 3 biblios with items
868 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
869 my $item_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber});
870 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
871 my $item_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber});
872 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
873 my $item_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber});
875 Koha::CirculationRules->set_rules(
879 itemtype => $itemtype->itemtype,
881 reservesallowed => 1,
882 holds_per_record => 99,
889 CanItemBeReserved( $patron, $item_1 ),
891 'Patron can reserve item with hold limit of 1, no holds placed'
896 branchcode => $library->branchcode,
897 borrowernumber => $patron->borrowernumber,
898 biblionumber => $biblio_1->biblionumber,
904 CanItemBeReserved( $patron, $item_1 ),
905 { status => 'tooManyReserves', limit => 1 },
906 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
909 # Raise reservesallowed to avoid tooManyReserves from it
910 Koha::CirculationRules->set_rule(
915 itemtype => $itemtype->itemtype,
916 rule_name => 'reservesallowed',
922 CanItemBeReserved( $patron, $item_2 ),
924 'Patron can reserve item with 2 reserves daily cap'
927 # Add a second reserve
928 my $res_id = AddReserve(
930 branchcode => $library->branchcode,
931 borrowernumber => $patron->borrowernumber,
932 biblionumber => $biblio_2->biblionumber,
937 CanItemBeReserved( $patron, $item_2 ),
938 { status => 'tooManyReservesToday', limit => 2 },
939 'Patron cannot a third item with 2 reserves daily cap'
942 # Update last hold so reservedate is in the past, so 2 holds, but different day
943 $hold = Koha::Holds->find($res_id);
944 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
945 $hold->reservedate($yesterday)->store;
948 CanItemBeReserved( $patron, $item_2 ),
950 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
953 # Set holds_per_day to 0
954 Koha::CirculationRules->set_rule(
959 itemtype => $itemtype->itemtype,
960 rule_name => 'holds_per_day',
966 # Delete existing holds
967 Koha::Holds->search->delete;
969 CanItemBeReserved( $patron, $item_2 ),
970 { status => 'tooManyReservesToday', limit => 0 },
971 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
974 Koha::CirculationRules->set_rule(
979 itemtype => $itemtype->itemtype,
980 rule_name => 'holds_per_day',
985 Koha::Holds->search->delete;
987 CanItemBeReserved( $patron, $item_2 ),
989 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
993 branchcode => $library->branchcode,
994 borrowernumber => $patron->borrowernumber,
995 biblionumber => $biblio_1->biblionumber,
1001 branchcode => $library->branchcode,
1002 borrowernumber => $patron->borrowernumber,
1003 biblionumber => $biblio_2->biblionumber,
1009 CanItemBeReserved( $patron, $item_3 ),
1011 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
1015 branchcode => $library->branchcode,
1016 borrowernumber => $patron->borrowernumber,
1017 biblionumber => $biblio_3->biblionumber,
1022 CanItemBeReserved( $patron, $item_3 ),
1023 { status => 'tooManyReserves', limit => 3 },
1024 'Unlimited daily holds, but reached reservesallowed'
1026 #results should be the same for both ReservesControlBranch settings
1027 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1029 CanItemBeReserved( $patron, $item_3 ),
1030 { status => 'tooManyReserves', limit => 3 },
1031 'Unlimited daily holds, but reached reservesallowed'
1034 $schema->storage->txn_rollback;
1037 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
1040 $schema->storage->txn_begin;
1042 Koha::CirculationRules->set_rule(
1044 branchcode => undef,
1045 categorycode => undef,
1047 rule_name => 'reservesallowed',
1053 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1054 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1057 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1058 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1059 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
1061 # Create library groups hierarchy
1062 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1063 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1064 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1067 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1068 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1070 # Create 3 biblios with items
1071 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1072 my $item_1 = $builder->build_sample_item(
1074 biblionumber => $biblio_1->biblionumber,
1075 library => $library1->branchcode
1078 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1079 my $item_2 = $builder->build_sample_item(
1081 biblionumber => $biblio_2->biblionumber,
1082 library => $library2->branchcode
1085 my $itemnumber_2 = $item_2->itemnumber;
1086 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1087 my $item_3 = $builder->build_sample_item(
1089 biblionumber => $biblio_3->biblionumber,
1090 library => $library1->branchcode
1094 # Test 1: Patron 3 can place hold
1096 CanItemBeReserved( $patron3, $item_2 ),
1098 'Patron can place hold if no circ_rules where defined'
1101 # Insert default circ rule of holds allowed only from local hold group for all libraries
1102 Koha::CirculationRules->set_rules(
1104 branchcode => undef,
1107 holdallowed => 'from_local_hold_group',
1108 hold_fulfillment_policy => 'any',
1109 returnbranch => 'any'
1114 # Test 2: Patron 1 can place hold
1116 CanItemBeReserved( $patron1, $item_2 ),
1118 'Patron can place hold because patron\'s home library is part of hold group'
1121 # Test 3: Patron 3 cannot place hold
1123 CanItemBeReserved( $patron3, $item_2 ),
1124 { status => 'branchNotInHoldGroup' },
1125 'Patron cannot place hold because patron\'s home library is not part of hold group'
1128 # Insert default circ rule to "any" for library 2
1129 Koha::CirculationRules->set_rules(
1131 branchcode => $library2->branchcode,
1134 holdallowed => 'from_any_library',
1135 hold_fulfillment_policy => 'any',
1136 returnbranch => 'any'
1141 # Test 4: Patron 3 can place hold
1143 CanItemBeReserved( $patron3, $item_2 ),
1145 'Patron can place hold if holdallowed is set to "any" for library 2'
1148 # Update default circ rule to "hold group" for library 2
1149 Koha::CirculationRules->set_rules(
1151 branchcode => $library2->branchcode,
1154 holdallowed => 'from_local_hold_group',
1155 hold_fulfillment_policy => 'any',
1156 returnbranch => 'any'
1161 # Test 5: Patron 3 cannot place hold
1163 CanItemBeReserved( $patron3, $item_2 ),
1164 { status => 'branchNotInHoldGroup' },
1165 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
1168 # Insert default item rule to "any" for itemtype 2
1169 Koha::CirculationRules->set_rules(
1171 branchcode => $library2->branchcode,
1172 itemtype => $itemtype2->itemtype,
1174 holdallowed => 'from_any_library',
1175 hold_fulfillment_policy => 'any',
1176 returnbranch => 'any'
1181 # Test 6: Patron 3 can place hold
1183 CanItemBeReserved( $patron3, $item_2 ),
1185 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
1188 # Update default item rule to "hold group" for itemtype 2
1189 Koha::CirculationRules->set_rules(
1191 branchcode => $library2->branchcode,
1192 itemtype => $itemtype2->itemtype,
1194 holdallowed => 'from_local_hold_group',
1195 hold_fulfillment_policy => 'any',
1196 returnbranch => 'any'
1201 # Test 7: Patron 3 cannot place hold
1203 CanItemBeReserved( $patron3, $item_2 ),
1204 { status => 'branchNotInHoldGroup' },
1205 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
1208 # Insert branch item rule to "any" for itemtype 2 and library 2
1209 Koha::CirculationRules->set_rules(
1211 branchcode => $library2->branchcode,
1212 itemtype => $itemtype2->itemtype,
1214 holdallowed => 'from_any_library',
1215 hold_fulfillment_policy => 'any',
1216 returnbranch => 'any'
1221 # Test 8: Patron 3 can place hold
1223 CanItemBeReserved( $patron3, $item_2 ),
1225 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1228 # Update branch item rule to "hold group" for itemtype 2 and library 2
1229 Koha::CirculationRules->set_rules(
1231 branchcode => $library2->branchcode,
1232 itemtype => $itemtype2->itemtype,
1234 holdallowed => 'from_local_hold_group',
1235 hold_fulfillment_policy => 'any',
1236 returnbranch => 'any'
1241 # Test 9: Patron 3 cannot place hold
1243 CanItemBeReserved( $patron3, $item_2 ),
1244 { status => 'branchNotInHoldGroup' },
1245 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1248 $schema->storage->txn_rollback;
1252 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1255 $schema->storage->txn_begin;
1256 Koha::CirculationRules->set_rule(
1258 branchcode => undef,
1259 categorycode => undef,
1261 rule_name => 'reservesallowed',
1267 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1268 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1271 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1272 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1273 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1275 # Create library groups hierarchy
1276 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1277 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1278 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1281 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1282 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1284 # Create 3 biblios with items
1285 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1286 my $item_1 = $builder->build_sample_item(
1288 biblionumber => $biblio_1->biblionumber,
1289 library => $library1->branchcode
1292 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1293 my $item_2 = $builder->build_sample_item(
1295 biblionumber => $biblio_2->biblionumber,
1296 library => $library2->branchcode
1299 my $itemnumber_2 = $item_2->itemnumber;
1300 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1301 my $item_3 = $builder->build_sample_item(
1303 biblionumber => $biblio_3->biblionumber,
1304 library => $library1->branchcode
1308 # Test 1: Patron 3 can place hold
1310 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1312 'Patron can place hold if no circ_rules where defined'
1315 # Insert default circ rule of holds allowed only from local hold group for all libraries
1316 Koha::CirculationRules->set_rules(
1318 branchcode => undef,
1321 holdallowed => 'from_any_library',
1322 hold_fulfillment_policy => 'holdgroup',
1323 returnbranch => 'any'
1328 # Test 2: Patron 1 can place hold
1330 CanItemBeReserved( $patron3, $item_2, $library1->branchcode ),
1332 'Patron can place hold because pickup location is part of hold group'
1335 # Test 3: Patron 3 cannot place hold
1337 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1338 { status => 'pickupNotInHoldGroup' },
1339 'Patron cannot place hold because pickup location is not part of hold group'
1342 # Insert default circ rule to "any" for library 2
1343 Koha::CirculationRules->set_rules(
1345 branchcode => $library2->branchcode,
1348 holdallowed => 'from_any_library',
1349 hold_fulfillment_policy => 'any',
1350 returnbranch => 'any'
1355 # Test 4: Patron 3 can place hold
1357 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1359 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1362 # Update default circ rule to "hold group" for library 2
1363 Koha::CirculationRules->set_rules(
1365 branchcode => $library2->branchcode,
1368 holdallowed => 'from_any_library',
1369 hold_fulfillment_policy => 'holdgroup',
1370 returnbranch => 'any'
1375 # Test 5: Patron 3 cannot place hold
1377 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1378 { status => 'pickupNotInHoldGroup' },
1379 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1382 # Insert default item rule to "any" for itemtype 2
1383 Koha::CirculationRules->set_rules(
1385 branchcode => $library2->branchcode,
1386 itemtype => $itemtype2->itemtype,
1388 holdallowed => 'from_any_library',
1389 hold_fulfillment_policy => 'any',
1390 returnbranch => 'any'
1395 # Test 6: Patron 3 can place hold
1397 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1399 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1402 # Update default item rule to "hold group" for itemtype 2
1403 Koha::CirculationRules->set_rules(
1405 branchcode => $library2->branchcode,
1406 itemtype => $itemtype2->itemtype,
1408 holdallowed => 'from_any_library',
1409 hold_fulfillment_policy => 'holdgroup',
1410 returnbranch => 'any'
1415 # Test 7: Patron 3 cannot place hold
1417 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1418 { status => 'pickupNotInHoldGroup' },
1419 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1422 # Insert branch item rule to "any" for itemtype 2 and library 2
1423 Koha::CirculationRules->set_rules(
1425 branchcode => $library2->branchcode,
1426 itemtype => $itemtype2->itemtype,
1428 holdallowed => 'from_any_library',
1429 hold_fulfillment_policy => 'any',
1430 returnbranch => 'any'
1435 # Test 8: Patron 3 can place hold
1437 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1439 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1442 # Update branch item rule to "hold group" for itemtype 2 and library 2
1443 Koha::CirculationRules->set_rules(
1445 branchcode => $library2->branchcode,
1446 itemtype => $itemtype2->itemtype,
1448 holdallowed => 'from_any_library',
1449 hold_fulfillment_policy => 'holdgroup',
1450 returnbranch => 'any'
1455 # Test 9: Patron 3 cannot place hold
1457 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1458 { status => 'pickupNotInHoldGroup' },
1459 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1462 $schema->storage->txn_rollback;
1465 subtest 'non priority holds' => sub {
1469 $schema->storage->txn_begin;
1471 Koha::CirculationRules->set_rules(
1473 branchcode => undef,
1474 categorycode => undef,
1477 renewalsallowed => 5,
1478 reservesallowed => 5,
1483 my $item = $builder->build_sample_item;
1485 my $patron1 = $builder->build_object(
1487 class => 'Koha::Patrons',
1488 value => { branchcode => $item->homebranch }
1491 my $patron2 = $builder->build_object(
1493 class => 'Koha::Patrons',
1494 value => { branchcode => $item->homebranch }
1498 Koha::Checkout->new(
1500 borrowernumber => $patron1->borrowernumber,
1501 itemnumber => $item->itemnumber,
1502 branchcode => $item->homebranch
1506 my $hid = AddReserve(
1508 branchcode => $item->homebranch,
1509 borrowernumber => $patron2->borrowernumber,
1510 biblionumber => $item->biblionumber,
1512 itemnumber => $item->itemnumber,
1517 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1519 ok( !$ok, 'Cannot renew' );
1520 is( $err, 'on_reserve', 'Item is on hold' );
1522 my $hold = Koha::Holds->find($hid);
1523 $hold->non_priority(1)->store;
1526 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1528 ok( $ok, 'Can renew' );
1529 is( $err, undef, 'Item is on non priority hold' );
1531 my $patron3 = $builder->build_object(
1533 class => 'Koha::Patrons',
1534 value => { branchcode => $item->homebranch }
1538 # Add second hold with non_priority = 0
1541 branchcode => $item->homebranch,
1542 borrowernumber => $patron3->borrowernumber,
1543 biblionumber => $item->biblionumber,
1545 itemnumber => $item->itemnumber,
1550 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1552 ok( !$ok, 'Cannot renew' );
1553 is( $err, 'on_reserve', 'Item is on hold' );
1555 $schema->storage->txn_rollback;
1558 subtest 'CanItemBeReserved / recall' => sub {
1561 $schema->storage->txn_begin;
1563 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1564 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1565 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1566 my $biblio1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1567 my $item1 = $builder->build_sample_item(
1569 biblionumber => $biblio1->biblionumber,
1570 library => $library1->branchcode
1574 patron_id => $patron1->borrowernumber,
1575 biblio_id => $biblio1->biblionumber,
1576 pickup_library_id => $library1->branchcode,
1577 item_id => $item1->itemnumber,
1578 created_date => '2020-05-04 10:10:10',
1581 is( CanItemBeReserved( $patron1, $item1, $library1->branchcode )->{status}, 'recall', "Can't reserve an item that they have already recalled" );
1583 $schema->storage->txn_rollback;
1586 subtest 'CanItemBeReserved rule precedence tests' => sub {
1590 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1591 $schema->storage->txn_begin;
1592 my $library = $builder->build_object( { class => 'Koha::Libraries', value => {
1593 pickup_location => 1,
1595 my $item = $builder->build_sample_item({
1596 homebranch => $library->branchcode,
1597 holdingbranch => $library->branchcode
1599 my $item2 = $builder->build_sample_item({
1600 homebranch => $library->branchcode,
1601 holdingbranch => $library->branchcode,
1602 itype => $item->itype
1604 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
1605 branchcode => $library->branchcode
1607 Koha::CirculationRules->set_rules(
1609 branchcode => undef,
1610 categorycode => $patron->categorycode,
1611 itemtype => $item->itype,
1613 reservesallowed => 1,
1618 CanItemBeReserved( $patron, $item, $library->branchcode ),
1620 'Patron of specified category can place 1 hold on specified itemtype'
1622 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
1623 biblionumber => $item2->biblionumber,
1624 itemnumber => $item2->itemnumber,
1627 branchcode => $library->branchcode,
1628 borrowernumber => $patron->borrowernumber,
1631 CanItemBeReserved( $patron, $item, $library->branchcode ),
1632 { status => 'tooManyReserves', limit => 1 },
1633 'Patron of specified category can place 1 hold on specified itemtype, cannot place a second'
1635 Koha::CirculationRules->set_rules(
1637 branchcode => $library->branchcode,
1638 categorycode => undef,
1641 reservesallowed => 2,
1646 CanItemBeReserved( $patron, $item, $library->branchcode ),
1648 'Patron of specified category can place 1 hold on specified itemtype if library rule for all types and categories set to 2'
1651 $schema->storage->txn_rollback;
1655 subtest 'ModReserve can only update expirationdate for found holds' => sub {
1658 $schema->storage->txn_begin;
1660 my $category = $builder->build({ source => 'Category' });
1661 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
1662 my $biblio = $builder->build_sample_biblio( { itemtype => 'DUMMY' } );
1663 my $itemnumber = $builder->build_sample_item(
1664 { library => $branch, biblionumber => $biblio->biblionumber } )
1667 my $borrowernumber = Koha::Patron->new(
1669 firstname => 'my firstname',
1670 surname => 'whatever surname',
1671 categorycode => $category->{categorycode},
1672 branchcode => $branch,
1674 )->store->borrowernumber;
1676 my $reserve_id = AddReserve(
1678 branchcode => $branch,
1679 borrowernumber => $borrowernumber,
1680 biblionumber => $biblio->biblionumber,
1682 C4::Reserves::CalculatePriority( $biblio->biblionumber ),
1683 itemnumber => $itemnumber,
1687 my $hold = Koha::Holds->find($reserve_id);
1689 $hold->set( { priority => 0, found => 'W' } )->store();
1693 reserve_id => $hold->id,
1694 expirationdate => '1981-06-10',
1700 $hold = Koha::Holds->find($reserve_id);
1702 is( $hold->expirationdate, '1981-06-10',
1703 'Found hold expiration date updated correctly' );
1704 is( $hold->priority, '0', 'Found hold priority was not updated' );
1706 $schema->storage->txn_rollback;
1710 subtest 'Koha::Holds->get_items_that_can_fill returns items with datecancelled or (inclusive) datearrived' => sub {
1712 # biblio item with date arrived and date cancelled
1713 my $biblio1 = $builder->build_sample_biblio();
1714 my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1716 my $transfer1 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1717 datecancelled => '2022-06-12',
1718 itemnumber => $item1->itemnumber
1721 my $hold1 = $builder->build_object({ class => 'Koha::Holds', value => {
1722 biblionumber => $biblio1->biblionumber,
1723 itemnumber => undef,
1728 # biblio item with date arrived and NO date cancelled
1729 my $biblio2 = $builder->build_sample_biblio();
1730 my $item2 = $builder->build_sample_item({ biblionumber => $biblio2->biblionumber });
1732 my $transfer2 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1733 datecancelled => undef,
1734 itemnumber => $item2->itemnumber
1737 my $hold2 = $builder->build_object({ class => 'Koha::Holds', value => {
1738 biblionumber => $biblio2->biblionumber,
1739 itemnumber => undef,
1744 # biblio item with NO date arrived and date cancelled
1745 my $biblio3 = $builder->build_sample_biblio();
1746 my $item3 = $builder->build_sample_item({ biblionumber => $biblio3->biblionumber });
1748 my $transfer3 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1749 datecancelled => '2022-06-12',
1750 itemnumber => $item3->itemnumber,
1751 datearrived => undef
1754 my $hold3 = $builder->build_object({ class => 'Koha::Holds', value => {
1755 biblionumber => $biblio3->biblionumber,
1756 itemnumber => undef,
1762 # biblio item with NO date arrived and NO date cancelled
1763 my $biblio4 = $builder->build_sample_biblio();
1764 my $item4 = $builder->build_sample_item({ biblionumber => $biblio4->biblionumber });
1766 my $transfer4 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1767 datecancelled => undef,
1768 itemnumber => $item4->itemnumber,
1769 datearrived => undef
1772 my $hold4 = $builder->build_object({ class => 'Koha::Holds', value => {
1773 biblionumber => $biblio4->biblionumber,
1774 itemnumber => undef,
1779 # create the holds which get_items_that_can_fill will be ran on
1780 my $holds1 = Koha::Holds->search({reserve_id => $hold1->id});
1781 my $holds2 = Koha::Holds->search({reserve_id => $hold2->id});
1782 my $holds3 = Koha::Holds->search({reserve_id => $hold3->id});
1783 my $holds4 = Koha::Holds->search({reserve_id => $hold4->id});
1785 my $items_that_can_fill1 = $holds1->get_items_that_can_fill;
1786 my $items_that_can_fill2 = $holds2->get_items_that_can_fill;
1787 my $items_that_can_fill3 = $holds3->get_items_that_can_fill;
1788 my $items_that_can_fill4 = $holds4->get_items_that_can_fill;
1790 is($items_that_can_fill1->next->id, $item1->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and datecancelled");
1791 is($items_that_can_fill1->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1792 is($items_that_can_fill2->next->id, $item2->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and undefined datecancelled");
1793 is($items_that_can_fill2->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1794 is($items_that_can_fill3->next->id, $item3->id, "Koha::Holds->get_items_that_can_fill returns item with undefined datearrived and defined datecancelled");
1795 is($items_that_can_fill3->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1796 is($items_that_can_fill4->next, undef, "Koha::Holds->get_items_that_can_fill doesn't return item with undefined datearrived and undefined datecancelled");
1797 is($items_that_can_fill4->count, 0, "Koha::Holds->get_items_that_can_fill returns 0 item");