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