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