Bug 24860: (QA follow-up) Add unit tests to cover changes to Reserves.pm
[koha.git] / t / db_dependent / Holds.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
7
8 use C4::Context;
9
10 use Test::More tests => 76;
11 use Test::Exception;
12
13 use MARC::Record;
14
15 use C4::Biblio;
16 use C4::Calendar;
17 use C4::Items;
18 use C4::Reserves qw( AddReserve CalculatePriority ModReserve ToggleSuspend AutoUnsuspendReserves SuspendAll ModReserveMinusPriority AlterPriority CanItemBeReserved CheckReserves );
19 use C4::Circulation qw( CanBookBeRenewed );
20
21 use Koha::Biblios;
22 use Koha::CirculationRules;
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::Holds qw( search );
26 use Koha::Checkout;
27 use Koha::Item::Transfer::Limits;
28 use Koha::Items;
29 use Koha::Libraries;
30 use Koha::Library::Groups;
31 use Koha::Patrons;
32 use Koha::Hold qw( get_items_that_can_fill );
33 use Koha::Item::Transfers;
34
35 BEGIN {
36     use FindBin;
37     use lib $FindBin::Bin;
38 }
39
40 my $schema  = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
42
43 my $builder = t::lib::TestBuilder->new();
44 my $dbh     = C4::Context->dbh;
45
46 # Create two random branches
47 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
48 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
49
50 my $category = $builder->build({ source => 'Category' });
51
52 my $borrowers_count = 5;
53
54 $dbh->do('DELETE FROM itemtypes');
55 $dbh->do('DELETE FROM reserves');
56 $dbh->do('DELETE FROM circulation_rules');
57 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
58 $insert_sth->execute('CAN');
59 $insert_sth->execute('CANNOT');
60 $insert_sth->execute('DUMMY');
61 $insert_sth->execute('ONLY1');
62
63 # Setup Test------------------------
64 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
65
66 # Create item instance for testing.
67 my $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
68
69 # Create some borrowers
70 my @borrowernumbers;
71 my @patrons;
72 foreach (1..$borrowers_count) {
73     my $patron = Koha::Patron->new({
74         firstname =>  'my firstname',
75         surname => 'my surname ' . $_,
76         categorycode => $category->{categorycode},
77         branchcode => $branch_1,
78     })->store;
79     push @patrons, $patron;
80     push @borrowernumbers, $patron->borrowernumber;
81 }
82
83 # Create five item level holds
84 foreach my $borrowernumber ( @borrowernumbers ) {
85     AddReserve(
86         {
87             branchcode     => $branch_1,
88             borrowernumber => $borrowernumber,
89             biblionumber   => $biblio->biblionumber,
90             priority       => C4::Reserves::CalculatePriority( $biblio->biblionumber ),
91             itemnumber     => $itemnumber,
92         }
93     );
94 }
95
96 my $holds = $biblio->holds;
97 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
98 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
99 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
100 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
101 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
102 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
103
104 my $item = Koha::Items->find( $itemnumber );
105 $holds = $item->current_holds;
106 my $first_hold = $holds->next;
107 my $reservedate = $first_hold->reservedate;
108 my $borrowernumber = $first_hold->borrowernumber;
109 my $branch_1code = $first_hold->branchcode;
110 my $reserve_id = $first_hold->reserve_id;
111 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
112 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
113 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
114 ok($reserve_id, "Test holds_placed_today()");
115
116 my $hold = Koha::Holds->find( $reserve_id );
117 ok( $hold, "Koha::Holds found the hold" );
118 my $hold_biblio = $hold->biblio();
119 ok( $hold_biblio, "Got biblio using biblio() method" );
120 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
121 my $hold_item = $hold->item();
122 ok( $hold_item, "Got item using item() method" );
123 ok( $hold_item == $hold->item(), "item method returns stashed item" );
124 my $hold_branch = $hold->branch();
125 ok( $hold_branch, "Got branch using branch() method" );
126 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
127 my $hold_found = $hold->found();
128 $hold->set({ found => 'W'})->store();
129 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
130 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
131
132 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
133 $holds = $patron->holds;
134 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
135
136
137 $holds = $item->current_holds;
138 $first_hold = $holds->next;
139 $borrowernumber = $first_hold->borrowernumber;
140 $branch_1code = $first_hold->branchcode;
141 $reserve_id = $first_hold->reserve_id;
142
143 ModReserve({
144     reserve_id    => $reserve_id,
145     rank          => '4',
146     branchcode    => $branch_1,
147     itemnumber    => $itemnumber,
148     suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
149 });
150
151 $hold = Koha::Holds->find( $reserve_id );
152 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
153 ok( $hold->suspend, "Test ModReserve, suspend hold" );
154 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
155
156 ModReserve({ # call without reserve_id
157     rank          => '3',
158     branchcode    => $branch_1,
159     biblionumber  => $biblio->biblionumber,
160     itemnumber    => $itemnumber,
161     borrowernumber => $borrowernumber,
162 });
163 $hold = Koha::Holds->find( $reserve_id );
164 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
165
166 ToggleSuspend( $reserve_id );
167 $hold = Koha::Holds->find( $reserve_id );
168 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
169
170 ToggleSuspend( $reserve_id, '2012-01-01' );
171 $hold = Koha::Holds->find( $reserve_id );
172 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
173
174 AutoUnsuspendReserves();
175 $hold = Koha::Holds->find( $reserve_id );
176 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
177
178 SuspendAll(
179     borrowernumber => $borrowernumber,
180     biblionumber   => $biblio->biblionumber,
181     suspend => 1,
182     suspend_until => '2012-01-01',
183 );
184 $hold = Koha::Holds->find( $reserve_id );
185 is( $hold->suspend, 1, "Test SuspendAll()" );
186 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
187
188 SuspendAll(
189     borrowernumber => $borrowernumber,
190     biblionumber   => $biblio->biblionumber,
191     suspend => 0,
192 );
193 $hold = Koha::Holds->find( $reserve_id );
194 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
195 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
196
197 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
198     AddReserve(
199         {
200             branchcode     => $branch_1,
201             borrowernumber => $borrowernumbers[0],
202             biblionumber   => $biblio->biblionumber,
203         }
204     );
205
206 $patron = Koha::Patrons->find( $borrowernumber );
207 $holds = $patron->holds;
208 my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
209 ModReserveMinusPriority( $itemnumber, $reserveid );
210 $holds = $patron->holds;
211 is( $holds->search({ itemnumber => $itemnumber })->count, 1, "Test ModReserveMinusPriority()" );
212
213 $holds = $biblio->holds;
214 $hold = $holds->next;
215 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
216 $hold = Koha::Holds->find( $reserveid );
217 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
218
219 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
220 $hold = Koha::Holds->find( $reserveid );
221 is( $hold->priority, '2', "Test AlterPriority(), move down" );
222
223 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
224 $hold = Koha::Holds->find( $reserveid );
225 is( $hold->priority, '1', "Test AlterPriority(), move up" );
226
227 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
228 $hold = Koha::Holds->find( $reserveid );
229 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
230
231
232 $hold->delete;
233 throws_ok
234     { C4::Reserves::ModReserve({ reserve_id => $hold->reserve_id }) }
235     'Koha::Exceptions::ObjectNotFound',
236     'No hold with id ' . $hold->reserve_id;
237
238 # Regression test for bug 2394
239 #
240 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
241 # a patron is not permittedo to request an item whose homebranch (i.e.,
242 # owner of the item) is different from the patron's own library.
243 # However, if canreservefromotherbranches is turned ON, the patron can
244 # create such hold requests.
245 #
246 # Note that canreservefromotherbranches has no effect if
247 # IndependentBranches is OFF.
248
249 my $foreign_biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
250 my $foreign_item = $builder->build_sample_item({ library => $branch_2, biblionumber => $foreign_biblio->biblionumber });
251 Koha::CirculationRules->set_rules(
252     {
253         categorycode => undef,
254         branchcode   => undef,
255         itemtype     => undef,
256         rules        => {
257             reservesallowed  => 25,
258             holds_per_record => 99,
259         }
260     }
261 );
262 Koha::CirculationRules->set_rules(
263     {
264         categorycode => undef,
265         branchcode   => undef,
266         itemtype     => 'CANNOT',
267         rules        => {
268             reservesallowed  => 0,
269             holds_per_record => 99,
270         }
271     }
272 );
273
274 # make sure some basic sysprefs are set
275 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
276 t::lib::Mocks::mock_preference('item-level_itypes', 1);
277
278 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
279 t::lib::Mocks::mock_preference('IndependentBranches', 0);
280
281 is(
282     CanItemBeReserved($patrons[0], $foreign_item)->{status}, 'OK',
283     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
284 );
285
286 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
287 t::lib::Mocks::mock_preference('IndependentBranches', 1);
288 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
289 ok(
290     CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'cannotReserveFromOtherBranches',
291     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
292 );
293
294 # ... unless canreservefromotherbranches is ON
295 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
296 ok(
297     CanItemBeReserved($patrons[0], $foreign_item)->{status} eq 'OK',
298     '... unless canreservefromotherbranches is ON (bug 2394)'
299 );
300
301 {
302     # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
303     $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
304     $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
305     my $reserveid1 = AddReserve(
306         {
307             branchcode     => $branch_1,
308             borrowernumber => $borrowernumbers[0],
309             biblionumber   => $biblio->biblionumber,
310             priority       => 1
311         }
312     );
313
314     $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
315     my $reserveid2 = AddReserve(
316         {
317             branchcode     => $branch_1,
318             borrowernumber => $borrowernumbers[1],
319             biblionumber   => $biblio->biblionumber,
320             priority       => 2
321         }
322     );
323
324     $itemnumber = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber })->itemnumber;
325     my $reserveid3 = AddReserve(
326         {
327             branchcode     => $branch_1,
328             borrowernumber => $borrowernumbers[2],
329             biblionumber   => $biblio->biblionumber,
330             priority       => 3
331         }
332     );
333
334     my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber });
335     my $hold3 = Koha::Holds->find( $reserveid3 );
336     is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
337     ModReserve({ reserve_id => $reserveid1, rank => 'del' });
338     ModReserve({ reserve_id => $reserveid2, rank => 'del' });
339     is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
340 }
341
342 my $damaged_item = Koha::Items->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
343 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
344 is( CanItemBeReserved( $patrons[0], $damaged_item)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
345 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
346
347 $hold = Koha::Hold->new(
348     {
349         borrowernumber => $borrowernumbers[0],
350         itemnumber     => $itemnumber,
351         biblionumber   => $biblio->biblionumber,
352         branchcode     => $branch_1,
353     }
354 )->store();
355 is( CanItemBeReserved( $patrons[0], $damaged_item )->{status},
356     'itemAlreadyOnHold',
357     "Patron cannot place a second item level hold for a given item" );
358 $hold->delete();
359
360 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
361 ok( CanItemBeReserved( $patrons[0], $damaged_item)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
362 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
363
364 # Items that are not for loan, but holdable should not be trapped until they are available for loan
365 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 0 );
366 my $nfl_item = Koha::Items->find($itemnumber)->damaged(0)->notforloan(-1)->store;
367 Koha::Holds->search({ biblionumber => $biblio->id })->delete();
368 is( CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
369 $hold = Koha::Hold->new(
370     {
371         borrowernumber => $borrowernumbers[0],
372         itemnumber     => $itemnumber,
373         biblionumber   => $biblio->biblionumber,
374         found          => undef,
375         priority       => 1,
376         reservedate    => dt_from_string,
377         branchcode     => $branch_1,
378     }
379 )->store();
380 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
381 t::lib::Mocks::mock_preference( 'TrapHoldsOnOrder', 1 );
382 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold is trapped for item that is not for loan but holdable ( notforloan < 0 )" );
383 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1' );
384 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
385 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '-1|1' );
386 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
387 t::lib::Mocks::mock_preference( 'SkipHoldTrapOnNotForLoanValue', '' );
388 my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
389 my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
390 $item_group_1->add_item({ item_id => $itemnumber });
391 $hold->item_group_id( $item_group_2->id )->update;
392 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for item with non-matching item group" );
393 is(
394     CanItemBeReserved( $patrons[0], $nfl_item)->{status}, 'itemAlreadyOnHold',
395     "cannot request item that you have already reservedd"
396 );
397 is(
398     CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'OK',
399     "can request item if we are not checking holds counts, but only if policy allows or forbids it"
400 );
401 $hold->delete();
402
403 # Regression test for bug 9532
404 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
405 $item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
406 AddReserve(
407     {
408         branchcode     => $branch_1,
409         borrowernumber => $borrowernumbers[0],
410         biblionumber   => $biblio->biblionumber,
411         priority       => 1,
412     }
413 );
414 is(
415     CanItemBeReserved( $patrons[0], $item)->{status}, 'noReservesAllowed',
416     "cannot request item if policy that matches on item-level item type forbids it"
417 );
418 is(
419     CanItemBeReserved( $patrons[0], $item, undef, { ignore_hold_counts => 1 })->{status}, 'noReservesAllowed',
420     "cannot request item if policy that matches on item-level item type forbids it even if ignoring counts"
421 );
422
423 subtest 'CanItemBeReserved' => sub {
424     plan tests => 2;
425
426     my $itemtype_can         = $builder->build({source => "Itemtype"})->{itemtype};
427     my $itemtype_cant        = $builder->build({source => "Itemtype"})->{itemtype};
428     my $itemtype_cant_record = $builder->build({source => "Itemtype"})->{itemtype};
429
430     Koha::CirculationRules->set_rules(
431         {
432             categorycode => undef,
433             branchcode   => undef,
434             itemtype     => $itemtype_cant,
435             rules        => {
436                 reservesallowed  => 0,
437                 holds_per_record => 99,
438             }
439         }
440     );
441     Koha::CirculationRules->set_rules(
442         {
443             categorycode => undef,
444             branchcode   => undef,
445             itemtype     => $itemtype_can,
446             rules        => {
447                 reservesallowed  => 2,
448                 holds_per_record => 2,
449             }
450         }
451     );
452     Koha::CirculationRules->set_rules(
453         {
454             categorycode => undef,
455             branchcode   => undef,
456             itemtype     => $itemtype_cant_record,
457             rules        => {
458                 reservesallowed  => 0,
459                 holds_per_record => 0,
460             }
461         }
462     );
463
464     Koha::CirculationRules->set_rules(
465         {
466             branchcode => $branch_1,
467             itemtype   => $itemtype_cant,
468             rules => {
469                 holdallowed => 0,
470                 returnbranch => 'homebranch',
471             }
472         }
473     );
474     Koha::CirculationRules->set_rules(
475         {
476             branchcode => $branch_1,
477             itemtype   => $itemtype_can,
478             rules => {
479                 holdallowed => 1,
480                 returnbranch => 'homebranch',
481             }
482         }
483     );
484
485     subtest 'noReservesAllowed' => sub {
486         plan tests => 5;
487
488         my $biblionumber_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant })->biblionumber;
489         my $biblionumber_can = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
490         my $biblionumber_record_cannot = $builder->build_sample_biblio({ itemtype => $itemtype_cant_record })->biblionumber;
491
492         my $item_1_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_cannot });
493         my $item_1_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_cannot });
494         my $item_2_can = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_can });
495         my $item_2_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant, biblionumber => $biblionumber_can });
496         my $item_3_cannot = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_cant_record, biblionumber => $biblionumber_record_cannot });
497
498         Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
499
500         t::lib::Mocks::mock_preference('item-level_itypes', 1);
501         is(
502             CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'noReservesAllowed',
503             "With item level set, rule from item must be picked (CANNOT)"
504         );
505         is(
506             CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'OK',
507             "With item level set, rule from item must be picked (CAN)"
508         );
509         t::lib::Mocks::mock_preference('item-level_itypes', 0);
510         is(
511             CanItemBeReserved( $patrons[0], $item_1_can)->{status}, 'noReservesAllowed',
512             "With biblio level set, rule from biblio must be picked (CANNOT)"
513         );
514         is(
515             CanItemBeReserved( $patrons[0], $item_2_cannot)->{status}, 'OK',
516             "With biblio level set, rule from biblio must be picked (CAN)"
517         );
518         is(
519             CanItemBeReserved( $patrons[0], $item_3_cannot)->{status}, 'noReservesAllowed',
520             "When no holds allowed and no holds per record allowed should return noReservesAllowed"
521         );
522     };
523
524     subtest 'tooManyHoldsForThisRecord + tooManyReserves + itemAlreadyOnHold' => sub {
525         plan tests => 7;
526
527         my $biblionumber_1 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
528         my $item_11 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
529         my $item_12 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_1 });
530         my $biblionumber_2 = $builder->build_sample_biblio({ itemtype => $itemtype_can })->biblionumber;
531         my $item_21 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
532         my $item_22 = $builder->build_sample_item({ homebranch => $branch_1, holdingbranch => $branch_1, itype => $itemtype_can, biblionumber => $biblionumber_2 });
533
534         Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
535
536         # Biblio-level hold
537         AddReserve({
538             branchcode => $branch_1,
539             borrowernumber => $borrowernumbers[0],
540             biblionumber => $biblionumber_1,
541         });
542         for my $item_level ( 0..1 ) {
543             t::lib::Mocks::mock_preference('item-level_itypes', $item_level);
544             is(
545                 # FIXME This is not really correct, but CanItemBeReserved does not check if biblio-level holds already exist
546                 CanItemBeReserved( $patrons[0], $item_11)->{status}, 'OK',
547                 "A biblio-level hold already exists - another hold can be placed on a specific item item"
548             );
549         }
550
551         Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
552         # Item-level hold
553         AddReserve({
554             branchcode => $branch_1,
555             borrowernumber => $borrowernumbers[0],
556             biblionumber => $biblionumber_1,
557             itemnumber => $item_11->itemnumber,
558         });
559
560         $dbh->do('DELETE FROM circulation_rules');
561         Koha::CirculationRules->set_rules(
562             {
563                 categorycode => undef,
564                 branchcode   => undef,
565                 itemtype     => undef,
566                 rules        => {
567                     reservesallowed  => 5,
568                     holds_per_record => 1,
569                 }
570             }
571         );
572         is(
573             CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
574             "A item-level hold already exists and holds_per_record=1, another hold cannot be placed on this record"
575         );
576         Koha::CirculationRules->set_rules(
577             {
578                 categorycode => undef,
579                 branchcode   => undef,
580                 itemtype     => undef,
581                 rules        => {
582                     reservesallowed  => 1,
583                     holds_per_record => 1,
584                 }
585             }
586         );
587         is(
588             CanItemBeReserved( $patrons[0], $item_12)->{status}, 'tooManyHoldsForThisRecord',
589             "A item-level hold already exists and holds_per_record=1 - tooManyHoldsForThisRecord has priority over tooManyReserves"
590         );
591         Koha::CirculationRules->set_rules(
592             {
593                 categorycode => undef,
594                 branchcode   => undef,
595                 itemtype     => undef,
596                 rules        => {
597                     reservesallowed  => 5,
598                     holds_per_record => 2,
599                 }
600             }
601         );
602         is(
603             CanItemBeReserved( $patrons[0], $item_12)->{status}, 'OK',
604             "A item-level hold already exists but holds_per_record=2- another item-level hold can be placed on this record"
605         );
606
607         AddReserve({
608             branchcode => $branch_1,
609             borrowernumber => $borrowernumbers[0],
610             biblionumber => $biblionumber_2,
611             itemnumber => $item_21->itemnumber
612         });
613         Koha::CirculationRules->set_rules(
614             {
615                 categorycode => undef,
616                 branchcode   => undef,
617                 itemtype     => undef,
618                 rules        => {
619                     reservesallowed  => 2,
620                     holds_per_record => 2,
621                 }
622             }
623         );
624         is(
625             CanItemBeReserved( $patrons[0], $item_21)->{status}, 'itemAlreadyOnHold',
626             "A item-level holds already exists on this item, itemAlreadyOnHold should be raised"
627         );
628         is(
629             CanItemBeReserved( $patrons[0], $item_22)->{status}, 'tooManyReserves',
630             "This patron has already placed reservesallowed holds, tooManyReserves should be raised"
631         );
632     };
633 };
634
635
636 # Test branch item rules
637
638 $dbh->do('DELETE FROM circulation_rules');
639 Koha::CirculationRules->set_rules(
640     {
641         categorycode => undef,
642         branchcode   => undef,
643         itemtype     => undef,
644         rules        => {
645             reservesallowed  => 25,
646             holds_per_record => 99,
647         }
648     }
649 );
650 Koha::CirculationRules->set_rules(
651     {
652         branchcode => $branch_1,
653         itemtype   => 'CANNOT',
654         rules => {
655             holdallowed => 'not_allowed',
656             returnbranch => 'homebranch',
657         }
658     }
659 );
660 Koha::CirculationRules->set_rules(
661     {
662         branchcode => $branch_1,
663         itemtype   => 'CAN',
664         rules => {
665             holdallowed => 'from_home_library',
666             returnbranch => 'homebranch',
667         }
668     }
669 );
670 $biblio = $builder->build_sample_biblio({ itemtype => 'CANNOT' });
671 my $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CANNOT', biblionumber => $biblio->biblionumber});
672 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'notReservable',
673     "CanItemBeReserved should return 'notReservable'");
674
675 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
676 $branch_rule_item = $builder->build_sample_item({ library => $branch_2, itype => 'CAN', biblionumber => $biblio->biblionumber});
677 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
678     'cannotReserveFromOtherBranches',
679     "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
680 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
681 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status},
682     'OK',
683     "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
684
685 $branch_rule_item = $builder->build_sample_item({ library => $branch_1, itype => 'CAN', biblionumber => $biblio->biblionumber});
686 is(CanItemBeReserved($patrons[0], $branch_rule_item)->{status}, 'OK',
687     "CanItemBeReserved should return 'OK'");
688
689 # Bug 12632
690 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
691 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
692
693 $dbh->do('DELETE FROM reserves');
694 $dbh->do('DELETE FROM issues');
695 $dbh->do('DELETE FROM items');
696 $dbh->do('DELETE FROM biblio');
697
698 $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
699 my $limit_count_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
700
701 Koha::CirculationRules->set_rules(
702     {
703         categorycode => undef,
704         branchcode   => undef,
705         itemtype     => 'ONLY1',
706         rules        => {
707             reservesallowed  => 1,
708             holds_per_record => 99,
709         }
710     }
711 );
712 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
713     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
714
715 my $res_id = AddReserve(
716     {
717         branchcode     => $branch_1,
718         borrowernumber => $borrowernumbers[0],
719         biblionumber   => $biblio->biblionumber,
720         priority       => 1,
721     }
722 );
723
724 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
725     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
726 is( CanItemBeReserved( $patrons[0], $limit_count_item, undef, { ignore_hold_counts => 1 } )->{status},
727     'OK', 'Patron can reserve item if checking policy but not counts' );
728
729     #results should be the same for both ReservesControlBranch settings
730 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
731 is( CanItemBeReserved( $patrons[0], $limit_count_item )->{status},
732     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
733 #reset for further tests
734 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
735
736 subtest 'Test max_holds per library/patron category' => sub {
737     plan tests => 6;
738
739     $dbh->do('DELETE FROM reserves');
740
741     $biblio = $builder->build_sample_biblio;
742     my $max_holds_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
743     Koha::CirculationRules->set_rules(
744         {
745             categorycode => undef,
746             branchcode   => undef,
747             itemtype     => $biblio->itemtype,
748             rules        => {
749                 reservesallowed  => 99,
750                 holds_per_record => 99,
751             }
752         }
753     );
754
755     for ( 1 .. 3 ) {
756         AddReserve(
757             {
758                 branchcode     => $branch_1,
759                 borrowernumber => $borrowernumbers[0],
760                 biblionumber   => $biblio->biblionumber,
761                 priority       => 1,
762             }
763         );
764     }
765
766     my $count =
767       Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
768     is( $count, 3, 'Patron now has 3 holds' );
769
770     my $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
771     is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
772
773     my $rule_all = Koha::CirculationRules->set_rule(
774         {
775             categorycode => $category->{categorycode},
776             branchcode   => undef,
777             rule_name    => 'max_holds',
778             rule_value   => 3,
779         }
780     );
781
782     my $rule_branch = Koha::CirculationRules->set_rule(
783         {
784             branchcode   => $branch_1,
785             categorycode => $category->{categorycode},
786             rule_name    => 'max_holds',
787             rule_value   => 5,
788         }
789     );
790
791     $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
792     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
793
794     $rule_branch->delete();
795
796     $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
797     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
798
799     $rule_all->delete();
800     $rule_branch->rule_value(3);
801     $rule_branch->store();
802
803     $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
804     is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
805
806     $rule_branch->rule_value(5);
807     $rule_branch->update();
808     $rule_branch->rule_value(5);
809     $rule_branch->store();
810
811     $ret = CanItemBeReserved( $patrons[0], $max_holds_item );
812     is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
813 };
814
815 subtest 'Pickup location availability tests' => sub {
816     plan tests => 4;
817
818     $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
819     my $pickup_item = $builder->build_sample_item({ library => $branch_1, biblionumber => $biblio->biblionumber});
820     #Add a default rule to allow some holds
821
822     Koha::CirculationRules->set_rules(
823         {
824             branchcode   => undef,
825             categorycode => undef,
826             itemtype     => undef,
827             rules        => {
828                 reservesallowed  => 25,
829                 holds_per_record => 99,
830             }
831         }
832     );
833     my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
834     my $library = Koha::Libraries->find($branch_to);
835     $library->pickup_location('1')->store;
836     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
837
838     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
839     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
840
841     $library->pickup_location('1')->store;
842     is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
843        'OK', 'Library is a pickup location');
844
845     my $limit = Koha::Item::Transfer::Limit->new({
846         fromBranch => $pickup_item->holdingbranch,
847         toBranch => $branch_to,
848         itemtype => $pickup_item->effective_itemtype,
849     })->store;
850     is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
851        'cannotBeTransferred', 'Item cannot be transferred');
852     $limit->delete;
853
854     $library->pickup_location('0')->store;
855     is(CanItemBeReserved($patron, $pickup_item, $branch_to)->{status},
856        'libraryNotPickupLocation', 'Library is not a pickup location');
857     is(CanItemBeReserved($patron, $pickup_item, 'nonexistent')->{status},
858        'libraryNotFound', 'Cannot set unknown library as pickup location');
859 };
860
861 $schema->storage->txn_rollback;
862
863 subtest 'CanItemBeReserved / holds_per_day tests' => sub {
864
865     plan tests => 10;
866
867     $schema->storage->txn_begin;
868
869     my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
870     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
871     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
872
873     # Create 3 biblios with items
874     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
875     my $item_1 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_1->biblionumber});
876     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
877     my $item_2 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_2->biblionumber});
878     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype });
879     my $item_3 = $builder->build_sample_item({ library => $library->branchcode, biblionumber => $biblio_3->biblionumber});
880
881     Koha::CirculationRules->set_rules(
882         {
883             categorycode => '*',
884             branchcode   => '*',
885             itemtype     => $itemtype->itemtype,
886             rules        => {
887                 reservesallowed  => 1,
888                 holds_per_record => 99,
889                 holds_per_day    => 2
890             }
891         }
892     );
893
894     is_deeply(
895         CanItemBeReserved( $patron, $item_1 ),
896         { status => 'OK' },
897         'Patron can reserve item with hold limit of 1, no holds placed'
898     );
899
900     AddReserve(
901         {
902             branchcode     => $library->branchcode,
903             borrowernumber => $patron->borrowernumber,
904             biblionumber   => $biblio_1->biblionumber,
905             priority       => 1,
906         }
907     );
908
909     is_deeply(
910         CanItemBeReserved( $patron, $item_1 ),
911         { status => 'tooManyReserves', limit => 1 },
912         'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
913     );
914
915     # Raise reservesallowed to avoid tooManyReserves from it
916     Koha::CirculationRules->set_rule(
917         {
918
919             categorycode => '*',
920             branchcode   => '*',
921             itemtype     => $itemtype->itemtype,
922             rule_name  => 'reservesallowed',
923             rule_value => 3,
924         }
925     );
926
927     is_deeply(
928         CanItemBeReserved( $patron, $item_2 ),
929         { status => 'OK' },
930         'Patron can reserve item with 2 reserves daily cap'
931     );
932
933     # Add a second reserve
934     my $res_id = AddReserve(
935         {
936             branchcode     => $library->branchcode,
937             borrowernumber => $patron->borrowernumber,
938             biblionumber   => $biblio_2->biblionumber,
939             priority       => 1,
940         }
941     );
942     is_deeply(
943         CanItemBeReserved( $patron, $item_2 ),
944         { status => 'tooManyReservesToday', limit => 2 },
945         'Patron cannot a third item with 2 reserves daily cap'
946     );
947
948     # Update last hold so reservedate is in the past, so 2 holds, but different day
949     $hold = Koha::Holds->find($res_id);
950     my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
951     $hold->reservedate($yesterday)->store;
952
953     is_deeply(
954         CanItemBeReserved( $patron, $item_2 ),
955         { status => 'OK' },
956         'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
957     );
958
959     # Set holds_per_day to 0
960     Koha::CirculationRules->set_rule(
961         {
962
963             categorycode => '*',
964             branchcode   => '*',
965             itemtype     => $itemtype->itemtype,
966             rule_name  => 'holds_per_day',
967             rule_value => 0,
968         }
969     );
970
971
972     # Delete existing holds
973     Koha::Holds->search->delete;
974     is_deeply(
975         CanItemBeReserved( $patron, $item_2 ),
976         { status => 'tooManyReservesToday', limit => 0 },
977         'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
978     );
979
980     Koha::CirculationRules->set_rule(
981         {
982
983             categorycode => '*',
984             branchcode   => '*',
985             itemtype     => $itemtype->itemtype,
986             rule_name  => 'holds_per_day',
987             rule_value => undef,
988         }
989     );
990
991     Koha::Holds->search->delete;
992     is_deeply(
993         CanItemBeReserved( $patron, $item_2 ),
994         { status => 'OK' },
995         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
996     );
997     AddReserve(
998         {
999             branchcode     => $library->branchcode,
1000             borrowernumber => $patron->borrowernumber,
1001             biblionumber   => $biblio_1->biblionumber,
1002             priority       => 1,
1003         }
1004     );
1005     AddReserve(
1006         {
1007             branchcode     => $library->branchcode,
1008             borrowernumber => $patron->borrowernumber,
1009             biblionumber   => $biblio_2->biblionumber,
1010             priority       => 1,
1011         }
1012     );
1013
1014     is_deeply(
1015         CanItemBeReserved( $patron, $item_3 ),
1016         { status => 'OK' },
1017         'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
1018     );
1019     AddReserve(
1020         {
1021             branchcode     => $library->branchcode,
1022             borrowernumber => $patron->borrowernumber,
1023             biblionumber   => $biblio_3->biblionumber,
1024             priority       => 1,
1025         }
1026     );
1027     is_deeply(
1028         CanItemBeReserved( $patron, $item_3 ),
1029         { status => 'tooManyReserves', limit => 3 },
1030         'Unlimited daily holds, but reached reservesallowed'
1031     );
1032     #results should be the same for both ReservesControlBranch settings
1033     t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1034     is_deeply(
1035         CanItemBeReserved( $patron, $item_3 ),
1036         { status => 'tooManyReserves', limit => 3 },
1037         'Unlimited daily holds, but reached reservesallowed'
1038     );
1039
1040     $schema->storage->txn_rollback;
1041 };
1042
1043 subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub {
1044     plan tests => 9;
1045
1046     $schema->storage->txn_begin;
1047
1048     Koha::CirculationRules->set_rule(
1049         {
1050             branchcode   => undef,
1051             categorycode => undef,
1052             itemtype     => undef,
1053             rule_name    => 'reservesallowed',
1054             rule_value   => 25,
1055         }
1056     );
1057
1058     # Create item types
1059     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1060     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1061
1062     # Create libraries
1063     my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
1064     my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
1065     my $library3  = $builder->build_object( { class => 'Koha::Libraries' } );
1066
1067     # Create library groups hierarchy
1068     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1069     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1070     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1071
1072     # Create 2 patrons
1073     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1074     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1075
1076     # Create 3 biblios with items
1077     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1078     my $item_1   = $builder->build_sample_item(
1079         {
1080             biblionumber => $biblio_1->biblionumber,
1081             library      => $library1->branchcode
1082         }
1083     );
1084     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1085     my $item_2   = $builder->build_sample_item(
1086         {
1087             biblionumber => $biblio_2->biblionumber,
1088             library      => $library2->branchcode
1089         }
1090     );
1091     my $itemnumber_2 = $item_2->itemnumber;
1092     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1093     my $item_3   = $builder->build_sample_item(
1094         {
1095             biblionumber => $biblio_3->biblionumber,
1096             library      => $library1->branchcode
1097         }
1098     );
1099
1100     # Test 1: Patron 3 can place hold
1101     is_deeply(
1102         CanItemBeReserved( $patron3, $item_2 ),
1103         { status => 'OK' },
1104         'Patron can place hold if no circ_rules where defined'
1105     );
1106
1107     # Insert default circ rule of holds allowed only from local hold group for all libraries
1108     Koha::CirculationRules->set_rules(
1109         {
1110             branchcode => undef,
1111             itemtype   => undef,
1112             rules => {
1113                 holdallowed => 'from_local_hold_group',
1114                 hold_fulfillment_policy => 'any',
1115                 returnbranch => 'any'
1116             }
1117         }
1118     );
1119
1120     # Test 2: Patron 1 can place hold
1121     is_deeply(
1122         CanItemBeReserved( $patron1, $item_2 ),
1123         { status => 'OK' },
1124         'Patron can place hold because patron\'s home library is part of hold group'
1125     );
1126
1127     # Test 3: Patron 3 cannot place hold
1128     is_deeply(
1129         CanItemBeReserved( $patron3, $item_2 ),
1130         { status => 'branchNotInHoldGroup' },
1131         'Patron cannot place hold because patron\'s home library is not part of hold group'
1132     );
1133
1134     # Insert default circ rule to "any" for library 2
1135     Koha::CirculationRules->set_rules(
1136         {
1137             branchcode => $library2->branchcode,
1138             itemtype   => undef,
1139             rules => {
1140                 holdallowed => 'from_any_library',
1141                 hold_fulfillment_policy => 'any',
1142                 returnbranch => 'any'
1143             }
1144         }
1145     );
1146
1147     # Test 4: Patron 3 can place hold
1148     is_deeply(
1149         CanItemBeReserved( $patron3, $item_2 ),
1150         { status => 'OK' },
1151         'Patron can place hold if holdallowed is set to "any" for library 2'
1152     );
1153
1154     # Update default circ rule to "hold group" for library 2
1155     Koha::CirculationRules->set_rules(
1156         {
1157             branchcode => $library2->branchcode,
1158             itemtype   => undef,
1159             rules => {
1160                 holdallowed => 'from_local_hold_group',
1161                 hold_fulfillment_policy => 'any',
1162                 returnbranch => 'any'
1163             }
1164         }
1165     );
1166
1167     # Test 5: Patron 3 cannot place hold
1168     is_deeply(
1169         CanItemBeReserved( $patron3, $item_2 ),
1170         { status => 'branchNotInHoldGroup' },
1171         'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
1172     );
1173
1174     # Insert default item rule to "any" for itemtype 2
1175     Koha::CirculationRules->set_rules(
1176         {
1177             branchcode => $library2->branchcode,
1178             itemtype   => $itemtype2->itemtype,
1179             rules => {
1180                 holdallowed => 'from_any_library',
1181                 hold_fulfillment_policy => 'any',
1182                 returnbranch => 'any'
1183             }
1184         }
1185     );
1186
1187     # Test 6: Patron 3 can place hold
1188     is_deeply(
1189         CanItemBeReserved( $patron3, $item_2 ),
1190         { status => 'OK' },
1191         'Patron can place hold if holdallowed is set to "any" for itemtype 2'
1192     );
1193
1194     # Update default item rule to "hold group" for itemtype 2
1195     Koha::CirculationRules->set_rules(
1196         {
1197             branchcode => $library2->branchcode,
1198             itemtype   => $itemtype2->itemtype,
1199             rules => {
1200                 holdallowed => 'from_local_hold_group',
1201                 hold_fulfillment_policy => 'any',
1202                 returnbranch => 'any'
1203             }
1204         }
1205     );
1206
1207     # Test 7: Patron 3 cannot place hold
1208     is_deeply(
1209         CanItemBeReserved( $patron3, $item_2 ),
1210         { status => 'branchNotInHoldGroup' },
1211         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
1212     );
1213
1214     # Insert branch item rule to "any" for itemtype 2 and library 2
1215     Koha::CirculationRules->set_rules(
1216         {
1217             branchcode => $library2->branchcode,
1218             itemtype   => $itemtype2->itemtype,
1219             rules => {
1220                 holdallowed => 'from_any_library',
1221                 hold_fulfillment_policy => 'any',
1222                 returnbranch => 'any'
1223             }
1224         }
1225     );
1226
1227     # Test 8: Patron 3 can place hold
1228     is_deeply(
1229         CanItemBeReserved( $patron3, $item_2 ),
1230         { status => 'OK' },
1231         'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1232     );
1233
1234     # Update branch item rule to "hold group" for itemtype 2 and library 2
1235     Koha::CirculationRules->set_rules(
1236         {
1237             branchcode => $library2->branchcode,
1238             itemtype   => $itemtype2->itemtype,
1239             rules => {
1240                 holdallowed => 'from_local_hold_group',
1241                 hold_fulfillment_policy => 'any',
1242                 returnbranch => 'any'
1243             }
1244         }
1245     );
1246
1247     # Test 9: Patron 3 cannot place hold
1248     is_deeply(
1249         CanItemBeReserved( $patron3, $item_2 ),
1250         { status => 'branchNotInHoldGroup' },
1251         'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1252     );
1253
1254     $schema->storage->txn_rollback;
1255
1256 };
1257
1258 subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1259     plan tests => 9;
1260
1261     $schema->storage->txn_begin;
1262     Koha::CirculationRules->set_rule(
1263         {
1264             branchcode   => undef,
1265             categorycode => undef,
1266             itemtype     => undef,
1267             rule_name    => 'reservesallowed',
1268             rule_value   => 25,
1269         }
1270     );
1271
1272     # Create item types
1273     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1274     my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1275
1276     # Create libraries
1277     my $library1  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1278     my $library2  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1279     my $library3  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1280
1281     # Create library groups hierarchy
1282     my $rootgroup  = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1283     my $group1  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1284     my $group2  = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode} } );
1285
1286     # Create 2 patrons
1287     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1288     my $patron3   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library3->branchcode} } );
1289
1290     # Create 3 biblios with items
1291     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1292     my $item_1   = $builder->build_sample_item(
1293         {
1294             biblionumber => $biblio_1->biblionumber,
1295             library      => $library1->branchcode
1296         }
1297     );
1298     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype2->itemtype });
1299     my $item_2   = $builder->build_sample_item(
1300         {
1301             biblionumber => $biblio_2->biblionumber,
1302             library      => $library2->branchcode
1303         }
1304     );
1305     my $itemnumber_2 = $item_2->itemnumber;
1306     my $biblio_3 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1307     my $item_3   = $builder->build_sample_item(
1308         {
1309             biblionumber => $biblio_3->biblionumber,
1310             library      => $library1->branchcode
1311         }
1312     );
1313
1314     # Test 1: Patron 3 can place hold
1315     is_deeply(
1316         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1317         { status => 'OK' },
1318         'Patron can place hold if no circ_rules where defined'
1319     );
1320
1321     # Insert default circ rule of holds allowed only from local hold group for all libraries
1322     Koha::CirculationRules->set_rules(
1323         {
1324             branchcode => undef,
1325             itemtype   => undef,
1326             rules => {
1327                 holdallowed => 'from_any_library',
1328                 hold_fulfillment_policy => 'holdgroup',
1329                 returnbranch => 'any'
1330             }
1331         }
1332     );
1333
1334     # Test 2: Patron 1 can place hold
1335     is_deeply(
1336         CanItemBeReserved( $patron3, $item_2, $library1->branchcode ),
1337         { status => 'OK' },
1338         'Patron can place hold because pickup location is part of hold group'
1339     );
1340
1341     # Test 3: Patron 3 cannot place hold
1342     is_deeply(
1343         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1344         { status => 'pickupNotInHoldGroup' },
1345         'Patron cannot place hold because pickup location is not part of hold group'
1346     );
1347
1348     # Insert default circ rule to "any" for library 2
1349     Koha::CirculationRules->set_rules(
1350         {
1351             branchcode => $library2->branchcode,
1352             itemtype   => undef,
1353             rules => {
1354                 holdallowed => 'from_any_library',
1355                 hold_fulfillment_policy => 'any',
1356                 returnbranch => 'any'
1357             }
1358         }
1359     );
1360
1361     # Test 4: Patron 3 can place hold
1362     is_deeply(
1363         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1364         { status => 'OK' },
1365         'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1366     );
1367
1368     # Update default circ rule to "hold group" for library 2
1369     Koha::CirculationRules->set_rules(
1370         {
1371             branchcode => $library2->branchcode,
1372             itemtype   => undef,
1373             rules => {
1374                 holdallowed => 'from_any_library',
1375                 hold_fulfillment_policy => 'holdgroup',
1376                 returnbranch => 'any'
1377             }
1378         }
1379     );
1380
1381     # Test 5: Patron 3 cannot place hold
1382     is_deeply(
1383         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1384         { status => 'pickupNotInHoldGroup' },
1385         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1386     );
1387
1388     # Insert default item rule to "any" for itemtype 2
1389     Koha::CirculationRules->set_rules(
1390         {
1391             branchcode => $library2->branchcode,
1392             itemtype   => $itemtype2->itemtype,
1393             rules => {
1394                 holdallowed => 'from_any_library',
1395                 hold_fulfillment_policy => 'any',
1396                 returnbranch => 'any'
1397             }
1398         }
1399     );
1400
1401     # Test 6: Patron 3 can place hold
1402     is_deeply(
1403         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1404         { status => 'OK' },
1405         'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1406     );
1407
1408     # Update default item rule to "hold group" for itemtype 2
1409     Koha::CirculationRules->set_rules(
1410         {
1411             branchcode => $library2->branchcode,
1412             itemtype   => $itemtype2->itemtype,
1413             rules => {
1414                 holdallowed => 'from_any_library',
1415                 hold_fulfillment_policy => 'holdgroup',
1416                 returnbranch => 'any'
1417             }
1418         }
1419     );
1420
1421     # Test 7: Patron 3 cannot place hold
1422     is_deeply(
1423         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1424         { status => 'pickupNotInHoldGroup' },
1425         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1426     );
1427
1428     # Insert branch item rule to "any" for itemtype 2 and library 2
1429     Koha::CirculationRules->set_rules(
1430         {
1431             branchcode => $library2->branchcode,
1432             itemtype   => $itemtype2->itemtype,
1433             rules => {
1434                 holdallowed => 'from_any_library',
1435                 hold_fulfillment_policy => 'any',
1436                 returnbranch => 'any'
1437             }
1438         }
1439     );
1440
1441     # Test 8: Patron 3 can place hold
1442     is_deeply(
1443         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1444         { status => 'OK' },
1445         'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1446     );
1447
1448     # Update branch item rule to "hold group" for itemtype 2 and library 2
1449     Koha::CirculationRules->set_rules(
1450         {
1451             branchcode => $library2->branchcode,
1452             itemtype   => $itemtype2->itemtype,
1453             rules => {
1454                 holdallowed => 'from_any_library',
1455                 hold_fulfillment_policy => 'holdgroup',
1456                 returnbranch => 'any'
1457             }
1458         }
1459     );
1460
1461     # Test 9: Patron 3 cannot place hold
1462     is_deeply(
1463         CanItemBeReserved( $patron3, $item_2, $library3->branchcode ),
1464         { status => 'pickupNotInHoldGroup' },
1465         'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1466     );
1467
1468     $schema->storage->txn_rollback;
1469 };
1470
1471 subtest 'non priority holds' => sub {
1472
1473     plan tests => 6;
1474
1475     $schema->storage->txn_begin;
1476
1477     Koha::CirculationRules->set_rules(
1478         {
1479             branchcode   => undef,
1480             categorycode => undef,
1481             itemtype     => undef,
1482             rules        => {
1483                 renewalsallowed => 5,
1484                 reservesallowed => 5,
1485             }
1486         }
1487     );
1488
1489     my $item = $builder->build_sample_item;
1490
1491     my $patron1 = $builder->build_object(
1492         {
1493             class => 'Koha::Patrons',
1494             value => { branchcode => $item->homebranch }
1495         }
1496     );
1497     my $patron2 = $builder->build_object(
1498         {
1499             class => 'Koha::Patrons',
1500             value => { branchcode => $item->homebranch }
1501         }
1502     );
1503
1504     Koha::Checkout->new(
1505         {
1506             borrowernumber => $patron1->borrowernumber,
1507             itemnumber     => $item->itemnumber,
1508             branchcode     => $item->homebranch
1509         }
1510     )->store;
1511
1512     my $hid = AddReserve(
1513         {
1514             branchcode     => $item->homebranch,
1515             borrowernumber => $patron2->borrowernumber,
1516             biblionumber   => $item->biblionumber,
1517             priority       => 1,
1518             itemnumber     => $item->itemnumber,
1519         }
1520     );
1521
1522     my ( $ok, $err ) =
1523       CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1524
1525     ok( !$ok, 'Cannot renew' );
1526     is( $err, 'on_reserve', 'Item is on hold' );
1527
1528     my $hold = Koha::Holds->find($hid);
1529     $hold->non_priority(1)->store;
1530
1531     ( $ok, $err ) =
1532       CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1533
1534     ok( $ok, 'Can renew' );
1535     is( $err, undef, 'Item is on non priority hold' );
1536
1537     my $patron3 = $builder->build_object(
1538         {
1539             class => 'Koha::Patrons',
1540             value => { branchcode => $item->homebranch }
1541         }
1542     );
1543
1544     # Add second hold with non_priority = 0
1545     AddReserve(
1546         {
1547             branchcode     => $item->homebranch,
1548             borrowernumber => $patron3->borrowernumber,
1549             biblionumber   => $item->biblionumber,
1550             priority       => 2,
1551             itemnumber     => $item->itemnumber,
1552         }
1553     );
1554
1555     ( $ok, $err ) =
1556       CanBookBeRenewed( $patron1->borrowernumber, $item->itemnumber );
1557
1558     ok( !$ok, 'Cannot renew' );
1559     is( $err, 'on_reserve', 'Item is on hold' );
1560
1561     $schema->storage->txn_rollback;
1562 };
1563
1564 subtest 'CanItemBeReserved / recall' => sub {
1565     plan tests => 1;
1566
1567     $schema->storage->txn_begin;
1568
1569     my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1570     my $library1  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
1571     my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
1572     my $biblio1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
1573     my $item1   = $builder->build_sample_item(
1574         {
1575             biblionumber => $biblio1->biblionumber,
1576             library      => $library1->branchcode
1577         }
1578     );
1579     Koha::Recall->new({
1580         patron_id => $patron1->borrowernumber,
1581         biblio_id => $biblio1->biblionumber,
1582         pickup_library_id => $library1->branchcode,
1583         item_id => $item1->itemnumber,
1584         created_date => '2020-05-04 10:10:10',
1585         item_level => 1,
1586     })->store;
1587     is( CanItemBeReserved( $patron1, $item1, $library1->branchcode )->{status}, 'recall', "Can't reserve an item that they have already recalled" );
1588
1589     $schema->storage->txn_rollback;
1590 };
1591
1592 subtest 'CanItemBeReserved rule precedence tests' => sub {
1593
1594     plan tests => 3;
1595
1596     t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1597     $schema->storage->txn_begin;
1598     my $library  = $builder->build_object( { class => 'Koha::Libraries', value => {
1599         pickup_location => 1,
1600     }});
1601     my $item = $builder->build_sample_item({
1602         homebranch    => $library->branchcode,
1603         holdingbranch => $library->branchcode
1604     });
1605     my $item2 = $builder->build_sample_item({
1606         homebranch    => $library->branchcode,
1607         holdingbranch => $library->branchcode,
1608         itype         => $item->itype
1609     });
1610     my $patron   = $builder->build_object({ class => 'Koha::Patrons', value => {
1611         branchcode => $library->branchcode
1612     }});
1613     Koha::CirculationRules->set_rules(
1614         {
1615             branchcode   => undef,
1616             categorycode => $patron->categorycode,
1617             itemtype     => $item->itype,
1618             rules        => {
1619                 reservesallowed  => 1,
1620             }
1621         }
1622     );
1623     is_deeply(
1624         CanItemBeReserved( $patron, $item, $library->branchcode ),
1625         { status => 'OK' },
1626         'Patron of specified category can place 1 hold on specified itemtype'
1627     );
1628     my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
1629         biblionumber   => $item2->biblionumber,
1630         itemnumber     => $item2->itemnumber,
1631         found          => undef,
1632         priority       => 1,
1633         branchcode     => $library->branchcode,
1634         borrowernumber => $patron->borrowernumber,
1635     }});
1636     is_deeply(
1637         CanItemBeReserved( $patron, $item, $library->branchcode ),
1638         { status => 'tooManyReserves', limit => 1 },
1639         'Patron of specified category can place 1 hold on specified itemtype, cannot place a second'
1640     );
1641     Koha::CirculationRules->set_rules(
1642         {
1643             branchcode   => $library->branchcode,
1644             categorycode => undef,
1645             itemtype     => undef,
1646             rules        => {
1647                 reservesallowed  => 2,
1648             }
1649         }
1650     );
1651     is_deeply(
1652         CanItemBeReserved( $patron, $item, $library->branchcode ),
1653         { status => 'OK' },
1654         'Patron of specified category can place 1 hold on specified itemtype if library rule for all types and categories set to 2'
1655     );
1656
1657     $schema->storage->txn_rollback;
1658
1659 };
1660
1661 subtest 'ModReserve can only update expirationdate for found holds' => sub {
1662     plan tests => 2;
1663
1664     $schema->storage->txn_begin;
1665
1666     my $category = $builder->build({ source => 'Category' });
1667     my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
1668     my $biblio = $builder->build_sample_biblio( { itemtype => 'DUMMY' } );
1669     my $itemnumber = $builder->build_sample_item(
1670         { library => $branch, biblionumber => $biblio->biblionumber } )
1671       ->itemnumber;
1672
1673     my $borrowernumber = Koha::Patron->new(
1674         {
1675             firstname    => 'my firstname',
1676             surname      => 'whatever surname',
1677             categorycode => $category->{categorycode},
1678             branchcode   => $branch,
1679         }
1680     )->store->borrowernumber;
1681
1682     my $reserve_id = AddReserve(
1683         {
1684             branchcode     => $branch,
1685             borrowernumber => $borrowernumber,
1686             biblionumber   => $biblio->biblionumber,
1687             priority       =>
1688               C4::Reserves::CalculatePriority( $biblio->biblionumber ),
1689             itemnumber => $itemnumber,
1690         }
1691     );
1692
1693     my $hold = Koha::Holds->find($reserve_id);
1694
1695     $hold->set( { priority => 0, found => 'W' } )->store();
1696
1697     ModReserve(
1698         {
1699             reserve_id     => $hold->id,
1700             expirationdate => '1981-06-10',
1701             priority       => 99,
1702             rank           => 0,
1703         }
1704     );
1705
1706     $hold = Koha::Holds->find($reserve_id);
1707
1708     is( $hold->expirationdate, '1981-06-10',
1709         'Found hold expiration date updated correctly' );
1710     is( $hold->priority, '0', 'Found hold priority was not updated' );
1711
1712     $schema->storage->txn_rollback;
1713
1714 };
1715
1716 subtest 'Koha::Holds->get_items_that_can_fill returns items with datecancelled or (inclusive) datearrived' => sub {
1717     plan tests => 8;
1718
1719     $schema->storage->txn_begin;
1720
1721     # biblio item with date arrived and date cancelled
1722     my $biblio1 = $builder->build_sample_biblio();
1723     my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1724
1725     my $transfer1 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1726         datecancelled => '2022-06-12',
1727         itemnumber => $item1->itemnumber
1728     }});
1729
1730     my $hold1 = $builder->build_object({ class => 'Koha::Holds', value => {
1731         biblionumber => $biblio1->biblionumber,
1732         itemnumber => undef,
1733         itemtype => undef,
1734         found => undef
1735     }});
1736
1737     # biblio item with date arrived and NO date cancelled
1738     my $biblio2 = $builder->build_sample_biblio();
1739     my $item2 = $builder->build_sample_item({ biblionumber => $biblio2->biblionumber });
1740
1741     my $transfer2 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1742         datecancelled => undef,
1743         itemnumber => $item2->itemnumber
1744     }});
1745
1746     my $hold2 = $builder->build_object({ class => 'Koha::Holds', value => {
1747         biblionumber => $biblio2->biblionumber,
1748         itemnumber => undef,
1749         itemtype => undef,
1750         found => undef
1751     }});
1752
1753     # biblio item with NO date arrived and date cancelled
1754     my $biblio3 = $builder->build_sample_biblio();
1755     my $item3 = $builder->build_sample_item({ biblionumber => $biblio3->biblionumber });
1756
1757     my $transfer3 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1758         datecancelled => '2022-06-12',
1759         itemnumber => $item3->itemnumber,
1760         datearrived => undef
1761     }});
1762
1763     my $hold3 = $builder->build_object({ class => 'Koha::Holds', value => {
1764         biblionumber => $biblio3->biblionumber,
1765         itemnumber => undef,
1766         itemtype => undef,
1767         found => undef
1768     }});
1769
1770
1771     # biblio item with NO date arrived and NO date cancelled
1772     my $biblio4 = $builder->build_sample_biblio();
1773     my $item4 = $builder->build_sample_item({ biblionumber => $biblio4->biblionumber });
1774
1775     my $transfer4 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
1776         datecancelled => undef,
1777         itemnumber => $item4->itemnumber,
1778         datearrived => undef
1779     }});
1780
1781     my $hold4 = $builder->build_object({ class => 'Koha::Holds', value => {
1782         biblionumber => $biblio4->biblionumber,
1783         itemnumber => undef,
1784         itemtype => undef,
1785         found => undef
1786     }});
1787
1788     # create the holds which get_items_that_can_fill will be ran on
1789     my $holds1 = Koha::Holds->search({reserve_id => $hold1->id});
1790     my $holds2 = Koha::Holds->search({reserve_id => $hold2->id});
1791     my $holds3 = Koha::Holds->search({reserve_id => $hold3->id});
1792     my $holds4 = Koha::Holds->search({reserve_id => $hold4->id});
1793
1794     my $items_that_can_fill1 = $holds1->get_items_that_can_fill;
1795     my $items_that_can_fill2 = $holds2->get_items_that_can_fill;
1796     my $items_that_can_fill3 = $holds3->get_items_that_can_fill;
1797     my $items_that_can_fill4 = $holds4->get_items_that_can_fill;
1798
1799     is($items_that_can_fill1->next->id, $item1->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and datecancelled");
1800     is($items_that_can_fill1->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1801     is($items_that_can_fill2->next->id, $item2->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and undefined datecancelled");
1802     is($items_that_can_fill2->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1803     is($items_that_can_fill3->next->id, $item3->id, "Koha::Holds->get_items_that_can_fill returns item with undefined datearrived and defined datecancelled");
1804     is($items_that_can_fill3->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
1805     is($items_that_can_fill4->next, undef, "Koha::Holds->get_items_that_can_fill doesn't return item with undefined datearrived and undefined datecancelled");
1806     is($items_that_can_fill4->count, 0, "Koha::Holds->get_items_that_can_fill returns 0 item");
1807
1808     $schema->storage->txn_rollback;
1809 };