6 use t::lib::TestBuilder;
10 use Test::More tests => 73;
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 $item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber });
68 my $itemnumber = $item->itemnumber;
70 # Create some borrowers
73 foreach (1..$borrowers_count) {
74 my $patron = Koha::Patron->new({
75 firstname => 'my firstname',
76 surname => 'my surname ' . $_,
77 categorycode => $category->{categorycode},
78 branchcode => $branch_1,
80 push @patrons, $patron;
81 push @borrowernumbers, $patron->borrowernumber;
84 # Create five item level holds
85 foreach my $borrowernumber ( @borrowernumbers ) {
88 branchcode => $branch_1,
89 borrowernumber => $borrowernumber,
90 biblionumber => $biblio->biblionumber,
91 priority => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
92 itemnumber => $itemnumber,
97 my $holds = $biblio->holds;
98 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
99 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
100 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
101 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
102 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
103 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
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 my $hold_item = $hold->item();
121 ok( $hold_item, "Got item using item() method" );
122 my $hold_branch = $hold->branch();
123 ok( $hold_branch, "Got branch using branch() method" );
124 my $hold_found = $hold->found();
125 $hold->set({ found => 'W'})->store();
126 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
127 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
129 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
130 $holds = $patron->holds;
131 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
134 $holds = $item->current_holds;
135 $first_hold = $holds->next;
136 $borrowernumber = $first_hold->borrowernumber;
137 $branch_1code = $first_hold->branchcode;
138 $reserve_id = $first_hold->reserve_id;
141 reserve_id => $reserve_id,
143 branchcode => $branch_1,
144 itemnumber => $itemnumber,
145 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
148 $hold = Koha::Holds->find( $reserve_id );
149 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
150 ok( $hold->suspend, "Test ModReserve, suspend hold" );
151 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
153 ModReserve({ # call without reserve_id
155 branchcode => $branch_1,
156 biblionumber => $biblio->biblionumber,
157 itemnumber => $itemnumber,
158 borrowernumber => $borrowernumber,
160 $hold = Koha::Holds->find( $reserve_id );
161 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
163 ToggleSuspend( $reserve_id );
164 $hold = Koha::Holds->find( $reserve_id );
165 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
167 ToggleSuspend( $reserve_id, '2012-01-01' );
168 $hold = Koha::Holds->find( $reserve_id );
169 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
171 AutoUnsuspendReserves();
172 $hold = Koha::Holds->find( $reserve_id );
173 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
176 borrowernumber => $borrowernumber,
177 biblionumber => $biblio->biblionumber,
179 suspend_until => '2012-01-01',
181 $hold = Koha::Holds->find( $reserve_id );
182 is( $hold->suspend, 1, "Test SuspendAll()" );
183 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
186 borrowernumber => $borrowernumber,
187 biblionumber => $biblio->biblionumber,
190 $hold = Koha::Holds->find( $reserve_id );
191 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
192 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
194 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
197 branchcode => $branch_1,
198 borrowernumber => $borrowernumbers[0],
199 biblionumber => $biblio->biblionumber,
203 $patron = Koha::Patrons->find( $borrowernumber );
204 $holds = $patron->holds;
205 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
206 ModReserveMinusPriority( $itemnumber, $reserveid );
207 $holds = $patron->holds;
208 is( $holds->search({ itemnumber => $itemnumber })->count, 1, "Test ModReserveMinusPriority()" );
210 $holds = $biblio->holds;
211 $hold = $holds->next;
212 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
213 $hold = Koha::Holds->find( $reserveid );
214 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
216 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
217 $hold = Koha::Holds->find( $reserveid );
218 is( $hold->priority, '2', "Test AlterPriority(), move down" );
220 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
221 $hold = Koha::Holds->find( $reserveid );
222 is( $hold->priority, '1', "Test AlterPriority(), move up" );
224 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
225 $hold = Koha::Holds->find( $reserveid );
226 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
231 { C4::Reserves::ModReserve({ reserve_id => $hold->reserve_id }) }
232 'Koha::Exceptions::ObjectNotFound',
233 'No hold with id ' . $hold->reserve_id;
235 # Regression test for bug 2394
237 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
238 # a patron is not permittedo to request an item whose homebranch (i.e.,
239 # owner of the item) is different from the patron's own library.
240 # However, if canreservefromotherbranches is turned ON, the patron can
241 # create such hold requests.
243 # Note that canreservefromotherbranches has no effect if
244 # IndependentBranches is OFF.
246 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
247 my $foreign_item = $builder->build_sample_item({ library => $branch_2, biblionumber => $foreign_biblio->biblionumber });
248 Koha::CirculationRules->set_rules(
250 categorycode => undef,
254 reservesallowed => 25,
255 holds_per_record => 99,
259 Koha::CirculationRules->set_rules(
261 categorycode => undef,
263 itemtype => 'CANNOT',
265 reservesallowed => 0,
266 holds_per_record => 99,
271 # make sure some basic sysprefs are set
272 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
273 t::lib::Mocks::mock_preference('item-level_itypes', 1);
275 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
276 t::lib::Mocks::mock_preference('IndependentBranches', 0);
279 CanItemBeReserved($patrons[0], $foreign_item)->{status}, 'OK',
280 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
283 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
284 t::lib::Mocks::mock_preference('IndependentBranches', 1);
285 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
287 CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'cannotReserveFromOtherBranches',
288 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
291 # ... unless canreservefromotherbranches is ON
292 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
294 CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'OK',
295 '... unless canreservefromotherbranches is ON (bug 2394)'
299 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
300 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
301 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
302 my $reserveid1 = AddReserve(
304 branchcode => $branch_1,
305 borrowernumber => $borrowernumbers[0],
306 biblionumber => $biblio->biblionumber,
311 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
312 my $reserveid2 = AddReserve(
314 branchcode => $branch_1,
315 borrowernumber => $borrowernumbers[1],
316 biblionumber => $biblio->biblionumber,
321 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
322 my $reserveid3 = AddReserve(
324 branchcode => $branch_1,
325 borrowernumber => $borrowernumbers[2],
326 biblionumber => $biblio->biblionumber,
331 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
332 my $hold3 = Koha::Holds->find( $reserveid3 );
333 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
334 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
335 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
336 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
339 my $damaged_item = Koha::Items->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
340 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
341 is( CanItemBeReserved( $patrons[0], $damaged_item)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
342 ok( defined( ( CheckReserves($damaged_item) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
344 $hold = Koha::Hold->new(
346 borrowernumber => $borrowernumbers[0],
347 itemnumber => $itemnumber,
348 biblionumber => $biblio->biblionumber,
349 branchcode => $branch_1,
352 is( CanItemBeReserved( $patrons[0], $damaged_item )->{status},
354 "Patron cannot place a second item level hold for a given item" );
357 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
358 ok( CanItemBeReserved( $patrons[0], $damaged_item)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
359 ok( !defined( ( CheckReserves($damaged_item) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
361 # Items that are not for loan, but holdable should not be trapped until they are available for loan
362 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 0 );
363 my $nfl_item = Koha::Items->find($itemnumber)->damaged(0)->notforloan(-1)->store;
364 Koha::Holds->search({ biblionumber => $biblio->id })->delete();
365 is( CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
366 $hold = Koha::Hold->new(
368 borrowernumber => $borrowernumbers[0],
369 itemnumber => $itemnumber,
370 biblionumber => $biblio->biblionumber,
373 reservedate => dt_from_string,
374 branchcode => $branch_1,
377 ok( !defined( ( CheckReserves($nfl_item) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
378 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 1 );
379 ok( defined( ( CheckReserves($nfl_item) )[1] ), "Hold is trapped for item that is not for loan but holdable ( notforloan < 0 )" );
380 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1' );
381 ok( !defined( ( CheckReserves($nfl_item) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
382 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1|1' );
383 ok( !defined( ( CheckReserves($nfl_item) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
384 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '' );
385 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
386 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
387 $item_group_1->add_item({ item_id => $itemnumber });
388 $hold->item_group_id( $item_group_2->id )->update;
389 ok( !defined( ( CheckReserves($nfl_item) )[1] ), "Hold cannot be trapped for item with non-matching item group" );
391 CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'itemAlreadyOnHold',
392 "cannot request item that you have already reservedd"
395 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'OK',
396 "can request item if we are not checking holds counts, but only if policy allows or forbids it"
400 # Regression test for bug 9532
401 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
402 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
405 branchcode => $branch_1,
406 borrowernumber => $borrowernumbers[0],
407 biblionumber => $biblio->biblionumber,
412 CanItemBeReserved( $patrons[0], $item)->{status}, 'noReservesAllowed',
413 "cannot request item if policy that matches on item-level item type forbids it"
416 CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'noReservesAllowed',
417 "cannot request item if policy that matches on item-level item type forbids it even if ignoring counts"
420 subtest 'CanItemBeReserved' => sub {
423 my $itemtype_can = $builder->build({source => "Itemtype"})->{itemtype};
424 my $itemtype_cant = $builder->build({source => "Itemtype"})->{itemtype};
425 my $itemtype_cant_record = $builder->build({source => "Itemtype"})->{itemtype};
427 Koha::CirculationRules->set_rules(
429 categorycode => undef,
431 itemtype => $itemtype_cant,
433 reservesallowed => 0,
434 holds_per_record => 99,
438 Koha::CirculationRules->set_rules(
440 categorycode => undef,
442 itemtype => $itemtype_can,
444 reservesallowed => 2,
445 holds_per_record => 2,
449 Koha::CirculationRules->set_rules(
451 categorycode => undef,
453 itemtype => $itemtype_cant_record,
455 reservesallowed => 0,
456 holds_per_record => 0,
461 Koha::CirculationRules->set_rules(
463 branchcode => $branch_1,
464 itemtype => $itemtype_cant,
467 returnbranch => 'homebranch',
471 Koha::CirculationRules->set_rules(
473 branchcode => $branch_1,
474 itemtype => $itemtype_can,
477 returnbranch => 'homebranch',
482 subtest 'noReservesAllowed' => sub {
485 my $biblionumber_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant })->biblionumber;
486 my $biblionumber_can = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
487 my $biblionumber_record_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant_record })->biblionumber;
489 my $item_1_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_cannot });
490 my $item_1_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_cannot });
491 my $item_2_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_can });
492 my $item_2_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_can });
493 my $item_3_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant_record, biblionumber => $biblionumber_record_cannot });
495 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
497 t::lib::Mocks::mock_preference('item-level_itypes', 1);
499 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'noReservesAllowed',
500 "With item level set, rule from item must be picked (CANNOT)"
503 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'OK',
504 "With item level set, rule from item must be picked (CAN)"
506 t::lib::Mocks::mock_preference('item-level_itypes', 0);
508 CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'noReservesAllowed',
509 "With biblio level set, rule from biblio must be picked (CANNOT)"
512 CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'OK',
513 "With biblio level set, rule from biblio must be picked (CAN)"
516 CanItemBeReserved( $patrons[0], $item_3_cannot)->{status}, 'noReservesAllowed',
517 "When no holds allowed and no holds per record allowed should return noReservesAllowed"
521 subtest 'tooManyHoldsForThisRecord + tooManyReserves + itemAlreadyOnHold' => sub {
524 my $biblionumber_1 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
525 my $item_11 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
526 my $item_12 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
527 my $biblionumber_2 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
528 my $item_21 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
529 my $item_22 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
531 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
535 branchcode => $branch_1,
536 borrowernumber => $borrowernumbers[0],
537 biblionumber => $biblionumber_1,
539 for my $item_level ( 0..1 ) {
540 t::lib::Mocks::mock_preference('item-level_itypes', $item_level);
542 # FIXME This is not really correct, but CanItemBeReserved does not check if biblio-level holds already exist
543 CanItemBeReserved( $patrons[0], $item_11)->{status}, 'OK',
544 "A biblio-level hold already exists - another hold can be placed on a specific item item"
548 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
551 branchcode => $branch_1,
552 borrowernumber => $borrowernumbers[0],
553 biblionumber => $biblionumber_1,
554 itemnumber => $item_11->itemnumber,
557 $dbh->do('DELETE FROM circulation_rules');
558 Koha::CirculationRules->set_rules(
560 categorycode => undef,
564 reservesallowed => 5,
565 holds_per_record => 1,
570 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
571 "A item-level hold already exists and holds_per_record=1, another hold cannot be placed on this record"
573 Koha::CirculationRules->set_rules(
575 categorycode => undef,
579 reservesallowed => 1,
580 holds_per_record => 1,
585 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
586 "A item-level hold already exists and holds_per_record=1 - tooManyHoldsForThisRecord has priority over tooManyReserves"
588 Koha::CirculationRules->set_rules(
590 categorycode => undef,
594 reservesallowed => 5,
595 holds_per_record => 2,
600 CanItemBeReserved( $patrons[0], $item_12)->{status}, 'OK',
601 "A item-level hold already exists but holds_per_record=2- another item-level hold can be placed on this record"
605 branchcode => $branch_1,
606 borrowernumber => $borrowernumbers[0],
607 biblionumber => $biblionumber_2,
608 itemnumber => $item_21->itemnumber
610 Koha::CirculationRules->set_rules(
612 categorycode => undef,
616 reservesallowed => 2,
617 holds_per_record => 2,
622 CanItemBeReserved( $patrons[0], $item_21)->{status}, 'itemAlreadyOnHold',
623 "A item-level holds already exists on this item, itemAlreadyOnHold should be raised"
626 CanItemBeReserved( $patrons[0], $item_22)->{status}, 'tooManyReserves',
627 "This patron has already placed reservesallowed holds, tooManyReserves should be raised"
633 # Test branch item rules
635 $dbh->do('DELETE FROM circulation_rules');
636 Koha::CirculationRules->set_rules(
638 categorycode => undef,
642 reservesallowed => 25,
643 holds_per_record => 99,
647 Koha::CirculationRules->set_rules(
649 branchcode => $branch_1,
650 itemtype => 'CANNOT',
652 holdallowed => 'not_allowed',
653 returnbranch => 'homebranch',
657 Koha::CirculationRules->set_rules(
659 branchcode => $branch_1,
662 holdallowed => 'from_home_library',
663 returnbranch => 'homebranch',
667 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
668 my $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
669 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'notReservable',
670 "CanItemBeReserved should return 'notReservable'");
672 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
673 $branch_rule_item = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber});
674 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
675 'cannotReserveFromOtherBranches',
676 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
677 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
678 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
680 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
682 $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber});
683 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'OK',
684 "CanItemBeReserved should return 'OK'");
687 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
688 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
690 $dbh->do('DELETE FROM reserves');
691 $dbh->do('DELETE FROM issues');
692 $dbh->do('DELETE FROM items');
693 $dbh->do('DELETE FROM biblio');
695 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
696 my $limit_count_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
698 Koha::CirculationRules->set_rules(
700 categorycode => undef,
704 reservesallowed => 1,
705 holds_per_record => 99,
709 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
710 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
712 my $res_id = AddReserve(
714 branchcode => $branch_1,
715 borrowernumber => $borrowernumbers[0],
716 biblionumber => $biblio->biblionumber,
721 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
722 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
723 is( CanItemBeReserved( $patrons[0], $limit_count_item, undef, { ignore_hold_counts => 1 } )->{status},
724 'OK', 'Patron can reserve item if checking policy but not counts' );
726 #results should be the same for both ReservesControlBranch settings
727 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
728 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
729 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
730 #reset for further tests
731 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
733 subtest 'Test max_holds per library/patron category' => sub {
736 $dbh->do('DELETE FROM reserves');
738 $biblio = $builder->build_sample_biblio;
739 my $max_holds_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
740 Koha::CirculationRules->set_rules(
742 categorycode => undef,
744 itemtype => $biblio->itemtype,
746 reservesallowed => 99,
747 holds_per_record => 99,
755 branchcode => $branch_1,
756 borrowernumber => $borrowernumbers[0],
757 biblionumber => $biblio->biblionumber,
764 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
765 is( $count, 3, 'Patron now has 3 holds' );
767 my $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
768 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
770 my $rule_all = Koha::CirculationRules->set_rule(
772 categorycode => $category->{categorycode},
774 rule_name => 'max_holds',
779 my $rule_branch = Koha::CirculationRules->set_rule(
781 branchcode => $branch_1,
782 categorycode => $category->{categorycode},
783 rule_name => 'max_holds',
788 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
789 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
791 $rule_branch->delete();
793 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
794 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
797 $rule_branch->rule_value(3);
798 $rule_branch->store();
800 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
801 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
803 $rule_branch->rule_value(5);
804 $rule_branch->update();
805 $rule_branch->rule_value(5);
806 $rule_branch->store();
808 $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
809 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
812 subtest 'Pickup location availability tests' => sub {
815 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
816 my $pickup_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
817 #Add a default rule to allow some holds
819 Koha::CirculationRules->set_rules(
822 categorycode => undef,
825 reservesallowed => 25,
826 holds_per_record => 99,
830 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
831 my $library = Koha::Libraries->find($branch_to);
832 $library->pickup_location('1')->store;
833 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
835 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
836 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
838 $library->pickup_location('1')->store;
839 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
840 'OK', 'Library is a pickup location');
842 my $limit = Koha::Item::Transfer::Limit->new({
843 fromBranch => $pickup_item->holdingbranch,
844 toBranch => $branch_to,
845 itemtype => $pickup_item->effective_itemtype,
847 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
848 'cannotBeTransferred', 'Item cannot be transferred');
851 $library->pickup_location('0')->store;
852 is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
853 'libraryNotPickupLocation', 'Library is not a pickup location');
854 is(CanItemBeReserved($patron, $pickup_item, 'nonexistent')->{status},
855 'libraryNotFound', 'Cannot set unknown library as pickup location');
858 $schema->storage->txn_rollback;
860 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
864 $schema->storage->txn_begin;
866 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
867 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
868 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
870 # Create 3 biblios with items
871 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
872 my $item_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber});
873 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
874 my $item_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber});
875 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
876 my $item_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber});
878 Koha::CirculationRules->set_rules(
882 itemtype => $itemtype->itemtype,
884 reservesallowed => 1,
885 holds_per_record => 99,
892 CanItemBeReserved( $patron, $item_1 ),
894 'Patron can reserve item with hold limit of 1, no holds placed'
899 branchcode => $library->branchcode,
900 borrowernumber => $patron->borrowernumber,
901 biblionumber => $biblio_1->biblionumber,
907 CanItemBeReserved( $patron, $item_1 ),
908 { status => 'tooManyReserves', limit => 1 },
909 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
912 # Raise reservesallowed to avoid tooManyReserves from it
913 Koha::CirculationRules->set_rule(
918 itemtype => $itemtype->itemtype,
919 rule_name => 'reservesallowed',
925 CanItemBeReserved( $patron, $item_2 ),
927 'Patron can reserve item with 2 reserves daily cap'
930 # Add a second reserve
931 my $res_id = AddReserve(
933 branchcode => $library->branchcode,
934 borrowernumber => $patron->borrowernumber,
935 biblionumber => $biblio_2->biblionumber,
940 CanItemBeReserved( $patron, $item_2 ),
941 { status => 'tooManyReservesToday', limit => 2 },
942 'Patron cannot a third item with 2 reserves daily cap'
945 # Update last hold so reservedate is in the past, so 2 holds, but different day
946 $hold = Koha::Holds->find($res_id);
947 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
948 $hold->reservedate($yesterday)->store;
951 CanItemBeReserved( $patron, $item_2 ),
953 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
956 # Set holds_per_day to 0
957 Koha::CirculationRules->set_rule(
962 itemtype => $itemtype->itemtype,
963 rule_name => 'holds_per_day',
969 # Delete existing holds
970 Koha::Holds->search->delete;
972 CanItemBeReserved( $patron, $item_2 ),
973 { status => 'tooManyReservesToday', limit => 0 },
974 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
977 Koha::CirculationRules->set_rule(
982 itemtype => $itemtype->itemtype,
983 rule_name => 'holds_per_day',
988 Koha::Holds->search->delete;
990 CanItemBeReserved( $patron, $item_2 ),
992 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
996 branchcode => $library->branchcode,
997 borrowernumber => $patron->borrowernumber,
998 biblionumber => $biblio_1->biblionumber,
1004 branchcode => $library->branchcode,
1005 borrowernumber => $patron->borrowernumber,
1006 biblionumber => $biblio_2->biblionumber,
1012 CanItemBeReserved( $patron, $item_3 ),
1014 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
1018 branchcode => $library->branchcode,
1019 borrowernumber => $patron->borrowernumber,
1020 biblionumber => $biblio_3->biblionumber,
1025 CanItemBeReserved( $patron, $item_3 ),
1026 { status => 'tooManyReserves', limit => 3 },
1027 'Unlimited daily holds, but reached reservesallowed'
1029 #results should be the same for both ReservesControlBranch settings
1030 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1032 CanItemBeReserved( $patron, $item_3 ),
1033 { status => 'tooManyReserves', limit => 3 },
1034 'Unlimited daily holds, but reached reservesallowed'
1037 $schema->storage->txn_rollback;
1040 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
1043 $schema->storage->txn_begin;
1045 Koha::CirculationRules->set_rule(
1047 branchcode => undef,
1048 categorycode => undef,
1050 rule_name => 'reservesallowed',
1056 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1057 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1060 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1061 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1062 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
1064 # Create library groups hierarchy
1065 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1066 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1067 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1070 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1071 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1073 # Create 3 biblios with items
1074 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1075 my $item_1 = $builder->build_sample_item(
1077 biblionumber => $biblio_1->biblionumber,
1078 library => $library1->branchcode
1081 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1082 my $item_2 = $builder->build_sample_item(
1084 biblionumber => $biblio_2->biblionumber,
1085 library => $library2->branchcode
1088 my $itemnumber_2 = $item_2->itemnumber;
1089 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1090 my $item_3 = $builder->build_sample_item(
1092 biblionumber => $biblio_3->biblionumber,
1093 library => $library1->branchcode
1097 # Test 1: Patron 3 can place hold
1099 CanItemBeReserved( $patron3, $item_2 ),
1101 'Patron can place hold if no circ_rules where defined'
1104 # Insert default circ rule of holds allowed only from local hold group for all libraries
1105 Koha::CirculationRules->set_rules(
1107 branchcode => undef,
1110 holdallowed => 'from_local_hold_group',
1111 hold_fulfillment_policy => 'any',
1112 returnbranch => 'any'
1117 # Test 2: Patron 1 can place hold
1119 CanItemBeReserved( $patron1, $item_2 ),
1121 'Patron can place hold because patron\'s home library is part of hold group'
1124 # Test 3: Patron 3 cannot place hold
1126 CanItemBeReserved( $patron3, $item_2 ),
1127 { status => 'branchNotInHoldGroup' },
1128 'Patron cannot place hold because patron\'s home library is not part of hold group'
1131 # Insert default circ rule to "any" for library 2
1132 Koha::CirculationRules->set_rules(
1134 branchcode => $library2->branchcode,
1137 holdallowed => 'from_any_library',
1138 hold_fulfillment_policy => 'any',
1139 returnbranch => 'any'
1144 # Test 4: Patron 3 can place hold
1146 CanItemBeReserved( $patron3, $item_2 ),
1148 'Patron can place hold if holdallowed is set to "any" for library 2'
1151 # Update default circ rule to "hold group" for library 2
1152 Koha::CirculationRules->set_rules(
1154 branchcode => $library2->branchcode,
1157 holdallowed => 'from_local_hold_group',
1158 hold_fulfillment_policy => 'any',
1159 returnbranch => 'any'
1164 # Test 5: Patron 3 cannot place hold
1166 CanItemBeReserved( $patron3, $item_2 ),
1167 { status => 'branchNotInHoldGroup' },
1168 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
1171 # Insert default item rule to "any" for itemtype 2
1172 Koha::CirculationRules->set_rules(
1174 branchcode => $library2->branchcode,
1175 itemtype => $itemtype2->itemtype,
1177 holdallowed => 'from_any_library',
1178 hold_fulfillment_policy => 'any',
1179 returnbranch => 'any'
1184 # Test 6: Patron 3 can place hold
1186 CanItemBeReserved( $patron3, $item_2 ),
1188 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
1191 # Update default item rule to "hold group" for itemtype 2
1192 Koha::CirculationRules->set_rules(
1194 branchcode => $library2->branchcode,
1195 itemtype => $itemtype2->itemtype,
1197 holdallowed => 'from_local_hold_group',
1198 hold_fulfillment_policy => 'any',
1199 returnbranch => 'any'
1204 # Test 7: Patron 3 cannot place hold
1206 CanItemBeReserved( $patron3, $item_2 ),
1207 { status => 'branchNotInHoldGroup' },
1208 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
1211 # Insert branch item rule to "any" for itemtype 2 and library 2
1212 Koha::CirculationRules->set_rules(
1214 branchcode => $library2->branchcode,
1215 itemtype => $itemtype2->itemtype,
1217 holdallowed => 'from_any_library',
1218 hold_fulfillment_policy => 'any',
1219 returnbranch => 'any'
1224 # Test 8: Patron 3 can place hold
1226 CanItemBeReserved( $patron3, $item_2 ),
1228 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1231 # Update branch item rule to "hold group" for itemtype 2 and library 2
1232 Koha::CirculationRules->set_rules(
1234 branchcode => $library2->branchcode,
1235 itemtype => $itemtype2->itemtype,
1237 holdallowed => 'from_local_hold_group',
1238 hold_fulfillment_policy => 'any',
1239 returnbranch => 'any'
1244 # Test 9: Patron 3 cannot place hold
1246 CanItemBeReserved( $patron3, $item_2 ),
1247 { status => 'branchNotInHoldGroup' },
1248 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1251 $schema->storage->txn_rollback;
1255 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1258 $schema->storage->txn_begin;
1259 Koha::CirculationRules->set_rule(
1261 branchcode => undef,
1262 categorycode => undef,
1264 rule_name => 'reservesallowed',
1270 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1271 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1274 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1275 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1276 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1278 # Create library groups hierarchy
1279 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1280 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1281 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1284 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1285 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1287 # Create 3 biblios with items
1288 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1289 my $item_1 = $builder->build_sample_item(
1291 biblionumber => $biblio_1->biblionumber,
1292 library => $library1->branchcode
1295 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1296 my $item_2 = $builder->build_sample_item(
1298 biblionumber => $biblio_2->biblionumber,
1299 library => $library2->branchcode
1302 my $itemnumber_2 = $item_2->itemnumber;
1303 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1304 my $item_3 = $builder->build_sample_item(
1306 biblionumber => $biblio_3->biblionumber,
1307 library => $library1->branchcode
1311 # Test 1: Patron 3 can place hold
1313 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1315 'Patron can place hold if no circ_rules where defined'
1318 # Insert default circ rule of holds allowed only from local hold group for all libraries
1319 Koha::CirculationRules->set_rules(
1321 branchcode => undef,
1324 holdallowed => 'from_any_library',
1325 hold_fulfillment_policy => 'holdgroup',
1326 returnbranch => 'any'
1331 # Test 2: Patron 1 can place hold
1333 CanItemBeReserved( $patron3, $item_2, $library1->branchcode ),
1335 'Patron can place hold because pickup location is part of hold group'
1338 # Test 3: Patron 3 cannot place hold
1340 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1341 { status => 'pickupNotInHoldGroup' },
1342 'Patron cannot place hold because pickup location is not part of hold group'
1345 # Insert default circ rule to "any" for library 2
1346 Koha::CirculationRules->set_rules(
1348 branchcode => $library2->branchcode,
1351 holdallowed => 'from_any_library',
1352 hold_fulfillment_policy => 'any',
1353 returnbranch => 'any'
1358 # Test 4: Patron 3 can place hold
1360 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1362 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1365 # Update default circ rule to "hold group" for library 2
1366 Koha::CirculationRules->set_rules(
1368 branchcode => $library2->branchcode,
1371 holdallowed => 'from_any_library',
1372 hold_fulfillment_policy => 'holdgroup',
1373 returnbranch => 'any'
1378 # Test 5: Patron 3 cannot place hold
1380 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1381 { status => 'pickupNotInHoldGroup' },
1382 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1385 # Insert default item rule to "any" for itemtype 2
1386 Koha::CirculationRules->set_rules(
1388 branchcode => $library2->branchcode,
1389 itemtype => $itemtype2->itemtype,
1391 holdallowed => 'from_any_library',
1392 hold_fulfillment_policy => 'any',
1393 returnbranch => 'any'
1398 # Test 6: Patron 3 can place hold
1400 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1402 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1405 # Update default item rule to "hold group" for itemtype 2
1406 Koha::CirculationRules->set_rules(
1408 branchcode => $library2->branchcode,
1409 itemtype => $itemtype2->itemtype,
1411 holdallowed => 'from_any_library',
1412 hold_fulfillment_policy => 'holdgroup',
1413 returnbranch => 'any'
1418 # Test 7: Patron 3 cannot place hold
1420 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1421 { status => 'pickupNotInHoldGroup' },
1422 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1425 # Insert branch item rule to "any" for itemtype 2 and library 2
1426 Koha::CirculationRules->set_rules(
1428 branchcode => $library2->branchcode,
1429 itemtype => $itemtype2->itemtype,
1431 holdallowed => 'from_any_library',
1432 hold_fulfillment_policy => 'any',
1433 returnbranch => 'any'
1438 # Test 8: Patron 3 can place hold
1440 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1442 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1445 # Update branch item rule to "hold group" for itemtype 2 and library 2
1446 Koha::CirculationRules->set_rules(
1448 branchcode => $library2->branchcode,
1449 itemtype => $itemtype2->itemtype,
1451 holdallowed => 'from_any_library',
1452 hold_fulfillment_policy => 'holdgroup',
1453 returnbranch => 'any'
1458 # Test 9: Patron 3 cannot place hold
1460 CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1461 { status => 'pickupNotInHoldGroup' },
1462 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1465 $schema->storage->txn_rollback;
1468 subtest 'non priority holds' => sub {
1472 $schema->storage->txn_begin;
1474 Koha::CirculationRules->set_rules(
1476 branchcode => undef,
1477 categorycode => undef,
1480 renewalsallowed => 5,
1481 reservesallowed => 5,
1486 my $item = $builder->build_sample_item;
1488 my $patron1 = $builder->build_object(
1490 class => 'Koha::Patrons',
1491 value => { branchcode => $item->homebranch }
1494 my $patron2 = $builder->build_object(
1496 class => 'Koha::Patrons',
1497 value => { branchcode => $item->homebranch }
1501 my $issue = Koha::Checkout->new(
1503 borrowernumber => $patron1->borrowernumber,
1504 itemnumber => $item->itemnumber,
1505 branchcode => $item->homebranch
1509 my $hid = AddReserve(
1511 branchcode => $item->homebranch,
1512 borrowernumber => $patron2->borrowernumber,
1513 biblionumber => $item->biblionumber,
1515 itemnumber => $item->itemnumber,
1520 CanBookBeRenewed( $patron1, $issue );
1522 ok( !$ok, 'Cannot renew' );
1523 is( $err, 'on_reserve', 'Item is on hold' );
1525 my $hold = Koha::Holds->find($hid);
1526 $hold->non_priority(1)->store;
1529 CanBookBeRenewed( $patron1, $issue );
1531 ok( $ok, 'Can renew' );
1532 is( $err, undef, 'Item is on non priority hold' );
1534 my $patron3 = $builder->build_object(
1536 class => 'Koha::Patrons',
1537 value => { branchcode => $item->homebranch }
1541 # Add second hold with non_priority = 0
1544 branchcode => $item->homebranch,
1545 borrowernumber => $patron3->borrowernumber,
1546 biblionumber => $item->biblionumber,
1548 itemnumber => $item->itemnumber,
1553 CanBookBeRenewed( $patron1, $issue );
1555 ok( !$ok, 'Cannot renew' );
1556 is( $err, 'on_reserve', 'Item is on hold' );
1558 $schema->storage->txn_rollback;
1561 subtest 'CanItemBeReserved / recall' => sub {
1564 $schema->storage->txn_begin;
1566 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1567 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1568 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1569 my $biblio1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1570 my $item1 = $builder->build_sample_item(
1572 biblionumber => $biblio1->biblionumber,
1573 library => $library1->branchcode
1577 patron_id => $patron1->borrowernumber,
1578 biblio_id => $biblio1->biblionumber,
1579 pickup_library_id => $library1->branchcode,
1580 item_id => $item1->itemnumber,
1581 created_date => '2020-05-04 10:10:10',
1584 is( CanItemBeReserved( $patron1, $item1, $library1->branchcode )->{status}, 'recall', "Can't reserve an item that they have already recalled" );
1586 $schema->storage->txn_rollback;
1589 subtest 'CanItemBeReserved rule precedence tests' => sub {
1593 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1594 $schema->storage->txn_begin;
1595 my $library = $builder->build_object( { class => 'Koha::Libraries', value => {
1596 pickup_location => 1,
1598 my $item = $builder->build_sample_item({
1599 homebranch => $library->branchcode,
1600 holdingbranch => $library->branchcode
1602 my $item2 = $builder->build_sample_item({
1603 homebranch => $library->branchcode,
1604 holdingbranch => $library->branchcode,
1605 itype => $item->itype
1607 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
1608 branchcode => $library->branchcode
1610 Koha::CirculationRules->set_rules(
1612 branchcode => undef,
1613 categorycode => $patron->categorycode,
1614 itemtype => $item->itype,
1616 reservesallowed => 1,
1621 CanItemBeReserved( $patron, $item, $library->branchcode ),
1623 'Patron of specified category can place 1 hold on specified itemtype'
1625 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
1626 biblionumber => $item2->biblionumber,
1627 itemnumber => $item2->itemnumber,
1630 branchcode => $library->branchcode,
1631 borrowernumber => $patron->borrowernumber,
1634 CanItemBeReserved( $patron, $item, $library->branchcode ),
1635 { status => 'tooManyReserves', limit => 1 },
1636 'Patron of specified category can place 1 hold on specified itemtype, cannot place a second'
1638 Koha::CirculationRules->set_rules(
1640 branchcode => $library->branchcode,
1641 categorycode => undef,
1644 reservesallowed => 2,
1649 CanItemBeReserved( $patron, $item, $library->branchcode ),
1651 'Patron of specified category can place 1 hold on specified itemtype if library rule for all types and categories set to 2'
1654 $schema->storage->txn_rollback;
1658 subtest 'ModReserve can only update expirationdate for found holds' => sub {
1661 $schema->storage->txn_begin;
1663 my $category = $builder->build({ source => 'Category' });
1664 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
1665 my $biblio = $builder->build_sample_biblio( { itemtype => 'DUMMY' } );
1666 my $itemnumber = $builder->build_sample_item(
1667 { library => $branch, biblionumber => $biblio->biblionumber } )
1670 my $borrowernumber = Koha::Patron->new(
1672 firstname => 'my firstname',
1673 surname => 'whatever surname',
1674 categorycode => $category->{categorycode},
1675 branchcode => $branch,
1677 )->store->borrowernumber;
1679 my $reserve_id = AddReserve(
1681 branchcode => $branch,
1682 borrowernumber => $borrowernumber,
1683 biblionumber => $biblio->biblionumber,
1685 C4::Reserves::CalculatePriority( $biblio->biblionumber ),
1686 itemnumber => $itemnumber,
1690 my $hold = Koha::Holds->find($reserve_id);
1692 $hold->set( { priority => 0, found => 'W' } )->store();
1696 reserve_id => $hold->id,
1697 expirationdate => '1981-06-10',
1703 $hold = Koha::Holds->find($reserve_id);
1705 is( $hold->expirationdate, '1981-06-10',
1706 'Found hold expiration date updated correctly' );
1707 is( $hold->priority, '0', 'Found hold priority was not updated' );
1709 $schema->storage->txn_rollback;
1713 subtest 'Koha::Holds->get_items_that_can_fill returns items with datecancelled or (inclusive) datearrived' => sub {
1716 $schema->storage->txn_begin;
1718 # biblio item with date arrived and date cancelled
1719 my $biblio1 = $builder->build_sample_biblio();
1720 my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1722 my $transfer1 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1723 datecancelled => '2022-06-12',
1724 itemnumber => $item1->itemnumber
1727 my $hold1 = $builder->build_object({ class => 'Koha::Holds', value => {
1728 biblionumber => $biblio1->biblionumber,
1729 itemnumber => undef,
1734 # biblio item with date arrived and NO date cancelled
1735 my $biblio2 = $builder->build_sample_biblio();
1736 my $item2 = $builder->build_sample_item({ biblionumber => $biblio2->biblionumber });
1738 my $transfer2 = $builder->build_object(
1739 { class => "Koha::Item::Transfers",
1741 datearrived => dt_from_string,
1742 datecancelled => undef,
1743 itemnumber => $item2->itemnumber
1748 my $hold2 = $builder->build_object({ class => 'Koha::Holds', value => {
1749 biblionumber => $biblio2->biblionumber,
1750 itemnumber => undef,
1755 # biblio item with NO date arrived and date cancelled
1756 my $biblio3 = $builder->build_sample_biblio();
1757 my $item3 = $builder->build_sample_item({ biblionumber => $biblio3->biblionumber });
1759 my $transfer3 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1760 datecancelled => '2022-06-12',
1761 itemnumber => $item3->itemnumber,
1762 datearrived => undef
1765 my $hold3 = $builder->build_object({ class => 'Koha::Holds', value => {
1766 biblionumber => $biblio3->biblionumber,
1767 itemnumber => undef,
1773 # biblio item with NO date arrived and NO date cancelled
1774 my $biblio4 = $builder->build_sample_biblio();
1775 my $item4 = $builder->build_sample_item({ biblionumber => $biblio4->biblionumber });
1777 my $transfer4 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1778 datecancelled => undef,
1779 itemnumber => $item4->itemnumber,
1780 datearrived => undef
1783 my $hold4 = $builder->build_object({ class => 'Koha::Holds', value => {
1784 biblionumber => $biblio4->biblionumber,
1785 itemnumber => undef,
1790 # create the holds which get_items_that_can_fill will be ran on
1791 my $holds1 = Koha::Holds->search({reserve_id => $hold1->id});
1792 my $holds2 = Koha::Holds->search({reserve_id => $hold2->id});
1793 my $holds3 = Koha::Holds->search({reserve_id => $hold3->id});
1794 my $holds4 = Koha::Holds->search({reserve_id => $hold4->id});
1796 my $items_that_can_fill1 = $holds1->get_items_that_can_fill;
1797 my $items_that_can_fill2 = $holds2->get_items_that_can_fill;
1798 my $items_that_can_fill3 = $holds3->get_items_that_can_fill;
1799 my $items_that_can_fill4 = $holds4->get_items_that_can_fill;
1801 is($items_that_can_fill1->next->id, $item1->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and datecancelled");
1802 is($items_that_can_fill1->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1803 is($items_that_can_fill2->next->id, $item2->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and undefined datecancelled");
1804 is($items_that_can_fill2->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1805 is($items_that_can_fill3->next->id, $item3->id, "Koha::Holds->get_items_that_can_fill returns item with undefined datearrived and defined datecancelled");
1806 is($items_that_can_fill3->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1807 is($items_that_can_fill4->next, undef, "Koha::Holds->get_items_that_can_fill doesn't return item with undefined datearrived and undefined datecancelled");
1808 is($items_that_can_fill4->count, 0, "Koha::Holds->get_items_that_can_fill returns 0 item");
1810 $schema->storage->txn_rollback;