Bug 18936: More fixes
[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;
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     => $biblio->itemtype,
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
539     Koha::CirculationRules->set_rules(
540         {
541             branchcode   => undef,
542             categorycode => undef,
543             itemtype     => undef,
544             rules        => {
545                 reservesallowed  => 25,
546                 holds_per_record => 99,
547             }
548         }
549     );
550     my $item = Koha::Items->find($itemnumber);
551     my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
552     my $library = Koha::Libraries->find($branch_to);
553     $library->pickup_location('1')->store;
554     my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
555
556     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
557     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
558
559     $library->pickup_location('1')->store;
560     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
561        'OK', 'Library is a pickup location');
562
563     my $limit = Koha::Item::Transfer::Limit->new({
564         fromBranch => $item->holdingbranch,
565         toBranch => $branch_to,
566         itemtype => $item->effective_itemtype,
567     })->store;
568     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
569        'cannotBeTransferred', 'Item cannot be transferred');
570     $limit->delete;
571
572     $library->pickup_location('0')->store;
573     is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
574        'libraryNotPickupLocation', 'Library is not a pickup location');
575     is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
576        'libraryNotFound', 'Cannot set unknown library as pickup location');
577 };
578
579 $schema->storage->txn_rollback;
580
581 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
582
583     plan tests => 10;
584
585     $schema->storage->txn_begin;
586
587     Koha::Holds->search->delete;
588     $dbh->do('DELETE FROM issues');
589     $dbh->do('DELETE FROM issuingrules');
590     $dbh->do('DELETE FROM circulation_rules');
591     Koha::Items->search->delete;
592     Koha::Biblios->search->delete;
593
594     my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
595     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
596     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
597
598     # Create 3 biblios with items
599     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
600     my ( undef, undef, $itemnumber_1 ) = AddItem(
601         {   homebranch    => $library->branchcode,
602             holdingbranch => $library->branchcode
603         },
604         $biblio_1->biblionumber
605     );
606     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
607     my ( undef, undef, $itemnumber_2 ) = AddItem(
608         {   homebranch    => $library->branchcode,
609             holdingbranch => $library->branchcode
610         },
611         $biblio_2->biblionumber
612     );
613     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
614     my ( undef, undef, $itemnumber_3 ) = AddItem(
615         {   homebranch    => $library->branchcode,
616             holdingbranch => $library->branchcode
617         },
618         $biblio_3->biblionumber
619     );
620
621     Koha::CirculationRules->search->delete;
622     Koha::CirculationRules->set_rules(
623         {
624             categorycode => '*',
625             branchcode   => '*',
626             itemtype     => $itemtype->itemtype,
627             rules        => {
628                 reservesallowed  => 1,
629                 holds_per_record => 99,
630                 holds_per_day    => 2
631             }
632         }
633     );
634
635     is_deeply(
636         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
637         { status => 'OK' },
638         'Patron can reserve item with hold limit of 1, no holds placed'
639     );
640
641     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
642
643     is_deeply(
644         CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
645         { status => 'tooManyReserves', limit => 1 },
646         'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
647     );
648
649     # Raise reservesallowed to avoid tooManyReserves from it
650     Koha::CirculationRules->set_rule(
651         {
652
653             categorycode => '*',
654             branchcode   => '*',
655             itemtype     => $itemtype->itemtype,
656             rule_name  => 'reservesallowed',
657             rule_value => 3,
658         }
659     );
660
661     is_deeply(
662         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
663         { status => 'OK' },
664         'Patron can reserve item with 2 reserves daily cap'
665     );
666
667     # Add a second reserve
668     my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
669     is_deeply(
670         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
671         { status => 'tooManyReservesToday', limit => 2 },
672         'Patron cannot a third item with 2 reserves daily cap'
673     );
674
675     # Update last hold so reservedate is in the past, so 2 holds, but different day
676     $hold = Koha::Holds->find($res_id);
677     my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
678     $hold->reservedate($yesterday)->store;
679
680     is_deeply(
681         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
682         { status => 'OK' },
683         'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
684     );
685
686     # Set holds_per_day to 0
687     Koha::CirculationRules->set_rule(
688         {
689
690             categorycode => '*',
691             branchcode   => '*',
692             itemtype     => $itemtype->itemtype,
693             rule_name  => 'holds_per_day',
694             rule_value => 0,
695         }
696     );
697
698
699     # Delete existing holds
700     Koha::Holds->search->delete;
701     is_deeply(
702         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
703         { status => 'tooManyReservesToday', limit => 0 },
704         'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
705     );
706
707     Koha::CirculationRules->set_rule(
708         {
709
710             categorycode => '*',
711             branchcode   => '*',
712             itemtype     => $itemtype->itemtype,
713             rule_name  => 'holds_per_day',
714             rule_value => undef,
715         }
716     );
717
718     Koha::Holds->search->delete;
719     is_deeply(
720         CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
721         { status => 'OK' },
722         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
723     );
724     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, );
725     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, );
726     is_deeply(
727         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
728         { status => 'OK' },
729         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
730     );
731     AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, );
732     is_deeply(
733         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
734         { status => 'tooManyReserves', limit => 3 },
735         'Unlimited daily holds, but reached reservesallowed'
736     );
737     #results should be the same for both ReservesControlBranch settings
738     t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
739     is_deeply(
740         CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
741         { status => 'tooManyReserves', limit => 3 },
742         'Unlimited daily holds, but reached reservesallowed'
743     );
744
745     $schema->storage->txn_rollback;
746 };
747
748 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
749     plan tests => 9;
750
751     $schema->storage->txn_begin;
752
753     # Cleanup database
754     Koha::Holds->search->delete;
755     $dbh->do('DELETE FROM issues');
756     $dbh->do('DELETE FROM issuingrules');
757     $dbh->do(
758         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
759         VALUES (?, ?, ?, ?)},
760         {},
761         '*', '*', '*', 25
762     );
763     $dbh->do('DELETE FROM circulation_rules');
764
765     Koha::Items->search->delete;
766     Koha::Biblios->search->delete;
767
768     # Create item types
769     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
770     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
771
772     # Create libraries
773     my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
774     my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
775     my $library3  = $builder->build_object( { class => 'Koha::Libraries' } );
776
777     # Create library groups hierarchy
778     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
779     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
780     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
781
782     # Create 2 patrons
783     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
784     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
785
786     # Create 3 biblios with items
787     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
788     my ( undef, undef, $itemnumber_1 ) = AddItem(
789         {   homebranch    => $library1->branchcode,
790             holdingbranch => $library1->branchcode
791         },
792         $biblio_1->biblionumber
793     );
794     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
795     my ( undef, undef, $itemnumber_2 ) = AddItem(
796         {   homebranch    => $library2->branchcode,
797             holdingbranch => $library2->branchcode
798         },
799         $biblio_2->biblionumber
800     );
801     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
802     my ( undef, undef, $itemnumber_3 ) = AddItem(
803         {   homebranch    => $library1->branchcode,
804             holdingbranch => $library1->branchcode
805         },
806         $biblio_3->biblionumber
807     );
808
809     # Test 1: Patron 3 can place hold
810     is_deeply(
811         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
812         { status => 'OK' },
813         'Patron can place hold if no circ_rules where defined'
814     );
815
816     # Insert default circ rule of holds allowed only from local hold group for all libraries
817     Koha::CirculationRules->set_rules(
818         {
819             branchcode => undef,
820             itemtype   => undef,
821             categorycode => undef,
822             rules => {
823                 holdallowed => 3,
824                 hold_fulfillment_policy => 'any',
825                 returnbranch => 'any'
826             }
827         }
828     );
829
830     # Test 2: Patron 1 can place hold
831     is_deeply(
832         CanItemBeReserved( $patron1->borrowernumber, $itemnumber_2 ),
833         { status => 'OK' },
834         'Patron can place hold because patron\'s home library is part of hold group'
835     );
836
837     # Test 3: Patron 3 cannot place hold
838     is_deeply(
839         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
840         { status => 'branchNotInHoldGroup' },
841         'Patron cannot place hold because patron\'s home library is not part of hold group'
842     );
843
844     # Cleanup default_cirt_rules
845     $dbh->do('DELETE FROM circulation_rules');
846
847     # Insert default circ rule to "any" for library 2
848     Koha::CirculationRules->set_rules(
849         {
850             branchcode => $library2->branchcode,
851             itemtype   => undef,
852             categorycode => undef,
853             rules => {
854                 holdallowed => 2,
855                 hold_fulfillment_policy => 'any',
856                 returnbranch => 'any'
857             }
858         }
859     );
860
861     # Test 4: Patron 3 can place hold
862     is_deeply(
863         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
864         { status => 'OK' },
865         'Patron can place hold if holdallowed is set to "any" for library 2'
866     );
867
868     # Update default circ rule to "hold group" for library 2
869     Koha::CirculationRules->set_rules(
870         {
871             branchcode => $library2->branchcode,
872             itemtype   => undef,
873             categorycode => undef,
874             rules => {
875                 holdallowed => 3,
876                 hold_fulfillment_policy => 'any',
877                 returnbranch => 'any'
878             }
879         }
880     );
881
882     # Test 5: Patron 3 cannot place hold
883     is_deeply(
884         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
885         { status => 'branchNotInHoldGroup' },
886         'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
887     );
888
889     # Cleanup default_branch_cirt_rules
890     $dbh->do('DELETE FROM circulation_rules');
891
892     # Insert default item rule to "any" for itemtype 2
893     Koha::CirculationRules->set_rules(
894         {
895             branchcode => undef,
896             itemtype   => $itemtype2->itemtype,
897             categorycode => undef,
898             rules => {
899                 holdallowed => 2,
900                 hold_fulfillment_policy => 'any',
901                 returnbranch => 'any'
902             }
903         }
904     );
905
906     # Test 6: Patron 3 can place hold
907     is_deeply(
908         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
909         { status => 'OK' },
910         'Patron can place hold if holdallowed is set to "any" for itemtype 2'
911     );
912
913     # Update default item rule to "hold group" for itemtype 2
914     Koha::CirculationRules->set_rules(
915         {
916             branchcode => undef,
917             itemtype   => $itemtype2->itemtype,
918             categorycode => undef,
919             rules => {
920                 holdallowed => 3,
921                 hold_fulfillment_policy => 'any',
922                 returnbranch => 'any'
923             }
924         }
925     );
926
927     # Test 7: Patron 3 cannot place hold
928     is_deeply(
929         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
930         { status => 'branchNotInHoldGroup' },
931         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
932     );
933
934     # Cleanup default_branch_item_rules
935     $dbh->do('DELETE FROM circulation_rules');
936
937     # Insert branch item rule to "any" for itemtype 2 and library 2
938     Koha::CirculationRules->set_rules(
939         {
940             branchcode => $library2->branchcode,
941             itemtype   => $itemtype2->itemtype,
942             categorycode => undef,
943             rules => {
944                 holdallowed => 2,
945                 hold_fulfillment_policy => 'any',
946                 returnbranch => 'any'
947             }
948         }
949     );
950
951     # Test 8: Patron 3 can place hold
952     is_deeply(
953         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
954         { status => 'OK' },
955         'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
956     );
957
958     # Update branch item rule to "hold group" for itemtype 2 and library 2
959     Koha::CirculationRules->set_rules(
960         {
961             branchcode => $library2->branchcode,
962             itemtype   => $itemtype2->itemtype,
963             categorycode => undef,
964             rules => {
965                 holdallowed => 3,
966                 hold_fulfillment_policy => 'any',
967                 returnbranch => 'any'
968             }
969         }
970     );
971
972     # Test 9: Patron 3 cannot place hold
973     is_deeply(
974         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2 ),
975         { status => 'branchNotInHoldGroup' },
976         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
977     );
978
979     $schema->storage->txn_rollback;
980
981 };
982
983 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
984     plan tests => 9;
985
986     $schema->storage->txn_begin;
987
988     # Cleanup database
989     Koha::Holds->search->delete;
990     $dbh->do('DELETE FROM issues');
991     $dbh->do('DELETE FROM issuingrules');
992     $dbh->do(
993         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
994         VALUES (?, ?, ?, ?)},
995         {},
996         '*', '*', '*', 25
997     );
998     $dbh->do('DELETE FROM circulation_rules');
999
1000     Koha::Items->search->delete;
1001     Koha::Biblios->search->delete;
1002
1003     # Create item types
1004     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1005     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1006
1007     # Create libraries
1008     my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
1009     my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
1010     my $library3  = $builder->build_object( { class => 'Koha::Libraries' } );
1011
1012     # Create library groups hierarchy
1013     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1014     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1015     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1016
1017     # Create 2 patrons
1018     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1019     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1020
1021     # Create 3 biblios with items
1022     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1023     my ( undef, undef, $itemnumber_1 ) = AddItem(
1024         {   homebranch    => $library1->branchcode,
1025             holdingbranch => $library1->branchcode
1026         },
1027         $biblio_1->biblionumber
1028     );
1029     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1030     my ( undef, undef, $itemnumber_2 ) = AddItem(
1031         {   homebranch    => $library2->branchcode,
1032             holdingbranch => $library2->branchcode
1033         },
1034         $biblio_2->biblionumber
1035     );
1036     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1037     my ( undef, undef, $itemnumber_3 ) = AddItem(
1038         {   homebranch    => $library1->branchcode,
1039             holdingbranch => $library1->branchcode
1040         },
1041         $biblio_3->biblionumber
1042     );
1043
1044     # Test 1: Patron 3 can place hold
1045     is_deeply(
1046         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1047         { status => 'OK' },
1048         'Patron can place hold if no circ_rules where defined'
1049     );
1050
1051     # Insert default circ rule of holds allowed only from local hold group for all libraries
1052     Koha::CirculationRules->set_rules(
1053         {
1054             branchcode => undef,
1055             itemtype   => undef,
1056             categorycode => undef,
1057             rules => {
1058                 holdallowed => 2,
1059                 hold_fulfillment_policy => 'holdgroup',
1060                 returnbranch => 'any'
1061             }
1062         }
1063     );
1064
1065     # Test 2: Patron 1 can place hold
1066     is_deeply(
1067         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1068         { status => 'OK' },
1069         'Patron can place hold because pickup location is part of hold group'
1070     );
1071
1072     # Test 3: Patron 3 cannot place hold
1073     is_deeply(
1074         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1075         { status => 'pickupNotInHoldGroup' },
1076         'Patron cannot place hold because pickup location is not part of hold group'
1077     );
1078
1079     # Cleanup default_cirt_rules
1080     $dbh->do('DELETE FROM circulation_rules');
1081
1082     # Insert default circ rule to "any" for library 2
1083     Koha::CirculationRules->set_rules(
1084         {
1085             branchcode => $library2->branchcode,
1086             itemtype   => undef,
1087             categorycode => undef,
1088             rules => {
1089                 holdallowed => 2,
1090                 hold_fulfillment_policy => 'any',
1091                 returnbranch => 'any'
1092             }
1093         }
1094     );
1095
1096     # Test 4: Patron 3 can place hold
1097     is_deeply(
1098         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1099         { status => 'OK' },
1100         'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1101     );
1102
1103     # Update default circ rule to "hold group" for library 2
1104     Koha::CirculationRules->set_rules(
1105         {
1106             branchcode => $library2->branchcode,
1107             itemtype   => undef,
1108             categorycode => undef,
1109             rules => {
1110                 holdallowed => 2,
1111                 hold_fulfillment_policy => 'holdgroup',
1112                 returnbranch => 'any'
1113             }
1114         }
1115     );
1116
1117     # Test 5: Patron 3 cannot place hold
1118     is_deeply(
1119         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1120         { status => 'pickupNotInHoldGroup' },
1121         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1122     );
1123
1124     # Cleanup default_branch_cirt_rules
1125     $dbh->do('DELETE FROM circulation_rules');
1126
1127     # Insert default item rule to "any" for itemtype 2
1128     Koha::CirculationRules->set_rules(
1129         {
1130             branchcode => undef,
1131             itemtype   => $itemtype2->itemtype,
1132             categorycode => undef,
1133             rules => {
1134                 holdallowed => 2,
1135                 hold_fulfillment_policy => 'any',
1136                 returnbranch => 'any'
1137             }
1138         }
1139     );
1140
1141     # Test 6: 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 hold_fulfillment_policy is set to "any" for itemtype 2'
1146     );
1147
1148     # Update default item rule to "hold group" for itemtype 2
1149     Koha::CirculationRules->set_rules(
1150         {
1151             branchcode => undef,
1152             itemtype   => $itemtype2->itemtype,
1153             categorycode => undef,
1154             rules => {
1155                 holdallowed => 2,
1156                 hold_fulfillment_policy => 'holdgroup',
1157                 returnbranch => 'any'
1158             }
1159         }
1160     );
1161
1162     # Test 7: Patron 3 cannot place hold
1163     is_deeply(
1164         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1165         { status => 'pickupNotInHoldGroup' },
1166         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1167     );
1168
1169     # Cleanup default_branch_item_rules
1170     $dbh->do('DELETE FROM circulation_rules');
1171
1172     # Insert branch item rule to "any" for itemtype 2 and library 2
1173     Koha::CirculationRules->set_rules(
1174         {
1175             branchcode => $library2->branchcode,
1176             itemtype   => $itemtype2->itemtype,
1177             categorycode => undef,
1178             rules => {
1179                 holdallowed => 2,
1180                 hold_fulfillment_policy => 'any',
1181                 returnbranch => 'any'
1182             }
1183         }
1184     );
1185
1186     # Test 8: Patron 3 can place hold
1187     is_deeply(
1188         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1189         { status => 'OK' },
1190         'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1191     );
1192
1193     # Update branch item rule to "hold group" for itemtype 2 and library 2
1194     Koha::CirculationRules->set_rules(
1195         {
1196             branchcode => $library2->branchcode,
1197             itemtype   => $itemtype2->itemtype,
1198             categorycode => undef,
1199             rules => {
1200                 holdallowed => 2,
1201                 hold_fulfillment_policy => 'holdgroup',
1202                 returnbranch => 'any'
1203             }
1204         }
1205     );
1206
1207     # Test 9: Patron 3 cannot place hold
1208     is_deeply(
1209         CanItemBeReserved( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1210         { status => 'pickupNotInHoldGroup' },
1211         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1212     );
1213
1214     $schema->storage->txn_rollback;
1215 };