6 use t::lib::TestBuilder;
10 use Test::More tests => 71;
20 use Koha::CirculationRules;
22 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::Item::Transfer::Limits;
28 use Koha::Library::Groups;
33 use lib $FindBin::Bin;
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
39 my $builder = t::lib::TestBuilder->new();
40 my $dbh = C4::Context->dbh;
42 # Create two random branches
43 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
44 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
46 my $category = $builder->build({ source => 'Category' });
48 my $borrowers_count = 5;
50 $dbh->do('DELETE FROM itemtypes');
51 $dbh->do('DELETE FROM reserves');
52 $dbh->do('DELETE FROM circulation_rules');
53 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
54 $insert_sth->execute('CAN');
55 $insert_sth->execute('CANNOT');
56 $insert_sth->execute('DUMMY');
57 $insert_sth->execute('ONLY1');
59 # Setup Test------------------------
60 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
62 # Create item instance for testing.
63 my $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
65 # Create some borrowers
67 foreach (1..$borrowers_count) {
68 my $borrowernumber = Koha::Patron->new({
69 firstname => 'my firstname',
70 surname => 'my surname ' . $_,
71 categorycode => $category->{categorycode},
72 branchcode => $branch_1,
73 })->store->borrowernumber;
74 push @borrowernumbers, $borrowernumber;
77 # Create five item level holds
78 foreach my $borrowernumber ( @borrowernumbers ) {
81 branchcode => $branch_1,
82 borrowernumber => $borrowernumber,
83 biblionumber => $biblio->biblionumber,
84 priority => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
85 itemnumber => $itemnumber,
90 my $holds = $biblio->holds;
91 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
92 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
93 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
94 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
95 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
96 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
98 my $item = Koha::Items->find( $itemnumber );
99 $holds = $item->current_holds;
100 my $first_hold = $holds->next;
101 my $reservedate = $first_hold->reservedate;
102 my $borrowernumber = $first_hold->borrowernumber;
103 my $branch_1code = $first_hold->branchcode;
104 my $reserve_id = $first_hold->reserve_id;
105 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
106 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
107 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
108 ok($reserve_id, "Test holds_placed_today()");
110 my $hold = Koha::Holds->find( $reserve_id );
111 ok( $hold, "Koha::Holds found the hold" );
112 my $hold_biblio = $hold->biblio();
113 ok( $hold_biblio, "Got biblio using biblio() method" );
114 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
115 my $hold_item = $hold->item();
116 ok( $hold_item, "Got item using item() method" );
117 ok( $hold_item == $hold->item(), "item method returns stashed item" );
118 my $hold_branch = $hold->branch();
119 ok( $hold_branch, "Got branch using branch() method" );
120 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
121 my $hold_found = $hold->found();
122 $hold->set({ found => 'W'})->store();
123 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
124 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
126 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
127 $holds = $patron->holds;
128 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
131 $holds = $item->current_holds;
132 $first_hold = $holds->next;
133 $borrowernumber = $first_hold->borrowernumber;
134 $branch_1code = $first_hold->branchcode;
135 $reserve_id = $first_hold->reserve_id;
138 reserve_id => $reserve_id,
140 branchcode => $branch_1,
141 itemnumber => $itemnumber,
142 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
145 $hold = Koha::Holds->find( $reserve_id );
146 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
147 ok( $hold->suspend, "Test ModReserve, suspend hold" );
148 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
150 ModReserve({ # call without reserve_id
152 biblionumber => $biblio->biblionumber,
153 itemnumber => $itemnumber,
154 borrowernumber => $borrowernumber,
156 $hold = Koha::Holds->find( $reserve_id );
157 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
159 ToggleSuspend( $reserve_id );
160 $hold = Koha::Holds->find( $reserve_id );
161 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
163 ToggleSuspend( $reserve_id, '2012-01-01' );
164 $hold = Koha::Holds->find( $reserve_id );
165 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
167 AutoUnsuspendReserves();
168 $hold = Koha::Holds->find( $reserve_id );
169 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
172 borrowernumber => $borrowernumber,
173 biblionumber => $biblio->biblionumber,
175 suspend_until => '2012-01-01',
177 $hold = Koha::Holds->find( $reserve_id );
178 is( $hold->suspend, 1, "Test SuspendAll()" );
179 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
182 borrowernumber => $borrowernumber,
183 biblionumber => $biblio->biblionumber,
186 $hold = Koha::Holds->find( $reserve_id );
187 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
188 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
190 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
193 branchcode => $branch_1,
194 borrowernumber => $borrowernumbers[0],
195 biblionumber => $biblio->biblionumber,
199 $patron = Koha::Patrons->find( $borrowernumber );
200 $holds = $patron->holds;
201 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
202 ModReserveMinusPriority( $itemnumber, $reserveid );
203 $holds = $patron->holds;
204 is( $holds->search({ itemnumber => $itemnumber })->count, 1, "Test ModReserveMinusPriority()" );
206 $holds = $biblio->holds;
207 $hold = $holds->next;
208 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
209 $hold = Koha::Holds->find( $reserveid );
210 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
212 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
213 $hold = Koha::Holds->find( $reserveid );
214 is( $hold->priority, '2', "Test AlterPriority(), move down" );
216 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
217 $hold = Koha::Holds->find( $reserveid );
218 is( $hold->priority, '1', "Test AlterPriority(), move up" );
220 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
221 $hold = Koha::Holds->find( $reserveid );
222 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
224 # Regression test for bug 2394
226 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
227 # a patron is not permittedo to request an item whose homebranch (i.e.,
228 # owner of the item) is different from the patron's own library.
229 # However, if canreservefromotherbranches is turned ON, the patron can
230 # create such hold requests.
232 # Note that canreservefromotherbranches has no effect if
233 # IndependentBranches is OFF.
235 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
236 my $foreign_itemnumber = $builder->build_sample_item({ library => $branch_2, biblionumber => $foreign_biblio->biblionumber })->itemnumber;
237 Koha::CirculationRules->set_rules(
239 categorycode => undef,
243 reservesallowed => 25,
244 holds_per_record => 99,
248 Koha::CirculationRules->set_rules(
250 categorycode => undef,
252 itemtype => 'CANNOT',
254 reservesallowed => 0,
255 holds_per_record => 99,
260 # make sure some basic sysprefs are set
261 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
262 t::lib::Mocks::mock_preference('item-level_itypes', 1);
264 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
265 t::lib::Mocks::mock_preference('IndependentBranches', 0);
268 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
269 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
272 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
273 t::lib::Mocks::mock_preference('IndependentBranches', 1);
274 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
276 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
277 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
280 # ... unless canreservefromotherbranches is ON
281 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
283 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
284 '... unless canreservefromotherbranches is ON (bug 2394)'
288 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
289 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
290 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
291 my $reserveid1 = AddReserve(
293 branchcode => $branch_1,
294 borrowernumber => $borrowernumbers[0],
295 biblionumber => $biblio->biblionumber,
300 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
301 my $reserveid2 = AddReserve(
303 branchcode => $branch_1,
304 borrowernumber => $borrowernumbers[1],
305 biblionumber => $biblio->biblionumber,
310 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
311 my $reserveid3 = AddReserve(
313 branchcode => $branch_1,
314 borrowernumber => $borrowernumbers[2],
315 biblionumber => $biblio->biblionumber,
320 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
321 my $hold3 = Koha::Holds->find( $reserveid3 );
322 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
323 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
324 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
325 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
328 Koha::Items->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
329 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
330 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
331 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
333 $hold = Koha::Hold->new(
335 borrowernumber => $borrowernumbers[0],
336 itemnumber => $itemnumber,
337 biblionumber => $biblio->biblionumber,
340 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
342 "Patron cannot place a second item level hold for a given item" );
345 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
346 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
347 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
349 # Items that are not for loan, but holdable should not be trapped until they are available for loan
350 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 0 );
351 Koha::Items->find($itemnumber)->damaged(0)->notforloan(-1)->store;
352 Koha::Holds->search({ biblionumber => $biblio->id })->delete();
353 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
354 $hold = Koha::Hold->new(
356 borrowernumber => $borrowernumbers[0],
357 itemnumber => $itemnumber,
358 biblionumber => $biblio->biblionumber,
361 reservedate => dt_from_string,
362 branchcode => $branch_1,
365 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
366 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 1 );
367 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold is trapped for item that is not for loan but holdable ( notforloan < 0 )" );
368 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1' );
369 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
370 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1|1' );
371 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
373 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'itemAlreadyOnHold',
374 "cannot request item that you have already reservedd"
377 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber, undef, { ignore_hold_counts => 1 })->{status}, 'OK',
378 "can request item if we are not checking holds counts, but only if policy allows or forbids it"
382 # Regression test for bug 9532
383 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
384 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
387 branchcode => $branch_1,
388 borrowernumber => $borrowernumbers[0],
389 biblionumber => $biblio->biblionumber,
394 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber)->{status}, 'noReservesAllowed',
395 "cannot request item if policy that matches on item-level item type forbids it"
398 CanItemBeReserved( $borrowernumbers[0], $item->itemnumber, undef, { ignore_hold_counts => 1 })->{status}, 'noReservesAllowed',
399 "cannot request item if policy that matches on item-level item type forbids it even if ignoring counts"
402 subtest 'CanItemBeReserved' => sub {
405 my $itemtype_can = $builder->build({source => "Itemtype"})->{itemtype};
406 my $itemtype_cant = $builder->build({source => "Itemtype"})->{itemtype};
407 my $itemtype_cant_record = $builder->build({source => "Itemtype"})->{itemtype};
409 Koha::CirculationRules->set_rules(
411 categorycode => undef,
413 itemtype => $itemtype_cant,
415 reservesallowed => 0,
416 holds_per_record => 99,
420 Koha::CirculationRules->set_rules(
422 categorycode => undef,
424 itemtype => $itemtype_can,
426 reservesallowed => 2,
427 holds_per_record => 2,
431 Koha::CirculationRules->set_rules(
433 categorycode => undef,
435 itemtype => $itemtype_cant_record,
437 reservesallowed => 0,
438 holds_per_record => 0,
443 Koha::CirculationRules->set_rules(
445 branchcode => $branch_1,
446 itemtype => $itemtype_cant,
449 returnbranch => 'homebranch',
453 Koha::CirculationRules->set_rules(
455 branchcode => $branch_1,
456 itemtype => $itemtype_can,
459 returnbranch => 'homebranch',
464 subtest 'noReservesAllowed' => sub {
467 my $biblionumber_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant })->biblionumber;
468 my $biblionumber_can = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
469 my $biblionumber_record_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant_record })->biblionumber;
471 my $itemnumber_1_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_cannot })->itemnumber;
472 my $itemnumber_1_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_cannot })->itemnumber;
473 my $itemnumber_2_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_can })->itemnumber;
474 my $itemnumber_2_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_can })->itemnumber;
475 my $itemnumber_3_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant_record, biblionumber => $biblionumber_record_cannot })->itemnumber;
477 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
479 t::lib::Mocks::mock_preference('item-level_itypes', 1);
481 CanItemBeReserved( $borrowernumbers[0], $itemnumber_2_cannot)->{status}, 'noReservesAllowed',
482 "With item level set, rule from item must be picked (CANNOT)"
485 CanItemBeReserved( $borrowernumbers[0], $itemnumber_1_can)->{status}, 'OK',
486 "With item level set, rule from item must be picked (CAN)"
488 t::lib::Mocks::mock_preference('item-level_itypes', 0);
490 CanItemBeReserved( $borrowernumbers[0], $itemnumber_1_can)->{status}, 'noReservesAllowed',
491 "With biblio level set, rule from biblio must be picked (CANNOT)"
494 CanItemBeReserved( $borrowernumbers[0], $itemnumber_2_cannot)->{status}, 'OK',
495 "With biblio level set, rule from biblio must be picked (CAN)"
498 CanItemBeReserved( $borrowernumbers[0], $itemnumber_3_cannot)->{status}, 'noReservesAllowed',
499 "When no holds allowed and no holds per record allowed should return noReservesAllowed"
503 subtest 'tooManyHoldsForThisRecord + tooManyReserves + itemAlreadyOnHold' => sub {
506 my $biblionumber_1 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
507 my $itemnumber_11 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 })->itemnumber;
508 my $itemnumber_12 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 })->itemnumber;
509 my $biblionumber_2 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
510 my $itemnumber_21 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 })->itemnumber;
511 my $itemnumber_22 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 })->itemnumber;
513 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
518 borrowernumber => $borrowernumbers[0],
519 biblionumber => $biblionumber_1,
521 for my $item_level ( 0..1 ) {
522 t::lib::Mocks::mock_preference('item-level_itypes', $item_level);
524 # FIXME This is not really correct, but CanItemBeReserved does not check if biblio-level holds already exist
525 CanItemBeReserved( $borrowernumbers[0], $itemnumber_11)->{status}, 'OK',
526 "A biblio-level hold already exists - another hold can be placed on a specific item item"
530 Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
534 borrowernumber => $borrowernumbers[0],
535 biblionumber => $biblionumber_1,
536 itemnumber => $itemnumber_11,
539 $dbh->do('DELETE FROM circulation_rules');
540 Koha::CirculationRules->set_rules(
542 categorycode => undef,
546 reservesallowed => 5,
547 holds_per_record => 1,
552 CanItemBeReserved( $borrowernumbers[0], $itemnumber_12)->{status}, 'tooManyHoldsForThisRecord',
553 "A item-level hold already exists and holds_per_record=1, another hold cannot be placed on this record"
555 Koha::CirculationRules->set_rules(
557 categorycode => undef,
561 reservesallowed => 1,
562 holds_per_record => 1,
567 CanItemBeReserved( $borrowernumbers[0], $itemnumber_12)->{status}, 'tooManyHoldsForThisRecord',
568 "A item-level hold already exists and holds_per_record=1 - tooManyHoldsForThisRecord has priority over tooManyReserves"
570 Koha::CirculationRules->set_rules(
572 categorycode => undef,
576 reservesallowed => 5,
577 holds_per_record => 2,
582 CanItemBeReserved( $borrowernumbers[0], $itemnumber_12)->{status}, 'OK',
583 "A item-level hold already exists but holds_per_record=2- another item-level hold can be placed on this record"
588 borrowernumber => $borrowernumbers[0],
589 biblionumber => $biblionumber_2,
590 itemnumber => $itemnumber_21
592 Koha::CirculationRules->set_rules(
594 categorycode => undef,
598 reservesallowed => 2,
599 holds_per_record => 2,
604 CanItemBeReserved( $borrowernumbers[0], $itemnumber_21)->{status}, 'itemAlreadyOnHold',
605 "A item-level holds already exists on this item, itemAlreadyOnHold should be raised"
608 CanItemBeReserved( $borrowernumbers[0], $itemnumber_22)->{status}, 'tooManyReserves',
609 "This patron has already placed reservesallowed holds, tooManyReserves should be raised"
615 # Test branch item rules
617 $dbh->do('DELETE FROM circulation_rules');
618 Koha::CirculationRules->set_rules(
620 categorycode => undef,
624 reservesallowed => 25,
625 holds_per_record => 99,
629 Koha::CirculationRules->set_rules(
631 branchcode => $branch_1,
632 itemtype => 'CANNOT',
634 holdallowed => 'not_allowed',
635 returnbranch => 'homebranch',
639 Koha::CirculationRules->set_rules(
641 branchcode => $branch_1,
644 holdallowed => 'from_home_library',
645 returnbranch => 'homebranch',
649 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
650 $itemnumber = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber})->itemnumber;
651 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
652 "CanItemBeReserved should return 'notReservable'");
654 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
655 $itemnumber = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber})->itemnumber;
656 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
657 'cannotReserveFromOtherBranches',
658 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
659 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
660 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
662 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
664 $itemnumber = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber})->itemnumber;
665 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
666 "CanItemBeReserved should return 'OK'");
669 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
670 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
672 $dbh->do('DELETE FROM reserves');
673 $dbh->do('DELETE FROM issues');
674 $dbh->do('DELETE FROM items');
675 $dbh->do('DELETE FROM biblio');
677 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
678 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
680 Koha::CirculationRules->set_rules(
682 categorycode => undef,
686 reservesallowed => 1,
687 holds_per_record => 99,
691 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
692 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
694 my $res_id = AddReserve(
696 branchcode => $branch_1,
697 borrowernumber => $borrowernumbers[0],
698 biblionumber => $biblio->biblionumber,
703 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
704 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
705 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber, undef, { ignore_hold_counts => 1 } )->{status},
706 'OK', 'Patron can reserve item if checking policy but not counts' );
708 #results should be the same for both ReservesControlBranch settings
709 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
710 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
711 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
712 #reset for further tests
713 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
715 subtest 'Test max_holds per library/patron category' => sub {
718 $dbh->do('DELETE FROM reserves');
720 $biblio = $builder->build_sample_biblio;
721 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
722 Koha::CirculationRules->set_rules(
724 categorycode => undef,
726 itemtype => $biblio->itemtype,
728 reservesallowed => 99,
729 holds_per_record => 99,
737 branchcode => $branch_1,
738 borrowernumber => $borrowernumbers[0],
739 biblionumber => $biblio->biblionumber,
746 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
747 is( $count, 3, 'Patron now has 3 holds' );
749 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
750 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
752 my $rule_all = Koha::CirculationRules->set_rule(
754 categorycode => $category->{categorycode},
756 rule_name => 'max_holds',
761 my $rule_branch = Koha::CirculationRules->set_rule(
763 branchcode => $branch_1,
764 categorycode => $category->{categorycode},
765 rule_name => 'max_holds',
770 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
771 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
773 $rule_branch->delete();
775 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
776 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
779 $rule_branch->rule_value(3);
780 $rule_branch->store();
782 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
783 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
785 $rule_branch->rule_value(5);
786 $rule_branch->update();
787 $rule_branch->rule_value(5);
788 $rule_branch->store();
790 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
791 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
794 subtest 'Pickup location availability tests' => sub {
797 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
798 $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber})->itemnumber;
799 #Add a default rule to allow some holds
801 Koha::CirculationRules->set_rules(
804 categorycode => undef,
807 reservesallowed => 25,
808 holds_per_record => 99,
812 my $item = Koha::Items->find($itemnumber);
813 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
814 my $library = Koha::Libraries->find($branch_to);
815 $library->pickup_location('1')->store;
816 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
818 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
819 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
821 $library->pickup_location('1')->store;
822 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
823 'OK', 'Library is a pickup location');
825 my $limit = Koha::Item::Transfer::Limit->new({
826 fromBranch => $item->holdingbranch,
827 toBranch => $branch_to,
828 itemtype => $item->effective_itemtype,
830 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
831 'cannotBeTransferred', 'Item cannot be transferred');
834 $library->pickup_location('0')->store;
835 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
836 'libraryNotPickupLocation', 'Library is not a pickup location');
837 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
838 'libraryNotFound', 'Cannot set unknown library as pickup location');
841 $schema->storage->txn_rollback;
843 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
847 $schema->storage->txn_begin;
849 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
850 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
851 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
853 # Create 3 biblios with items
854 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
855 my $itemnumber_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber})->itemnumber;
856 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
857 my $itemnumber_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber})->itemnumber;
858 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
859 my $itemnumber_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber})->itemnumber;
861 Koha::CirculationRules->set_rules(
865 itemtype => $itemtype->itemtype,
867 reservesallowed => 1,
868 holds_per_record => 99,
875 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
877 'Patron can reserve item with hold limit of 1, no holds placed'
882 branchcode => $library->branchcode,
883 borrowernumber => $patron->borrowernumber,
884 biblionumber => $biblio_1->biblionumber,
890 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
891 { status => 'tooManyReserves', limit => 1 },
892 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
895 # Raise reservesallowed to avoid tooManyReserves from it
896 Koha::CirculationRules->set_rule(
901 itemtype => $itemtype->itemtype,
902 rule_name => 'reservesallowed',
908 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
910 'Patron can reserve item with 2 reserves daily cap'
913 # Add a second reserve
914 my $res_id = AddReserve(
916 branchcode => $library->branchcode,
917 borrowernumber => $patron->borrowernumber,
918 biblionumber => $biblio_2->biblionumber,
923 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
924 { status => 'tooManyReservesToday', limit => 2 },
925 'Patron cannot a third item with 2 reserves daily cap'
928 # Update last hold so reservedate is in the past, so 2 holds, but different day
929 $hold = Koha::Holds->find($res_id);
930 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
931 $hold->reservedate($yesterday)->store;
934 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
936 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
939 # Set holds_per_day to 0
940 Koha::CirculationRules->set_rule(
945 itemtype => $itemtype->itemtype,
946 rule_name => 'holds_per_day',
952 # Delete existing holds
953 Koha::Holds->search->delete;
955 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
956 { status => 'tooManyReservesToday', limit => 0 },
957 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
960 Koha::CirculationRules->set_rule(
965 itemtype => $itemtype->itemtype,
966 rule_name => 'holds_per_day',
971 Koha::Holds->search->delete;
973 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
975 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
979 branchcode => $library->branchcode,
980 borrowernumber => $patron->borrowernumber,
981 biblionumber => $biblio_1->biblionumber,
987 branchcode => $library->branchcode,
988 borrowernumber => $patron->borrowernumber,
989 biblionumber => $biblio_2->biblionumber,
995 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
997 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
1001 branchcode => $library->branchcode,
1002 borrowernumber => $patron->borrowernumber,
1003 biblionumber => $biblio_3->biblionumber,
1008 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
1009 { status => 'tooManyReserves', limit => 3 },
1010 'Unlimited daily holds, but reached reservesallowed'
1012 #results should be the same for both ReservesControlBranch settings
1013 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1015 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
1016 { status => 'tooManyReserves', limit => 3 },
1017 'Unlimited daily holds, but reached reservesallowed'
1020 $schema->storage->txn_rollback;
1023 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
1026 $schema->storage->txn_begin;
1028 Koha::CirculationRules->set_rule(
1030 branchcode => undef,
1031 categorycode => undef,
1033 rule_name => 'reservesallowed',
1039 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1040 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1043 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1044 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1045 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
1047 # Create library groups hierarchy
1048 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1049 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1050 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1053 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1054 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1056 # Create 3 biblios with items
1057 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1058 my $item_1 = $builder->build_sample_item(
1060 biblionumber => $biblio_1->biblionumber,
1061 library => $library1->branchcode
1064 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1065 my $item_2 = $builder->build_sample_item(
1067 biblionumber => $biblio_2->biblionumber,
1068 library => $library2->branchcode
1071 my $itemnumber_2 = $item_2->itemnumber;
1072 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1073 my $item_3 = $builder->build_sample_item(
1075 biblionumber => $biblio_3->biblionumber,
1076 library => $library1->branchcode
1080 # Test 1: Patron 3 can place hold
1082 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1084 'Patron can place hold if no circ_rules where defined'
1087 # Insert default circ rule of holds allowed only from local hold group for all libraries
1088 Koha::CirculationRules->set_rules(
1090 branchcode => undef,
1093 holdallowed => 'from_local_hold_group',
1094 hold_fulfillment_policy => 'any',
1095 returnbranch => 'any'
1100 # Test 2: Patron 1 can place hold
1102 CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
1104 'Patron can place hold because patron\'s home library is part of hold group'
1107 # Test 3: Patron 3 cannot place hold
1109 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1110 { status => 'branchNotInHoldGroup' },
1111 'Patron cannot place hold because patron\'s home library is not part of hold group'
1114 # Insert default circ rule to "any" for library 2
1115 Koha::CirculationRules->set_rules(
1117 branchcode => $library2->branchcode,
1120 holdallowed => 'from_any_library',
1121 hold_fulfillment_policy => 'any',
1122 returnbranch => 'any'
1127 # Test 4: Patron 3 can place hold
1129 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1131 'Patron can place hold if holdallowed is set to "any" for library 2'
1134 # Update default circ rule to "hold group" for library 2
1135 Koha::CirculationRules->set_rules(
1137 branchcode => $library2->branchcode,
1140 holdallowed => 'from_local_hold_group',
1141 hold_fulfillment_policy => 'any',
1142 returnbranch => 'any'
1147 # Test 5: Patron 3 cannot place hold
1149 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1150 { status => 'branchNotInHoldGroup' },
1151 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
1154 # Insert default item rule to "any" for itemtype 2
1155 Koha::CirculationRules->set_rules(
1157 branchcode => $library2->branchcode,
1158 itemtype => $itemtype2->itemtype,
1160 holdallowed => 'from_any_library',
1161 hold_fulfillment_policy => 'any',
1162 returnbranch => 'any'
1167 # Test 6: Patron 3 can place hold
1169 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1171 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
1174 # Update default item rule to "hold group" for itemtype 2
1175 Koha::CirculationRules->set_rules(
1177 branchcode => $library2->branchcode,
1178 itemtype => $itemtype2->itemtype,
1180 holdallowed => 'from_local_hold_group',
1181 hold_fulfillment_policy => 'any',
1182 returnbranch => 'any'
1187 # Test 7: Patron 3 cannot place hold
1189 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1190 { status => 'branchNotInHoldGroup' },
1191 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
1194 # Insert branch item rule to "any" for itemtype 2 and library 2
1195 Koha::CirculationRules->set_rules(
1197 branchcode => $library2->branchcode,
1198 itemtype => $itemtype2->itemtype,
1200 holdallowed => 'from_any_library',
1201 hold_fulfillment_policy => 'any',
1202 returnbranch => 'any'
1207 # Test 8: Patron 3 can place hold
1209 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1211 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1214 # Update branch item rule to "hold group" for itemtype 2 and library 2
1215 Koha::CirculationRules->set_rules(
1217 branchcode => $library2->branchcode,
1218 itemtype => $itemtype2->itemtype,
1220 holdallowed => 'from_local_hold_group',
1221 hold_fulfillment_policy => 'any',
1222 returnbranch => 'any'
1227 # Test 9: Patron 3 cannot place hold
1229 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1230 { status => 'branchNotInHoldGroup' },
1231 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1234 $schema->storage->txn_rollback;
1238 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1241 $schema->storage->txn_begin;
1242 Koha::CirculationRules->set_rule(
1244 branchcode => undef,
1245 categorycode => undef,
1247 rule_name => 'reservesallowed',
1253 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1254 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1257 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1258 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1259 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1261 # Create library groups hierarchy
1262 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1263 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1264 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1267 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1268 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1270 # Create 3 biblios with items
1271 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1272 my $item_1 = $builder->build_sample_item(
1274 biblionumber => $biblio_1->biblionumber,
1275 library => $library1->branchcode
1278 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1279 my $item_2 = $builder->build_sample_item(
1281 biblionumber => $biblio_2->biblionumber,
1282 library => $library2->branchcode
1285 my $itemnumber_2 = $item_2->itemnumber;
1286 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1287 my $item_3 = $builder->build_sample_item(
1289 biblionumber => $biblio_3->biblionumber,
1290 library => $library1->branchcode
1294 # Test 1: Patron 3 can place hold
1296 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1298 'Patron can place hold if no circ_rules where defined'
1301 # Insert default circ rule of holds allowed only from local hold group for all libraries
1302 Koha::CirculationRules->set_rules(
1304 branchcode => undef,
1307 holdallowed => 'from_any_library',
1308 hold_fulfillment_policy => 'holdgroup',
1309 returnbranch => 'any'
1314 # Test 2: Patron 1 can place hold
1316 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1318 'Patron can place hold because pickup location is part of hold group'
1321 # Test 3: Patron 3 cannot place hold
1323 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1324 { status => 'pickupNotInHoldGroup' },
1325 'Patron cannot place hold because pickup location is not part of hold group'
1328 # Insert default circ rule to "any" for library 2
1329 Koha::CirculationRules->set_rules(
1331 branchcode => $library2->branchcode,
1334 holdallowed => 'from_any_library',
1335 hold_fulfillment_policy => 'any',
1336 returnbranch => 'any'
1341 # Test 4: Patron 3 can place hold
1343 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1345 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1348 # Update default circ rule to "hold group" for library 2
1349 Koha::CirculationRules->set_rules(
1351 branchcode => $library2->branchcode,
1354 holdallowed => 'from_any_library',
1355 hold_fulfillment_policy => 'holdgroup',
1356 returnbranch => 'any'
1361 # Test 5: Patron 3 cannot place hold
1363 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1364 { status => 'pickupNotInHoldGroup' },
1365 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1368 # Insert default item rule to "any" for itemtype 2
1369 Koha::CirculationRules->set_rules(
1371 branchcode => $library2->branchcode,
1372 itemtype => $itemtype2->itemtype,
1374 holdallowed => 'from_any_library',
1375 hold_fulfillment_policy => 'any',
1376 returnbranch => 'any'
1381 # Test 6: Patron 3 can place hold
1383 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1385 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1388 # Update default item rule to "hold group" for itemtype 2
1389 Koha::CirculationRules->set_rules(
1391 branchcode => $library2->branchcode,
1392 itemtype => $itemtype2->itemtype,
1394 holdallowed => 'from_any_library',
1395 hold_fulfillment_policy => 'holdgroup',
1396 returnbranch => 'any'
1401 # Test 7: Patron 3 cannot place hold
1403 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1404 { status => 'pickupNotInHoldGroup' },
1405 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1408 # Insert branch item rule to "any" for itemtype 2 and library 2
1409 Koha::CirculationRules->set_rules(
1411 branchcode => $library2->branchcode,
1412 itemtype => $itemtype2->itemtype,
1414 holdallowed => 'from_any_library',
1415 hold_fulfillment_policy => 'any',
1416 returnbranch => 'any'
1421 # Test 8: Patron 3 can place hold
1423 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1425 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1428 # Update branch item rule to "hold group" for itemtype 2 and library 2
1429 Koha::CirculationRules->set_rules(
1431 branchcode => $library2->branchcode,
1432 itemtype => $itemtype2->itemtype,
1434 holdallowed => 'from_any_library',
1435 hold_fulfillment_policy => 'holdgroup',
1436 returnbranch => 'any'
1441 # Test 9: Patron 3 cannot place hold
1443 CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1444 { status => 'pickupNotInHoldGroup' },
1445 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1448 $schema->storage->txn_rollback;
1451 subtest 'non priority holds' => sub {
1455 $schema->storage->txn_begin;
1457 Koha::CirculationRules->set_rules(
1459 branchcode => undef,
1460 categorycode => undef,
1463 renewalsallowed => 5,
1464 reservesallowed => 5,
1469 my $item = $builder->build_sample_item;
1471 my $patron1 = $builder->build_object(
1473 class => 'Koha::Patrons',
1474 value => { branchcode => $item->homebranch }
1477 my $patron2 = $builder->build_object(
1479 class => 'Koha::Patrons',
1480 value => { branchcode => $item->homebranch }
1484 Koha::Checkout->new(
1486 borrowernumber => $patron1->borrowernumber,
1487 itemnumber => $item->itemnumber,
1488 branchcode => $item->homebranch
1492 my $hid = AddReserve(
1494 branchcode => $item->homebranch,
1495 borrowernumber => $patron2->borrowernumber,
1496 biblionumber => $item->biblionumber,
1498 itemnumber => $item->itemnumber,
1503 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1505 ok( !$ok, 'Cannot renew' );
1506 is( $err, 'on_reserve', 'Item is on hold' );
1508 my $hold = Koha::Holds->find($hid);
1509 $hold->non_priority(1)->store;
1512 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1514 ok( $ok, 'Can renew' );
1515 is( $err, undef, 'Item is on non priority hold' );
1517 my $patron3 = $builder->build_object(
1519 class => 'Koha::Patrons',
1520 value => { branchcode => $item->homebranch }
1524 # Add second hold with non_priority = 0
1527 branchcode => $item->homebranch,
1528 borrowernumber => $patron3->borrowernumber,
1529 biblionumber => $item->biblionumber,
1531 itemnumber => $item->itemnumber,
1536 CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1538 ok( !$ok, 'Cannot renew' );
1539 is( $err, 'on_reserve', 'Item is on hold' );
1541 $schema->storage->txn_rollback;
1545 subtest 'CanItemBeReserved rule precedence tests' => sub {
1549 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1550 $schema->storage->txn_begin;
1551 my $library = $builder->build_object( { class => 'Koha::Libraries', value => {
1552 pickup_location => 1,
1554 my $item = $builder->build_sample_item({
1555 homebranch => $library->branchcode,
1556 holdingbranch => $library->branchcode
1558 my $item2 = $builder->build_sample_item({
1559 homebranch => $library->branchcode,
1560 holdingbranch => $library->branchcode,
1561 itype => $item->itype
1563 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
1564 branchcode => $library->branchcode
1566 Koha::CirculationRules->set_rules(
1568 branchcode => undef,
1569 categorycode => $patron->categorycode,
1570 itemtype => $item->itype,
1572 reservesallowed => 1,
1577 CanItemBeReserved( $patron->borrowernumber, $item->itemnumber, $library->branchcode ),
1579 'Patron of specified category can place 1 hold on specified itemtype'
1581 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
1582 biblionumber => $item2->biblionumber,
1583 itemnumber => $item2->itemnumber,
1586 branchcode => $library->branchcode,
1587 borrowernumber => $patron->borrowernumber,
1590 CanItemBeReserved( $patron->borrowernumber, $item->itemnumber, $library->branchcode ),
1591 { status => 'tooManyReserves', limit => 1 },
1592 'Patron of specified category can place 1 hold on specified itemtype, cannot place a second'
1594 Koha::CirculationRules->set_rules(
1596 branchcode => $library->branchcode,
1597 categorycode => undef,
1600 reservesallowed => 2,
1605 CanItemBeReserved( $patron->borrowernumber, $item->itemnumber, $library->branchcode ),
1607 'Patron of specified category can place 1 hold on specified itemtype if library rule for all types and categories set to 2'
1610 $schema->storage->txn_rollback;