Bug 22709: (follow-up) Move new test file into a Plugins subdirectory
[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 => 59;
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 $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');
56
57 # Setup Test------------------------
58 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
59
60 # Create item instance for testing.
61 my ($item_bibnum, $item_bibitemnum, $itemnumber)
62     = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
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 # 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,
90     );
91 }
92
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" );
100
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()");
112
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" );
128
129 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
130 $holds = $patron->holds;
131 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
132
133
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;
139
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 } ),
146 });
147
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" );
152
153 ModReserve({ # call without reserve_id
154     rank          => '3',
155     biblionumber  => $item_bibnum,
156     itemnumber    => $itemnumber,
157     borrowernumber => $borrowernumber,
158 });
159 $hold = Koha::Holds->find( $reserve_id );
160 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
161
162 ToggleSuspend( $reserve_id );
163 $hold = Koha::Holds->find( $reserve_id );
164 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
165
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" );
169
170 AutoUnsuspendReserves();
171 $hold = Koha::Holds->find( $reserve_id );
172 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
173
174 SuspendAll(
175     borrowernumber => $borrowernumber,
176     biblionumber   => $biblio->biblionumber,
177     suspend => 1,
178     suspend_until => '2012-01-01',
179 );
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" );
183
184 SuspendAll(
185     borrowernumber => $borrowernumber,
186     biblionumber   => $biblio->biblionumber,
187     suspend => 0,
188 );
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" );
192
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,
206 );
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()" );
213
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" );
219
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" );
223
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" );
227
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" );
231
232 # Regression test for bug 2394
233 #
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.
239 #
240 # Note that canreservefromotherbranches has no effect if
241 # IndependentBranches is OFF.
242
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 (?, ?, ?, ?, ?)},
250     {},
251     '*', '*', '*', 25, 99
252 );
253 $dbh->do(
254     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
255       VALUES (?, ?, ?, ?, ?)},
256     {},
257     '*', '*', 'CANNOT', 0, 99
258 );
259
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);
263
264 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
265 t::lib::Mocks::mock_preference('IndependentBranches', 0);
266 ok(
267     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
268     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
269 );
270
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);
274 ok(
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)'
277 );
278
279 # ... unless canreservefromotherbranches is ON
280 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
281 ok(
282     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
283     '... unless canreservefromotherbranches is ON (bug 2394)'
284 );
285
286 {
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" );
301 }
302
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" );
307
308 $hold = Koha::Hold->new(
309     {
310         borrowernumber => $borrowernumbers[0],
311         itemnumber     => $itemnumber,
312         biblionumber   => $item_bibnum,
313     }
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();
319
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" );
323
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,
331     '',
332     1,
333 );
334 is(
335     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
336     "cannot request item if policy that matches on item-level item type forbids it"
337 );
338 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
339 ok(
340     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
341     "can request item if policy that matches on item type allows it"
342 );
343
344 t::lib::Mocks::mock_preference('item-level_itypes', 0);
345 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
346 ok(
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)"
349 );
350
351
352 # Test branch item rules
353
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 (?, ?, ?, ?)},
359     {},
360     '*', '*', '*', 25
361 );
362 Koha::CirculationRules->set_rules(
363     {
364         branchcode => $branch_1,
365         itemtype   => 'CANNOT',
366         categorycode => undef,
367         rules => {
368             holdallowed => 0,
369             returnbranch => 'homebranch',
370         }
371     }
372 );
373 Koha::CirculationRules->set_rules(
374     {
375         branchcode => $branch_1,
376         itemtype   => 'CAN',
377         categorycode => undef,
378         rules => {
379             holdallowed => 1,
380             returnbranch => 'homebranch',
381         }
382     }
383 );
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'");
389
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'");
400
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'");
405
406 # Bug 12632
407 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
408 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
409
410 $dbh->do('DELETE FROM reserves');
411 $dbh->do('DELETE FROM issues');
412 $dbh->do('DELETE FROM items');
413 $dbh->do('DELETE FROM biblio');
414
415 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
416 ( $item_bibnum, $item_bibitemnum, $itemnumber )
417     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
418
419 $dbh->do(
420     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
421       VALUES (?, ?, ?, ?, ?)},
422     {},
423     '*', '*', 'ONLY1', 1, 99
424 );
425 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
426     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
427
428 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
429
430 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
431     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
432
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' );
439
440 subtest 'Test max_holds per library/patron category' => sub {
441     plan tests => 6;
442
443     $dbh->do('DELETE FROM reserves');
444     $dbh->do('DELETE FROM issuingrules');
445     $dbh->do('DELETE FROM circulation_rules');
446
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(
452         q{
453             INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
454             VALUES (?, ?, ?, ?, ?)
455         },
456         {},
457         '*', '*', 'TEST', 99, 99
458     );
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, );
462
463     my $count =
464       Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
465     is( $count, 3, 'Patron now has 3 holds' );
466
467     my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
468     is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
469
470     my $rule_all = Koha::CirculationRules->set_rule(
471         {
472             categorycode => $category->{categorycode},
473             branchcode   => undef,
474             itemtype     => undef,
475             rule_name    => 'max_holds',
476             rule_value   => 3,
477         }
478     );
479
480     my $rule_branch = Koha::CirculationRules->set_rule(
481         {
482             branchcode   => $branch_1,
483             categorycode => $category->{categorycode},
484             itemtype     => undef,
485             rule_name    => 'max_holds',
486             rule_value   => 5,
487         }
488     );
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 3' );
492
493     $rule_branch->delete();
494
495     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
496     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
497
498     $rule_all->delete();
499     $rule_branch->rule_value(3);
500     $rule_branch->store();
501
502     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
503     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
504
505     $rule_branch->rule_value(5);
506     $rule_branch->update();
507     $rule_branch->rule_value(5);
508     $rule_branch->store();
509
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' );
512 };
513
514 subtest 'Pickup location availability tests' => sub {
515     plan tests => 4;
516
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 (?, ?, ?, ?, ?)},
524         {},
525         '*', '*', '*', 25, 99
526     );
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 };
532
533     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
534     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
535
536     $library->pickup_location('1')->store;
537     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
538        'OK', 'Library is a pickup location');
539
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;
548
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');
554 };
555
556 $schema->storage->txn_rollback;
557
558 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
559
560     plan tests => 10;
561
562     $schema->storage->txn_begin;
563
564     Koha::Holds->search->delete;
565     $dbh->do('DELETE FROM issues');
566     Koha::Items->search->delete;
567     Koha::Biblios->search->delete;
568
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' } );
572
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
578         },
579         $biblio_1->biblionumber
580     );
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
585         },
586         $biblio_2->biblionumber
587     );
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
592         },
593         $biblio_3->biblionumber
594     );
595
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
604         }
605     )->store;
606
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'
611     );
612
613     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
614
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'
619     );
620
621     # Raise reservesallowed to avoid tooManyReserves from it
622     $issuingrule->set( { reservesallowed => 3 } )->store;
623
624     is_deeply(
625         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
626         { status => 'OK' },
627         'Patron can reserve item with 2 reserves daily cap'
628     );
629
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'
636     );
637
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;
642
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'
647     );
648
649     # Set holds_per_day to 0
650     $issuingrule->set( { holds_per_day => 0 } )->store;
651
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)'
658     );
659
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)'
666     );
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)'
673     );
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'
679     );
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'
686     );
687
688     $schema->storage->txn_rollback;
689 };