Bug 25184: Items with a negative notforloan status should not be captured for holds
[koha.git] / t / db_dependent / Holds.t
blob7a1d3496e291550cb2d3d4916ab285c1d6617055
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
8 use C4::Context;
10 use Test::More tests => 60;
11 use MARC::Record;
13 use C4::Biblio;
14 use C4::Calendar;
15 use C4::Items;
16 use C4::Reserves;
18 use Koha::Biblios;
19 use Koha::CirculationRules;
20 use Koha::Database;
21 use Koha::DateUtils qw( dt_from_string output_pref );
22 use Koha::Holds;
23 use Koha::IssuingRules;
24 use Koha::Item::Transfer::Limits;
25 use Koha::Items;
26 use Koha::Libraries;
27 use Koha::Patrons;
29 BEGIN {
30 use FindBin;
31 use lib $FindBin::Bin;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new();
38 my $dbh = C4::Context->dbh;
40 # Create two random branches
41 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
42 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
44 my $category = $builder->build({ source => 'Category' });
46 my $borrowers_count = 5;
48 $dbh->do('DELETE FROM itemtypes');
49 $dbh->do('DELETE FROM reserves');
50 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
51 $insert_sth->execute('CAN');
52 $insert_sth->execute('CANNOT');
53 $insert_sth->execute('DUMMY');
54 $insert_sth->execute('ONLY1');
56 # Setup Test------------------------
57 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
59 # Create item instance for testing.
60 my ($item_bibnum, $item_bibitemnum, $itemnumber)
61 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
63 # Create some borrowers
64 my @borrowernumbers;
65 foreach (1..$borrowers_count) {
66 my $borrowernumber = Koha::Patron->new({
67 firstname => 'my firstname',
68 surname => 'my surname ' . $_,
69 categorycode => $category->{categorycode},
70 branchcode => $branch_1,
71 })->store->borrowernumber;
72 push @borrowernumbers, $borrowernumber;
75 # Create five item level holds
76 foreach my $borrowernumber ( @borrowernumbers ) {
77 AddReserve(
78 $branch_1,
79 $borrowernumber,
80 $biblio->biblionumber,
81 my $bibitems = q{},
82 my $priority = C4::Reserves::CalculatePriority( $biblio->biblionumber ),
83 my $resdate,
84 my $expdate,
85 my $notes = q{},
86 'a title',
87 my $checkitem = $itemnumber,
88 my $found,
92 my $holds = $biblio->holds;
93 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
94 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
95 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
96 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
97 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
98 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
100 my $item = Koha::Items->find( $itemnumber );
101 $holds = $item->current_holds;
102 my $first_hold = $holds->next;
103 my $reservedate = $first_hold->reservedate;
104 my $borrowernumber = $first_hold->borrowernumber;
105 my $branch_1code = $first_hold->branchcode;
106 my $reserve_id = $first_hold->reserve_id;
107 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
108 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
109 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
110 ok($reserve_id, "Test holds_placed_today()");
112 my $hold = Koha::Holds->find( $reserve_id );
113 ok( $hold, "Koha::Holds found the hold" );
114 my $hold_biblio = $hold->biblio();
115 ok( $hold_biblio, "Got biblio using biblio() method" );
116 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
117 my $hold_item = $hold->item();
118 ok( $hold_item, "Got item using item() method" );
119 ok( $hold_item == $hold->item(), "item method returns stashed item" );
120 my $hold_branch = $hold->branch();
121 ok( $hold_branch, "Got branch using branch() method" );
122 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
123 my $hold_found = $hold->found();
124 $hold->set({ found => 'W'})->store();
125 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
126 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
128 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
129 $holds = $patron->holds;
130 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
133 $holds = $item->current_holds;
134 $first_hold = $holds->next;
135 $borrowernumber = $first_hold->borrowernumber;
136 $branch_1code = $first_hold->branchcode;
137 $reserve_id = $first_hold->reserve_id;
139 ModReserve({
140 reserve_id => $reserve_id,
141 rank => '4',
142 branchcode => $branch_1,
143 itemnumber => $itemnumber,
144 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
147 $hold = Koha::Holds->find( $reserve_id );
148 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
149 ok( $hold->suspend, "Test ModReserve, suspend hold" );
150 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
152 ModReserve({ # call without reserve_id
153 rank => '3',
154 biblionumber => $item_bibnum,
155 itemnumber => $itemnumber,
156 borrowernumber => $borrowernumber,
158 $hold = Koha::Holds->find( $reserve_id );
159 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
161 ToggleSuspend( $reserve_id );
162 $hold = Koha::Holds->find( $reserve_id );
163 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
165 ToggleSuspend( $reserve_id, '2012-01-01' );
166 $hold = Koha::Holds->find( $reserve_id );
167 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
169 AutoUnsuspendReserves();
170 $hold = Koha::Holds->find( $reserve_id );
171 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
173 SuspendAll(
174 borrowernumber => $borrowernumber,
175 biblionumber => $biblio->biblionumber,
176 suspend => 1,
177 suspend_until => '2012-01-01',
179 $hold = Koha::Holds->find( $reserve_id );
180 is( $hold->suspend, 1, "Test SuspendAll()" );
181 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
183 SuspendAll(
184 borrowernumber => $borrowernumber,
185 biblionumber => $biblio->biblionumber,
186 suspend => 0,
188 $hold = Koha::Holds->find( $reserve_id );
189 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
190 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
192 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
193 AddReserve(
194 $branch_1,
195 $borrowernumbers[0],
196 $biblio->biblionumber,
197 my $bibitems = q{},
198 my $priority,
199 my $resdate,
200 my $expdate,
201 my $notes = q{},
202 'a title',
203 my $checkitem,
204 my $found,
206 $patron = Koha::Patrons->find( $borrowernumber );
207 $holds = $patron->holds;
208 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
209 ModReserveMinusPriority( $itemnumber, $reserveid );
210 $holds = $patron->holds;
211 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
213 $holds = $biblio->holds;
214 $hold = $holds->next;
215 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
216 $hold = Koha::Holds->find( $reserveid );
217 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
219 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '2', "Test AlterPriority(), move down" );
223 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
224 $hold = Koha::Holds->find( $reserveid );
225 is( $hold->priority, '1', "Test AlterPriority(), move up" );
227 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
228 $hold = Koha::Holds->find( $reserveid );
229 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
231 # Regression test for bug 2394
233 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
234 # a patron is not permittedo to request an item whose homebranch (i.e.,
235 # owner of the item) is different from the patron's own library.
236 # However, if canreservefromotherbranches is turned ON, the patron can
237 # create such hold requests.
239 # Note that canreservefromotherbranches has no effect if
240 # IndependentBranches is OFF.
242 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
243 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
244 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber);
245 $dbh->do('DELETE FROM issuingrules');
246 $dbh->do(
247 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
248 VALUES (?, ?, ?, ?, ?)},
250 '*', '*', '*', 25, 99
252 $dbh->do(
253 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
254 VALUES (?, ?, ?, ?, ?)},
256 '*', '*', 'CANNOT', 0, 99
259 # make sure some basic sysprefs are set
260 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
261 t::lib::Mocks::mock_preference('item-level_itypes', 1);
263 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
264 t::lib::Mocks::mock_preference('IndependentBranches', 0);
266 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
267 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
270 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
271 t::lib::Mocks::mock_preference('IndependentBranches', 1);
272 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
274 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
275 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
278 # ... unless canreservefromotherbranches is ON
279 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
281 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
282 '... unless canreservefromotherbranches is ON (bug 2394)'
286 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
287 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
288 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
289 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1);
290 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
291 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $biblio->biblionumber, '', 2);
292 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
293 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $biblio->biblionumber, '', 3);
294 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
295 my $hold3 = Koha::Holds->find( $reserveid3 );
296 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
297 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
298 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
299 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
302 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
303 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
304 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
305 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
307 $hold = Koha::Hold->new(
309 borrowernumber => $borrowernumbers[0],
310 itemnumber => $itemnumber,
311 biblionumber => $item_bibnum,
313 )->store();
314 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
315 'itemAlreadyOnHold',
316 "Patron cannot place a second item level hold for a given item" );
317 $hold->delete();
319 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
320 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
321 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
323 # Items that are not for loan, but holdable should not be trapped until they are available for loan
324 Koha::Items->find($itemnumber)->damaged(0)->notforloan(-1)->store;
325 Koha::Holds->search({ biblionumber => $biblio->id })->delete();
326 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
327 $hold = Koha::Hold->new(
329 borrowernumber => $borrowernumbers[0],
330 itemnumber => $itemnumber,
331 biblionumber => $biblio->biblionumber,
333 )->store();
334 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
335 $hold->delete();
337 # Regression test for bug 9532
338 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
339 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
340 AddReserve(
341 $branch_1,
342 $borrowernumbers[0],
343 $biblio->biblionumber,
348 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
349 "cannot request item if policy that matches on item-level item type forbids it"
351 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
353 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
354 "can request item if policy that matches on item type allows it"
357 t::lib::Mocks::mock_preference('item-level_itypes', 0);
358 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
360 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
361 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
365 # Test branch item rules
367 $dbh->do('DELETE FROM issuingrules');
368 $dbh->do(
369 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
370 VALUES (?, ?, ?, ?)},
372 '*', '*', '*', 25
374 $dbh->do('DELETE FROM branch_item_rules');
375 $dbh->do('DELETE FROM default_branch_circ_rules');
376 $dbh->do('DELETE FROM default_branch_item_rules');
377 $dbh->do('DELETE FROM default_circ_rules');
378 $dbh->do(q{
379 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
380 VALUES (?, ?, ?, ?)
381 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
382 $dbh->do(q{
383 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
384 VALUES (?, ?, ?, ?)
385 }, {}, $branch_1, 'CAN', 1, 'homebranch');
386 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
387 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
388 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
389 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
390 "CanItemBeReserved should return 'notReservable'");
392 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
393 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
394 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
395 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
396 'cannotReserveFromOtherBranches',
397 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
398 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
399 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
400 'OK',
401 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
403 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
404 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
405 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
406 "CanItemBeReserved should return 'OK'");
408 # Bug 12632
409 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
410 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
412 $dbh->do('DELETE FROM reserves');
413 $dbh->do('DELETE FROM issues');
414 $dbh->do('DELETE FROM items');
415 $dbh->do('DELETE FROM biblio');
417 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
418 ( $item_bibnum, $item_bibitemnum, $itemnumber )
419 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
421 $dbh->do(
422 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
423 VALUES (?, ?, ?, ?, ?)},
425 '*', '*', 'ONLY1', 1, 99
427 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
428 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
430 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
432 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
433 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
435 #results should be the same for both ReservesControlBranch settings
436 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
437 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
438 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
439 #reset for further tests
440 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
442 subtest 'Test max_holds per library/patron category' => sub {
443 plan tests => 6;
445 $dbh->do('DELETE FROM reserves');
446 $dbh->do('DELETE FROM issuingrules');
447 $dbh->do('DELETE FROM circulation_rules');
449 $biblio = $builder->build_sample_biblio({ itemtype => 'TEST' });
450 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
451 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
452 $biblio->biblionumber );
453 $dbh->do(
455 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
456 VALUES (?, ?, ?, ?, ?)
459 '*', '*', 'TEST', 99, 99
461 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
462 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
463 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
465 my $count =
466 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
467 is( $count, 3, 'Patron now has 3 holds' );
469 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
470 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
472 my $rule_all = Koha::CirculationRules->set_rule(
474 categorycode => $category->{categorycode},
475 branchcode => undef,
476 itemtype => undef,
477 rule_name => 'max_holds',
478 rule_value => 3,
482 my $rule_branch = Koha::CirculationRules->set_rule(
484 branchcode => $branch_1,
485 categorycode => $category->{categorycode},
486 itemtype => undef,
487 rule_name => 'max_holds',
488 rule_value => 5,
492 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
493 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
495 $rule_branch->delete();
497 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
498 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
500 $rule_all->delete();
501 $rule_branch->rule_value(3);
502 $rule_branch->store();
504 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
505 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
507 $rule_branch->rule_value(5);
508 $rule_branch->update();
509 $rule_branch->rule_value(5);
510 $rule_branch->store();
512 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
513 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
516 subtest 'Pickup location availability tests' => sub {
517 plan tests => 4;
519 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
520 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
521 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
522 #Add a default rule to allow some holds
523 $dbh->do(
524 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
525 VALUES (?, ?, ?, ?, ?)},
527 '*', '*', '*', 25, 99
529 my $item = Koha::Items->find($itemnumber);
530 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
531 my $library = Koha::Libraries->find($branch_to);
532 $library->pickup_location('1')->store;
533 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
535 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
536 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
538 $library->pickup_location('1')->store;
539 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
540 'OK', 'Library is a pickup location');
542 my $limit = Koha::Item::Transfer::Limit->new({
543 fromBranch => $item->holdingbranch,
544 toBranch => $branch_to,
545 itemtype => $item->effective_itemtype,
546 })->store;
547 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
548 'cannotBeTransferred', 'Item cannot be transferred');
549 $limit->delete;
551 $library->pickup_location('0')->store;
552 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
553 'libraryNotPickupLocation', 'Library is not a pickup location');
554 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
555 'libraryNotFound', 'Cannot set unknown library as pickup location');
558 $schema->storage->txn_rollback;
560 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
562 plan tests => 10;
564 $schema->storage->txn_begin;
566 Koha::Holds->search->delete;
567 $dbh->do('DELETE FROM issues');
568 Koha::Items->search->delete;
569 Koha::Biblios->search->delete;
571 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
572 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
573 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
575 # Create 3 biblios with items
576 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
577 my ( undef, undef, $itemnumber_1 ) = AddItem(
578 { homebranch => $library->branchcode,
579 holdingbranch => $library->branchcode
581 $biblio_1->biblionumber
583 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
584 my ( undef, undef, $itemnumber_2 ) = AddItem(
585 { homebranch => $library->branchcode,
586 holdingbranch => $library->branchcode
588 $biblio_2->biblionumber
590 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
591 my ( undef, undef, $itemnumber_3 ) = AddItem(
592 { homebranch => $library->branchcode,
593 holdingbranch => $library->branchcode
595 $biblio_3->biblionumber
598 Koha::IssuingRules->search->delete;
599 my $issuingrule = Koha::IssuingRule->new(
600 { categorycode => '*',
601 branchcode => '*',
602 itemtype => $itemtype->itemtype,
603 reservesallowed => 1,
604 holds_per_record => 99,
605 holds_per_day => 2
607 )->store;
609 is_deeply(
610 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
611 { status => 'OK' },
612 'Patron can reserve item with hold limit of 1, no holds placed'
615 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
617 is_deeply(
618 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
619 { status => 'tooManyReserves', limit => 1 },
620 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
623 # Raise reservesallowed to avoid tooManyReserves from it
624 $issuingrule->set( { reservesallowed => 3 } )->store;
626 is_deeply(
627 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
628 { status => 'OK' },
629 'Patron can reserve item with 2 reserves daily cap'
632 # Add a second reserve
633 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
634 is_deeply(
635 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
636 { status => 'tooManyReservesToday', limit => 2 },
637 'Patron cannot a third item with 2 reserves daily cap'
640 # Update last hold so reservedate is in the past, so 2 holds, but different day
641 $hold = Koha::Holds->find($res_id);
642 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
643 $hold->reservedate($yesterday)->store;
645 is_deeply(
646 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
647 { status => 'OK' },
648 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
651 # Set holds_per_day to 0
652 $issuingrule->set( { holds_per_day => 0 } )->store;
654 # Delete existing holds
655 Koha::Holds->search->delete;
656 is_deeply(
657 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
658 { status => 'tooManyReservesToday', limit => 0 },
659 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
662 $issuingrule->set( { holds_per_day => undef } )->store;
663 Koha::Holds->search->delete;
664 is_deeply(
665 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
666 { status => 'OK' },
667 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
669 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
670 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
671 is_deeply(
672 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
673 { status => 'OK' },
674 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
676 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
677 is_deeply(
678 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
679 { status => 'tooManyReserves', limit => 3 },
680 'Unlimited daily holds, but reached reservesallowed'
682 #results should be the same for both ReservesControlBranch settings
683 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
684 is_deeply(
685 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
686 { status => 'tooManyReserves', limit => 3 },
687 'Unlimited daily holds, but reached reservesallowed'
690 $schema->storage->txn_rollback;