Bug 18936: Remove last bit of Koha::IssuingRule
[koha-ffzg.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 => 61;
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::Item::Transfer::Limits;
24 use Koha::Items;
25 use Koha::Libraries;
26 use Koha::Library::Groups;
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 Koha::CirculationRules->set_rules(
247     {
248         categorycode => undef,
249         branchcode   => undef,
250         itemtype     => undef,
251         rules        => {
252             reservesallowed  => 25,
253             holds_per_record => 99,
254         }
255     }
256 );
257 Koha::CirculationRules->set_rules(
258     {
259         categorycode => undef,
260         branchcode   => undef,
261         itemtype     => 'CANNOT',
262         rules        => {
263             reservesallowed  => 0,
264             holds_per_record => 99,
265         }
266     }
267 );
268
269 # make sure some basic sysprefs are set
270 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
271 t::lib::Mocks::mock_preference('item-level_itypes', 1);
272
273 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
274 t::lib::Mocks::mock_preference('IndependentBranches', 0);
275
276 is(
277     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
278     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
279 );
280
281 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
282 t::lib::Mocks::mock_preference('IndependentBranches', 1);
283 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
284 ok(
285     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
286     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
287 );
288
289 # ... unless canreservefromotherbranches is ON
290 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
291 ok(
292     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
293     '... unless canreservefromotherbranches is ON (bug 2394)'
294 );
295
296 {
297     # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
298     $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
299     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
300     my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1);
301     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
302     my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $biblio->biblionumber, '', 2);
303     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
304     my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $biblio->biblionumber, '', 3);
305     my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
306     my $hold3 = Koha::Holds->find( $reserveid3 );
307     is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
308     ModReserve({ reserve_id => $reserveid1, rank => 'del' });
309     ModReserve({ reserve_id => $reserveid2, rank => 'del' });
310     is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
311 }
312
313 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
314 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
315 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
316 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
317
318 $hold = Koha::Hold->new(
319     {
320         borrowernumber => $borrowernumbers[0],
321         itemnumber     => $itemnumber,
322         biblionumber   => $item_bibnum,
323     }
324 )->store();
325 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
326     'itemAlreadyOnHold',
327     "Patron cannot place a second item level hold for a given item" );
328 $hold->delete();
329
330 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
331 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
332 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
333
334 # Regression test for bug 9532
335 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
336 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
337 AddReserve(
338     $branch_1,
339     $borrowernumbers[0],
340     $biblio->biblionumber,
341     '',
342     1,
343 );
344 is(
345     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
346     "cannot request item if policy that matches on item-level item type forbids it"
347 );
348 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
349 ok(
350     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
351     "can request item if policy that matches on item type allows it"
352 );
353
354 t::lib::Mocks::mock_preference('item-level_itypes', 0);
355 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
356 ok(
357     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
358     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
359 );
360
361
362 # Test branch item rules
363
364 $dbh->do('DELETE FROM circulation_rules');
365 Koha::CirculationRules->set_rules(
366     {
367         categorycode => undef,
368         branchcode   => undef,
369         itemtype     => undef,
370         rules        => {
371             reservesallowed  => 25,
372             holds_per_record => 99,
373         }
374     }
375 );
376 Koha::CirculationRules->set_rules(
377     {
378         branchcode => $branch_1,
379         itemtype   => 'CANNOT',
380         rules => {
381             holdallowed => 0,
382             returnbranch => 'homebranch',
383         }
384     }
385 );
386 Koha::CirculationRules->set_rules(
387     {
388         branchcode => $branch_1,
389         itemtype   => 'CAN',
390         rules => {
391             holdallowed => 1,
392             returnbranch => 'homebranch',
393         }
394     }
395 );
396 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
397 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
398     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
399 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
400     "CanItemBeReserved should return 'notReservable'");
401
402 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
403 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
404     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
405 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
406     'cannotReserveFromOtherBranches',
407     "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
408 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
409 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
410     'OK',
411     "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
412
413 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
414     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
415 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
416     "CanItemBeReserved should return 'OK'");
417
418 # Bug 12632
419 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
420 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
421
422 $dbh->do('DELETE FROM reserves');
423 $dbh->do('DELETE FROM issues');
424 $dbh->do('DELETE FROM items');
425 $dbh->do('DELETE FROM biblio');
426
427 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
428 ( $item_bibnum, $item_bibitemnum, $itemnumber )
429     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
430
431 Koha::CirculationRules->set_rules(
432     {
433         categorycode => undef,
434         branchcode   => undef,
435         itemtype     => 'ONLY1',
436         rules        => {
437             reservesallowed  => 1,
438             holds_per_record => 99,
439         }
440     }
441 );
442 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
443     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
444
445 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
446
447 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
448     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
449
450     #results should be the same for both ReservesControlBranch settings
451 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
452 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
453     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
454 #reset for further tests
455 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
456
457 subtest 'Test max_holds per library/patron category' => sub {
458     plan tests => 6;
459
460     $dbh->do('DELETE FROM reserves');
461     $dbh->do('DELETE FROM circulation_rules');
462
463     $biblio = $builder->build_sample_biblio({ itemtype => 'TEST' });
464     ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
465       AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
466         $biblio->biblionumber );
467     Koha::CirculationRules->set_rules(
468         {
469             categorycode => undef,
470             branchcode   => undef,
471             itemtype     => $testitemtype,
472             rules        => {
473                 reservesallowed  => 99,
474                 holds_per_record => 99,
475             }
476         }
477     );
478     AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
479     AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
480     AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, );
481
482     my $count =
483       Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
484     is( $count, 3, 'Patron now has 3 holds' );
485
486     my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
487     is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
488
489     my $rule_all = Koha::CirculationRules->set_rule(
490         {
491             categorycode => $category->{categorycode},
492             branchcode   => undef,
493             rule_name    => 'max_holds',
494             rule_value   => 3,
495         }
496     );
497
498     my $rule_branch = Koha::CirculationRules->set_rule(
499         {
500             branchcode   => $branch_1,
501             categorycode => $category->{categorycode},
502             rule_name    => 'max_holds',
503             rule_value   => 5,
504         }
505     );
506
507     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
508     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
509
510     $rule_branch->delete();
511
512     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
513     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
514
515     $rule_all->delete();
516     $rule_branch->rule_value(3);
517     $rule_branch->store();
518
519     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
520     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
521
522     $rule_branch->rule_value(5);
523     $rule_branch->update();
524     $rule_branch->rule_value(5);
525     $rule_branch->store();
526
527     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
528     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
529 };
530
531 subtest 'Pickup location availability tests' => sub {
532     plan tests => 4;
533
534     $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
535     my ( $item_bibnum, $item_bibitemnum, $itemnumber )
536     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
537     #Add a default rule to allow some holds
538     $dbh->do(
539         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
540           VALUES (?, ?, ?, ?, ?)},
541         {},
542         '*', '*', '*', 25, 99
543     );
544     my $item = Koha::Items->find($itemnumber);
545     my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
546     my $library = Koha::Libraries->find($branch_to);
547     $library->pickup_location('1')->store;
548     my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
549
550     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
551     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
552
553     $library->pickup_location('1')->store;
554     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
555        'OK', 'Library is a pickup location');
556
557     my $limit = Koha::Item::Transfer::Limit->new({
558         fromBranch => $item->holdingbranch,
559         toBranch => $branch_to,
560         itemtype => $item->effective_itemtype,
561     })->store;
562     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
563        'cannotBeTransferred', 'Item cannot be transferred');
564     $limit->delete;
565
566     $library->pickup_location('0')->store;
567     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
568        'libraryNotPickupLocation', 'Library is not a pickup location');
569     is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
570        'libraryNotFound', 'Cannot set unknown library as pickup location');
571 };
572
573 $schema->storage->txn_rollback;
574
575 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
576
577     plan tests => 10;
578
579     $schema->storage->txn_begin;
580
581     Koha::Holds->search->delete;
582     $dbh->do('DELETE FROM issues');
583     $dbh->do('DELETE FROM issuingrules');
584     $dbh->do('DELETE FROM circulation_rules');
585     Koha::Items->search->delete;
586     Koha::Biblios->search->delete;
587
588     my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
589     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
590     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
591
592     # Create 3 biblios with items
593     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
594     my ( undef, undef, $itemnumber_1 ) = AddItem(
595         {   homebranch    => $library->branchcode,
596             holdingbranch => $library->branchcode
597         },
598         $biblio_1->biblionumber
599     );
600     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
601     my ( undef, undef, $itemnumber_2 ) = AddItem(
602         {   homebranch    => $library->branchcode,
603             holdingbranch => $library->branchcode
604         },
605         $biblio_2->biblionumber
606     );
607     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
608     my ( undef, undef, $itemnumber_3 ) = AddItem(
609         {   homebranch    => $library->branchcode,
610             holdingbranch => $library->branchcode
611         },
612         $biblio_3->biblionumber
613     );
614
615     Koha::CirculationRules->search->delete;
616     Koha::CirculationRules->set_rules(
617         {
618             categorycode => '*',
619             branchcode   => '*',
620             itemtype     => $itemtype->itemtype,
621             rules        => {
622                 reservesallowed  => 1,
623                 holds_per_record => 99,
624                 holds_per_day    => 2
625             }
626         }
627     );
628
629     is_deeply(
630         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
631         { status => 'OK' },
632         'Patron can reserve item with hold limit of 1, no holds placed'
633     );
634
635     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
636
637     is_deeply(
638         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
639         { status => 'tooManyReserves', limit => 1 },
640         'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
641     );
642
643     # Raise reservesallowed to avoid tooManyReserves from it
644     $issuingrule->set( { reservesallowed => 3 } )->store;
645
646     is_deeply(
647         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
648         { status => 'OK' },
649         'Patron can reserve item with 2 reserves daily cap'
650     );
651
652     # Add a second reserve
653     my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
654     is_deeply(
655         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
656         { status => 'tooManyReservesToday', limit => 2 },
657         'Patron cannot a third item with 2 reserves daily cap'
658     );
659
660     # Update last hold so reservedate is in the past, so 2 holds, but different day
661     $hold = Koha::Holds->find($res_id);
662     my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
663     $hold->reservedate($yesterday)->store;
664
665     is_deeply(
666         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
667         { status => 'OK' },
668         'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
669     );
670
671     # Set holds_per_day to 0
672     $issuingrule->set( { holds_per_day => 0 } )->store;
673
674     # Delete existing holds
675     Koha::Holds->search->delete;
676     is_deeply(
677         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
678         { status => 'tooManyReservesToday', limit => 0 },
679         'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
680     );
681
682     $issuingrule->set( { holds_per_day => undef } )->store;
683     Koha::Holds->search->delete;
684     is_deeply(
685         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
686         { status => 'OK' },
687         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
688     );
689     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
690     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
691     is_deeply(
692         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
693         { status => 'OK' },
694         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
695     );
696     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
697     is_deeply(
698         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
699         { status => 'tooManyReserves', limit => 3 },
700         'Unlimited daily holds, but reached reservesallowed'
701     );
702     #results should be the same for both ReservesControlBranch settings
703     t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
704     is_deeply(
705         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
706         { status => 'tooManyReserves', limit => 3 },
707         'Unlimited daily holds, but reached reservesallowed'
708     );
709
710     $schema->storage->txn_rollback;
711 };
712
713 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
714     plan tests => 9;
715
716     $schema->storage->txn_begin;
717
718     # Cleanup database
719     Koha::Holds->search->delete;
720     $dbh->do('DELETE FROM issues');
721     $dbh->do('DELETE FROM issuingrules');
722     $dbh->do(
723         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
724         VALUES (?, ?, ?, ?)},
725         {},
726         '*', '*', '*', 25
727     );
728     $dbh->do('DELETE FROM circulation_rules');
729
730     Koha::Items->search->delete;
731     Koha::Biblios->search->delete;
732
733     # Create item types
734     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
735     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
736
737     # Create libraries
738     my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
739     my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
740     my $library3  = $builder->build_object( { class => 'Koha::Libraries' } );
741
742     # Create library groups hierarchy
743     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
744     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
745     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
746
747     # Create 2 patrons
748     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
749     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
750
751     # Create 3 biblios with items
752     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
753     my ( undef, undef, $itemnumber_1 ) = AddItem(
754         {   homebranch    => $library1->branchcode,
755             holdingbranch => $library1->branchcode
756         },
757         $biblio_1->biblionumber
758     );
759     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
760     my ( undef, undef, $itemnumber_2 ) = AddItem(
761         {   homebranch    => $library2->branchcode,
762             holdingbranch => $library2->branchcode
763         },
764         $biblio_2->biblionumber
765     );
766     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
767     my ( undef, undef, $itemnumber_3 ) = AddItem(
768         {   homebranch    => $library1->branchcode,
769             holdingbranch => $library1->branchcode
770         },
771         $biblio_3->biblionumber
772     );
773
774     # Test 1: Patron 3 can place hold
775     is_deeply(
776         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
777         { status => 'OK' },
778         'Patron can place hold if no circ_rules where defined'
779     );
780
781     # Insert default circ rule of holds allowed only from local hold group for all libraries
782     Koha::CirculationRules->set_rules(
783         {
784             branchcode => undef,
785             itemtype   => undef,
786             categorycode => undef,
787             rules => {
788                 holdallowed => 3,
789                 hold_fulfillment_policy => 'any',
790                 returnbranch => 'any'
791             }
792         }
793     );
794
795     # Test 2: Patron 1 can place hold
796     is_deeply(
797         CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
798         { status => 'OK' },
799         'Patron can place hold because patron\'s home library is part of hold group'
800     );
801
802     # Test 3: Patron 3 cannot place hold
803     is_deeply(
804         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
805         { status => 'branchNotInHoldGroup' },
806         'Patron cannot place hold because patron\'s home library is not part of hold group'
807     );
808
809     # Cleanup default_cirt_rules
810     $dbh->do('DELETE FROM circulation_rules');
811
812     # Insert default circ rule to "any" for library 2
813     Koha::CirculationRules->set_rules(
814         {
815             branchcode => $library2->branchcode,
816             itemtype   => undef,
817             categorycode => undef,
818             rules => {
819                 holdallowed => 2,
820                 hold_fulfillment_policy => 'any',
821                 returnbranch => 'any'
822             }
823         }
824     );
825
826     # Test 4: Patron 3 can place hold
827     is_deeply(
828         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
829         { status => 'OK' },
830         'Patron can place hold if holdallowed is set to "any" for library 2'
831     );
832
833     # Update default circ rule to "hold group" for library 2
834     Koha::CirculationRules->set_rules(
835         {
836             branchcode => $library2->branchcode,
837             itemtype   => undef,
838             categorycode => undef,
839             rules => {
840                 holdallowed => 3,
841                 hold_fulfillment_policy => 'any',
842                 returnbranch => 'any'
843             }
844         }
845     );
846
847     # Test 5: Patron 3 cannot place hold
848     is_deeply(
849         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
850         { status => 'branchNotInHoldGroup' },
851         'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
852     );
853
854     # Cleanup default_branch_cirt_rules
855     $dbh->do('DELETE FROM circulation_rules');
856
857     # Insert default item rule to "any" for itemtype 2
858     Koha::CirculationRules->set_rules(
859         {
860             branchcode => undef,
861             itemtype   => $itemtype2->itemtype,
862             categorycode => undef,
863             rules => {
864                 holdallowed => 2,
865                 hold_fulfillment_policy => 'any',
866                 returnbranch => 'any'
867             }
868         }
869     );
870
871     # Test 6: Patron 3 can place hold
872     is_deeply(
873         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
874         { status => 'OK' },
875         'Patron can place hold if holdallowed is set to "any" for itemtype 2'
876     );
877
878     # Update default item rule to "hold group" for itemtype 2
879     Koha::CirculationRules->set_rules(
880         {
881             branchcode => undef,
882             itemtype   => $itemtype2->itemtype,
883             categorycode => undef,
884             rules => {
885                 holdallowed => 3,
886                 hold_fulfillment_policy => 'any',
887                 returnbranch => 'any'
888             }
889         }
890     );
891
892     # Test 7: Patron 3 cannot place hold
893     is_deeply(
894         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
895         { status => 'branchNotInHoldGroup' },
896         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
897     );
898
899     # Cleanup default_branch_item_rules
900     $dbh->do('DELETE FROM circulation_rules');
901
902     # Insert branch item rule to "any" for itemtype 2 and library 2
903     Koha::CirculationRules->set_rules(
904         {
905             branchcode => $library2->branchcode,
906             itemtype   => $itemtype2->itemtype,
907             categorycode => undef,
908             rules => {
909                 holdallowed => 2,
910                 hold_fulfillment_policy => 'any',
911                 returnbranch => 'any'
912             }
913         }
914     );
915
916     # Test 8: Patron 3 can place hold
917     is_deeply(
918         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
919         { status => 'OK' },
920         'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
921     );
922
923     # Update branch item rule to "hold group" for itemtype 2 and library 2
924     Koha::CirculationRules->set_rules(
925         {
926             branchcode => $library2->branchcode,
927             itemtype   => $itemtype2->itemtype,
928             categorycode => undef,
929             rules => {
930                 holdallowed => 3,
931                 hold_fulfillment_policy => 'any',
932                 returnbranch => 'any'
933             }
934         }
935     );
936
937     # Test 9: Patron 3 cannot place hold
938     is_deeply(
939         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
940         { status => 'branchNotInHoldGroup' },
941         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
942     );
943
944     $schema->storage->txn_rollback;
945
946 };
947
948 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
949     plan tests => 9;
950
951     $schema->storage->txn_begin;
952
953     # Cleanup database
954     Koha::Holds->search->delete;
955     $dbh->do('DELETE FROM issues');
956     $dbh->do('DELETE FROM issuingrules');
957     $dbh->do(
958         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
959         VALUES (?, ?, ?, ?)},
960         {},
961         '*', '*', '*', 25
962     );
963     $dbh->do('DELETE FROM circulation_rules');
964
965     Koha::Items->search->delete;
966     Koha::Biblios->search->delete;
967
968     # Create item types
969     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
970     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
971
972     # Create libraries
973     my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
974     my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
975     my $library3  = $builder->build_object( { class => 'Koha::Libraries' } );
976
977     # Create library groups hierarchy
978     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
979     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
980     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
981
982     # Create 2 patrons
983     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
984     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
985
986     # Create 3 biblios with items
987     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
988     my ( undef, undef, $itemnumber_1 ) = AddItem(
989         {   homebranch    => $library1->branchcode,
990             holdingbranch => $library1->branchcode
991         },
992         $biblio_1->biblionumber
993     );
994     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
995     my ( undef, undef, $itemnumber_2 ) = AddItem(
996         {   homebranch    => $library2->branchcode,
997             holdingbranch => $library2->branchcode
998         },
999         $biblio_2->biblionumber
1000     );
1001     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1002     my ( undef, undef, $itemnumber_3 ) = AddItem(
1003         {   homebranch    => $library1->branchcode,
1004             holdingbranch => $library1->branchcode
1005         },
1006         $biblio_3->biblionumber
1007     );
1008
1009     # Test 1: Patron 3 can place hold
1010     is_deeply(
1011         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1012         { status => 'OK' },
1013         'Patron can place hold if no circ_rules where defined'
1014     );
1015
1016     # Insert default circ rule of holds allowed only from local hold group for all libraries
1017     Koha::CirculationRules->set_rules(
1018         {
1019             branchcode => undef,
1020             itemtype   => undef,
1021             categorycode => undef,
1022             rules => {
1023                 holdallowed => 2,
1024                 hold_fulfillment_policy => 'holdgroup',
1025                 returnbranch => 'any'
1026             }
1027         }
1028     );
1029
1030     # Test 2: Patron 1 can place hold
1031     is_deeply(
1032         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1033         { status => 'OK' },
1034         'Patron can place hold because pickup location is part of hold group'
1035     );
1036
1037     # Test 3: Patron 3 cannot place hold
1038     is_deeply(
1039         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1040         { status => 'pickupNotInHoldGroup' },
1041         'Patron cannot place hold because pickup location is not part of hold group'
1042     );
1043
1044     # Cleanup default_cirt_rules
1045     $dbh->do('DELETE FROM circulation_rules');
1046
1047     # Insert default circ rule to "any" for library 2
1048     Koha::CirculationRules->set_rules(
1049         {
1050             branchcode => $library2->branchcode,
1051             itemtype   => undef,
1052             categorycode => undef,
1053             rules => {
1054                 holdallowed => 2,
1055                 hold_fulfillment_policy => 'any',
1056                 returnbranch => 'any'
1057             }
1058         }
1059     );
1060
1061     # Test 4: Patron 3 can place hold
1062     is_deeply(
1063         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1064         { status => 'OK' },
1065         'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1066     );
1067
1068     # Update default circ rule to "hold group" for library 2
1069     Koha::CirculationRules->set_rules(
1070         {
1071             branchcode => $library2->branchcode,
1072             itemtype   => undef,
1073             categorycode => undef,
1074             rules => {
1075                 holdallowed => 2,
1076                 hold_fulfillment_policy => 'holdgroup',
1077                 returnbranch => 'any'
1078             }
1079         }
1080     );
1081
1082     # Test 5: Patron 3 cannot place hold
1083     is_deeply(
1084         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1085         { status => 'pickupNotInHoldGroup' },
1086         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1087     );
1088
1089     # Cleanup default_branch_cirt_rules
1090     $dbh->do('DELETE FROM circulation_rules');
1091
1092     # Insert default item rule to "any" for itemtype 2
1093     Koha::CirculationRules->set_rules(
1094         {
1095             branchcode => undef,
1096             itemtype   => $itemtype2->itemtype,
1097             categorycode => undef,
1098             rules => {
1099                 holdallowed => 2,
1100                 hold_fulfillment_policy => 'any',
1101                 returnbranch => 'any'
1102             }
1103         }
1104     );
1105
1106     # Test 6: Patron 3 can place hold
1107     is_deeply(
1108         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1109         { status => 'OK' },
1110         'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1111     );
1112
1113     # Update default item rule to "hold group" for itemtype 2
1114     Koha::CirculationRules->set_rules(
1115         {
1116             branchcode => undef,
1117             itemtype   => $itemtype2->itemtype,
1118             categorycode => undef,
1119             rules => {
1120                 holdallowed => 2,
1121                 hold_fulfillment_policy => 'holdgroup',
1122                 returnbranch => 'any'
1123             }
1124         }
1125     );
1126
1127     # Test 7: Patron 3 cannot place hold
1128     is_deeply(
1129         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1130         { status => 'pickupNotInHoldGroup' },
1131         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1132     );
1133
1134     # Cleanup default_branch_item_rules
1135     $dbh->do('DELETE FROM circulation_rules');
1136
1137     # Insert branch item rule to "any" for itemtype 2 and library 2
1138     Koha::CirculationRules->set_rules(
1139         {
1140             branchcode => $library2->branchcode,
1141             itemtype   => $itemtype2->itemtype,
1142             categorycode => undef,
1143             rules => {
1144                 holdallowed => 2,
1145                 hold_fulfillment_policy => 'any',
1146                 returnbranch => 'any'
1147             }
1148         }
1149     );
1150
1151     # Test 8: Patron 3 can place hold
1152     is_deeply(
1153         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1154         { status => 'OK' },
1155         'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1156     );
1157
1158     # Update branch item rule to "hold group" for itemtype 2 and library 2
1159     Koha::CirculationRules->set_rules(
1160         {
1161             branchcode => $library2->branchcode,
1162             itemtype   => $itemtype2->itemtype,
1163             categorycode => undef,
1164             rules => {
1165                 holdallowed => 2,
1166                 hold_fulfillment_policy => 'holdgroup',
1167                 returnbranch => 'any'
1168             }
1169         }
1170     );
1171
1172     # Test 9: Patron 3 cannot place hold
1173     is_deeply(
1174         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1175         { status => 'pickupNotInHoldGroup' },
1176         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1177     );
1178
1179     $schema->storage->txn_rollback;
1180 };