1e76ede04d12f24cc0cbc394f13a90d7ebe0bdb6
[koha.git] / t / db_dependent / Holds.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
7
8 use C4::Context;
9
10 use Test::More tests => 57;
11 use MARC::Record;
12
13 use C4::Biblio;
14 use C4::Calendar;
15 use C4::Items;
16 use C4::Reserves;
17
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;
28
29 BEGIN {
30     use FindBin;
31     use lib $FindBin::Bin;
32 }
33
34 my $schema  = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 my $builder = t::lib::TestBuilder->new();
38 my $dbh     = C4::Context->dbh;
39
40 # Create two random branches
41 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
42 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
43
44 my $category = $builder->build({ source => 'Category' });
45
46 my $borrowers_count = 5;
47
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');
55
56 # Setup Test------------------------
57 # Create a biblio instance for testing
58 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
59
60 # Create item instance for testing.
61 my ($item_bibnum, $item_bibitemnum, $itemnumber)
62     = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
63
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;
74 }
75
76 my $biblionumber = $bibnum;
77
78 # Create five item level holds
79 foreach my $borrowernumber ( @borrowernumbers ) {
80     AddReserve(
81         $branch_1,
82         $borrowernumber,
83         $biblionumber,
84         my $bibitems = q{},
85         my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
86         my $resdate,
87         my $expdate,
88         my $notes = q{},
89         $title,
90         my $checkitem = $itemnumber,
91         my $found,
92     );
93 }
94
95 my $biblio = Koha::Biblios->find( $biblionumber );
96 my $holds = $biblio->holds;
97 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
98 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
99 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
100 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
101 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
102 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
103
104 my $item = Koha::Items->find( $itemnumber );
105 $holds = $item->current_holds;
106 my $first_hold = $holds->next;
107 my $reservedate = $first_hold->reservedate;
108 my $borrowernumber = $first_hold->borrowernumber;
109 my $branch_1code = $first_hold->branchcode;
110 my $reserve_id = $first_hold->reserve_id;
111 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
112 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
113 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
114 ok($reserve_id, "Test holds_placed_today()");
115
116 my $hold = Koha::Holds->find( $reserve_id );
117 ok( $hold, "Koha::Holds found the hold" );
118 my $hold_biblio = $hold->biblio();
119 ok( $hold_biblio, "Got biblio using biblio() method" );
120 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
121 my $hold_item = $hold->item();
122 ok( $hold_item, "Got item using item() method" );
123 ok( $hold_item == $hold->item(), "item method returns stashed item" );
124 my $hold_branch = $hold->branch();
125 ok( $hold_branch, "Got branch using branch() method" );
126 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
127 my $hold_found = $hold->found();
128 $hold->set({ found => 'W'})->store();
129 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
130 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
131
132 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
133 $holds = $patron->holds;
134 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
135
136
137 $holds = $item->current_holds;
138 $first_hold = $holds->next;
139 $borrowernumber = $first_hold->borrowernumber;
140 $branch_1code = $first_hold->branchcode;
141 $reserve_id = $first_hold->reserve_id;
142
143 ModReserve({
144     reserve_id    => $reserve_id,
145     rank          => '4',
146     branchcode    => $branch_1,
147     itemnumber    => $itemnumber,
148     suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
149 });
150
151 $hold = Koha::Holds->find( $reserve_id );
152 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
153 ok( $hold->suspend, "Test ModReserve, suspend hold" );
154 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
155
156 ModReserve({ # call without reserve_id
157     rank          => '3',
158     biblionumber  => $item_bibnum,
159     itemnumber    => $itemnumber,
160     borrowernumber => $borrowernumber,
161 });
162 $hold = Koha::Holds->find( $reserve_id );
163 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
164
165 ToggleSuspend( $reserve_id );
166 $hold = Koha::Holds->find( $reserve_id );
167 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
168
169 ToggleSuspend( $reserve_id, '2012-01-01' );
170 $hold = Koha::Holds->find( $reserve_id );
171 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
172
173 AutoUnsuspendReserves();
174 $hold = Koha::Holds->find( $reserve_id );
175 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
176
177 SuspendAll(
178     borrowernumber => $borrowernumber,
179     biblionumber   => $biblionumber,
180     suspend => 1,
181     suspend_until => '2012-01-01',
182 );
183 $hold = Koha::Holds->find( $reserve_id );
184 is( $hold->suspend, 1, "Test SuspendAll()" );
185 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
186
187 SuspendAll(
188     borrowernumber => $borrowernumber,
189     biblionumber   => $biblionumber,
190     suspend => 0,
191 );
192 $hold = Koha::Holds->find( $reserve_id );
193 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
194 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
195
196 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
197 AddReserve(
198     $branch_1,
199     $borrowernumbers[0],
200     $biblionumber,
201     my $bibitems = q{},
202     my $priority,
203     my $resdate,
204     my $expdate,
205     my $notes = q{},
206     $title,
207     my $checkitem,
208     my $found,
209 );
210 $patron = Koha::Patrons->find( $borrowernumber );
211 $holds = $patron->holds;
212 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
213 ModReserveMinusPriority( $itemnumber, $reserveid );
214 $holds = $patron->holds;
215 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
216
217 $holds = $biblio->holds;
218 $hold = $holds->next;
219 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
222
223 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
224 $hold = Koha::Holds->find( $reserveid );
225 is( $hold->priority, '2', "Test AlterPriority(), move down" );
226
227 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
228 $hold = Koha::Holds->find( $reserveid );
229 is( $hold->priority, '1', "Test AlterPriority(), move up" );
230
231 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
232 $hold = Koha::Holds->find( $reserveid );
233 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
234
235 # Regression test for bug 2394
236 #
237 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
238 # a patron is not permittedo to request an item whose homebranch (i.e.,
239 # owner of the item) is different from the patron's own library.
240 # However, if canreservefromotherbranches is turned ON, the patron can
241 # create such hold requests.
242 #
243 # Note that canreservefromotherbranches has no effect if
244 # IndependentBranches is OFF.
245
246 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
247 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
248   = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
249 $dbh->do('DELETE FROM issuingrules');
250 $dbh->do(
251     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
252       VALUES (?, ?, ?, ?, ?)},
253     {},
254     '*', '*', '*', 25, 99
255 );
256 $dbh->do(
257     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
258       VALUES (?, ?, ?, ?, ?)},
259     {},
260     '*', '*', 'CANNOT', 0, 99
261 );
262
263 # make sure some basic sysprefs are set
264 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
265 t::lib::Mocks::mock_preference('item-level_itypes', 1);
266
267 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
268 t::lib::Mocks::mock_preference('IndependentBranches', 0);
269 ok(
270     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
271     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
272 );
273
274 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
275 t::lib::Mocks::mock_preference('IndependentBranches', 1);
276 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
277 ok(
278     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
279     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
280 );
281
282 # ... unless canreservefromotherbranches is ON
283 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
284 ok(
285     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
286     '... unless canreservefromotherbranches is ON (bug 2394)'
287 );
288
289 {
290     # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
291     ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
292     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
293     my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
294     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
295     my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
296     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
297     my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
298     my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
299     my $hold3 = Koha::Holds->find( $reserveid3 );
300     is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
301     ModReserve({ reserve_id => $reserveid1, rank => 'del' });
302     ModReserve({ reserve_id => $reserveid2, rank => 'del' });
303     is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
304 }
305
306 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
307 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
308 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
309 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
310
311 $hold = Koha::Hold->new(
312     {
313         borrowernumber => $borrowernumbers[0],
314         itemnumber     => $itemnumber,
315         biblionumber   => $item_bibnum,
316     }
317 )->store();
318 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
319     'itemAlreadyOnHold',
320     "Patron cannot place a second item level hold for a given item" );
321 $hold->delete();
322
323 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
324 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
325 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
326
327 # Regression test for bug 9532
328 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
329 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
330 AddReserve(
331     $branch_1,
332     $borrowernumbers[0],
333     $bibnum,
334     '',
335     1,
336 );
337 is(
338     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
339     "cannot request item if policy that matches on item-level item type forbids it"
340 );
341 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
342 ok(
343     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
344     "can request item if policy that matches on item type allows it"
345 );
346
347 t::lib::Mocks::mock_preference('item-level_itypes', 0);
348 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
349 ok(
350     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
351     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
352 );
353
354
355 # Test branch item rules
356
357 $dbh->do('DELETE FROM issuingrules');
358 $dbh->do(
359     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
360       VALUES (?, ?, ?, ?)},
361     {},
362     '*', '*', '*', 25
363 );
364 $dbh->do('DELETE FROM branch_item_rules');
365 $dbh->do('DELETE FROM default_branch_circ_rules');
366 $dbh->do('DELETE FROM default_branch_item_rules');
367 $dbh->do('DELETE FROM default_circ_rules');
368 $dbh->do(q{
369     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
370     VALUES (?, ?, ?, ?)
371 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
372 $dbh->do(q{
373     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
374     VALUES (?, ?, ?, ?)
375 }, {}, $branch_1, 'CAN', 1, 'homebranch');
376 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
377 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
378     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
379 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
380     "CanItemBeReserved should return 'notReservable'");
381
382 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
383     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
384 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
385     'cannotReserveFromOtherBranches',
386     "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
387
388 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
389     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
390 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
391     "CanItemBeReserved should return 'OK'");
392
393 # Bug 12632
394 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
395 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
396
397 $dbh->do('DELETE FROM reserves');
398 $dbh->do('DELETE FROM issues');
399 $dbh->do('DELETE FROM items');
400 $dbh->do('DELETE FROM biblio');
401
402 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
403 ( $item_bibnum, $item_bibitemnum, $itemnumber )
404     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
405
406 $dbh->do(
407     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
408       VALUES (?, ?, ?, ?, ?)},
409     {},
410     '*', '*', 'ONLY1', 1, 99
411 );
412 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
413     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
414
415 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
416
417 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
418     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
419
420 subtest 'Test max_holds per library/patron category' => sub {
421     plan tests => 6;
422
423     $dbh->do('DELETE FROM reserves');
424     $dbh->do('DELETE FROM issuingrules');
425     $dbh->do('DELETE FROM circulation_rules');
426
427     ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('TEST');
428     ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
429       AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
430         $bibnum );
431     $dbh->do(
432         q{
433             INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
434             VALUES (?, ?, ?, ?, ?)
435         },
436         {},
437         '*', '*', 'TEST', 99, 99
438     );
439     AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
440     AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
441     AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
442
443     my $count =
444       Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
445     is( $count, 3, 'Patron now has 3 holds' );
446
447     my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
448     is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
449
450     my $rule_all = Koha::CirculationRules->set_rule(
451         {
452             categorycode => $category->{categorycode},
453             branchcode   => undef,
454             itemtype     => undef,
455             rule_name    => 'max_holds',
456             rule_value   => 3,
457         }
458     );
459
460     my $rule_branch = Koha::CirculationRules->set_rule(
461         {
462             branchcode   => $branch_1,
463             categorycode => $category->{categorycode},
464             itemtype     => undef,
465             rule_name    => 'max_holds',
466             rule_value   => 5,
467         }
468     );
469
470     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
471     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
472
473     $rule_branch->delete();
474
475     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
476     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
477
478     $rule_all->delete();
479     $rule_branch->rule_value(3);
480     $rule_branch->store();
481
482     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
483     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
484
485     $rule_branch->rule_value(5);
486     $rule_branch->update();
487     $rule_branch->rule_value(5);
488     $rule_branch->store();
489
490     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
491     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
492 };
493
494 subtest 'Pickup location availability tests' => sub {
495     plan tests => 4;
496
497     my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
498     my ( $item_bibnum, $item_bibitemnum, $itemnumber )
499     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
500     #Add a default rule to allow some holds
501     $dbh->do(
502         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
503           VALUES (?, ?, ?, ?, ?)},
504         {},
505         '*', '*', '*', 25, 99
506     );
507     my $item = Koha::Items->find($itemnumber);
508     my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
509     my $library = Koha::Libraries->find($branch_to);
510     $library->pickup_location('1')->store;
511     my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
512
513     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
514     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
515
516     $library->pickup_location('1')->store;
517     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
518        'OK', 'Library is a pickup location');
519
520     my $limit = Koha::Item::Transfer::Limit->new({
521         fromBranch => $item->holdingbranch,
522         toBranch => $branch_to,
523         itemtype => $item->effective_itemtype,
524     })->store;
525     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to),
526        'cannotBeTransferred', 'Item cannot be transferred');
527     $limit->delete;
528
529     $library->pickup_location('0')->store;
530     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
531        'libraryNotPickupLocation', 'Library is not a pickup location');
532     is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
533        'libraryNotFound', 'Cannot set unknown library as pickup location');
534 };
535
536 $schema->storage->txn_rollback;
537
538 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
539
540     plan tests => 9;
541
542     $schema->storage->txn_begin;
543
544     Koha::Holds->search->delete;
545     $dbh->do('DELETE FROM issues');
546     Koha::Items->search->delete;
547     Koha::Biblios->search->delete;
548
549     my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
550     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
551     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
552
553     # Create 3 biblios with items
554     my ($bibnum_1) = create_helper_biblio( $itemtype->itemtype );
555     my ( undef, undef, $itemnumber_1 ) = AddItem(
556         {   homebranch    => $library->branchcode,
557             holdingbranch => $library->branchcode
558         },
559         $bibnum
560     );
561     my ($bibnum_2) = create_helper_biblio( $itemtype->itemtype );
562     my ( undef, undef, $itemnumber_2 ) = AddItem(
563         {   homebranch    => $library->branchcode,
564             holdingbranch => $library->branchcode
565         },
566         $bibnum_2
567     );
568     my ($bibnum_3) = create_helper_biblio( $itemtype->itemtype );
569     my ( undef, undef, $itemnumber_3 ) = AddItem(
570         {   homebranch    => $library->branchcode,
571             holdingbranch => $library->branchcode
572         },
573         $bibnum_3
574     );
575
576     Koha::IssuingRules->search->delete;
577     my $issuingrule = Koha::IssuingRule->new(
578         {   categorycode     => '*',
579             branchcode       => '*',
580             itemtype         => $itemtype->itemtype,
581             reservesallowed  => 1,
582             holds_per_record => 99,
583             holds_per_day    => 2
584         }
585     )->store;
586
587     is_deeply(
588         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
589         { status => 'OK' },
590         'Patron can reserve item with hold limit of 1, no holds placed'
591     );
592
593     AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
594
595     is_deeply(
596         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
597         { status => 'tooManyReserves', limit => 1 },
598         'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
599     );
600
601     # Raise reservesallowed to avoid tooManyReserves from it
602     $issuingrule->set( { reservesallowed => 3 } )->store;
603
604     is_deeply(
605         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
606         { status => 'OK' },
607         'Patron can reserve item with 2 reserves daily cap'
608     );
609
610     # Add a second reserve
611     my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
612     is_deeply(
613         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
614         { status => 'tooManyReservesToday', limit => 2 },
615         'Patron cannot a third item with 2 reserves daily cap'
616     );
617
618     # Update last hold so reservedate is in the past, so 2 holds, but different day
619     $hold = Koha::Holds->find($res_id);
620     my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
621     $hold->reservedate($yesterday)->store;
622
623     is_deeply(
624         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
625         { status => 'OK' },
626         'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
627     );
628
629     # Set holds_per_day to 0
630     $issuingrule->set( { holds_per_day => 0 } )->store;
631
632     # Delete existing holds
633     Koha::Holds->search->delete;
634     is_deeply(
635         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
636         { status => 'tooManyReservesToday', limit => 0 },
637         'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
638     );
639
640     $issuingrule->set( { holds_per_day => undef } )->store;
641     Koha::Holds->search->delete;
642     is_deeply(
643         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
644         { status => 'OK' },
645         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
646     );
647     AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
648     AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
649     is_deeply(
650         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
651         { status => 'OK' },
652         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
653     );
654     AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_3, '', 1, );
655     is_deeply(
656         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
657         { status => 'tooManyReserves', limit => 3 },
658         'Unlimited daily holds, but reached reservesallowed'
659     );
660
661     $schema->storage->txn_rollback;
662 };
663
664 # Helper method to set up a Biblio.
665 sub create_helper_biblio {
666     my $itemtype = shift;
667     my $bib = MARC::Record->new();
668     my $title = 'Silence in the library';
669     $bib->append_fields(
670         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
671         MARC::Field->new('245', ' ', ' ', a => $title),
672         MARC::Field->new('942', ' ', ' ', c => $itemtype),
673     );
674     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
675 }