Bug 18928: (follow-up) Make DB update idempotent
[koha.git] / t / db_dependent / Holds.t
blobf7919b3827a40951c392f0faa976fb747902f5f6
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 => 59;
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 $dbh->do('DELETE FROM circulation_rules');
51 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
52 $insert_sth->execute('CAN');
53 $insert_sth->execute('CANNOT');
54 $insert_sth->execute('DUMMY');
55 $insert_sth->execute('ONLY1');
57 # Setup Test------------------------
58 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
60 # Create item instance for testing.
61 my ($item_bibnum, $item_bibitemnum, $itemnumber)
62 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
64 # Create some borrowers
65 my @borrowernumbers;
66 foreach (1..$borrowers_count) {
67 my $borrowernumber = Koha::Patron->new({
68 firstname => 'my firstname',
69 surname => 'my surname ' . $_,
70 categorycode => $category->{categorycode},
71 branchcode => $branch_1,
72 })->store->borrowernumber;
73 push @borrowernumbers, $borrowernumber;
76 # Create five item level holds
77 foreach my $borrowernumber ( @borrowernumbers ) {
78 AddReserve(
79 $branch_1,
80 $borrowernumber,
81 $biblio->biblionumber,
82 my $bibitems = q{},
83 my $priority = C4::Reserves::CalculatePriority( $biblio->biblionumber ),
84 my $resdate,
85 my $expdate,
86 my $notes = q{},
87 'a title',
88 my $checkitem = $itemnumber,
89 my $found,
93 my $holds = $biblio->holds;
94 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
95 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
96 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
97 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
98 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
99 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
101 my $item = Koha::Items->find( $itemnumber );
102 $holds = $item->current_holds;
103 my $first_hold = $holds->next;
104 my $reservedate = $first_hold->reservedate;
105 my $borrowernumber = $first_hold->borrowernumber;
106 my $branch_1code = $first_hold->branchcode;
107 my $reserve_id = $first_hold->reserve_id;
108 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
109 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
110 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
111 ok($reserve_id, "Test holds_placed_today()");
113 my $hold = Koha::Holds->find( $reserve_id );
114 ok( $hold, "Koha::Holds found the hold" );
115 my $hold_biblio = $hold->biblio();
116 ok( $hold_biblio, "Got biblio using biblio() method" );
117 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
118 my $hold_item = $hold->item();
119 ok( $hold_item, "Got item using item() method" );
120 ok( $hold_item == $hold->item(), "item method returns stashed item" );
121 my $hold_branch = $hold->branch();
122 ok( $hold_branch, "Got branch using branch() method" );
123 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
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;
140 ModReserve({
141 reserve_id => $reserve_id,
142 rank => '4',
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
154 rank => '3',
155 biblionumber => $item_bibnum,
156 itemnumber => $itemnumber,
157 borrowernumber => $borrowernumber,
159 $hold = Koha::Holds->find( $reserve_id );
160 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
162 ToggleSuspend( $reserve_id );
163 $hold = Koha::Holds->find( $reserve_id );
164 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
166 ToggleSuspend( $reserve_id, '2012-01-01' );
167 $hold = Koha::Holds->find( $reserve_id );
168 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
170 AutoUnsuspendReserves();
171 $hold = Koha::Holds->find( $reserve_id );
172 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
174 SuspendAll(
175 borrowernumber => $borrowernumber,
176 biblionumber => $biblio->biblionumber,
177 suspend => 1,
178 suspend_until => '2012-01-01',
180 $hold = Koha::Holds->find( $reserve_id );
181 is( $hold->suspend, 1, "Test SuspendAll()" );
182 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
184 SuspendAll(
185 borrowernumber => $borrowernumber,
186 biblionumber => $biblio->biblionumber,
187 suspend => 0,
189 $hold = Koha::Holds->find( $reserve_id );
190 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
191 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
193 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
194 AddReserve(
195 $branch_1,
196 $borrowernumbers[0],
197 $biblio->biblionumber,
198 my $bibitems = q{},
199 my $priority,
200 my $resdate,
201 my $expdate,
202 my $notes = q{},
203 'a title',
204 my $checkitem,
205 my $found,
207 $patron = Koha::Patrons->find( $borrowernumber );
208 $holds = $patron->holds;
209 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
210 ModReserveMinusPriority( $itemnumber, $reserveid );
211 $holds = $patron->holds;
212 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
214 $holds = $biblio->holds;
215 $hold = $holds->next;
216 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
217 $hold = Koha::Holds->find( $reserveid );
218 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
220 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
221 $hold = Koha::Holds->find( $reserveid );
222 is( $hold->priority, '2', "Test AlterPriority(), move down" );
224 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
225 $hold = Koha::Holds->find( $reserveid );
226 is( $hold->priority, '1', "Test AlterPriority(), move up" );
228 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
229 $hold = Koha::Holds->find( $reserveid );
230 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
232 # Regression test for bug 2394
234 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
235 # a patron is not permittedo to request an item whose homebranch (i.e.,
236 # owner of the item) is different from the patron's own library.
237 # However, if canreservefromotherbranches is turned ON, the patron can
238 # create such hold requests.
240 # Note that canreservefromotherbranches has no effect if
241 # IndependentBranches is OFF.
243 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
244 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
245 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber);
246 $dbh->do('DELETE FROM issuingrules');
247 $dbh->do(
248 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
249 VALUES (?, ?, ?, ?, ?)},
251 '*', '*', '*', 25, 99
253 $dbh->do(
254 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
255 VALUES (?, ?, ?, ?, ?)},
257 '*', '*', 'CANNOT', 0, 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);
267 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
268 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
271 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
272 t::lib::Mocks::mock_preference('IndependentBranches', 1);
273 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
275 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
276 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
279 # ... unless canreservefromotherbranches is ON
280 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
282 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
283 '... unless canreservefromotherbranches is ON (bug 2394)'
287 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
288 $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
289 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
290 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1);
291 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
292 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $biblio->biblionumber, '', 2);
293 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
294 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $biblio->biblionumber, '', 3);
295 my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
296 my $hold3 = Koha::Holds->find( $reserveid3 );
297 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
298 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
299 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
300 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
303 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
304 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
305 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
306 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
308 $hold = Koha::Hold->new(
310 borrowernumber => $borrowernumbers[0],
311 itemnumber => $itemnumber,
312 biblionumber => $item_bibnum,
314 )->store();
315 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
316 'itemAlreadyOnHold',
317 "Patron cannot place a second item level hold for a given item" );
318 $hold->delete();
320 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
321 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
322 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
324 # Regression test for bug 9532
325 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
326 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
327 AddReserve(
328 $branch_1,
329 $borrowernumbers[0],
330 $biblio->biblionumber,
335 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
336 "cannot request item if policy that matches on item-level item type forbids it"
338 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
340 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
341 "can request item if policy that matches on item type allows it"
344 t::lib::Mocks::mock_preference('item-level_itypes', 0);
345 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
347 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
348 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
352 # Test branch item rules
354 $dbh->do('DELETE FROM issuingrules');
355 $dbh->do('DELETE FROM circulation_rules');
356 $dbh->do(
357 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
358 VALUES (?, ?, ?, ?)},
360 '*', '*', '*', 25
362 Koha::CirculationRules->set_rules(
364 branchcode => $branch_1,
365 itemtype => 'CANNOT',
366 categorycode => undef,
367 rules => {
368 holdallowed => 0,
369 returnbranch => 'homebranch',
373 Koha::CirculationRules->set_rules(
375 branchcode => $branch_1,
376 itemtype => 'CAN',
377 categorycode => undef,
378 rules => {
379 holdallowed => 1,
380 returnbranch => 'homebranch',
384 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
385 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
386 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
387 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
388 "CanItemBeReserved should return 'notReservable'");
390 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
391 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
392 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
393 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
394 'cannotReserveFromOtherBranches',
395 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
396 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
397 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
398 'OK',
399 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
401 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
402 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
403 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
404 "CanItemBeReserved should return 'OK'");
406 # Bug 12632
407 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
408 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
410 $dbh->do('DELETE FROM reserves');
411 $dbh->do('DELETE FROM issues');
412 $dbh->do('DELETE FROM items');
413 $dbh->do('DELETE FROM biblio');
415 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
416 ( $item_bibnum, $item_bibitemnum, $itemnumber )
417 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
419 $dbh->do(
420 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
421 VALUES (?, ?, ?, ?, ?)},
423 '*', '*', 'ONLY1', 1, 99
425 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
426 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
428 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
430 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
431 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
433 #results should be the same for both ReservesControlBranch settings
434 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
435 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
436 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
437 #reset for further tests
438 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
440 subtest 'Test max_holds per library/patron category' => sub {
441 plan tests => 6;
443 $dbh->do('DELETE FROM reserves');
444 $dbh->do('DELETE FROM issuingrules');
445 $dbh->do('DELETE FROM circulation_rules');
447 $biblio = $builder->build_sample_biblio({ itemtype => 'TEST' });
448 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
449 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
450 $biblio->biblionumber );
451 $dbh->do(
453 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
454 VALUES (?, ?, ?, ?, ?)
457 '*', '*', 'TEST', 99, 99
459 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
460 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
461 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
463 my $count =
464 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
465 is( $count, 3, 'Patron now has 3 holds' );
467 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
468 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
470 my $rule_all = Koha::CirculationRules->set_rule(
472 categorycode => $category->{categorycode},
473 branchcode => undef,
474 itemtype => undef,
475 rule_name => 'max_holds',
476 rule_value => 3,
480 my $rule_branch = Koha::CirculationRules->set_rule(
482 branchcode => $branch_1,
483 categorycode => $category->{categorycode},
484 itemtype => undef,
485 rule_name => 'max_holds',
486 rule_value => 5,
490 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
491 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
493 $rule_branch->delete();
495 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
496 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
498 $rule_all->delete();
499 $rule_branch->rule_value(3);
500 $rule_branch->store();
502 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
503 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
505 $rule_branch->rule_value(5);
506 $rule_branch->update();
507 $rule_branch->rule_value(5);
508 $rule_branch->store();
510 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
511 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
514 subtest 'Pickup location availability tests' => sub {
515 plan tests => 4;
517 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
518 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
519 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
520 #Add a default rule to allow some holds
521 $dbh->do(
522 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
523 VALUES (?, ?, ?, ?, ?)},
525 '*', '*', '*', 25, 99
527 my $item = Koha::Items->find($itemnumber);
528 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
529 my $library = Koha::Libraries->find($branch_to);
530 $library->pickup_location('1')->store;
531 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
533 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
534 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
536 $library->pickup_location('1')->store;
537 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
538 'OK', 'Library is a pickup location');
540 my $limit = Koha::Item::Transfer::Limit->new({
541 fromBranch => $item->holdingbranch,
542 toBranch => $branch_to,
543 itemtype => $item->effective_itemtype,
544 })->store;
545 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
546 'cannotBeTransferred', 'Item cannot be transferred');
547 $limit->delete;
549 $library->pickup_location('0')->store;
550 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
551 'libraryNotPickupLocation', 'Library is not a pickup location');
552 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
553 'libraryNotFound', 'Cannot set unknown library as pickup location');
556 $schema->storage->txn_rollback;
558 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
560 plan tests => 10;
562 $schema->storage->txn_begin;
564 Koha::Holds->search->delete;
565 $dbh->do('DELETE FROM issues');
566 Koha::Items->search->delete;
567 Koha::Biblios->search->delete;
569 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
570 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
571 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
573 # Create 3 biblios with items
574 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
575 my ( undef, undef, $itemnumber_1 ) = AddItem(
576 { homebranch => $library->branchcode,
577 holdingbranch => $library->branchcode
579 $biblio_1->biblionumber
581 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
582 my ( undef, undef, $itemnumber_2 ) = AddItem(
583 { homebranch => $library->branchcode,
584 holdingbranch => $library->branchcode
586 $biblio_2->biblionumber
588 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
589 my ( undef, undef, $itemnumber_3 ) = AddItem(
590 { homebranch => $library->branchcode,
591 holdingbranch => $library->branchcode
593 $biblio_3->biblionumber
596 Koha::IssuingRules->search->delete;
597 my $issuingrule = Koha::IssuingRule->new(
598 { categorycode => '*',
599 branchcode => '*',
600 itemtype => $itemtype->itemtype,
601 reservesallowed => 1,
602 holds_per_record => 99,
603 holds_per_day => 2
605 )->store;
607 is_deeply(
608 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
609 { status => 'OK' },
610 'Patron can reserve item with hold limit of 1, no holds placed'
613 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
615 is_deeply(
616 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
617 { status => 'tooManyReserves', limit => 1 },
618 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
621 # Raise reservesallowed to avoid tooManyReserves from it
622 $issuingrule->set( { reservesallowed => 3 } )->store;
624 is_deeply(
625 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
626 { status => 'OK' },
627 'Patron can reserve item with 2 reserves daily cap'
630 # Add a second reserve
631 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
632 is_deeply(
633 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
634 { status => 'tooManyReservesToday', limit => 2 },
635 'Patron cannot a third item with 2 reserves daily cap'
638 # Update last hold so reservedate is in the past, so 2 holds, but different day
639 $hold = Koha::Holds->find($res_id);
640 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
641 $hold->reservedate($yesterday)->store;
643 is_deeply(
644 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
645 { status => 'OK' },
646 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
649 # Set holds_per_day to 0
650 $issuingrule->set( { holds_per_day => 0 } )->store;
652 # Delete existing holds
653 Koha::Holds->search->delete;
654 is_deeply(
655 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
656 { status => 'tooManyReservesToday', limit => 0 },
657 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
660 $issuingrule->set( { holds_per_day => undef } )->store;
661 Koha::Holds->search->delete;
662 is_deeply(
663 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
664 { status => 'OK' },
665 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
667 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
668 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
669 is_deeply(
670 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
671 { status => 'OK' },
672 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
674 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
675 is_deeply(
676 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
677 { status => 'tooManyReserves', limit => 3 },
678 'Unlimited daily holds, but reached reservesallowed'
680 #results should be the same for both ReservesControlBranch settings
681 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
682 is_deeply(
683 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
684 { status => 'tooManyReserves', limit => 3 },
685 'Unlimited daily holds, but reached reservesallowed'
688 $schema->storage->txn_rollback;