Bug 19489: (QA follow-up) Update test to use objects and module methods for creating...
[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 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 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
379     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
380 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
381     'cannotReserveFromOtherBranches',
382     "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
383
384 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
385     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
386 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
387     "CanItemBeReserved should return 'OK'");
388
389 # Bug 12632
390 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
391 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
392
393 $dbh->do('DELETE FROM reserves');
394 $dbh->do('DELETE FROM issues');
395 $dbh->do('DELETE FROM items');
396 $dbh->do('DELETE FROM biblio');
397
398 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
399 ( $item_bibnum, $item_bibitemnum, $itemnumber )
400     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
401
402 $dbh->do(
403     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
404       VALUES (?, ?, ?, ?, ?)},
405     {},
406     '*', '*', 'ONLY1', 1, 99
407 );
408 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
409     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
410
411 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
412
413 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
414     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
415
416 subtest 'Test max_holds per library/patron category' => sub {
417     plan tests => 6;
418
419     $dbh->do('DELETE FROM reserves');
420     $dbh->do('DELETE FROM issuingrules');
421     $dbh->do('DELETE FROM circulation_rules');
422
423     $biblio = $builder->build_sample_biblio({ itemtype => 'TEST' });
424     ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
425       AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
426         $biblio->biblionumber );
427     $dbh->do(
428         q{
429             INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
430             VALUES (?, ?, ?, ?, ?)
431         },
432         {},
433         '*', '*', 'TEST', 99, 99
434     );
435     AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
436     AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
437     AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
438
439     my $count =
440       Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
441     is( $count, 3, 'Patron now has 3 holds' );
442
443     my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
444     is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
445
446     my $rule_all = Koha::CirculationRules->set_rule(
447         {
448             categorycode => $category->{categorycode},
449             branchcode   => undef,
450             itemtype     => undef,
451             rule_name    => 'max_holds',
452             rule_value   => 3,
453         }
454     );
455
456     my $rule_branch = Koha::CirculationRules->set_rule(
457         {
458             branchcode   => $branch_1,
459             categorycode => $category->{categorycode},
460             itemtype     => undef,
461             rule_name    => 'max_holds',
462             rule_value   => 5,
463         }
464     );
465
466     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
467     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
468
469     $rule_branch->delete();
470
471     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
472     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
473
474     $rule_all->delete();
475     $rule_branch->rule_value(3);
476     $rule_branch->store();
477
478     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
479     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
480
481     $rule_branch->rule_value(5);
482     $rule_branch->update();
483     $rule_branch->rule_value(5);
484     $rule_branch->store();
485
486     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
487     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
488 };
489
490 subtest 'Pickup location availability tests' => sub {
491     plan tests => 4;
492
493     $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
494     my ( $item_bibnum, $item_bibitemnum, $itemnumber )
495     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
496     #Add a default rule to allow some holds
497     $dbh->do(
498         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
499           VALUES (?, ?, ?, ?, ?)},
500         {},
501         '*', '*', '*', 25, 99
502     );
503     my $item = Koha::Items->find($itemnumber);
504     my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
505     my $library = Koha::Libraries->find($branch_to);
506     $library->pickup_location('1')->store;
507     my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
508
509     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
510     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
511
512     $library->pickup_location('1')->store;
513     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
514        'OK', 'Library is a pickup location');
515
516     my $limit = Koha::Item::Transfer::Limit->new({
517         fromBranch => $item->holdingbranch,
518         toBranch => $branch_to,
519         itemtype => $item->effective_itemtype,
520     })->store;
521     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to),
522        'cannotBeTransferred', 'Item cannot be transferred');
523     $limit->delete;
524
525     $library->pickup_location('0')->store;
526     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
527        'libraryNotPickupLocation', 'Library is not a pickup location');
528     is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
529        'libraryNotFound', 'Cannot set unknown library as pickup location');
530 };
531
532 $schema->storage->txn_rollback;
533
534 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
535
536     plan tests => 9;
537
538     $schema->storage->txn_begin;
539
540     Koha::Holds->search->delete;
541     $dbh->do('DELETE FROM issues');
542     Koha::Items->search->delete;
543     Koha::Biblios->search->delete;
544
545     my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
546     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
547     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
548
549     # Create 3 biblios with items
550     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
551     my ( undef, undef, $itemnumber_1 ) = AddItem(
552         {   homebranch    => $library->branchcode,
553             holdingbranch => $library->branchcode
554         },
555         $biblio_1->biblionumber
556     );
557     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
558     my ( undef, undef, $itemnumber_2 ) = AddItem(
559         {   homebranch    => $library->branchcode,
560             holdingbranch => $library->branchcode
561         },
562         $biblio_2->biblionumber
563     );
564     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
565     my ( undef, undef, $itemnumber_3 ) = AddItem(
566         {   homebranch    => $library->branchcode,
567             holdingbranch => $library->branchcode
568         },
569         $biblio_3->biblionumber
570     );
571
572     Koha::IssuingRules->search->delete;
573     my $issuingrule = Koha::IssuingRule->new(
574         {   categorycode     => '*',
575             branchcode       => '*',
576             itemtype         => $itemtype->itemtype,
577             reservesallowed  => 1,
578             holds_per_record => 99,
579             holds_per_day    => 2
580         }
581     )->store;
582
583     is_deeply(
584         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
585         { status => 'OK' },
586         'Patron can reserve item with hold limit of 1, no holds placed'
587     );
588
589     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
590
591     is_deeply(
592         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
593         { status => 'tooManyReserves', limit => 1 },
594         'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
595     );
596
597     # Raise reservesallowed to avoid tooManyReserves from it
598     $issuingrule->set( { reservesallowed => 3 } )->store;
599
600     is_deeply(
601         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
602         { status => 'OK' },
603         'Patron can reserve item with 2 reserves daily cap'
604     );
605
606     # Add a second reserve
607     my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
608     is_deeply(
609         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
610         { status => 'tooManyReservesToday', limit => 2 },
611         'Patron cannot a third item with 2 reserves daily cap'
612     );
613
614     # Update last hold so reservedate is in the past, so 2 holds, but different day
615     $hold = Koha::Holds->find($res_id);
616     my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
617     $hold->reservedate($yesterday)->store;
618
619     is_deeply(
620         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
621         { status => 'OK' },
622         'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
623     );
624
625     # Set holds_per_day to 0
626     $issuingrule->set( { holds_per_day => 0 } )->store;
627
628     # Delete existing holds
629     Koha::Holds->search->delete;
630     is_deeply(
631         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
632         { status => 'tooManyReservesToday', limit => 0 },
633         'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
634     );
635
636     $issuingrule->set( { holds_per_day => undef } )->store;
637     Koha::Holds->search->delete;
638     is_deeply(
639         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
640         { status => 'OK' },
641         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
642     );
643     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
644     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
645     is_deeply(
646         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
647         { status => 'OK' },
648         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
649     );
650     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
651     is_deeply(
652         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
653         { status => 'tooManyReserves', limit => 3 },
654         'Unlimited daily holds, but reached reservesallowed'
655     );
656
657     $schema->storage->txn_rollback;
658 };