Bug 23150: Do not consider gdpr_proc_consent a mandatory field
[koha.git] / t / db_dependent / Holds.t
blob1515b4bef4eedf521076f09ec5e290212c9501fc
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 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 # Regression test for bug 9532
324 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
325 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
326 AddReserve(
327 $branch_1,
328 $borrowernumbers[0],
329 $biblio->biblionumber,
334 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
335 "cannot request item if policy that matches on item-level item type forbids it"
337 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
339 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
340 "can request item if policy that matches on item type allows it"
343 t::lib::Mocks::mock_preference('item-level_itypes', 0);
344 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
346 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
347 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
351 # Test branch item rules
353 $dbh->do('DELETE FROM issuingrules');
354 $dbh->do(
355 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
356 VALUES (?, ?, ?, ?)},
358 '*', '*', '*', 25
360 $dbh->do('DELETE FROM branch_item_rules');
361 $dbh->do('DELETE FROM default_branch_circ_rules');
362 $dbh->do('DELETE FROM default_branch_item_rules');
363 $dbh->do('DELETE FROM default_circ_rules');
364 $dbh->do(q{
365 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
366 VALUES (?, ?, ?, ?)
367 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
368 $dbh->do(q{
369 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
370 VALUES (?, ?, ?, ?)
371 }, {}, $branch_1, 'CAN', 1, 'homebranch');
372 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
373 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
374 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
375 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
376 "CanItemBeReserved should return 'notReservable'");
378 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
379 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
380 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
381 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
382 'cannotReserveFromOtherBranches',
383 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
384 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
385 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
386 'OK',
387 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
389 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
390 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
391 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
392 "CanItemBeReserved should return 'OK'");
394 # Bug 12632
395 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
396 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
398 $dbh->do('DELETE FROM reserves');
399 $dbh->do('DELETE FROM issues');
400 $dbh->do('DELETE FROM items');
401 $dbh->do('DELETE FROM biblio');
403 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
404 ( $item_bibnum, $item_bibitemnum, $itemnumber )
405 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
407 $dbh->do(
408 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
409 VALUES (?, ?, ?, ?, ?)},
411 '*', '*', 'ONLY1', 1, 99
413 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
414 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
416 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
418 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
419 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
421 #results should be the same for both ReservesControlBranch settings
422 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
423 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
424 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
425 #reset for further tests
426 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
428 subtest 'Test max_holds per library/patron category' => sub {
429 plan tests => 6;
431 $dbh->do('DELETE FROM reserves');
432 $dbh->do('DELETE FROM issuingrules');
433 $dbh->do('DELETE FROM circulation_rules');
435 $biblio = $builder->build_sample_biblio({ itemtype => 'TEST' });
436 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
437 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
438 $biblio->biblionumber );
439 $dbh->do(
441 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
442 VALUES (?, ?, ?, ?, ?)
445 '*', '*', 'TEST', 99, 99
447 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
448 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
449 AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
451 my $count =
452 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
453 is( $count, 3, 'Patron now has 3 holds' );
455 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
456 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
458 my $rule_all = Koha::CirculationRules->set_rule(
460 categorycode => $category->{categorycode},
461 branchcode => undef,
462 itemtype => undef,
463 rule_name => 'max_holds',
464 rule_value => 3,
468 my $rule_branch = Koha::CirculationRules->set_rule(
470 branchcode => $branch_1,
471 categorycode => $category->{categorycode},
472 itemtype => undef,
473 rule_name => 'max_holds',
474 rule_value => 5,
478 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
479 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
481 $rule_branch->delete();
483 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
484 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
486 $rule_all->delete();
487 $rule_branch->rule_value(3);
488 $rule_branch->store();
490 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
491 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
493 $rule_branch->rule_value(5);
494 $rule_branch->update();
495 $rule_branch->rule_value(5);
496 $rule_branch->store();
498 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
499 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
502 subtest 'Pickup location availability tests' => sub {
503 plan tests => 4;
505 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
506 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
507 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
508 #Add a default rule to allow some holds
509 $dbh->do(
510 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
511 VALUES (?, ?, ?, ?, ?)},
513 '*', '*', '*', 25, 99
515 my $item = Koha::Items->find($itemnumber);
516 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
517 my $library = Koha::Libraries->find($branch_to);
518 $library->pickup_location('1')->store;
519 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
521 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
522 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
524 $library->pickup_location('1')->store;
525 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
526 'OK', 'Library is a pickup location');
528 my $limit = Koha::Item::Transfer::Limit->new({
529 fromBranch => $item->holdingbranch,
530 toBranch => $branch_to,
531 itemtype => $item->effective_itemtype,
532 })->store;
533 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
534 'cannotBeTransferred', 'Item cannot be transferred');
535 $limit->delete;
537 $library->pickup_location('0')->store;
538 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
539 'libraryNotPickupLocation', 'Library is not a pickup location');
540 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
541 'libraryNotFound', 'Cannot set unknown library as pickup location');
544 $schema->storage->txn_rollback;
546 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
548 plan tests => 10;
550 $schema->storage->txn_begin;
552 Koha::Holds->search->delete;
553 $dbh->do('DELETE FROM issues');
554 Koha::Items->search->delete;
555 Koha::Biblios->search->delete;
557 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
558 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
559 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
561 # Create 3 biblios with items
562 my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
563 my ( undef, undef, $itemnumber_1 ) = AddItem(
564 { homebranch => $library->branchcode,
565 holdingbranch => $library->branchcode
567 $biblio_1->biblionumber
569 my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
570 my ( undef, undef, $itemnumber_2 ) = AddItem(
571 { homebranch => $library->branchcode,
572 holdingbranch => $library->branchcode
574 $biblio_2->biblionumber
576 my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
577 my ( undef, undef, $itemnumber_3 ) = AddItem(
578 { homebranch => $library->branchcode,
579 holdingbranch => $library->branchcode
581 $biblio_3->biblionumber
584 Koha::IssuingRules->search->delete;
585 my $issuingrule = Koha::IssuingRule->new(
586 { categorycode => '*',
587 branchcode => '*',
588 itemtype => $itemtype->itemtype,
589 reservesallowed => 1,
590 holds_per_record => 99,
591 holds_per_day => 2
593 )->store;
595 is_deeply(
596 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
597 { status => 'OK' },
598 'Patron can reserve item with hold limit of 1, no holds placed'
601 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
603 is_deeply(
604 CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
605 { status => 'tooManyReserves', limit => 1 },
606 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
609 # Raise reservesallowed to avoid tooManyReserves from it
610 $issuingrule->set( { reservesallowed => 3 } )->store;
612 is_deeply(
613 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
614 { status => 'OK' },
615 'Patron can reserve item with 2 reserves daily cap'
618 # Add a second reserve
619 my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
620 is_deeply(
621 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
622 { status => 'tooManyReservesToday', limit => 2 },
623 'Patron cannot a third item with 2 reserves daily cap'
626 # Update last hold so reservedate is in the past, so 2 holds, but different day
627 $hold = Koha::Holds->find($res_id);
628 my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
629 $hold->reservedate($yesterday)->store;
631 is_deeply(
632 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
633 { status => 'OK' },
634 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
637 # Set holds_per_day to 0
638 $issuingrule->set( { holds_per_day => 0 } )->store;
640 # Delete existing holds
641 Koha::Holds->search->delete;
642 is_deeply(
643 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
644 { status => 'tooManyReservesToday', limit => 0 },
645 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
648 $issuingrule->set( { holds_per_day => undef } )->store;
649 Koha::Holds->search->delete;
650 is_deeply(
651 CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
652 { status => 'OK' },
653 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
655 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
656 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
657 is_deeply(
658 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
659 { status => 'OK' },
660 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
662 AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
663 is_deeply(
664 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
665 { status => 'tooManyReserves', limit => 3 },
666 'Unlimited daily holds, but reached reservesallowed'
668 #results should be the same for both ReservesControlBranch settings
669 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
670 is_deeply(
671 CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
672 { status => 'tooManyReserves', limit => 3 },
673 'Unlimited daily holds, but reached reservesallowed'
676 $schema->storage->txn_rollback;