6 use t::lib::TestBuilder;
10 use Test::More tests => 56;
19 use Koha::DateUtils qw( dt_from_string output_pref );
27 use lib $FindBin::Bin;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new();
34 my $dbh = C4::Context->dbh;
36 # Create two random branches
37 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
38 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
40 my $category = $builder->build({ source => 'Category' });
42 my $borrowers_count = 5;
44 $dbh->do('DELETE FROM itemtypes');
45 $dbh->do('DELETE FROM reserves');
46 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
47 $insert_sth->execute('CAN');
48 $insert_sth->execute('CANNOT');
49 $insert_sth->execute('DUMMY');
50 $insert_sth->execute('ONLY1');
52 # Setup Test------------------------
53 # Create a biblio instance for testing
54 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
56 # Create item instance for testing.
57 my ($item_bibnum, $item_bibitemnum, $itemnumber)
58 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
60 # Create some borrowers
62 foreach (1..$borrowers_count) {
63 my $borrowernumber = Koha::Patron->new({
64 firstname => 'my firstname',
65 surname => 'my surname ' . $_,
66 categorycode => $category->{categorycode},
67 branchcode => $branch_1,
68 })->store->borrowernumber;
69 push @borrowernumbers, $borrowernumber;
72 my $biblionumber = $bibnum;
74 # Create five item level holds
75 foreach my $borrowernumber ( @borrowernumbers ) {
81 my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
86 my $checkitem = $itemnumber,
91 my $biblio = Koha::Biblios->find( $biblionumber );
92 my $holds = $biblio->holds;
93 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
94 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
95 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
96 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
97 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
98 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
100 my $item = Koha::Items->find( $itemnumber );
101 $holds = $item->current_holds;
102 my $first_hold = $holds->next;
103 my $reservedate = $first_hold->reservedate;
104 my $borrowernumber = $first_hold->borrowernumber;
105 my $branch_1code = $first_hold->branchcode;
106 my $reserve_id = $first_hold->reserve_id;
107 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
108 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
109 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
110 ok($reserve_id, "Test holds_placed_today()");
112 my $hold = Koha::Holds->find( $reserve_id );
113 ok( $hold, "Koha::Holds found the hold" );
114 my $hold_biblio = $hold->biblio();
115 ok( $hold_biblio, "Got biblio using biblio() method" );
116 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
117 my $hold_item = $hold->item();
118 ok( $hold_item, "Got item using item() method" );
119 ok( $hold_item == $hold->item(), "item method returns stashed item" );
120 my $hold_branch = $hold->branch();
121 ok( $hold_branch, "Got branch using branch() method" );
122 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
123 my $hold_found = $hold->found();
124 $hold->set({ found => 'W'})->store();
125 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
126 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
128 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
129 $holds = $patron->holds;
130 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
133 $holds = $item->current_holds;
134 $first_hold = $holds->next;
135 $borrowernumber = $first_hold->borrowernumber;
136 $branch_1code = $first_hold->branchcode;
137 $reserve_id = $first_hold->reserve_id;
140 reserve_id => $reserve_id,
142 branchcode => $branch_1,
143 itemnumber => $itemnumber,
144 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
147 $hold = Koha::Holds->find( $reserve_id );
148 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
149 ok( $hold->suspend, "Test ModReserve, suspend hold" );
150 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
152 ModReserve({ # call without reserve_id
154 biblionumber => $item_bibnum,
155 itemnumber => $itemnumber,
156 borrowernumber => $borrowernumber,
158 $hold = Koha::Holds->find( $reserve_id );
159 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
161 ToggleSuspend( $reserve_id );
162 $hold = Koha::Holds->find( $reserve_id );
163 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
165 ToggleSuspend( $reserve_id, '2012-01-01' );
166 $hold = Koha::Holds->find( $reserve_id );
167 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
169 AutoUnsuspendReserves();
170 $hold = Koha::Holds->find( $reserve_id );
171 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
174 borrowernumber => $borrowernumber,
175 biblionumber => $biblionumber,
177 suspend_until => '2012-01-01',
179 $hold = Koha::Holds->find( $reserve_id );
180 is( $hold->suspend, 1, "Test SuspendAll()" );
181 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
184 borrowernumber => $borrowernumber,
185 biblionumber => $biblionumber,
188 $hold = Koha::Holds->find( $reserve_id );
189 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
190 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
192 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
206 $patron = Koha::Patrons->find( $borrowernumber );
207 $holds = $patron->holds;
208 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
209 ModReserveMinusPriority( $itemnumber, $reserveid );
210 $holds = $patron->holds;
211 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
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" );
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" );
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" );
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" );
231 # Regression test for bug 2394
233 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
234 # a patron is not permittedo to request an item whose homebranch (i.e.,
235 # owner of the item) is different from the patron's own library.
236 # However, if canreservefromotherbranches is turned ON, the patron can
237 # create such hold requests.
239 # Note that canreservefromotherbranches has no effect if
240 # IndependentBranches is OFF.
242 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
243 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
244 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
245 $dbh->do('DELETE FROM issuingrules');
247 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
248 VALUES (?, ?, ?, ?, ?)},
250 '*', '*', '*', 25, 99
253 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
254 VALUES (?, ?, ?, ?, ?)},
256 '*', '*', 'CANNOT', 0, 99
259 # make sure some basic sysprefs are set
260 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
261 t::lib::Mocks::mock_preference('item-level_itypes', 1);
263 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
264 t::lib::Mocks::mock_preference('IndependentBranches', 0);
266 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq '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 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
288 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
289 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
290 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
291 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
292 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
293 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
294 my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
295 my $hold3 = Koha::Holds->find( $reserveid3 );
296 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
297 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
298 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
299 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
302 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
303 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
304 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
305 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
307 $hold = Koha::Hold->new(
309 borrowernumber => $borrowernumbers[0],
310 itemnumber => $itemnumber,
311 biblionumber => $item_bibnum,
314 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
316 "Patron cannot place a second item level hold for a given item" );
319 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
320 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
321 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
323 # Regression test for bug 9532
324 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
325 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
334 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
335 "cannot request item if policy that matches on item-level item type forbids it"
337 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
339 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
340 "can request item if policy that matches on item type allows it"
343 t::lib::Mocks::mock_preference('item-level_itypes', 0);
344 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
346 CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
347 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
351 # Test branch item rules
353 $dbh->do('DELETE FROM issuingrules');
355 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
356 VALUES (?, ?, ?, ?)},
360 $dbh->do('DELETE FROM branch_item_rules');
361 $dbh->do('DELETE FROM default_branch_circ_rules');
362 $dbh->do('DELETE FROM default_branch_item_rules');
363 $dbh->do('DELETE FROM default_circ_rules');
365 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
367 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
369 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
371 }, {}, $branch_1, 'CAN', 1, 'homebranch');
372 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
373 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
374 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
375 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
376 "CanItemBeReserved should return 'notReservable'");
378 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
379 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
380 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
381 'cannotReserveFromOtherBranches',
382 "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
384 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
385 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
386 is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
387 "CanItemBeReserved should return 'OK'");
390 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
391 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
393 $dbh->do('DELETE FROM reserves');
394 $dbh->do('DELETE FROM issues');
395 $dbh->do('DELETE FROM items');
396 $dbh->do('DELETE FROM biblio');
398 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
399 ( $item_bibnum, $item_bibitemnum, $itemnumber )
400 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
403 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
404 VALUES (?, ?, ?, ?, ?)},
406 '*', '*', 'ONLY1', 1, 99
408 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
409 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
411 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
413 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
414 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
416 subtest 'Test max_holds per library/patron category' => sub {
419 $dbh->do('DELETE FROM reserves');
420 $dbh->do('DELETE FROM issuingrules');
422 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('TEST');
423 ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
424 AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
428 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
429 VALUES (?, ?, ?, ?, ?)
432 '*', '*', 'TEST', 99, 99
434 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
435 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
436 AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
439 Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
440 is( $count, 3, 'Patron now has 3 holds' );
442 my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
443 is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
445 my $rule_all = $schema->resultset('DefaultBorrowerCircRule')->new(
447 categorycode => $category->{categorycode},
452 my $rule_branch = $schema->resultset('BranchBorrowerCircRule')->new(
454 branchcode => $branch_1,
455 categorycode => $category->{categorycode},
460 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
461 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
463 $rule_branch->delete();
465 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
466 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
469 $rule_branch->max_holds(3);
470 $rule_branch->insert();
472 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
473 is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
475 $rule_branch->max_holds(5);
476 $rule_branch->update();
477 $rule_all->max_holds(5);
480 $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
481 is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
484 subtest 'Pickup location availability tests' => sub {
487 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
488 my ( $item_bibnum, $item_bibitemnum, $itemnumber )
489 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
490 #Add a default rule to allow some holds
492 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
493 VALUES (?, ?, ?, ?, ?)},
495 '*', '*', '*', 25, 99
497 my $item = Koha::Items->find($itemnumber);
498 my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
499 my $library = Koha::Libraries->find($branch_to);
500 my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
502 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
503 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
504 $library->pickup_location('1')->store;
505 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
506 'OK', 'Library is a pickup location');
507 $library->pickup_location('0')->store;
508 is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
509 'libraryNotPickupLocation', 'Library is not a pickup location');
510 is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
511 'libraryNotFound', 'Cannot set unknown library as pickup location');
514 # Helper method to set up a Biblio.
515 sub create_helper_biblio {
516 my $itemtype = shift;
517 my $bib = MARC::Record->new();
518 my $title = 'Silence in the library';
520 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
521 MARC::Field->new('245', ' ', ' ', a => $title),
522 MARC::Field->new('942', ' ', ' ', c => $itemtype),
524 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');