Bug 20837: Unit 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 => 58;
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 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
58
59 # Create item instance for testing.
60 my ($item_bibnum, $item_bibitemnum, $itemnumber)
61     = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
62
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;
73 }
74
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,
89     );
90 }
91
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" );
99
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()");
111
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" );
127
128 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
129 $holds = $patron->holds;
130 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
131
132
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;
138
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 } ),
145 });
146
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" );
151
152 ModReserve({ # call without reserve_id
153     rank          => '3',
154     biblionumber  => $item_bibnum,
155     itemnumber    => $itemnumber,
156     borrowernumber => $borrowernumber,
157 });
158 $hold = Koha::Holds->find( $reserve_id );
159 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
160
161 ToggleSuspend( $reserve_id );
162 $hold = Koha::Holds->find( $reserve_id );
163 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
164
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" );
168
169 AutoUnsuspendReserves();
170 $hold = Koha::Holds->find( $reserve_id );
171 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
172
173 SuspendAll(
174     borrowernumber => $borrowernumber,
175     biblionumber   => $biblio->biblionumber,
176     suspend => 1,
177     suspend_until => '2012-01-01',
178 );
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" );
182
183 SuspendAll(
184     borrowernumber => $borrowernumber,
185     biblionumber   => $biblio->biblionumber,
186     suspend => 0,
187 );
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" );
191
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,
205 );
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()" );
212
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" );
218
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" );
222
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" );
226
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" );
230
231 # Regression test for bug 2394
232 #
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.
238 #
239 # Note that canreservefromotherbranches has no effect if
240 # IndependentBranches is OFF.
241
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 (?, ?, ?, ?, ?)},
249     {},
250     '*', '*', '*', 25, 99
251 );
252 $dbh->do(
253     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
254       VALUES (?, ?, ?, ?, ?)},
255     {},
256     '*', '*', 'CANNOT', 0, 99
257 );
258
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);
262
263 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
264 t::lib::Mocks::mock_preference('IndependentBranches', 0);
265 ok(
266     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
267     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
268 );
269
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);
273 ok(
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)'
276 );
277
278 # ... unless canreservefromotherbranches is ON
279 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
280 ok(
281     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
282     '... unless canreservefromotherbranches is ON (bug 2394)'
283 );
284
285 {
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" );
300 }
301
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" );
306
307 $hold = Koha::Hold->new(
308     {
309         borrowernumber => $borrowernumbers[0],
310         itemnumber     => $itemnumber,
311         biblionumber   => $item_bibnum,
312     }
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();
318
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" );
322
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,
330     '',
331     1,
332 );
333 is(
334     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
335     "cannot request item if policy that matches on item-level item type forbids it"
336 );
337 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
338 ok(
339     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
340     "can request item if policy that matches on item type allows it"
341 );
342
343 t::lib::Mocks::mock_preference('item-level_itypes', 0);
344 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
345 ok(
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)"
348 );
349
350
351 # Test branch item rules
352
353 $dbh->do('DELETE FROM issuingrules');
354 $dbh->do(
355     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
356       VALUES (?, ?, ?, ?)},
357     {},
358     '*', '*', '*', 25
359 );
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'");
377
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'");
388
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'");
393
394 # Bug 12632
395 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
396 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
397
398 $dbh->do('DELETE FROM reserves');
399 $dbh->do('DELETE FROM issues');
400 $dbh->do('DELETE FROM items');
401 $dbh->do('DELETE FROM biblio');
402
403 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
404 ( $item_bibnum, $item_bibitemnum, $itemnumber )
405     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
406
407 $dbh->do(
408     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
409       VALUES (?, ?, ?, ?, ?)},
410     {},
411     '*', '*', 'ONLY1', 1, 99
412 );
413 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
414     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
415
416 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
417
418 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
419     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
420
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' );
427
428 subtest 'Test max_holds per library/patron category' => sub {
429     plan tests => 6;
430
431     $dbh->do('DELETE FROM reserves');
432     $dbh->do('DELETE FROM issuingrules');
433     $dbh->do('DELETE FROM circulation_rules');
434
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(
440         q{
441             INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
442             VALUES (?, ?, ?, ?, ?)
443         },
444         {},
445         '*', '*', 'TEST', 99, 99
446     );
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, );
450
451     my $count =
452       Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
453     is( $count, 3, 'Patron now has 3 holds' );
454
455     my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
456     is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
457
458     my $rule_all = Koha::CirculationRules->set_rule(
459         {
460             categorycode => $category->{categorycode},
461             branchcode   => undef,
462             itemtype     => undef,
463             rule_name    => 'max_holds',
464             rule_value   => 3,
465         }
466     );
467
468     my $rule_branch = Koha::CirculationRules->set_rule(
469         {
470             branchcode   => $branch_1,
471             categorycode => $category->{categorycode},
472             itemtype     => undef,
473             rule_name    => 'max_holds',
474             rule_value   => 5,
475         }
476     );
477
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' );
480
481     $rule_branch->delete();
482
483     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
484     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
485
486     $rule_all->delete();
487     $rule_branch->rule_value(3);
488     $rule_branch->store();
489
490     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
491     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
492
493     $rule_branch->rule_value(5);
494     $rule_branch->update();
495     $rule_branch->rule_value(5);
496     $rule_branch->store();
497
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' );
500 };
501
502 subtest 'Pickup location availability tests' => sub {
503     plan tests => 4;
504
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 (?, ?, ?, ?, ?)},
512         {},
513         '*', '*', '*', 25, 99
514     );
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 };
520
521     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
522     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
523
524     $library->pickup_location('1')->store;
525     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
526        'OK', 'Library is a pickup location');
527
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;
536
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');
542 };
543
544 $schema->storage->txn_rollback;
545
546 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
547
548     plan tests => 10;
549
550     $schema->storage->txn_begin;
551
552     Koha::Holds->search->delete;
553     $dbh->do('DELETE FROM issues');
554     Koha::Items->search->delete;
555     Koha::Biblios->search->delete;
556
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' } );
560
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
566         },
567         $biblio_1->biblionumber
568     );
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
573         },
574         $biblio_2->biblionumber
575     );
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
580         },
581         $biblio_3->biblionumber
582     );
583
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
592         }
593     )->store;
594
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'
599     );
600
601     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
602
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'
607     );
608
609     # Raise reservesallowed to avoid tooManyReserves from it
610     $issuingrule->set( { reservesallowed => 3 } )->store;
611
612     is_deeply(
613         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
614         { status => 'OK' },
615         'Patron can reserve item with 2 reserves daily cap'
616     );
617
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'
624     );
625
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;
630
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'
635     );
636
637     # Set holds_per_day to 0
638     $issuingrule->set( { holds_per_day => 0 } )->store;
639
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)'
646     );
647
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)'
654     );
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)'
661     );
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'
667     );
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'
674     );
675
676     $schema->storage->txn_rollback;
677 };