6 use t::lib::TestBuilder;
10 use Test::More tests => 56;
19 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::CirculationRules;
29 use lib $FindBin::Bin;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new();
36 my $dbh = C4::Context->dbh;
38 # Create two random branches
39 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
40 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
42 my $category = $builder->build({ source => 'Category' });
44 my $borrowers_count = 5;
46 $dbh->do('DELETE FROM itemtypes');
47 $dbh->do('DELETE FROM reserves');
48 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
49 $insert_sth->execute('CAN');
50 $insert_sth->execute('CANNOT');
51 $insert_sth->execute('DUMMY');
52 $insert_sth->execute('ONLY1');
54 # Setup Test------------------------
55 # Create a biblio instance for testing
56 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
58 # Create item instance for testing.
59 my ($item_bibnum, $item_bibitemnum, $itemnumber)
60 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
62 # Create some borrowers
64 foreach (1..$borrowers_count) {
65 my $borrowernumber = Koha::Patron->new({
66 firstname => 'my firstname',
67 surname => 'my surname ' . $_,
68 categorycode => $category->{categorycode},
69 branchcode => $branch_1,
70 })->store->borrowernumber;
71 push @borrowernumbers, $borrowernumber;
74 my $biblionumber = $bibnum;
76 # Create five item level holds
77 foreach my $borrowernumber ( @borrowernumbers ) {
83 my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
88 my $checkitem = $itemnumber,
93 my $biblio = Koha::Biblios->find( $biblionumber );
94 my $holds = $biblio->holds;
95 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
96 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
97 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
98 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
99 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
100 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
102 my $item = Koha::Items->find( $itemnumber );
103 $holds = $item->current_holds;
104 my $first_hold = $holds->next;
105 my $reservedate = $first_hold->reservedate;
106 my $borrowernumber = $first_hold->borrowernumber;
107 my $branch_1code = $first_hold->branchcode;
108 my $reserve_id = $first_hold->reserve_id;
109 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
110 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
111 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
112 ok($reserve_id, "Test holds_placed_today()");
114 my $hold = Koha::Holds->find( $reserve_id );
115 ok( $hold, "Koha::Holds found the hold" );
116 my $hold_biblio = $hold->biblio();
117 ok( $hold_biblio, "Got biblio using biblio() method" );
118 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
119 my $hold_item = $hold->item();
120 ok( $hold_item, "Got item using item() method" );
121 ok( $hold_item == $hold->item(), "item method returns stashed item" );
122 my $hold_branch = $hold->branch();
123 ok( $hold_branch, "Got branch using branch() method" );
124 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
125 my $hold_found = $hold->found();
126 $hold->set({ found => 'W'})->store();
127 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
128 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
130 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
131 $holds = $patron->holds;
132 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
135 $holds = $item->current_holds;
136 $first_hold = $holds->next;
137 $borrowernumber = $first_hold->borrowernumber;
138 $branch_1code = $first_hold->branchcode;
139 $reserve_id = $first_hold->reserve_id;
142 reserve_id => $reserve_id,
144 branchcode => $branch_1,
145 itemnumber => $itemnumber,
146 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
149 $hold = Koha::Holds->find( $reserve_id );
150 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
151 ok( $hold->suspend, "Test ModReserve, suspend hold" );
152 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
154 ModReserve({ # call without reserve_id
156 biblionumber => $item_bibnum,
157 itemnumber => $itemnumber,
158 borrowernumber => $borrowernumber,
160 $hold = Koha::Holds->find( $reserve_id );
161 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
163 ToggleSuspend( $reserve_id );
164 $hold = Koha::Holds->find( $reserve_id );
165 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
167 ToggleSuspend( $reserve_id, '2012-01-01' );
168 $hold = Koha::Holds->find( $reserve_id );
169 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
171 AutoUnsuspendReserves();
172 $hold = Koha::Holds->find( $reserve_id );
173 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
176 borrowernumber => $borrowernumber,
177 biblionumber => $biblionumber,
179 suspend_until => '2012-01-01',
181 $hold = Koha::Holds->find( $reserve_id );
182 is( $hold->suspend, 1, "Test SuspendAll()" );
183 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
186 borrowernumber => $borrowernumber,
187 biblionumber => $biblionumber,
190 $hold = Koha::Holds->find( $reserve_id );
191 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
192 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
194 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
208 $patron = Koha::Patrons->find( $borrowernumber );
209 $holds = $patron->holds;
210 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
211 ModReserveMinusPriority( $itemnumber, $reserveid );
212 $holds = $patron->holds;
213 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
215 $holds = $biblio->holds;
216 $hold = $holds->next;
217 AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
218 $hold = Koha::Holds->find( $reserveid );
219 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
221 AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
222 $hold = Koha::Holds->find( $reserveid );
223 is( $hold->priority, '2', "Test AlterPriority(), move down" );
225 AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
226 $hold = Koha::Holds->find( $reserveid );
227 is( $hold->priority, '1', "Test AlterPriority(), move up" );
229 AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
230 $hold = Koha::Holds->find( $reserveid );
231 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
233 # Regression test for bug 2394
235 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
236 # a patron is not permittedo to request an item whose homebranch (i.e.,
237 # owner of the item) is different from the patron's own library.
238 # However, if canreservefromotherbranches is turned ON, the patron can
239 # create such hold requests.
241 # Note that canreservefromotherbranches has no effect if
242 # IndependentBranches is OFF.
244 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
245 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
246 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
247 $dbh->do('DELETE FROM issuingrules');
249 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
250 VALUES (?, ?, ?, ?, ?)},
252 '*', '*', '*', 25, 99
255 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
256 VALUES (?, ?, ?, ?, ?)},
258 '*', '*', 'CANNOT', 0, 99
261 # make sure some basic sysprefs are set
262 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
263 t::lib::Mocks::mock_preference('item-level_itypes', 1);
265 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
266 t::lib::Mocks::mock_preference('IndependentBranches', 0);
268 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
269 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
272 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
273 t::lib::Mocks::mock_preference('IndependentBranches', 1);
274 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
276 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
277 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
280 # ... unless canreservefromotherbranches is ON
281 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
283 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
284 '... unless canreservefromotherbranches is ON (bug 2394)'
288 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
289 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
290 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
291 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
292 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
293 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
294 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
295 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
296 my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
297 my $hold3 = Koha::Holds->find( $reserveid3 );
298 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
299 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
300 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
301 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
304 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
305 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
306 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
307 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
309 $hold = Koha::Hold->new(
311 borrowernumber => $borrowernumbers[0],
312 itemnumber => $itemnumber,
313 biblionumber => $item_bibnum,
316 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
318 "Patron cannot place a second item level hold for a given item" );
321 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
322 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
323 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
325 # Regression test for bug 9532
326 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
327 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
336 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
337 "cannot request item if policy that matches on item-level item type forbids it"
339 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
341 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
342 "can request item if policy that matches on item type allows it"
345 t::lib::Mocks::mock_preference('item-level_itypes', 0);
346 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
348 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
349 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
353 # Test branch item rules
355 $dbh->do('DELETE FROM issuingrules');
357 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
358 VALUES (?, ?, ?, ?)},
362 $dbh->do('DELETE FROM branch_item_rules');
363 $dbh->do('DELETE FROM default_branch_circ_rules');
364 $dbh->do('DELETE FROM default_branch_item_rules');
365 $dbh->do('DELETE FROM default_circ_rules');
367 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
369 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
371 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
373 }, {}, $branch_1, 'CAN', 1, 'homebranch');
374 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
375 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
376 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
377 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
378 "CanItemBeReserved should return 'notReservable'");
380 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
381 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
382 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
383 'cannotReserveFromOtherBranches',
384 "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
386 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
387 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
388 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
389 "CanItemBeReserved should return 'OK'");
392 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
393 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
395 $dbh->do('DELETE FROM reserves');
396 $dbh->do('DELETE FROM issues');
397 $dbh->do('DELETE FROM items');
398 $dbh->do('DELETE FROM biblio');
400 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
401 ( $item_bibnum, $item_bibitemnum, $itemnumber )
402 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
405 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
406 VALUES (?, ?, ?, ?, ?)},
408 '*', '*', 'ONLY1', 1, 99
410 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
411 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
413 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
415 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
416 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
418 subtest 'Test max_holds per library/patron category' => sub {
421 $dbh->do('DELETE FROM reserves');
422 $dbh->do('DELETE FROM issuingrules');
423 $dbh->do('DELETE FROM circulation_rules');
425 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('TEST');
426 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
427 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
431 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
432 VALUES (?, ?, ?, ?, ?)
435 '*', '*', 'TEST', 99, 99
437 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
438 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
439 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
442 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
443 is( $count, 3, 'Patron now has 3 holds' );
445 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
446 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
448 my $rule_all = Koha::CirculationRules->set_rule(
450 categorycode => $category->{categorycode},
453 rule_name => 'max_holds',
458 my $rule_branch = Koha::CirculationRules->set_rule(
460 branchcode => $branch_1,
461 categorycode => $category->{categorycode},
463 rule_name => 'max_holds',
468 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
469 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
471 $rule_branch->delete();
473 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
474 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
477 $rule_branch->rule_value(3);
478 $rule_branch->store();
480 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
481 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
483 $rule_branch->rule_value(5);
484 $rule_branch->update();
485 $rule_branch->rule_value(5);
486 $rule_branch->store();
488 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
489 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
492 subtest 'Pickup location availability tests' => sub {
495 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
496 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
497 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
498 #Add a default rule to allow some holds
500 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
501 VALUES (?, ?, ?, ?, ?)},
503 '*', '*', '*', 25, 99
505 my $item = Koha::Items->find($itemnumber);
506 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
507 my $library = Koha::Libraries->find($branch_to);
508 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
510 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
511 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
512 $library->pickup_location('1')->store;
513 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
514 'OK', 'Library is a pickup location');
515 $library->pickup_location('0')->store;
516 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
517 'libraryNotPickupLocation', 'Library is not a pickup location');
518 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
519 'libraryNotFound', 'Cannot set unknown library as pickup location');
522 # Helper method to set up a Biblio.
523 sub create_helper_biblio {
524 my $itemtype = shift;
525 my $bib = MARC::Record->new();
526 my $title = 'Silence in the library';
528 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
529 MARC::Field->new('245', ' ', ' ', a => $title),
530 MARC::Field->new('942', ' ', ' ', c => $itemtype),
532 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');