6 use t
::lib
::TestBuilder
;
10 use Test
::More tests
=> 65;
19 use Koha
::CirculationRules
;
21 use Koha
::DateUtils
qw( dt_from_string output_pref );
23 use Koha
::Item
::Transfer
::Limits
;
26 use Koha
::Library
::Groups
;
31 use lib
$FindBin::Bin
;
34 my $schema = Koha
::Database
->new->schema;
35 $schema->storage->txn_begin;
37 my $builder = t
::lib
::TestBuilder
->new();
38 my $dbh = C4
::Context
->dbh;
40 # Create two random branches
41 my $branch_1 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
42 my $branch_2 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
44 my $category = $builder->build({ source
=> 'Category' });
46 my $borrowers_count = 5;
48 $dbh->do('DELETE FROM itemtypes');
49 $dbh->do('DELETE FROM reserves');
50 $dbh->do('DELETE FROM circulation_rules');
51 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
52 $insert_sth->execute('CAN');
53 $insert_sth->execute('CANNOT');
54 $insert_sth->execute('DUMMY');
55 $insert_sth->execute('ONLY1');
57 # Setup Test------------------------
58 my $biblio = $builder->build_sample_biblio({ itemtype
=> 'DUMMY' });
60 # Create item instance for testing.
61 my $itemnumber = $builder->build_sample_item({ library
=> $branch_1, biblionumber
=> $biblio->biblionumber })->itemnumber;
63 # Create some borrowers
65 foreach (1..$borrowers_count) {
66 my $borrowernumber = Koha
::Patron
->new({
67 firstname
=> 'my firstname',
68 surname
=> 'my surname ' . $_,
69 categorycode
=> $category->{categorycode
},
70 branchcode
=> $branch_1,
71 })->store->borrowernumber;
72 push @borrowernumbers, $borrowernumber;
75 # Create five item level holds
76 foreach my $borrowernumber ( @borrowernumbers ) {
79 branchcode
=> $branch_1,
80 borrowernumber
=> $borrowernumber,
81 biblionumber
=> $biblio->biblionumber,
82 priority
=> C4
::Reserves
::CalculatePriority
( $biblio->biblionumber ),
83 itemnumber
=> $itemnumber,
88 my $holds = $biblio->holds;
89 is
( $holds->count, $borrowers_count, 'Test GetReserves()' );
90 is
( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
91 is
( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
92 is
( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
93 is
( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
94 is
( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
96 my $item = Koha
::Items
->find( $itemnumber );
97 $holds = $item->current_holds;
98 my $first_hold = $holds->next;
99 my $reservedate = $first_hold->reservedate;
100 my $borrowernumber = $first_hold->borrowernumber;
101 my $branch_1code = $first_hold->branchcode;
102 my $reserve_id = $first_hold->reserve_id;
103 is
( $reservedate, output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 }), "holds_placed_today should return a valid reserve date");
104 is
( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
105 is
( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
106 ok
($reserve_id, "Test holds_placed_today()");
108 my $hold = Koha
::Holds
->find( $reserve_id );
109 ok
( $hold, "Koha::Holds found the hold" );
110 my $hold_biblio = $hold->biblio();
111 ok
( $hold_biblio, "Got biblio using biblio() method" );
112 ok
( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
113 my $hold_item = $hold->item();
114 ok
( $hold_item, "Got item using item() method" );
115 ok
( $hold_item == $hold->item(), "item method returns stashed item" );
116 my $hold_branch = $hold->branch();
117 ok
( $hold_branch, "Got branch using branch() method" );
118 ok
( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
119 my $hold_found = $hold->found();
120 $hold->set({ found
=> 'W'})->store();
121 is
( Koha
::Holds
->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
122 is
( Koha
::Holds
->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
124 my $patron = Koha
::Patrons
->find( $borrowernumbers[0] );
125 $holds = $patron->holds;
126 is
( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
129 $holds = $item->current_holds;
130 $first_hold = $holds->next;
131 $borrowernumber = $first_hold->borrowernumber;
132 $branch_1code = $first_hold->branchcode;
133 $reserve_id = $first_hold->reserve_id;
136 reserve_id
=> $reserve_id,
138 branchcode
=> $branch_1,
139 itemnumber
=> $itemnumber,
140 suspend_until
=> output_pref
( { dt
=> dt_from_string
( "2013-01-01", "iso" ), dateonly
=> 1 } ),
143 $hold = Koha
::Holds
->find( $reserve_id );
144 ok
( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
145 ok
( $hold->suspend, "Test ModReserve, suspend hold" );
146 is
( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
148 ModReserve
({ # call without reserve_id
150 biblionumber
=> $biblio->biblionumber,
151 itemnumber
=> $itemnumber,
152 borrowernumber
=> $borrowernumber,
154 $hold = Koha
::Holds
->find( $reserve_id );
155 ok
( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
157 ToggleSuspend
( $reserve_id );
158 $hold = Koha
::Holds
->find( $reserve_id );
159 ok
( ! $hold->suspend, "Test ToggleSuspend(), no date" );
161 ToggleSuspend
( $reserve_id, '2012-01-01' );
162 $hold = Koha
::Holds
->find( $reserve_id );
163 is
( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
165 AutoUnsuspendReserves
();
166 $hold = Koha
::Holds
->find( $reserve_id );
167 ok
( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
170 borrowernumber
=> $borrowernumber,
171 biblionumber
=> $biblio->biblionumber,
173 suspend_until
=> '2012-01-01',
175 $hold = Koha
::Holds
->find( $reserve_id );
176 is
( $hold->suspend, 1, "Test SuspendAll()" );
177 is
( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
180 borrowernumber
=> $borrowernumber,
181 biblionumber
=> $biblio->biblionumber,
184 $hold = Koha
::Holds
->find( $reserve_id );
185 is
( $hold->suspend, 0, "Test resuming with SuspendAll()" );
186 is
( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
188 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
191 branchcode
=> $branch_1,
192 borrowernumber
=> $borrowernumbers[0],
193 biblionumber
=> $biblio->biblionumber,
197 $patron = Koha
::Patrons
->find( $borrowernumber );
198 $holds = $patron->holds;
199 my $reserveid = Koha
::Holds
->search({ biblionumber
=> $biblio->biblionumber, borrowernumber
=> $borrowernumbers[0] })->next->reserve_id;
200 ModReserveMinusPriority
( $itemnumber, $reserveid );
201 $holds = $patron->holds;
202 is
( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
204 $holds = $biblio->holds;
205 $hold = $holds->next;
206 AlterPriority
( 'top', $hold->reserve_id, undef, 2, 1, 6 );
207 $hold = Koha
::Holds
->find( $reserveid );
208 is
( $hold->priority, '1', "Test AlterPriority(), move to top" );
210 AlterPriority
( 'down', $hold->reserve_id, undef, 2, 1, 6 );
211 $hold = Koha
::Holds
->find( $reserveid );
212 is
( $hold->priority, '2', "Test AlterPriority(), move down" );
214 AlterPriority
( 'up', $hold->reserve_id, 1, 3, 1, 6 );
215 $hold = Koha
::Holds
->find( $reserveid );
216 is
( $hold->priority, '1', "Test AlterPriority(), move up" );
218 AlterPriority
( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
219 $hold = Koha
::Holds
->find( $reserveid );
220 is
( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
222 # Regression test for bug 2394
224 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
225 # a patron is not permittedo to request an item whose homebranch (i.e.,
226 # owner of the item) is different from the patron's own library.
227 # However, if canreservefromotherbranches is turned ON, the patron can
228 # create such hold requests.
230 # Note that canreservefromotherbranches has no effect if
231 # IndependentBranches is OFF.
233 my $foreign_biblio = $builder->build_sample_biblio({ itemtype
=> 'DUMMY' });
234 my $foreign_itemnumber = $builder->build_sample_item({ library
=> $branch_2, biblionumber
=> $foreign_biblio->biblionumber })->itemnumber;
235 Koha
::CirculationRules
->set_rules(
237 categorycode
=> undef,
241 reservesallowed
=> 25,
242 holds_per_record
=> 99,
246 Koha
::CirculationRules
->set_rules(
248 categorycode
=> undef,
250 itemtype
=> 'CANNOT',
252 reservesallowed
=> 0,
253 holds_per_record
=> 99,
258 # make sure some basic sysprefs are set
259 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'ItemHomeLibrary');
260 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
262 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
263 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
266 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber)->{status
}, 'OK',
267 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
270 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
271 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 1);
272 t
::lib
::Mocks
::mock_preference
('canreservefromotherbranches', 0);
274 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber)->{status
} eq 'cannotReserveFromOtherBranches',
275 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
278 # ... unless canreservefromotherbranches is ON
279 t
::lib
::Mocks
::mock_preference
('canreservefromotherbranches', 1);
281 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber)->{status
} eq 'OK',
282 '... unless canreservefromotherbranches is ON (bug 2394)'
286 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
287 $biblio = $builder->build_sample_biblio({ itemtype
=> 'DUMMY' });
288 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, biblionumber
=> $biblio->biblionumber })->itemnumber;
289 my $reserveid1 = AddReserve
(
291 branchcode
=> $branch_1,
292 borrowernumber
=> $borrowernumbers[0],
293 biblionumber
=> $biblio->biblionumber,
298 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, biblionumber
=> $biblio->biblionumber })->itemnumber;
299 my $reserveid2 = AddReserve
(
301 branchcode
=> $branch_1,
302 borrowernumber
=> $borrowernumbers[1],
303 biblionumber
=> $biblio->biblionumber,
308 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, biblionumber
=> $biblio->biblionumber })->itemnumber;
309 my $reserveid3 = AddReserve
(
311 branchcode
=> $branch_1,
312 borrowernumber
=> $borrowernumbers[2],
313 biblionumber
=> $biblio->biblionumber,
318 my $hhh = Koha
::Holds
->search({ biblionumber
=> $biblio->biblionumber });
319 my $hold3 = Koha
::Holds
->find( $reserveid3 );
320 is
( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
321 ModReserve
({ reserve_id
=> $reserveid1, rank
=> 'del' });
322 ModReserve
({ reserve_id
=> $reserveid2, rank
=> 'del' });
323 is
( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
326 Koha
::Items
->find($itemnumber)->damaged(1)->store; # FIXME The $itemnumber is a bit confusing here
327 t
::lib
::Mocks
::mock_preference
( 'AllowHoldsOnDamagedItems', 1 );
328 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber)->{status
}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
329 ok
( defined( ( CheckReserves
($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
331 $hold = Koha
::Hold
->new(
333 borrowernumber
=> $borrowernumbers[0],
334 itemnumber
=> $itemnumber,
335 biblionumber
=> $biblio->biblionumber,
338 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber )->{status
},
340 "Patron cannot place a second item level hold for a given item" );
343 t
::lib
::Mocks
::mock_preference
( 'AllowHoldsOnDamagedItems', 0 );
344 ok
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber)->{status
} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
345 ok
( !defined( ( CheckReserves
($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
347 # Items that are not for loan, but holdable should not be trapped until they are available for loan
348 t
::lib
::Mocks
::mock_preference
( 'TrapHoldsOnOrder', 0 );
349 Koha
::Items
->find($itemnumber)->damaged(0)->notforloan(-1)->store;
350 Koha
::Holds
->search({ biblionumber
=> $biblio->id })->delete();
351 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber)->{status
}, 'OK', "Patron can place hold on item that is not for loan but holdable ( notforloan < 0 )" );
352 $hold = Koha
::Hold
->new(
354 borrowernumber
=> $borrowernumbers[0],
355 itemnumber
=> $itemnumber,
356 biblionumber
=> $biblio->biblionumber,
359 reservedate
=> dt_from_string
,
360 branchcode
=> $branch_1,
363 ok
( !defined( ( CheckReserves
($itemnumber) )[1] ), "Hold cannot be trapped for item that is not for loan but holdable ( notforloan < 0 )" );
364 t
::lib
::Mocks
::mock_preference
( 'TrapHoldsOnOrder', 1 );
365 ok
( defined( ( CheckReserves
($itemnumber) )[1] ), "Hold is trapped for item that is not for loan but holdable ( notforloan < 0 )" );
366 t
::lib
::Mocks
::mock_preference
( 'SkipHoldTrapOnNotForLoanValue', '-1' );
367 ok
( !defined( ( CheckReserves
($itemnumber) )[1] ), "Hold cannot be trapped for item with notforloan value matching SkipHoldTrapOnNotForLoanValue" );
370 # Regression test for bug 9532
371 $biblio = $builder->build_sample_biblio({ itemtype
=> 'CANNOT' });
372 $item = $builder->build_sample_item({ library
=> $branch_1, itype
=> 'CANNOT', biblionumber
=> $biblio->biblionumber});
375 branchcode
=> $branch_1,
376 borrowernumber
=> $borrowernumbers[0],
377 biblionumber
=> $biblio->biblionumber,
382 CanItemBeReserved
( $borrowernumbers[0], $item->itemnumber)->{status
}, 'tooManyReserves',
383 "cannot request item if policy that matches on item-level item type forbids it"
386 $item->itype('CAN')->store;
388 CanItemBeReserved
( $borrowernumbers[0], $item->itemnumber)->{status
} eq 'OK',
389 "can request item if policy that matches on item type allows it"
392 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0);
393 $item->itype(undef)->store;
395 CanItemBeReserved
( $borrowernumbers[0], $item->itemnumber)->{status
} eq 'tooManyReserves',
396 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
400 # Test branch item rules
402 $dbh->do('DELETE FROM circulation_rules');
403 Koha
::CirculationRules
->set_rules(
405 categorycode
=> undef,
409 reservesallowed
=> 25,
410 holds_per_record
=> 99,
414 Koha
::CirculationRules
->set_rules(
416 branchcode
=> $branch_1,
417 itemtype
=> 'CANNOT',
420 returnbranch
=> 'homebranch',
424 Koha
::CirculationRules
->set_rules(
426 branchcode
=> $branch_1,
430 returnbranch
=> 'homebranch',
434 $biblio = $builder->build_sample_biblio({ itemtype
=> 'CANNOT' });
435 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, itype
=> 'CANNOT', biblionumber
=> $biblio->biblionumber})->itemnumber;
436 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber)->{status
}, 'notReservable',
437 "CanItemBeReserved should return 'notReservable'");
439 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
440 $itemnumber = $builder->build_sample_item({ library
=> $branch_2, itype
=> 'CAN', biblionumber
=> $biblio->biblionumber})->itemnumber;
441 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber)->{status
},
442 'cannotReserveFromOtherBranches',
443 "CanItemBeReserved should use PatronLibrary rule when ReservesControlBranch set to 'PatronLibrary'");
444 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'ItemHomeLibrary' );
445 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber)->{status
},
447 "CanItemBeReserved should use item home library rule when ReservesControlBranch set to 'ItemsHomeLibrary'");
449 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, itype
=> 'CAN', biblionumber
=> $biblio->biblionumber})->itemnumber;
450 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber)->{status
}, 'OK',
451 "CanItemBeReserved should return 'OK'");
454 t
::lib
::Mocks
::mock_preference
( 'item-level_itypes', 1 );
455 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
457 $dbh->do('DELETE FROM reserves');
458 $dbh->do('DELETE FROM issues');
459 $dbh->do('DELETE FROM items');
460 $dbh->do('DELETE FROM biblio');
462 $biblio = $builder->build_sample_biblio({ itemtype
=> 'ONLY1' });
463 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, biblionumber
=> $biblio->biblionumber})->itemnumber;
465 Koha
::CirculationRules
->set_rules(
467 categorycode
=> undef,
471 reservesallowed
=> 1,
472 holds_per_record
=> 99,
476 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber )->{status
},
477 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
479 my $res_id = AddReserve
(
481 branchcode
=> $branch_1,
482 borrowernumber
=> $borrowernumbers[0],
483 biblionumber
=> $biblio->biblionumber,
488 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber )->{status
},
489 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
491 #results should be the same for both ReservesControlBranch settings
492 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'ItemHomeLibrary' );
493 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber )->{status
},
494 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
495 #reset for further tests
496 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
498 subtest
'Test max_holds per library/patron category' => sub {
501 $dbh->do('DELETE FROM reserves');
503 $biblio = $builder->build_sample_biblio;
504 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, biblionumber
=> $biblio->biblionumber})->itemnumber;
505 Koha
::CirculationRules
->set_rules(
507 categorycode
=> undef,
509 itemtype
=> $biblio->itemtype,
511 reservesallowed
=> 99,
512 holds_per_record
=> 99,
520 branchcode
=> $branch_1,
521 borrowernumber
=> $borrowernumbers[0],
522 biblionumber
=> $biblio->biblionumber,
529 Koha
::Holds
->search( { borrowernumber
=> $borrowernumbers[0] } )->count();
530 is
( $count, 3, 'Patron now has 3 holds' );
532 my $ret = CanItemBeReserved
( $borrowernumbers[0], $itemnumber );
533 is
( $ret->{status
}, 'OK', 'Patron can place hold with no borrower circ rules' );
535 my $rule_all = Koha
::CirculationRules
->set_rule(
537 categorycode
=> $category->{categorycode
},
539 rule_name
=> 'max_holds',
544 my $rule_branch = Koha
::CirculationRules
->set_rule(
546 branchcode
=> $branch_1,
547 categorycode
=> $category->{categorycode
},
548 rule_name
=> 'max_holds',
553 $ret = CanItemBeReserved
( $borrowernumbers[0], $itemnumber );
554 is
( $ret->{status
}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
556 $rule_branch->delete();
558 $ret = CanItemBeReserved
( $borrowernumbers[0], $itemnumber );
559 is
( $ret->{status
}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
562 $rule_branch->rule_value(3);
563 $rule_branch->store();
565 $ret = CanItemBeReserved
( $borrowernumbers[0], $itemnumber );
566 is
( $ret->{status
}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
568 $rule_branch->rule_value(5);
569 $rule_branch->update();
570 $rule_branch->rule_value(5);
571 $rule_branch->store();
573 $ret = CanItemBeReserved
( $borrowernumbers[0], $itemnumber );
574 is
( $ret->{status
}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
577 subtest
'Pickup location availability tests' => sub {
580 $biblio = $builder->build_sample_biblio({ itemtype
=> 'ONLY1' });
581 $itemnumber = $builder->build_sample_item({ library
=> $branch_1, biblionumber
=> $biblio->biblionumber})->itemnumber;
582 #Add a default rule to allow some holds
584 Koha
::CirculationRules
->set_rules(
587 categorycode
=> undef,
590 reservesallowed
=> 25,
591 holds_per_record
=> 99,
595 my $item = Koha
::Items
->find($itemnumber);
596 my $branch_to = $builder->build({ source
=> 'Branch' })->{ branchcode
};
597 my $library = Koha
::Libraries
->find($branch_to);
598 $library->pickup_location('1')->store;
599 my $patron = $builder->build({ source
=> 'Borrower' })->{ borrowernumber
};
601 t
::lib
::Mocks
::mock_preference
('UseBranchTransferLimits', 1);
602 t
::lib
::Mocks
::mock_preference
('BranchTransferLimitsType', 'itemtype');
604 $library->pickup_location('1')->store;
605 is
(CanItemBeReserved
($patron, $item->itemnumber, $branch_to)->{status
},
606 'OK', 'Library is a pickup location');
608 my $limit = Koha
::Item
::Transfer
::Limit
->new({
609 fromBranch
=> $item->holdingbranch,
610 toBranch
=> $branch_to,
611 itemtype
=> $item->effective_itemtype,
613 is
(CanItemBeReserved
($patron, $item->itemnumber, $branch_to)->{status
},
614 'cannotBeTransferred', 'Item cannot be transferred');
617 $library->pickup_location('0')->store;
618 is
(CanItemBeReserved
($patron, $item->itemnumber, $branch_to)->{status
},
619 'libraryNotPickupLocation', 'Library is not a pickup location');
620 is
(CanItemBeReserved
($patron, $item->itemnumber, 'nonexistent')->{status
},
621 'libraryNotFound', 'Cannot set unknown library as pickup location');
624 $schema->storage->txn_rollback;
626 subtest
'CanItemBeReserved / holds_per_day tests' => sub {
630 $schema->storage->txn_begin;
632 Koha
::Holds
->search->delete;
633 $dbh->do('DELETE FROM issues');
634 Koha
::Items
->search->delete;
635 Koha
::Biblios
->search->delete;
637 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
638 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
639 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
641 # Create 3 biblios with items
642 my $biblio_1 = $builder->build_sample_biblio({ itemtype
=> $itemtype->itemtype });
643 my $itemnumber_1 = $builder->build_sample_item({ library
=> $library->branchcode, biblionumber
=> $biblio_1->biblionumber})->itemnumber;
644 my $biblio_2 = $builder->build_sample_biblio({ itemtype
=> $itemtype->itemtype });
645 my $itemnumber_2 = $builder->build_sample_item({ library
=> $library->branchcode, biblionumber
=> $biblio_2->biblionumber})->itemnumber;
646 my $biblio_3 = $builder->build_sample_biblio({ itemtype
=> $itemtype->itemtype });
647 my $itemnumber_3 = $builder->build_sample_item({ library
=> $library->branchcode, biblionumber
=> $biblio_3->biblionumber})->itemnumber;
649 Koha
::CirculationRules
->search->delete;
650 Koha
::CirculationRules
->set_rules(
654 itemtype
=> $itemtype->itemtype,
656 reservesallowed
=> 1,
657 holds_per_record
=> 99,
664 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_1 ),
666 'Patron can reserve item with hold limit of 1, no holds placed'
671 branchcode
=> $library->branchcode,
672 borrowernumber
=> $patron->borrowernumber,
673 biblionumber
=> $biblio_1->biblionumber,
679 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_1 ),
680 { status
=> 'tooManyReserves', limit
=> 1 },
681 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
684 # Raise reservesallowed to avoid tooManyReserves from it
685 Koha
::CirculationRules
->set_rule(
690 itemtype
=> $itemtype->itemtype,
691 rule_name
=> 'reservesallowed',
697 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_2 ),
699 'Patron can reserve item with 2 reserves daily cap'
702 # Add a second reserve
703 my $res_id = AddReserve
(
705 branchcode
=> $library->branchcode,
706 borrowernumber
=> $patron->borrowernumber,
707 biblionumber
=> $biblio_2->biblionumber,
712 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_2 ),
713 { status
=> 'tooManyReservesToday', limit
=> 2 },
714 'Patron cannot a third item with 2 reserves daily cap'
717 # Update last hold so reservedate is in the past, so 2 holds, but different day
718 $hold = Koha
::Holds
->find($res_id);
719 my $yesterday = dt_from_string
() - DateTime
::Duration
->new( days
=> 1 );
720 $hold->reservedate($yesterday)->store;
723 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_2 ),
725 'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
728 # Set holds_per_day to 0
729 Koha
::CirculationRules
->set_rule(
734 itemtype
=> $itemtype->itemtype,
735 rule_name
=> 'holds_per_day',
741 # Delete existing holds
742 Koha
::Holds
->search->delete;
744 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_2 ),
745 { status
=> 'tooManyReservesToday', limit
=> 0 },
746 'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
749 Koha
::CirculationRules
->set_rule(
754 itemtype
=> $itemtype->itemtype,
755 rule_name
=> 'holds_per_day',
760 Koha
::Holds
->search->delete;
762 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_2 ),
764 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
768 branchcode
=> $library->branchcode,
769 borrowernumber
=> $patron->borrowernumber,
770 biblionumber
=> $biblio_1->biblionumber,
776 branchcode
=> $library->branchcode,
777 borrowernumber
=> $patron->borrowernumber,
778 biblionumber
=> $biblio_2->biblionumber,
784 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_3 ),
786 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
790 branchcode
=> $library->branchcode,
791 borrowernumber
=> $patron->borrowernumber,
792 biblionumber
=> $biblio_3->biblionumber,
797 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_3 ),
798 { status
=> 'tooManyReserves', limit
=> 3 },
799 'Unlimited daily holds, but reached reservesallowed'
801 #results should be the same for both ReservesControlBranch settings
802 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'ItemHomeLibrary');
804 CanItemBeReserved
( $patron->borrowernumber, $itemnumber_3 ),
805 { status
=> 'tooManyReserves', limit
=> 3 },
806 'Unlimited daily holds, but reached reservesallowed'
809 $schema->storage->txn_rollback;
812 subtest
'CanItemBeReserved / branch_not_in_hold_group' => sub {
815 $schema->storage->txn_begin;
818 Koha
::Holds
->search->delete;
819 $dbh->do('DELETE FROM issues');
820 Koha
::CirculationRules
->set_rule(
823 categorycode
=> undef,
825 rule_name
=> 'reservesallowed',
830 Koha
::Items
->search->delete;
831 Koha
::Biblios
->search->delete;
834 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
835 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
838 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
839 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
840 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
842 # Create library groups hierarchy
843 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value
=> {ft_local_hold_group
=> 1} } );
844 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value
=> {parent_id
=> $rootgroup->id, branchcode
=> $library1->branchcode}} );
845 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value
=> {parent_id
=> $rootgroup->id, branchcode
=> $library2->branchcode} } );
848 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value
=> {branchcode
=> $library1->branchcode} } );
849 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value
=> {branchcode
=> $library3->branchcode} } );
851 # Create 3 biblios with items
852 my $biblio_1 = $builder->build_sample_biblio({ itemtype
=> $itemtype1->itemtype });
853 my $item_1 = $builder->build_sample_item(
855 biblionumber
=> $biblio_1->biblionumber,
856 library
=> $library1->branchcode
859 my $biblio_2 = $builder->build_sample_biblio({ itemtype
=> $itemtype2->itemtype });
860 my $item_2 = $builder->build_sample_item(
862 biblionumber
=> $biblio_2->biblionumber,
863 library
=> $library2->branchcode
866 my $itemnumber_2 = $item_2->itemnumber;
867 my $biblio_3 = $builder->build_sample_biblio({ itemtype
=> $itemtype1->itemtype });
868 my $item_3 = $builder->build_sample_item(
870 biblionumber
=> $biblio_3->biblionumber,
871 library
=> $library1->branchcode
875 # Test 1: Patron 3 can place hold
877 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
879 'Patron can place hold if no circ_rules where defined'
882 # Insert default circ rule of holds allowed only from local hold group for all libraries
883 Koha
::CirculationRules
->set_rules(
889 hold_fulfillment_policy
=> 'any',
890 returnbranch
=> 'any'
895 # Test 2: Patron 1 can place hold
897 CanItemBeReserved
( $patron1->borrowernumber, $itemnumber_2 ),
899 'Patron can place hold because patron\'s home library is part of hold group'
902 # Test 3: Patron 3 cannot place hold
904 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
905 { status
=> 'branchNotInHoldGroup' },
906 'Patron cannot place hold because patron\'s home library is not part of hold group'
909 # Insert default circ rule to "any" for library 2
910 Koha
::CirculationRules
->set_rules(
912 branchcode
=> $library2->branchcode,
916 hold_fulfillment_policy
=> 'any',
917 returnbranch
=> 'any'
922 # Test 4: Patron 3 can place hold
924 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
926 'Patron can place hold if holdallowed is set to "any" for library 2'
929 # Update default circ rule to "hold group" for library 2
930 Koha
::CirculationRules
->set_rules(
932 branchcode
=> $library2->branchcode,
936 hold_fulfillment_policy
=> 'any',
937 returnbranch
=> 'any'
942 # Test 5: Patron 3 cannot place hold
944 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
945 { status
=> 'branchNotInHoldGroup' },
946 'Patron cannot place hold if holdallowed is set to "hold group" for library 2'
949 # Insert default item rule to "any" for itemtype 2
950 Koha
::CirculationRules
->set_rules(
952 branchcode
=> $library2->branchcode,
953 itemtype
=> $itemtype2->itemtype,
956 hold_fulfillment_policy
=> 'any',
957 returnbranch
=> 'any'
962 # Test 6: Patron 3 can place hold
964 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
966 'Patron can place hold if holdallowed is set to "any" for itemtype 2'
969 # Update default item rule to "hold group" for itemtype 2
970 Koha
::CirculationRules
->set_rules(
972 branchcode
=> $library2->branchcode,
973 itemtype
=> $itemtype2->itemtype,
976 hold_fulfillment_policy
=> 'any',
977 returnbranch
=> 'any'
982 # Test 7: Patron 3 cannot place hold
984 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
985 { status
=> 'branchNotInHoldGroup' },
986 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2'
989 # Insert branch item rule to "any" for itemtype 2 and library 2
990 Koha
::CirculationRules
->set_rules(
992 branchcode
=> $library2->branchcode,
993 itemtype
=> $itemtype2->itemtype,
996 hold_fulfillment_policy
=> 'any',
997 returnbranch
=> 'any'
1002 # Test 8: Patron 3 can place hold
1004 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
1006 'Patron can place hold if holdallowed is set to "any" for itemtype 2 and library 2'
1009 # Update branch item rule to "hold group" for itemtype 2 and library 2
1010 Koha
::CirculationRules
->set_rules(
1012 branchcode
=> $library2->branchcode,
1013 itemtype
=> $itemtype2->itemtype,
1016 hold_fulfillment_policy
=> 'any',
1017 returnbranch
=> 'any'
1022 # Test 9: Patron 3 cannot place hold
1024 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2 ),
1025 { status
=> 'branchNotInHoldGroup' },
1026 'Patron cannot place hold if holdallowed is set to "hold group" for itemtype 2 and library 2'
1029 $schema->storage->txn_rollback;
1033 subtest
'CanItemBeReserved / pickup_not_in_hold_group' => sub {
1036 $schema->storage->txn_begin;
1039 Koha
::Holds
->search->delete;
1040 $dbh->do('DELETE FROM issues');
1041 Koha
::CirculationRules
->set_rule(
1043 branchcode
=> undef,
1044 categorycode
=> undef,
1046 rule_name
=> 'reservesallowed',
1051 Koha
::Items
->search->delete;
1052 Koha
::Biblios
->search->delete;
1055 my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1056 my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } );
1059 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value
=> {pickup_location
=> 1} } );
1060 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value
=> {pickup_location
=> 1} } );
1061 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value
=> {pickup_location
=> 1} } );
1063 # Create library groups hierarchy
1064 my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value
=> {ft_local_hold_group
=> 1} } );
1065 my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value
=> {parent_id
=> $rootgroup->id, branchcode
=> $library1->branchcode}} );
1066 my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value
=> {parent_id
=> $rootgroup->id, branchcode
=> $library2->branchcode} } );
1069 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value
=> {branchcode
=> $library1->branchcode} } );
1070 my $patron3 = $builder->build_object( { class => 'Koha::Patrons', value
=> {branchcode
=> $library3->branchcode} } );
1072 # Create 3 biblios with items
1073 my $biblio_1 = $builder->build_sample_biblio({ itemtype
=> $itemtype1->itemtype });
1074 my $item_1 = $builder->build_sample_item(
1076 biblionumber
=> $biblio_1->biblionumber,
1077 library
=> $library1->branchcode
1080 my $biblio_2 = $builder->build_sample_biblio({ itemtype
=> $itemtype2->itemtype });
1081 my $item_2 = $builder->build_sample_item(
1083 biblionumber
=> $biblio_2->biblionumber,
1084 library
=> $library2->branchcode
1087 my $itemnumber_2 = $item_2->itemnumber;
1088 my $biblio_3 = $builder->build_sample_biblio({ itemtype
=> $itemtype1->itemtype });
1089 my $item_3 = $builder->build_sample_item(
1091 biblionumber
=> $biblio_3->biblionumber,
1092 library
=> $library1->branchcode
1096 # Test 1: Patron 3 can place hold
1098 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1100 'Patron can place hold if no circ_rules where defined'
1103 # Insert default circ rule of holds allowed only from local hold group for all libraries
1104 Koha
::CirculationRules
->set_rules(
1106 branchcode
=> undef,
1110 hold_fulfillment_policy
=> 'holdgroup',
1111 returnbranch
=> 'any'
1116 # Test 2: Patron 1 can place hold
1118 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library1->branchcode ),
1120 'Patron can place hold because pickup location is part of hold group'
1123 # Test 3: Patron 3 cannot place hold
1125 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1126 { status
=> 'pickupNotInHoldGroup' },
1127 'Patron cannot place hold because pickup location is not part of hold group'
1130 # Insert default circ rule to "any" for library 2
1131 Koha
::CirculationRules
->set_rules(
1133 branchcode
=> $library2->branchcode,
1137 hold_fulfillment_policy
=> 'any',
1138 returnbranch
=> 'any'
1143 # Test 4: Patron 3 can place hold
1145 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1147 'Patron can place hold if default_branch_circ_rules is set to "any" for library 2'
1150 # Update default circ rule to "hold group" for library 2
1151 Koha
::CirculationRules
->set_rules(
1153 branchcode
=> $library2->branchcode,
1157 hold_fulfillment_policy
=> 'holdgroup',
1158 returnbranch
=> 'any'
1163 # Test 5: Patron 3 cannot place hold
1165 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1166 { status
=> 'pickupNotInHoldGroup' },
1167 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for library 2'
1170 # Insert default item rule to "any" for itemtype 2
1171 Koha
::CirculationRules
->set_rules(
1173 branchcode
=> $library2->branchcode,
1174 itemtype
=> $itemtype2->itemtype,
1177 hold_fulfillment_policy
=> 'any',
1178 returnbranch
=> 'any'
1183 # Test 6: Patron 3 can place hold
1185 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1187 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2'
1190 # Update default item rule to "hold group" for itemtype 2
1191 Koha
::CirculationRules
->set_rules(
1193 branchcode
=> $library2->branchcode,
1194 itemtype
=> $itemtype2->itemtype,
1197 hold_fulfillment_policy
=> 'holdgroup',
1198 returnbranch
=> 'any'
1203 # Test 7: Patron 3 cannot place hold
1205 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1206 { status
=> 'pickupNotInHoldGroup' },
1207 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2'
1210 # Insert branch item rule to "any" for itemtype 2 and library 2
1211 Koha
::CirculationRules
->set_rules(
1213 branchcode
=> $library2->branchcode,
1214 itemtype
=> $itemtype2->itemtype,
1217 hold_fulfillment_policy
=> 'any',
1218 returnbranch
=> 'any'
1223 # Test 8: Patron 3 can place hold
1225 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1227 'Patron can place hold if hold_fulfillment_policy is set to "any" for itemtype 2 and library 2'
1230 # Update branch item rule to "hold group" for itemtype 2 and library 2
1231 Koha
::CirculationRules
->set_rules(
1233 branchcode
=> $library2->branchcode,
1234 itemtype
=> $itemtype2->itemtype,
1237 hold_fulfillment_policy
=> 'holdgroup',
1238 returnbranch
=> 'any'
1243 # Test 9: Patron 3 cannot place hold
1245 CanItemBeReserved
( $patron3->borrowernumber, $itemnumber_2, $library3->branchcode ),
1246 { status
=> 'pickupNotInHoldGroup' },
1247 'Patron cannot place hold if hold_fulfillment_policy is set to "hold group" for itemtype 2 and library 2'
1250 $schema->storage->txn_rollback;