Bug 24657: Fix t/db_dependent/Koha/Item.t and t/db_dependent/Holds.t
[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 => 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         {
80             branchcode     => $branch_1,
81             borrowernumber => $borrowernumber,
82             biblionumber   => $biblio->biblionumber,
83             priority       => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
84             itemnumber     => $itemnumber,
85         }
86     );
87 }
88
89 my $holds = $biblio->holds;
90 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
91 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
92 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
93 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
94 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
95 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
96
97 my $item = Koha::Items->find( $itemnumber );
98 $holds = $item->current_holds;
99 my $first_hold = $holds->next;
100 my $reservedate = $first_hold->reservedate;
101 my $borrowernumber = $first_hold->borrowernumber;
102 my $branch_1code = $first_hold->branchcode;
103 my $reserve_id = $first_hold->reserve_id;
104 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
105 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
106 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
107 ok($reserve_id, "Test holds_placed_today()");
108
109 my $hold = Koha::Holds->find( $reserve_id );
110 ok( $hold, "Koha::Holds found the hold" );
111 my $hold_biblio = $hold->biblio();
112 ok( $hold_biblio, "Got biblio using biblio() method" );
113 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
114 my $hold_item = $hold->item();
115 ok( $hold_item, "Got item using item() method" );
116 ok( $hold_item == $hold->item(), "item method returns stashed item" );
117 my $hold_branch = $hold->branch();
118 ok( $hold_branch, "Got branch using branch() method" );
119 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
120 my $hold_found = $hold->found();
121 $hold->set({ found => 'W'})->store();
122 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
123 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
124
125 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
126 $holds = $patron->holds;
127 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
128
129
130 $holds = $item->current_holds;
131 $first_hold = $holds->next;
132 $borrowernumber = $first_hold->borrowernumber;
133 $branch_1code = $first_hold->branchcode;
134 $reserve_id = $first_hold->reserve_id;
135
136 ModReserve({
137     reserve_id    => $reserve_id,
138     rank          => '4',
139     branchcode    => $branch_1,
140     itemnumber    => $itemnumber,
141     suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
142 });
143
144 $hold = Koha::Holds->find( $reserve_id );
145 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
146 ok( $hold->suspend, "Test ModReserve, suspend hold" );
147 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
148
149 ModReserve({ # call without reserve_id
150     rank          => '3',
151     biblionumber  => $item_bibnum,
152     itemnumber    => $itemnumber,
153     borrowernumber => $borrowernumber,
154 });
155 $hold = Koha::Holds->find( $reserve_id );
156 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
157
158 ToggleSuspend( $reserve_id );
159 $hold = Koha::Holds->find( $reserve_id );
160 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
161
162 ToggleSuspend( $reserve_id, '2012-01-01' );
163 $hold = Koha::Holds->find( $reserve_id );
164 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
165
166 AutoUnsuspendReserves();
167 $hold = Koha::Holds->find( $reserve_id );
168 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
169
170 SuspendAll(
171     borrowernumber => $borrowernumber,
172     biblionumber   => $biblio->biblionumber,
173     suspend => 1,
174     suspend_until => '2012-01-01',
175 );
176 $hold = Koha::Holds->find( $reserve_id );
177 is( $hold->suspend, 1, "Test SuspendAll()" );
178 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
179
180 SuspendAll(
181     borrowernumber => $borrowernumber,
182     biblionumber   => $biblio->biblionumber,
183     suspend => 0,
184 );
185 $hold = Koha::Holds->find( $reserve_id );
186 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
187 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
188
189 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
190     AddReserve(
191         {
192             branchcode     => $branch_1,
193             borrowernumber => $borrowernumbers[0],
194             biblionumber   => $biblio->biblionumber,
195         }
196     );
197
198 $patron = Koha::Patrons->find( $borrowernumber );
199 $holds = $patron->holds;
200 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
201 ModReserveMinusPriority( $itemnumber, $reserveid );
202 $holds = $patron->holds;
203 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
204
205 $holds = $biblio->holds;
206 $hold = $holds->next;
207 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
208 $hold = Koha::Holds->find( $reserveid );
209 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
210
211 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
212 $hold = Koha::Holds->find( $reserveid );
213 is( $hold->priority, '2', "Test AlterPriority(), move down" );
214
215 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
216 $hold = Koha::Holds->find( $reserveid );
217 is( $hold->priority, '1', "Test AlterPriority(), move up" );
218
219 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
222
223 # Regression test for bug 2394
224 #
225 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
226 # a patron is not permittedo to request an item whose homebranch (i.e.,
227 # owner of the item) is different from the patron's own library.
228 # However, if canreservefromotherbranches is turned ON, the patron can
229 # create such hold requests.
230 #
231 # Note that canreservefromotherbranches has no effect if
232 # IndependentBranches is OFF.
233
234 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
235 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
236   = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber);
237 Koha::CirculationRules->set_rules(
238     {
239         categorycode => undef,
240         branchcode   => undef,
241         itemtype     => undef,
242         rules        => {
243             reservesallowed  => 25,
244             holds_per_record => 99,
245         }
246     }
247 );
248 Koha::CirculationRules->set_rules(
249     {
250         categorycode => undef,
251         branchcode   => undef,
252         itemtype     => 'CANNOT',
253         rules        => {
254             reservesallowed  => 0,
255             holds_per_record => 99,
256         }
257     }
258 );
259
260 # make sure some basic sysprefs are set
261 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
262 t::lib::Mocks::mock_preference('item-level_itypes', 1);
263
264 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
265 t::lib::Mocks::mock_preference('IndependentBranches', 0);
266
267 is(
268     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status}, 'OK',
269     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
270 );
271
272 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
273 t::lib::Mocks::mock_preference('IndependentBranches', 1);
274 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
275 ok(
276     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
277     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
278 );
279
280 # ... unless canreservefromotherbranches is ON
281 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
282 ok(
283     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
284     '... unless canreservefromotherbranches is ON (bug 2394)'
285 );
286
287 {
288     # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
289     $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
290     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
291     my $reserveid1 = AddReserve(
292         {
293             branchcode     => $branch_1,
294             borrowernumber => $borrowernumbers[0],
295             biblionumber   => $biblio->biblionumber,
296             priority       => 1
297         }
298     );
299
300     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
301     my $reserveid2 = AddReserve(
302         {
303             branchcode     => $branch_1,
304             borrowernumber => $borrowernumbers[1],
305             biblionumber   => $biblio->biblionumber,
306             priority       => 2
307         }
308     );
309
310     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber);
311     my $reserveid3 = AddReserve(
312         {
313             branchcode     => $branch_1,
314             borrowernumber => $borrowernumbers[2],
315             biblionumber   => $biblio->biblionumber,
316             priority       => 3
317         }
318     );
319
320     my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
321     my $hold3 = Koha::Holds->find( $reserveid3 );
322     is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
323     ModReserve({ reserve_id => $reserveid1, rank => 'del' });
324     ModReserve({ reserve_id => $reserveid2, rank => 'del' });
325     is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
326 }
327
328 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
329 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
330 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
331 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
332
333 $hold = Koha::Hold->new(
334     {
335         borrowernumber => $borrowernumbers[0],
336         itemnumber     => $itemnumber,
337         biblionumber   => $item_bibnum,
338     }
339 )->store();
340 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
341     'itemAlreadyOnHold',
342     "Patron cannot place a second item level hold for a given item" );
343 $hold->delete();
344
345 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
346 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
347 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
348
349 # Regression test for bug 9532
350 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
351 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
352 AddReserve(
353     {
354         branchcode     => $branch_1,
355         borrowernumber => $borrowernumbers[0],
356         biblionumber   => $biblio->biblionumber,
357         priority       => 1,
358     }
359 );
360 is(
361     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
362     "cannot request item if policy that matches on item-level item type forbids it"
363 );
364 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
365 ok(
366     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
367     "can request item if policy that matches on item type allows it"
368 );
369
370 t::lib::Mocks::mock_preference('item-level_itypes', 0);
371 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
372 ok(
373     CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
374     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
375 );
376
377
378 # Test branch item rules
379
380 $dbh->do('DELETE FROM circulation_rules');
381 Koha::CirculationRules->set_rules(
382     {
383         categorycode => undef,
384         branchcode   => undef,
385         itemtype     => undef,
386         rules        => {
387             reservesallowed  => 25,
388             holds_per_record => 99,
389         }
390     }
391 );
392 Koha::CirculationRules->set_rules(
393     {
394         branchcode => $branch_1,
395         itemtype   => 'CANNOT',
396         rules => {
397             holdallowed => 0,
398             returnbranch => 'homebranch',
399         }
400     }
401 );
402 Koha::CirculationRules->set_rules(
403     {
404         branchcode => $branch_1,
405         itemtype   => 'CAN',
406         rules => {
407             holdallowed => 1,
408             returnbranch => 'homebranch',
409         }
410     }
411 );
412 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
413 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
414     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber);
415 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
416     "CanItemBeReserved should return 'notReservable'");
417
418 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
419 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
420     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
421 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
422     'cannotReserveFromOtherBranches',
423     "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
424 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
425 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
426     'OK',
427     "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
428
429 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
430     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber);
431 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
432     "CanItemBeReserved should return 'OK'");
433
434 # Bug 12632
435 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
436 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
437
438 $dbh->do('DELETE FROM reserves');
439 $dbh->do('DELETE FROM issues');
440 $dbh->do('DELETE FROM items');
441 $dbh->do('DELETE FROM biblio');
442
443 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
444 ( $item_bibnum, $item_bibitemnum, $itemnumber )
445     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
446
447 Koha::CirculationRules->set_rules(
448     {
449         categorycode => undef,
450         branchcode   => undef,
451         itemtype     => 'ONLY1',
452         rules        => {
453             reservesallowed  => 1,
454             holds_per_record => 99,
455         }
456     }
457 );
458 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
459     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
460
461 my $res_id = AddReserve(
462     {
463         branchcode     => $branch_1,
464         borrowernumber => $borrowernumbers[0],
465         biblionumber   => $biblio->biblionumber,
466         priority       => 1,
467     }
468 );
469
470 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
471     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
472
473     #results should be the same for both ReservesControlBranch settings
474 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
475 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
476     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
477 #reset for further tests
478 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
479
480 subtest 'Test max_holds per library/patron category' => sub {
481     plan tests => 6;
482
483     $dbh->do('DELETE FROM reserves');
484
485     $biblio = $builder->build_sample_biblio;
486     ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
487       AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
488         $biblio->biblionumber );
489     Koha::CirculationRules->set_rules(
490         {
491             categorycode => undef,
492             branchcode   => undef,
493             itemtype     => $biblio->itemtype,
494             rules        => {
495                 reservesallowed  => 99,
496                 holds_per_record => 99,
497             }
498         }
499     );
500
501     for ( 1 .. 3 ) {
502         AddReserve(
503             {
504                 branchcode     => $branch_1,
505                 borrowernumber => $borrowernumbers[0],
506                 biblionumber   => $biblio->biblionumber,
507                 priority       => 1,
508             }
509         );
510     }
511
512     my $count =
513       Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
514     is( $count, 3, 'Patron now has 3 holds' );
515
516     my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
517     is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
518
519     my $rule_all = Koha::CirculationRules->set_rule(
520         {
521             categorycode => $category->{categorycode},
522             branchcode   => undef,
523             rule_name    => 'max_holds',
524             rule_value   => 3,
525         }
526     );
527
528     my $rule_branch = Koha::CirculationRules->set_rule(
529         {
530             branchcode   => $branch_1,
531             categorycode => $category->{categorycode},
532             rule_name    => 'max_holds',
533             rule_value   => 5,
534         }
535     );
536
537     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
538     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
539
540     $rule_branch->delete();
541
542     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
543     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
544
545     $rule_all->delete();
546     $rule_branch->rule_value(3);
547     $rule_branch->store();
548
549     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
550     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
551
552     $rule_branch->rule_value(5);
553     $rule_branch->update();
554     $rule_branch->rule_value(5);
555     $rule_branch->store();
556
557     $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
558     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
559 };
560
561 subtest 'Pickup location availability tests' => sub {
562     plan tests => 4;
563
564     $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
565     my ( $item_bibnum, $item_bibitemnum, $itemnumber )
566     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber );
567     #Add a default rule to allow some holds
568
569     Koha::CirculationRules->set_rules(
570         {
571             branchcode   => undef,
572             categorycode => undef,
573             itemtype     => undef,
574             rules        => {
575                 reservesallowed  => 25,
576                 holds_per_record => 99,
577             }
578         }
579     );
580     my $item = Koha::Items->find($itemnumber);
581     my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
582     my $library = Koha::Libraries->find($branch_to);
583     $library->pickup_location('1')->store;
584     my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
585
586     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
587     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
588
589     $library->pickup_location('1')->store;
590     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
591        'OK', 'Library is a pickup location');
592
593     my $limit = Koha::Item::Transfer::Limit->new({
594         fromBranch => $item->holdingbranch,
595         toBranch => $branch_to,
596         itemtype => $item->effective_itemtype,
597     })->store;
598     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
599        'cannotBeTransferred', 'Item cannot be transferred');
600     $limit->delete;
601
602     $library->pickup_location('0')->store;
603     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
604        'libraryNotPickupLocation', 'Library is not a pickup location');
605     is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
606        'libraryNotFound', 'Cannot set unknown library as pickup location');
607 };
608
609 $schema->storage->txn_rollback;
610
611 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
612
613     plan tests => 10;
614
615     $schema->storage->txn_begin;
616
617     Koha::Holds->search->delete;
618     $dbh->do('DELETE FROM issues');
619     Koha::Items->search->delete;
620     Koha::Biblios->search->delete;
621
622     my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
623     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
624     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
625
626     # Create 3 biblios with items
627     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
628     my ( undef, undef, $itemnumber_1 ) = AddItem(
629         {   homebranch    => $library->branchcode,
630             holdingbranch => $library->branchcode
631         },
632         $biblio_1->biblionumber
633     );
634     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
635     my ( undef, undef, $itemnumber_2 ) = AddItem(
636         {   homebranch    => $library->branchcode,
637             holdingbranch => $library->branchcode
638         },
639         $biblio_2->biblionumber
640     );
641     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
642     my ( undef, undef, $itemnumber_3 ) = AddItem(
643         {   homebranch    => $library->branchcode,
644             holdingbranch => $library->branchcode
645         },
646         $biblio_3->biblionumber
647     );
648
649     Koha::CirculationRules->search->delete;
650     Koha::CirculationRules->set_rules(
651         {
652             categorycode => '*',
653             branchcode   => '*',
654             itemtype     => $itemtype->itemtype,
655             rules        => {
656                 reservesallowed  => 1,
657                 holds_per_record => 99,
658                 holds_per_day    => 2
659             }
660         }
661     );
662
663     is_deeply(
664         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
665         { status => 'OK' },
666         'Patron can reserve item with hold limit of 1, no holds placed'
667     );
668
669     AddReserve(
670         {
671             branchcode     => $library->branchcode,
672             borrowernumber => $patron->borrowernumber,
673             biblionumber   => $biblio_1->biblionumber,
674             priority       => 1,
675         }
676     );
677
678     is_deeply(
679         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
680         { status => 'tooManyReserves', limit => 1 },
681         'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
682     );
683
684     # Raise reservesallowed to avoid tooManyReserves from it
685     Koha::CirculationRules->set_rule(
686         {
687
688             categorycode => '*',
689             branchcode   => '*',
690             itemtype     => $itemtype->itemtype,
691             rule_name  => 'reservesallowed',
692             rule_value => 3,
693         }
694     );
695
696     is_deeply(
697         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
698         { status => 'OK' },
699         'Patron can reserve item with 2 reserves daily cap'
700     );
701
702     # Add a second reserve
703     my $res_id = AddReserve(
704         {
705             branchcode     => $library->branchcode,
706             borrowernumber => $patron->borrowernumber,
707             biblionumber   => $biblio_2->biblionumber,
708             priority       => 1,
709         }
710     );
711     is_deeply(
712         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
713         { status => 'tooManyReservesToday', limit => 2 },
714         'Patron cannot a third item with 2 reserves daily cap'
715     );
716
717     # Update last hold so reservedate is in the past, so 2 holds, but different day
718     $hold = Koha::Holds->find($res_id);
719     my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
720     $hold->reservedate($yesterday)->store;
721
722     is_deeply(
723         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
724         { status => 'OK' },
725         'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
726     );
727
728     # Set holds_per_day to 0
729     Koha::CirculationRules->set_rule(
730         {
731
732             categorycode => '*',
733             branchcode   => '*',
734             itemtype     => $itemtype->itemtype,
735             rule_name  => 'holds_per_day',
736             rule_value => 0,
737         }
738     );
739
740
741     # Delete existing holds
742     Koha::Holds->search->delete;
743     is_deeply(
744         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
745         { status => 'tooManyReservesToday', limit => 0 },
746         'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
747     );
748
749     Koha::CirculationRules->set_rule(
750         {
751
752             categorycode => '*',
753             branchcode   => '*',
754             itemtype     => $itemtype->itemtype,
755             rule_name  => 'holds_per_day',
756             rule_value => undef,
757         }
758     );
759
760     Koha::Holds->search->delete;
761     is_deeply(
762         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
763         { status => 'OK' },
764         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
765     );
766     AddReserve(
767         {
768             branchcode     => $library->branchcode,
769             borrowernumber => $patron->borrowernumber,
770             biblionumber   => $biblio_1->biblionumber,
771             priority       => 1,
772         }
773     );
774     AddReserve(
775         {
776             branchcode     => $library->branchcode,
777             borrowernumber => $patron->borrowernumber,
778             biblionumber   => $biblio_2->biblionumber,
779             priority       => 1,
780         }
781     );
782
783     is_deeply(
784         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
785         { status => 'OK' },
786         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
787     );
788     AddReserve(
789         {
790             branchcode     => $library->branchcode,
791             borrowernumber => $patron->borrowernumber,
792             biblionumber   => $biblio_3->biblionumber,
793             priority       => 1,
794         }
795     );
796     is_deeply(
797         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
798         { status => 'tooManyReserves', limit => 3 },
799         'Unlimited daily holds, but reached reservesallowed'
800     );
801     #results should be the same for both ReservesControlBranch settings
802     t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
803     is_deeply(
804         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
805         { status => 'tooManyReserves', limit => 3 },
806         'Unlimited daily holds, but reached reservesallowed'
807     );
808
809     $schema->storage->txn_rollback;
810 };
811
812 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
813     plan tests => 9;
814
815     $schema->storage->txn_begin;
816
817     # Cleanup database
818     Koha::Holds->search->delete;
819     $dbh->do('DELETE FROM issues');
820     Koha::CirculationRules->set_rule(
821         {
822             branchcode   => undef,
823             categorycode => undef,
824             itemtype     => undef,
825             rule_name    => 'reservesallowed',
826             rule_value   => 25,
827         }
828     );
829
830     Koha::Items->search->delete;
831     Koha::Biblios->search->delete;
832
833     # Create item types
834     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
835     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
836
837     # Create libraries
838     my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
839     my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
840     my $library3  = $builder->build_object( { class => 'Koha::Libraries' } );
841
842     # Create library groups hierarchy
843     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
844     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
845     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
846
847     # Create 2 patrons
848     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
849     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
850
851     # Create 3 biblios with items
852     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
853     my ( undef, undef, $itemnumber_1 ) = AddItem(
854         {   homebranch    => $library1->branchcode,
855             holdingbranch => $library1->branchcode
856         },
857         $biblio_1->biblionumber
858     );
859     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
860     my ( undef, undef, $itemnumber_2 ) = AddItem(
861         {   homebranch    => $library2->branchcode,
862             holdingbranch => $library2->branchcode
863         },
864         $biblio_2->biblionumber
865     );
866     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
867     my ( undef, undef, $itemnumber_3 ) = AddItem(
868         {   homebranch    => $library1->branchcode,
869             holdingbranch => $library1->branchcode
870         },
871         $biblio_3->biblionumber
872     );
873
874     # Test 1: Patron 3 can place hold
875     is_deeply(
876         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
877         { status => 'OK' },
878         'Patron can place hold if no circ_rules where defined'
879     );
880
881     # Insert default circ rule of holds allowed only from local hold group for all libraries
882     Koha::CirculationRules->set_rules(
883         {
884             branchcode => undef,
885             itemtype   => undef,
886             rules => {
887                 holdallowed => 3,
888                 hold_fulfillment_policy => 'any',
889                 returnbranch => 'any'
890             }
891         }
892     );
893
894     # Test 2: Patron 1 can place hold
895     is_deeply(
896         CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
897         { status => 'OK' },
898         'Patron can place hold because patron\'s home library is part of hold group'
899     );
900
901     # Test 3: Patron 3 cannot place hold
902     is_deeply(
903         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
904         { status => 'branchNotInHoldGroup' },
905         'Patron cannot place hold because patron\'s home library is not part of hold group'
906     );
907
908     # Insert default circ rule to "any" for library 2
909     Koha::CirculationRules->set_rules(
910         {
911             branchcode => $library2->branchcode,
912             itemtype   => undef,
913             rules => {
914                 holdallowed => 2,
915                 hold_fulfillment_policy => 'any',
916                 returnbranch => 'any'
917             }
918         }
919     );
920
921     # Test 4: Patron 3 can place hold
922     is_deeply(
923         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
924         { status => 'OK' },
925         'Patron can place hold if holdallowed is set to "any" for library 2'
926     );
927
928     # Update default circ rule to "hold group" for library 2
929     Koha::CirculationRules->set_rules(
930         {
931             branchcode => $library2->branchcode,
932             itemtype   => undef,
933             rules => {
934                 holdallowed => 3,
935                 hold_fulfillment_policy => 'any',
936                 returnbranch => 'any'
937             }
938         }
939     );
940
941     # Test 5: Patron 3 cannot place hold
942     is_deeply(
943         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
944         { status => 'branchNotInHoldGroup' },
945         'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
946     );
947
948     # Insert default item rule to "any" for itemtype 2
949     Koha::CirculationRules->set_rules(
950         {
951             branchcode => $library2->branchcode,
952             itemtype   => $itemtype2->itemtype,
953             rules => {
954                 holdallowed => 2,
955                 hold_fulfillment_policy => 'any',
956                 returnbranch => 'any'
957             }
958         }
959     );
960
961     # Test 6: Patron 3 can place hold
962     is_deeply(
963         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
964         { status => 'OK' },
965         'Patron can place hold if holdallowed is set to "any" for itemtype 2'
966     );
967
968     # Update default item rule to "hold group" for itemtype 2
969     Koha::CirculationRules->set_rules(
970         {
971             branchcode => $library2->branchcode,
972             itemtype   => $itemtype2->itemtype,
973             rules => {
974                 holdallowed => 3,
975                 hold_fulfillment_policy => 'any',
976                 returnbranch => 'any'
977             }
978         }
979     );
980
981     # Test 7: Patron 3 cannot place hold
982     is_deeply(
983         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
984         { status => 'branchNotInHoldGroup' },
985         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
986     );
987
988     # Insert branch item rule to "any" for itemtype 2 and library 2
989     Koha::CirculationRules->set_rules(
990         {
991             branchcode => $library2->branchcode,
992             itemtype   => $itemtype2->itemtype,
993             rules => {
994                 holdallowed => 2,
995                 hold_fulfillment_policy => 'any',
996                 returnbranch => 'any'
997             }
998         }
999     );
1000
1001     # Test 8: Patron 3 can place hold
1002     is_deeply(
1003         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1004         { status => 'OK' },
1005         'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1006     );
1007
1008     # Update branch item rule to "hold group" for itemtype 2 and library 2
1009     Koha::CirculationRules->set_rules(
1010         {
1011             branchcode => $library2->branchcode,
1012             itemtype   => $itemtype2->itemtype,
1013             rules => {
1014                 holdallowed => 3,
1015                 hold_fulfillment_policy => 'any',
1016                 returnbranch => 'any'
1017             }
1018         }
1019     );
1020
1021     # Test 9: Patron 3 cannot place hold
1022     is_deeply(
1023         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
1024         { status => 'branchNotInHoldGroup' },
1025         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1026     );
1027
1028     $schema->storage->txn_rollback;
1029
1030 };
1031
1032 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1033     plan tests => 9;
1034
1035     $schema->storage->txn_begin;
1036
1037     # Cleanup database
1038     Koha::Holds->search->delete;
1039     $dbh->do('DELETE FROM issues');
1040     Koha::CirculationRules->set_rule(
1041         {
1042             branchcode   => undef,
1043             categorycode => undef,
1044             itemtype     => undef,
1045             rule_name    => 'reservesallowed',
1046             rule_value   => 25,
1047         }
1048     );
1049
1050     Koha::Items->search->delete;
1051     Koha::Biblios->search->delete;
1052
1053     # Create item types
1054     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1055     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1056
1057     # Create libraries
1058     my $library1  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1059     my $library2  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1060     my $library3  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1061
1062     # Create library groups hierarchy
1063     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1064     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1065     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1066
1067     # Create 2 patrons
1068     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1069     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1070
1071     # Create 3 biblios with items
1072     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1073     my ( undef, undef, $itemnumber_1 ) = AddItem(
1074         {   homebranch    => $library1->branchcode,
1075             holdingbranch => $library1->branchcode
1076         },
1077         $biblio_1->biblionumber
1078     );
1079     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1080     my ( undef, undef, $itemnumber_2 ) = AddItem(
1081         {   homebranch    => $library2->branchcode,
1082             holdingbranch => $library2->branchcode
1083         },
1084         $biblio_2->biblionumber
1085     );
1086     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1087     my ( undef, undef, $itemnumber_3 ) = AddItem(
1088         {   homebranch    => $library1->branchcode,
1089             holdingbranch => $library1->branchcode
1090         },
1091         $biblio_3->biblionumber
1092     );
1093
1094     # Test 1: Patron 3 can place hold
1095     is_deeply(
1096         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1097         { status => 'OK' },
1098         'Patron can place hold if no circ_rules where defined'
1099     );
1100
1101     # Insert default circ rule of holds allowed only from local hold group for all libraries
1102     Koha::CirculationRules->set_rules(
1103         {
1104             branchcode => undef,
1105             itemtype   => undef,
1106             rules => {
1107                 holdallowed => 2,
1108                 hold_fulfillment_policy => 'holdgroup',
1109                 returnbranch => 'any'
1110             }
1111         }
1112     );
1113
1114     # Test 2: Patron 1 can place hold
1115     is_deeply(
1116         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1117         { status => 'OK' },
1118         'Patron can place hold because pickup location is part of hold group'
1119     );
1120
1121     # Test 3: Patron 3 cannot place hold
1122     is_deeply(
1123         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1124         { status => 'pickupNotInHoldGroup' },
1125         'Patron cannot place hold because pickup location is not part of hold group'
1126     );
1127
1128     # Insert default circ rule to "any" for library 2
1129     Koha::CirculationRules->set_rules(
1130         {
1131             branchcode => $library2->branchcode,
1132             itemtype   => undef,
1133             rules => {
1134                 holdallowed => 2,
1135                 hold_fulfillment_policy => 'any',
1136                 returnbranch => 'any'
1137             }
1138         }
1139     );
1140
1141     # Test 4: Patron 3 can place hold
1142     is_deeply(
1143         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1144         { status => 'OK' },
1145         'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1146     );
1147
1148     # Update default circ rule to "hold group" for library 2
1149     Koha::CirculationRules->set_rules(
1150         {
1151             branchcode => $library2->branchcode,
1152             itemtype   => undef,
1153             rules => {
1154                 holdallowed => 2,
1155                 hold_fulfillment_policy => 'holdgroup',
1156                 returnbranch => 'any'
1157             }
1158         }
1159     );
1160
1161     # Test 5: Patron 3 cannot place hold
1162     is_deeply(
1163         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1164         { status => 'pickupNotInHoldGroup' },
1165         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1166     );
1167
1168     # Insert default item rule to "any" for itemtype 2
1169     Koha::CirculationRules->set_rules(
1170         {
1171             branchcode => $library2->branchcode,
1172             itemtype   => $itemtype2->itemtype,
1173             rules => {
1174                 holdallowed => 2,
1175                 hold_fulfillment_policy => 'any',
1176                 returnbranch => 'any'
1177             }
1178         }
1179     );
1180
1181     # Test 6: Patron 3 can place hold
1182     is_deeply(
1183         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1184         { status => 'OK' },
1185         'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1186     );
1187
1188     # Update default item rule to "hold group" for itemtype 2
1189     Koha::CirculationRules->set_rules(
1190         {
1191             branchcode => $library2->branchcode,
1192             itemtype   => $itemtype2->itemtype,
1193             rules => {
1194                 holdallowed => 2,
1195                 hold_fulfillment_policy => 'holdgroup',
1196                 returnbranch => 'any'
1197             }
1198         }
1199     );
1200
1201     # Test 7: Patron 3 cannot place hold
1202     is_deeply(
1203         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1204         { status => 'pickupNotInHoldGroup' },
1205         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1206     );
1207
1208     # Insert branch item rule to "any" for itemtype 2 and library 2
1209     Koha::CirculationRules->set_rules(
1210         {
1211             branchcode => $library2->branchcode,
1212             itemtype   => $itemtype2->itemtype,
1213             rules => {
1214                 holdallowed => 2,
1215                 hold_fulfillment_policy => 'any',
1216                 returnbranch => 'any'
1217             }
1218         }
1219     );
1220
1221     # Test 8: Patron 3 can place hold
1222     is_deeply(
1223         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1224         { status => 'OK' },
1225         'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1226     );
1227
1228     # Update branch item rule to "hold group" for itemtype 2 and library 2
1229     Koha::CirculationRules->set_rules(
1230         {
1231             branchcode => $library2->branchcode,
1232             itemtype   => $itemtype2->itemtype,
1233             rules => {
1234                 holdallowed => 2,
1235                 hold_fulfillment_policy => 'holdgroup',
1236                 returnbranch => 'any'
1237             }
1238         }
1239     );
1240
1241     # Test 9: Patron 3 cannot place hold
1242     is_deeply(
1243         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1244         { status => 'pickupNotInHoldGroup' },
1245         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1246     );
1247
1248     $schema->storage->txn_rollback;
1249 };