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