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