5 use Test::More tests => 47;
8 use DateTime::Duration;
21 use_ok('C4::Reserves');
24 # a very minimal mack of userenv for use by the test of DelItemCheck
25 *C4::Context::userenv = sub {
29 my $dbh = C4::Context->dbh;
32 $dbh->{AutoCommit} = 0;
33 $dbh->{RaiseError} = 1;
35 # Setup Test------------------------
37 # Add branches if not existing
38 foreach my $addbra ('CPL', 'FPL', 'RPL') {
39 $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES (?,?)", undef, ($addbra,"$addbra branch")) unless GetBranchName($addbra);
42 # Add categories if not existing
43 foreach my $addcat ('S', 'PT') {
44 $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
48 diag("\nCreating biblio instance for testing.");
49 my $bib = MARC::Record->new();
50 my $title = 'Silence in the library';
51 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
53 MARC::Field->new('600', '', '1', a => 'Moffat, Steven'),
54 MARC::Field->new('200', '', '', a => $title),
59 MARC::Field->new('100', '', '', a => 'Moffat, Steven'),
60 MARC::Field->new('245', '', '', a => $title),
63 my ($bibnum, $bibitemnum);
64 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
66 # Helper item for that biblio.
67 diag("Creating item instance for testing.");
68 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
70 # Modify item; setting barcode.
71 my $testbarcode = '97531';
72 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
76 firstname => 'my firstname',
77 surname => 'my surname',
81 my $borrowernumber = AddMember(%data);
82 my $borrower = GetMember( borrowernumber => $borrowernumber );
83 my $biblionumber = $bibnum;
84 my $barcode = $testbarcode;
92 my $checkitem = undef;
95 my @branches = GetBranchesLoop();
96 my $branch = $branches[0][0]{value};
98 AddReserve($branch, $borrowernumber, $biblionumber,
99 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
100 $title, $checkitem, $found);
102 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
104 is($status, "Reserved", "CheckReserves Test 1");
106 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
108 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
109 is($status, "Reserved", "CheckReserves Test 2");
111 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
112 is($status, "Reserved", "CheckReserves Test 3");
114 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
115 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
117 'ItemHomeLib' eq GetReservesControlBranch(
118 { homebranch => 'ItemHomeLib' },
119 { branchcode => 'PatronHomeLib' }
120 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
122 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
124 'PatronHomeLib' eq GetReservesControlBranch(
125 { homebranch => 'ItemHomeLib' },
126 { branchcode => 'PatronHomeLib' }
127 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
129 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
132 ### Regression test for bug 10272
135 $requesters{'CPL'} = AddMember(
137 categorycode => 'PT',
138 surname => 'borrower from CPL',
140 $requesters{'FPL'} = AddMember(
142 categorycode => 'PT',
143 surname => 'borrower from FPL',
145 $requesters{'RPL'} = AddMember(
147 categorycode => 'PT',
148 surname => 'borrower from RPL',
151 # Configure rules so that CPL allows only CPL patrons
152 # to request its items, while FPL will allow its items
153 # to fill holds from anywhere.
155 $dbh->do('DELETE FROM issuingrules');
156 $dbh->do('DELETE FROM branch_item_rules');
157 $dbh->do('DELETE FROM branch_borrower_circ_rules');
158 $dbh->do('DELETE FROM default_borrower_circ_rules');
159 $dbh->do('DELETE FROM default_branch_item_rules');
160 $dbh->do('DELETE FROM default_branch_circ_rules');
161 $dbh->do('DELETE FROM default_circ_rules');
163 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
164 VALUES (?, ?, ?, ?)},
169 # CPL allows only its own patrons to request its items
171 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
172 VALUES (?, ?, ?, ?)},
174 'CPL', 10, 1, 'homebranch',
177 # ... while FPL allows anybody to request its items
179 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
180 VALUES (?, ?, ?, ?)},
182 'FPL', 10, 2, 'homebranch',
185 # helper biblio for the bug 10272 regression test
186 my $bib2 = MARC::Record->new();
187 $bib2->append_fields(
188 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
189 MARC::Field->new('245', ' ', ' ', a => $title),
192 # create one item belonging to FPL and one belonging to CPL
193 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
194 my ($itemnum_cpl, $itemnum_fpl);
195 (undef, undef, $itemnum_cpl) = AddItem({
197 holdingbranch => 'CPL',
198 barcode => 'bug10272_CPL'
200 (undef, undef, $itemnum_fpl) = AddItem({
202 holdingbranch => 'FPL',
203 barcode => 'bug10272_FPL'
206 # Ensure that priorities are numbered correcly when a hold is moved to waiting
208 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
209 AddReserve('RPL', $requesters{'RPL'}, $bibnum2,
210 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
211 $title, $checkitem, $found);
212 AddReserve('FPL', $requesters{'FPL'}, $bibnum2,
213 $constraint, $bibitems, 2, $resdate, $expdate, $notes,
214 $title, $checkitem, $found);
215 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
216 $constraint, $bibitems, 3, $resdate, $expdate, $notes,
217 $title, $checkitem, $found);
218 ModReserveAffect($itemnum_cpl, $requesters{'RPL'}, 0);
220 # Now it should have different priorities.
221 my $title_reserves = GetReservesFromBiblionumber({biblionumber => $bibnum2});
222 # Sort by reserve number in case the database gives us oddly ordered results
223 my @reserves = sort { $a->{reserve_id} <=> $b->{reserve_id} } @$title_reserves;
224 is($reserves[0]{priority}, 0, 'Item is correctly waiting');
225 is($reserves[1]{priority}, 1, 'Item is correctly priority 1');
226 is($reserves[2]{priority}, 2, 'Item is correctly priority 2');
229 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
230 AddReserve('RPL', $requesters{'RPL'}, $bibnum2,
231 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
232 $title, $checkitem, $found);
233 AddReserve('FPL', $requesters{'FPL'}, $bibnum2,
234 $constraint, $bibitems, 2, $resdate, $expdate, $notes,
235 $title, $checkitem, $found);
236 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
237 $constraint, $bibitems, 3, $resdate, $expdate, $notes,
238 $title, $checkitem, $found);
240 # Ensure that the item's home library controls hold policy lookup
241 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
244 # Return the CPL item at FPL. The hold that should be triggered is
245 # the one placed by the CPL patron, as the other two patron's hold
246 # requests cannot be filled by that item per policy.
247 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
248 is( $messages->{ResFound}->{borrowernumber},
250 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
252 # Return the FPL item at FPL. The hold that should be triggered is
253 # the one placed by the RPL patron, as that patron is first in line
254 # and RPL imposes no restrictions on whose holds its items can fill.
255 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
256 is( $messages->{ResFound}->{borrowernumber},
258 'for generous library, its items fill first hold request in line (bug 10272)');
260 my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber});
261 isa_ok($reserves, 'ARRAY');
262 is(scalar @$reserves, 1, "Only one reserves for this biblio");
263 my $reserve_id = $reserves->[0]->{reserve_id};
265 $reserve = GetReserve($reserve_id);
266 isa_ok($reserve, 'HASH', "GetReserve return");
267 is($reserve->{biblionumber}, $biblionumber);
269 $reserve = CancelReserve({reserve_id => $reserve_id});
270 isa_ok($reserve, 'HASH', "CancelReserve return");
271 is($reserve->{biblionumber}, $biblionumber);
273 $reserve = GetReserve($reserve_id);
274 is($reserve, undef, "GetReserve returns undef after deletion");
276 $reserve = CancelReserve({reserve_id => $reserve_id});
277 is($reserve, undef, "CancelReserve return undef if reserve does not exist");
280 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
281 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
282 # Test 9761a: Add a reserve without date, CheckReserve should return it
283 $resdate= undef; #defaults to today in AddReserve
284 $expdate= undef; #no expdate
285 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
286 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
287 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
288 $title, $checkitem, $found);
289 ($status)=CheckReserves($itemnumber,undef,undef);
290 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
291 ($status)=CheckReserves($itemnumber,undef,7);
292 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
294 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
295 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
296 C4::Context->set_preference('AllowHoldDateInFuture', 1);
297 $resdate= dt_from_string();
298 $resdate->add_duration(DateTime::Duration->new(days => 4));
299 $resdate=output_pref($resdate);
300 $expdate= undef; #no expdate
301 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
302 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
303 $title, $checkitem, $found);
304 ($status)=CheckReserves($itemnumber,undef,undef);
305 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
307 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
308 ($status)=CheckReserves($itemnumber,undef,3);
309 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
310 ($status)=CheckReserves($itemnumber,undef,4);
311 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
313 # Test 9761d: Check ResFound message of AddReturn for future hold
314 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
315 # In this test we do not need an issued item; it is just a 'checkin'
316 C4::Context->set_preference('ConfirmFutureHolds', 0);
317 (my $doreturn, $messages)= AddReturn('97531','CPL');
318 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
319 C4::Context->set_preference('ConfirmFutureHolds', 3);
320 ($doreturn, $messages)= AddReturn('97531','CPL');
321 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
322 C4::Context->set_preference('ConfirmFutureHolds', 7);
323 ($doreturn, $messages)= AddReturn('97531','CPL');
324 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
326 # End of tests for bug 9761 (ConfirmFutureHolds)
328 # test marking a hold as captured
329 my $hold_notice_count = count_hold_print_messages();
330 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
331 my $new_count = count_hold_print_messages();
332 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
334 # test that duplicate notices aren't generated
335 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
336 $new_count = count_hold_print_messages();
337 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
339 # avoiding the not_same_branch error
340 t::lib::Mocks::mock_preference('IndependentBranches', 0);
342 DelItemCheck($dbh, $bibnum, $itemnumber),
344 'item that is captured to fill a hold cannot be deleted',
347 my $letter = ReserveSlip('CPL', $requesters{'CPL'}, $bibnum);
348 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
350 # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait?
351 # 9788a: GetReservesFromItemnumber does not return future next available hold
352 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
353 C4::Context->set_preference('ConfirmFutureHolds', 2);
354 C4::Context->set_preference('AllowHoldDateInFuture', 1);
355 $resdate= dt_from_string();
356 $resdate->add_duration(DateTime::Duration->new(days => 2));
357 $resdate=output_pref($resdate);
358 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
359 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
360 $title, $checkitem, $found);
361 my @results= GetReservesFromItemnumber($itemnumber);
362 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future next available hold');
363 # 9788b: GetReservesFromItemnumber does not return future item level hold
364 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
365 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
366 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
367 $title, $itemnumber, $found); #item level hold
368 @results= GetReservesFromItemnumber($itemnumber);
369 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future item level hold');
370 # 9788c: GetReservesFromItemnumber returns future wait (confirmed future hold)
371 ModReserveAffect( $itemnumber, $requesters{'CPL'} , 0); #confirm hold
372 @results= GetReservesFromItemnumber($itemnumber);
373 is(defined $results[3]?1:0, 1, 'GetReservesFromItemnumber returns a future wait (confirmed future hold)');
374 # End of tests for bug 9788
376 # Tests for CalculatePriority (bug 8918)
377 my $p = C4::Reserves::CalculatePriority($bibnum2);
378 is($p, 4, 'CalculatePriority should now return priority 4');
380 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
381 $constraint, $bibitems, $p, $resdate, $expdate, $notes,
382 $title, $checkitem, $found);
383 $p = C4::Reserves::CalculatePriority($bibnum2);
384 is($p, 5, 'CalculatePriority should now return priority 5');
385 #some tests on bibnum
386 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
387 $p = C4::Reserves::CalculatePriority($bibnum);
388 is($p, 1, 'CalculatePriority should now return priority 1');
389 #add a new reserve and confirm it to waiting
390 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
391 $constraint, $bibitems, $p, $resdate, $expdate, $notes,
392 $title, $itemnumber, $found);
393 $p = C4::Reserves::CalculatePriority($bibnum);
394 is($p, 2, 'CalculatePriority should now return priority 2');
395 ModReserveAffect( $itemnumber, $requesters{'CPL'} , 0);
396 $p = C4::Reserves::CalculatePriority($bibnum);
397 is($p, 1, 'CalculatePriority should now return priority 1');
398 #add another biblio hold, no resdate
399 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
400 $constraint, $bibitems, $p, $resdate, $expdate, $notes,
401 $title, $checkitem, $found);
402 $p = C4::Reserves::CalculatePriority($bibnum);
403 is($p, 2, 'CalculatePriority should now return priority 2');
404 #add another future hold
405 C4::Context->set_preference('AllowHoldDateInFuture', 1);
406 $resdate= dt_from_string();
407 $resdate->add_duration(DateTime::Duration->new(days => 1));
408 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
409 $constraint, $bibitems, $p, output_pref($resdate), $expdate, $notes,
410 $title, $checkitem, $found);
411 $p = C4::Reserves::CalculatePriority($bibnum);
412 is($p, 2, 'CalculatePriority should now still return priority 2');
413 #calc priority with future resdate
414 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
415 is($p, 3, 'CalculatePriority should now return priority 3');
416 # End of tests for bug 8918
418 # Tests for cancel reserves by users from OPAC.
419 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
420 AddReserve('CPL', $requesters{'CPL'}, $item_bibnum,
421 $constraint, $bibitems, 1, undef, $expdate, $notes,
422 $title, $checkitem, '');
423 my (undef, $canres, undef) = CheckReserves($itemnumber);
425 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
426 is($cancancel, 1, 'Can user cancel its own reserve');
428 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'FPL'});
429 is($cancancel, 0, 'Other user cant cancel reserve');
431 ModReserveAffect($itemnumber, $requesters{'CPL'}, 1);
432 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
433 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
435 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
436 AddReserve('CPL', $requesters{'CPL'}, $item_bibnum,
437 $constraint, $bibitems, 1, undef, $expdate, $notes,
438 $title, $checkitem, '');
439 (undef, $canres, undef) = CheckReserves($itemnumber);
441 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
442 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
443 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
445 # End of tests for bug 12876
449 sub count_hold_print_messages {
450 my $message_count = $dbh->selectall_arrayref(q{
453 WHERE letter_code = 'HOLD'
454 AND message_transport_type = 'print'
456 return $message_count->[0]->[0];