5 use Test::More tests => 43;
7 use DateTime::Duration;
20 use_ok('C4::Reserves');
23 # a very minimal mack of userenv for use by the test of DelItemCheck
24 *C4::Context::userenv = sub {
28 my $dbh = C4::Context->dbh;
31 $dbh->{AutoCommit} = 0;
32 $dbh->{RaiseError} = 1;
34 # Setup Test------------------------
36 # Add branches if not existing
37 foreach my $addbra ('CPL', 'FPL', 'RPL') {
38 $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES (?,?)", undef, ($addbra,"$addbra branch")) unless GetBranchName($addbra);
41 # Add categories if not existing
42 foreach my $addcat ('S', 'PT') {
43 $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
47 diag("\nCreating biblio instance for testing.");
48 my $bib = MARC::Record->new();
49 my $title = 'Silence in the library';
50 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
52 MARC::Field->new('600', '', '1', a => 'Moffat, Steven'),
53 MARC::Field->new('200', '', '', a => $title),
58 MARC::Field->new('100', '', '', a => 'Moffat, Steven'),
59 MARC::Field->new('245', '', '', a => $title),
62 my ($bibnum, $bibitemnum);
63 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
65 # Helper item for that biblio.
66 diag("Creating item instance for testing.");
67 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
69 # Modify item; setting barcode.
70 my $testbarcode = '97531';
71 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
75 firstname => 'my firstname',
76 surname => 'my surname',
80 my $borrowernumber = AddMember(%data);
81 my $borrower = GetMember( borrowernumber => $borrowernumber );
82 my $biblionumber = $bibnum;
83 my $barcode = $testbarcode;
91 my $checkitem = undef;
94 my @branches = GetBranchesLoop();
95 my $branch = $branches[0][0]{value};
97 AddReserve($branch, $borrowernumber, $biblionumber,
98 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
99 $title, $checkitem, $found);
101 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
103 is($status, "Reserved", "CheckReserves Test 1");
105 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
107 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
108 is($status, "Reserved", "CheckReserves Test 2");
110 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
111 is($status, "Reserved", "CheckReserves Test 3");
113 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
114 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
116 'ItemHomeLib' eq GetReservesControlBranch(
117 { homebranch => 'ItemHomeLib' },
118 { branchcode => 'PatronHomeLib' }
119 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
121 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
123 'PatronHomeLib' eq GetReservesControlBranch(
124 { homebranch => 'ItemHomeLib' },
125 { branchcode => 'PatronHomeLib' }
126 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
128 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
131 ### Regression test for bug 10272
134 $requesters{'CPL'} = AddMember(
136 categorycode => 'PT',
137 surname => 'borrower from CPL',
139 $requesters{'FPL'} = AddMember(
141 categorycode => 'PT',
142 surname => 'borrower from FPL',
144 $requesters{'RPL'} = AddMember(
146 categorycode => 'PT',
147 surname => 'borrower from RPL',
150 # Configure rules so that CPL allows only CPL patrons
151 # to request its items, while FPL will allow its items
152 # to fill holds from anywhere.
154 $dbh->do('DELETE FROM issuingrules');
155 $dbh->do('DELETE FROM branch_item_rules');
156 $dbh->do('DELETE FROM branch_borrower_circ_rules');
157 $dbh->do('DELETE FROM default_borrower_circ_rules');
158 $dbh->do('DELETE FROM default_branch_item_rules');
159 $dbh->do('DELETE FROM default_branch_circ_rules');
160 $dbh->do('DELETE FROM default_circ_rules');
162 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
163 VALUES (?, ?, ?, ?)},
168 # CPL allows only its own patrons to request its items
170 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
171 VALUES (?, ?, ?, ?)},
173 'CPL', 10, 1, 'homebranch',
176 # ... while FPL allows anybody to request its items
178 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
179 VALUES (?, ?, ?, ?)},
181 'FPL', 10, 2, 'homebranch',
184 # helper biblio for the bug 10272 regression test
185 my $bib2 = MARC::Record->new();
186 $bib2->append_fields(
187 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
188 MARC::Field->new('245', ' ', ' ', a => $title),
191 # create one item belonging to FPL and one belonging to CPL
192 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
193 my ($itemnum_cpl, $itemnum_fpl);
194 (undef, undef, $itemnum_cpl) = AddItem({
196 holdingbranch => 'CPL',
197 barcode => 'bug10272_CPL'
199 (undef, undef, $itemnum_fpl) = AddItem({
201 holdingbranch => 'FPL',
202 barcode => 'bug10272_FPL'
205 # Ensure that priorities are numbered correcly when a hold is moved to waiting
207 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
208 AddReserve('RPL', $requesters{'RPL'}, $bibnum2,
209 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
210 $title, $checkitem, $found);
211 AddReserve('FPL', $requesters{'FPL'}, $bibnum2,
212 $constraint, $bibitems, 2, $resdate, $expdate, $notes,
213 $title, $checkitem, $found);
214 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
215 $constraint, $bibitems, 3, $resdate, $expdate, $notes,
216 $title, $checkitem, $found);
217 ModReserveAffect($itemnum_cpl, $requesters{'RPL'}, 0);
219 # Now it should have different priorities.
220 my $title_reserves = GetReservesFromBiblionumber({biblionumber => $bibnum2});
221 # Sort by reserve number in case the database gives us oddly ordered results
222 my @reserves = sort { $a->{reserve_id} <=> $b->{reserve_id} } @$title_reserves;
223 is($reserves[0]{priority}, 0, 'Item is correctly waiting');
224 is($reserves[1]{priority}, 1, 'Item is correctly priority 1');
225 is($reserves[2]{priority}, 2, 'Item is correctly priority 2');
228 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
229 AddReserve('RPL', $requesters{'RPL'}, $bibnum2,
230 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
231 $title, $checkitem, $found);
232 AddReserve('FPL', $requesters{'FPL'}, $bibnum2,
233 $constraint, $bibitems, 2, $resdate, $expdate, $notes,
234 $title, $checkitem, $found);
235 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
236 $constraint, $bibitems, 3, $resdate, $expdate, $notes,
237 $title, $checkitem, $found);
239 # Ensure that the item's home library controls hold policy lookup
240 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
243 # Return the CPL item at FPL. The hold that should be triggered is
244 # the one placed by the CPL patron, as the other two patron's hold
245 # requests cannot be filled by that item per policy.
246 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
247 is( $messages->{ResFound}->{borrowernumber},
249 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
251 # Return the FPL item at FPL. The hold that should be triggered is
252 # the one placed by the RPL patron, as that patron is first in line
253 # and RPL imposes no restrictions on whose holds its items can fill.
254 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
255 is( $messages->{ResFound}->{borrowernumber},
257 'for generous library, its items fill first hold request in line (bug 10272)');
259 my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber});
260 isa_ok($reserves, 'ARRAY');
261 is(scalar @$reserves, 1, "Only one reserves for this biblio");
262 my $reserve_id = $reserves->[0]->{reserve_id};
264 $reserve = GetReserve($reserve_id);
265 isa_ok($reserve, 'HASH', "GetReserve return");
266 is($reserve->{biblionumber}, $biblionumber);
268 $reserve = CancelReserve({reserve_id => $reserve_id});
269 isa_ok($reserve, 'HASH', "CancelReserve return");
270 is($reserve->{biblionumber}, $biblionumber);
272 $reserve = GetReserve($reserve_id);
273 is($reserve, undef, "GetReserve returns undef after deletion");
275 $reserve = CancelReserve({reserve_id => $reserve_id});
276 is($reserve, undef, "CancelReserve return undef if reserve does not exist");
279 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
280 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
281 # Test 9761a: Add a reserve without date, CheckReserve should return it
282 $resdate= undef; #defaults to today in AddReserve
283 $expdate= undef; #no expdate
284 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
285 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
286 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
287 $title, $checkitem, $found);
288 ($status)=CheckReserves($itemnumber,undef,undef);
289 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
290 ($status)=CheckReserves($itemnumber,undef,7);
291 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
293 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
294 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
295 C4::Context->set_preference('AllowHoldDateInFuture', 1);
296 $resdate= dt_from_string();
297 $resdate->add_duration(DateTime::Duration->new(days => 4));
298 $resdate=output_pref($resdate);
299 $expdate= undef; #no expdate
300 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
301 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
302 $title, $checkitem, $found);
303 ($status)=CheckReserves($itemnumber,undef,undef);
304 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
306 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
307 ($status)=CheckReserves($itemnumber,undef,3);
308 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
309 ($status)=CheckReserves($itemnumber,undef,4);
310 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
312 # Test 9761d: Check ResFound message of AddReturn for future hold
313 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
314 # In this test we do not need an issued item; it is just a 'checkin'
315 C4::Context->set_preference('ConfirmFutureHolds', 0);
316 (my $doreturn, $messages)= AddReturn('97531','CPL');
317 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
318 C4::Context->set_preference('ConfirmFutureHolds', 3);
319 ($doreturn, $messages)= AddReturn('97531','CPL');
320 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
321 C4::Context->set_preference('ConfirmFutureHolds', 7);
322 ($doreturn, $messages)= AddReturn('97531','CPL');
323 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
325 # End of tests for bug 9761 (ConfirmFutureHolds)
327 # test marking a hold as captured
328 my $hold_notice_count = count_hold_print_messages();
329 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
330 my $new_count = count_hold_print_messages();
331 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
333 # test that duplicate notices aren't generated
334 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
335 $new_count = count_hold_print_messages();
336 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
338 # avoiding the not_same_branch error
339 t::lib::Mocks::mock_preference('IndependentBranches', 0);
341 DelItemCheck($dbh, $bibnum, $itemnumber),
343 'item that is captured to fill a hold cannot be deleted',
346 my $letter = ReserveSlip('CPL', $requesters{'CPL'}, $bibnum);
347 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
349 # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait?
350 # 9788a: GetReservesFromItemnumber does not return future next available hold
351 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
352 C4::Context->set_preference('ConfirmFutureHolds', 2);
353 C4::Context->set_preference('AllowHoldDateInFuture', 1);
354 $resdate= dt_from_string();
355 $resdate->add_duration(DateTime::Duration->new(days => 2));
356 $resdate=output_pref($resdate);
357 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
358 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
359 $title, $checkitem, $found);
360 my @results= GetReservesFromItemnumber($itemnumber);
361 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future next available hold');
362 # 9788b: GetReservesFromItemnumber does not return future item level hold
363 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
364 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
365 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
366 $title, $itemnumber, $found); #item level hold
367 @results= GetReservesFromItemnumber($itemnumber);
368 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future item level hold');
369 # 9788c: GetReservesFromItemnumber returns future wait (confirmed future hold)
370 ModReserveAffect( $itemnumber, $requesters{'CPL'} , 0); #confirm hold
371 @results= GetReservesFromItemnumber($itemnumber);
372 is(defined $results[3]?1:0, 1, 'GetReservesFromItemnumber returns a future wait (confirmed future hold)');
373 # End of tests for bug 9788
375 # Tests for CalculatePriority (bug 8918)
376 my $p = C4::Reserves::CalculatePriority($bibnum2);
377 is($p, 4, 'CalculatePriority should now return priority 4');
379 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
380 $constraint, $bibitems, $p, $resdate, $expdate, $notes,
381 $title, $checkitem, $found);
382 $p = C4::Reserves::CalculatePriority($bibnum2);
383 is($p, 5, 'CalculatePriority should now return priority 5');
384 #some tests on bibnum
385 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
386 $p = C4::Reserves::CalculatePriority($bibnum);
387 is($p, 1, 'CalculatePriority should now return priority 1');
388 #add a new reserve and confirm it to waiting
389 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
390 $constraint, $bibitems, $p, $resdate, $expdate, $notes,
391 $title, $itemnumber, $found);
392 $p = C4::Reserves::CalculatePriority($bibnum);
393 is($p, 2, 'CalculatePriority should now return priority 2');
394 ModReserveAffect( $itemnumber, $requesters{'CPL'} , 0);
395 $p = C4::Reserves::CalculatePriority($bibnum);
396 is($p, 1, 'CalculatePriority should now return priority 1');
397 #add another biblio hold, no resdate
398 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
399 $constraint, $bibitems, $p, $resdate, $expdate, $notes,
400 $title, $checkitem, $found);
401 $p = C4::Reserves::CalculatePriority($bibnum);
402 is($p, 2, 'CalculatePriority should now return priority 2');
403 #add another future hold
404 C4::Context->set_preference('AllowHoldDateInFuture', 1);
405 $resdate= dt_from_string();
406 $resdate->add_duration(DateTime::Duration->new(days => 1));
407 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
408 $constraint, $bibitems, $p, output_pref($resdate), $expdate, $notes,
409 $title, $checkitem, $found);
410 $p = C4::Reserves::CalculatePriority($bibnum);
411 is($p, 2, 'CalculatePriority should now still return priority 2');
412 #calc priority with future resdate
413 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
414 is($p, 3, 'CalculatePriority should now return priority 3');
415 # End of tests for bug 8918
419 sub count_hold_print_messages {
420 my $message_count = $dbh->selectall_arrayref(q{
423 WHERE letter_code = 'HOLD'
424 AND message_transport_type = 'print'
426 return $message_count->[0]->[0];