3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 72;
25 use t::lib::TestBuilder;
28 use DateTime::Duration;
38 use Koha::Patron::Categories;
41 require_ok('C4::Reserves');
44 # a very minimal mack of userenv for use by the test of DelItemCheck
45 my $module = new Test::MockModule('C4::Context');
46 $module->mock('userenv', sub {
51 my $database = Koha::Database->new();
52 my $schema = $database->schema();
53 $schema->storage->txn_begin();
54 my $dbh = C4::Context->dbh;
56 my $builder = t::lib::TestBuilder->new;
58 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
59 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a'");
63 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
64 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
65 my $branch_3 = $builder->build({ source => 'Branch' })->{ branchcode };
67 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
68 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
70 my $itemtype = $builder->build(
71 { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
74 # Create a helper biblio
75 my $bib = MARC::Record->new();
76 my $title = 'Silence in the library';
77 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
79 MARC::Field->new('600', '', '1', a => 'Moffat, Steven'),
80 MARC::Field->new('200', '', '', a => $title),
85 MARC::Field->new('100', '', '', a => 'Moffat, Steven'),
86 MARC::Field->new('245', '', '', a => $title),
89 my ($bibnum, $bibitemnum);
90 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
92 # Create a helper item instance for testing
93 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
94 { homebranch => $branch_1,
95 holdingbranch => $branch_1,
102 # Modify item; setting barcode.
103 my $testbarcode = '97531';
104 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
108 firstname => 'my firstname',
109 surname => 'my surname',
110 categorycode => $category_1,
111 branchcode => $branch_1,
113 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
114 my $borrowernumber = AddMember(%data);
115 my $borrower = GetMember( borrowernumber => $borrowernumber );
116 my $biblionumber = $bibnum;
117 my $barcode = $testbarcode;
124 my $checkitem = undef;
127 my $branchcode = Koha::Libraries->search->next->branchcode;
129 AddReserve($branchcode, $borrowernumber, $biblionumber,
130 $bibitems, $priority, $resdate, $expdate, $notes,
131 $title, $checkitem, $found);
133 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
135 is($status, "Reserved", "CheckReserves Test 1");
137 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
139 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
140 is($status, "Reserved", "CheckReserves Test 2");
142 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
143 is($status, "Reserved", "CheckReserves Test 3");
145 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
146 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
148 'ItemHomeLib' eq GetReservesControlBranch(
149 { homebranch => 'ItemHomeLib' },
150 { branchcode => 'PatronHomeLib' }
151 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
153 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
155 'PatronHomeLib' eq GetReservesControlBranch(
156 { homebranch => 'ItemHomeLib' },
157 { branchcode => 'PatronHomeLib' }
158 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
160 t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
163 ### Regression test for bug 10272
166 $requesters{$branch_1} = AddMember(
167 branchcode => $branch_1,
168 categorycode => $category_2,
169 surname => "borrower from $branch_1",
171 for my $i ( 2 .. 5 ) {
172 $requesters{"CPL$i"} = AddMember(
173 branchcode => $branch_1,
174 categorycode => $category_2,
175 surname => "borrower $i from $branch_1",
178 $requesters{$branch_2} = AddMember(
179 branchcode => $branch_2,
180 categorycode => $category_2,
181 surname => "borrower from $branch_2",
183 $requesters{$branch_3} = AddMember(
184 branchcode => $branch_3,
185 categorycode => $category_2,
186 surname => "borrower from $branch_3",
189 # Configure rules so that $branch_1 allows only $branch_1 patrons
190 # to request its items, while $branch_2 will allow its items
191 # to fill holds from anywhere.
193 $dbh->do('DELETE FROM issuingrules');
194 $dbh->do('DELETE FROM branch_item_rules');
195 $dbh->do('DELETE FROM branch_borrower_circ_rules');
196 $dbh->do('DELETE FROM default_borrower_circ_rules');
197 $dbh->do('DELETE FROM default_branch_item_rules');
198 $dbh->do('DELETE FROM default_branch_circ_rules');
199 $dbh->do('DELETE FROM default_circ_rules');
201 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
202 VALUES (?, ?, ?, ?)},
207 # CPL allows only its own patrons to request its items
209 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
210 VALUES (?, ?, ?, ?)},
212 $branch_1, 10, 1, 'homebranch',
215 # ... while FPL allows anybody to request its items
217 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
218 VALUES (?, ?, ?, ?)},
220 $branch_2, 10, 2, 'homebranch',
223 # helper biblio for the bug 10272 regression test
224 my $bib2 = MARC::Record->new();
225 $bib2->append_fields(
226 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
227 MARC::Field->new('245', ' ', ' ', a => $title),
230 # create one item belonging to FPL and one belonging to CPL
231 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
232 my ($itemnum_cpl, $itemnum_fpl);
233 ( undef, undef, $itemnum_cpl ) = AddItem(
234 { homebranch => $branch_1,
235 holdingbranch => $branch_1,
236 barcode => 'bug10272_CPL',
241 ( undef, undef, $itemnum_fpl ) = AddItem(
242 { homebranch => $branch_2,
243 holdingbranch => $branch_2,
244 barcode => 'bug10272_FPL',
251 # Ensure that priorities are numbered correcly when a hold is moved to waiting
253 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
254 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
255 $bibitems, 1, $resdate, $expdate, $notes,
256 $title, $checkitem, $found);
257 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
258 $bibitems, 2, $resdate, $expdate, $notes,
259 $title, $checkitem, $found);
260 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
261 $bibitems, 3, $resdate, $expdate, $notes,
262 $title, $checkitem, $found);
263 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
265 # Now it should have different priorities.
266 my $title_reserves = GetReservesFromBiblionumber({biblionumber => $bibnum2});
267 # Sort by reserve number in case the database gives us oddly ordered results
268 my @reserves = sort { $a->{reserve_id} <=> $b->{reserve_id} } @$title_reserves;
269 is($reserves[0]{priority}, 0, 'Item is correctly waiting');
270 is($reserves[1]{priority}, 1, 'Item is correctly priority 1');
271 is($reserves[2]{priority}, 2, 'Item is correctly priority 2');
273 @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting();
274 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
275 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
278 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
279 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
280 $bibitems, 1, $resdate, $expdate, $notes,
281 $title, $checkitem, $found);
282 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
283 $bibitems, 2, $resdate, $expdate, $notes,
284 $title, $checkitem, $found);
285 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
286 $bibitems, 3, $resdate, $expdate, $notes,
287 $title, $checkitem, $found);
289 # Ensure that the item's home library controls hold policy lookup
290 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
293 # Return the CPL item at FPL. The hold that should be triggered is
294 # the one placed by the CPL patron, as the other two patron's hold
295 # requests cannot be filled by that item per policy.
296 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
297 is( $messages->{ResFound}->{borrowernumber},
298 $requesters{$branch_1},
299 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
301 # Return the FPL item at FPL. The hold that should be triggered is
302 # the one placed by the RPL patron, as that patron is first in line
303 # and RPL imposes no restrictions on whose holds its items can fill.
305 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
306 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
308 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
309 is( $messages->{ResFound}->{borrowernumber},
310 $requesters{$branch_3},
311 'for generous library, its items fill first hold request in line (bug 10272)');
313 my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber});
314 isa_ok($reserves, 'ARRAY');
315 is(scalar @$reserves, 1, "Only one reserves for this biblio");
316 my $reserve_id = $reserves->[0]->{reserve_id};
318 $reserve = GetReserve($reserve_id);
319 isa_ok($reserve, 'HASH', "GetReserve return");
320 is($reserve->{biblionumber}, $biblionumber);
322 $reserve = CancelReserve({reserve_id => $reserve_id});
323 isa_ok($reserve, 'HASH', "CancelReserve return");
324 is($reserve->{biblionumber}, $biblionumber);
326 $reserve = GetReserve($reserve_id);
327 is($reserve, undef, "GetReserve returns undef after deletion");
329 $reserve = CancelReserve({reserve_id => $reserve_id});
330 is($reserve, undef, "CancelReserve return undef if reserve does not exist");
333 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
334 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
335 # Test 9761a: Add a reserve without date, CheckReserve should return it
336 $resdate= undef; #defaults to today in AddReserve
337 $expdate= undef; #no expdate
338 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
339 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
340 $bibitems, 1, $resdate, $expdate, $notes,
341 $title, $checkitem, $found);
342 ($status)=CheckReserves($itemnumber,undef,undef);
343 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
344 ($status)=CheckReserves($itemnumber,undef,7);
345 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
347 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
348 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
349 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
350 $resdate= dt_from_string();
351 $resdate->add_duration(DateTime::Duration->new(days => 4));
352 $resdate=output_pref($resdate);
353 $expdate= undef; #no expdate
354 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
355 $bibitems, 1, $resdate, $expdate, $notes,
356 $title, $checkitem, $found);
357 ($status)=CheckReserves($itemnumber,undef,undef);
358 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
360 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
361 ($status)=CheckReserves($itemnumber,undef,3);
362 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
363 ($status)=CheckReserves($itemnumber,undef,4);
364 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
366 # Test 9761d: Check ResFound message of AddReturn for future hold
367 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
368 # In this test we do not need an issued item; it is just a 'checkin'
369 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
370 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
371 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
372 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
373 ($doreturn, $messages)= AddReturn('97531',$branch_1);
374 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
375 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
376 ($doreturn, $messages)= AddReturn('97531',$branch_1);
377 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
379 # End of tests for bug 9761 (ConfirmFutureHolds)
381 # test marking a hold as captured
382 my $hold_notice_count = count_hold_print_messages();
383 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
384 my $new_count = count_hold_print_messages();
385 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
387 # test that duplicate notices aren't generated
388 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
389 $new_count = count_hold_print_messages();
390 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
392 # avoiding the not_same_branch error
393 t::lib::Mocks::mock_preference('IndependentBranches', 0);
395 DelItemCheck( $bibnum, $itemnumber),
397 'item that is captured to fill a hold cannot be deleted',
400 my $letter = ReserveSlip($branch_1, $requesters{$branch_1}, $bibnum);
401 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
403 # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait?
404 # 9788a: GetReservesFromItemnumber does not return future next available hold
405 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
406 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
407 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
408 $resdate= dt_from_string();
409 $resdate->add_duration(DateTime::Duration->new(days => 2));
410 $resdate=output_pref($resdate);
411 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
412 $bibitems, 1, $resdate, $expdate, $notes,
413 $title, $checkitem, $found);
414 my @results= GetReservesFromItemnumber($itemnumber);
415 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future next available hold');
416 # 9788b: GetReservesFromItemnumber does not return future item level hold
417 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
418 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
419 $bibitems, 1, $resdate, $expdate, $notes,
420 $title, $itemnumber, $found); #item level hold
421 @results= GetReservesFromItemnumber($itemnumber);
422 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future item level hold');
423 # 9788c: GetReservesFromItemnumber returns future wait (confirmed future hold)
424 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0); #confirm hold
425 @results= GetReservesFromItemnumber($itemnumber);
426 is(defined $results[3]?1:0, 1, 'GetReservesFromItemnumber returns a future wait (confirmed future hold)');
427 # End of tests for bug 9788
429 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
430 # Tests for CalculatePriority (bug 8918)
431 my $p = C4::Reserves::CalculatePriority($bibnum2);
432 is($p, 4, 'CalculatePriority should now return priority 4');
434 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum2,
435 $bibitems, $p, $resdate, $expdate, $notes,
436 $title, $checkitem, $found);
437 $p = C4::Reserves::CalculatePriority($bibnum2);
438 is($p, 5, 'CalculatePriority should now return priority 5');
439 #some tests on bibnum
440 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
441 $p = C4::Reserves::CalculatePriority($bibnum);
442 is($p, 1, 'CalculatePriority should now return priority 1');
443 #add a new reserve and confirm it to waiting
444 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
445 $bibitems, $p, $resdate, $expdate, $notes,
446 $title, $itemnumber, $found);
447 $p = C4::Reserves::CalculatePriority($bibnum);
448 is($p, 2, 'CalculatePriority should now return priority 2');
449 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0);
450 $p = C4::Reserves::CalculatePriority($bibnum);
451 is($p, 1, 'CalculatePriority should now return priority 1');
452 #add another biblio hold, no resdate
453 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum,
454 $bibitems, $p, $resdate, $expdate, $notes,
455 $title, $checkitem, $found);
456 $p = C4::Reserves::CalculatePriority($bibnum);
457 is($p, 2, 'CalculatePriority should now return priority 2');
458 #add another future hold
459 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
460 $resdate= dt_from_string();
461 $resdate->add_duration(DateTime::Duration->new(days => 1));
462 AddReserve($branch_1, $requesters{'CPL3'}, $bibnum,
463 $bibitems, $p, output_pref($resdate), $expdate, $notes,
464 $title, $checkitem, $found);
465 $p = C4::Reserves::CalculatePriority($bibnum);
466 is($p, 2, 'CalculatePriority should now still return priority 2');
467 #calc priority with future resdate
468 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
469 is($p, 3, 'CalculatePriority should now return priority 3');
470 # End of tests for bug 8918
472 # Tests for cancel reserves by users from OPAC.
473 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
474 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
475 $bibitems, 1, undef, $expdate, $notes,
476 $title, $checkitem, '');
477 my (undef, $canres, undef) = CheckReserves($itemnumber);
479 is( CanReserveBeCanceledFromOpac(), undef,
480 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
483 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
485 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
488 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
490 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
493 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
494 is($cancancel, 1, 'Can user cancel its own reserve');
496 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
497 is($cancancel, 0, 'Other user cant cancel reserve');
499 ModReserveAffect($itemnumber, $requesters{$branch_1}, 1);
500 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
501 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
503 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
504 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
505 $bibitems, 1, undef, $expdate, $notes,
506 $title, $checkitem, '');
507 (undef, $canres, undef) = CheckReserves($itemnumber);
509 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
510 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
511 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
513 # End of tests for bug 12876
516 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
519 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
521 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
523 #Set the ageRestriction for the Biblio
524 my $record = GetMarcBiblio( $bibnum );
525 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
526 $record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
527 C4::Biblio::ModBiblio( $record, $bibnum, '' );
529 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
531 #Set the dateofbirth for the Borrower making him "too young".
532 my $now = DateTime->now();
533 C4::Members::SetAge( $borrower, '0015-00-00' );
534 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
536 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
538 #Set the dateofbirth for the Borrower making him "too old".
539 C4::Members::SetAge( $borrower, '0030-00-00' );
540 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
542 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
544 ####### EO Bug 13113 <<<
547 my $item = GetItem($itemnumber);
549 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $borrower), "Reserving a book on item level" );
551 my $itype = C4::Reserves::_get_itype($item);
552 my $categorycode = $borrower->{categorycode};
553 my $holdingbranch = $item->{holdingbranch};
554 my $rule = C4::Circulation::GetIssuingRule($categorycode, $itype, $holdingbranch);
557 "UPDATE issuingrules SET onshelfholds = 1 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
559 $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
561 ok( C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() allowed" );
563 "UPDATE issuingrules SET onshelfholds = 0 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
565 $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
567 ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
569 # Tests for bug 14464
571 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
572 my ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
573 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines at beginning' );
575 # First, test cancelling a reserve when there's no charge configured.
576 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
578 my $bz14464_reserve = AddReserve(
592 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
594 CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
596 my $old_reserve = Koha::Database->new()->schema()->resultset('OldReserve')->find( $bz14464_reserve );
597 is($old_reserve->get_column('found'), 'W', 'Bug 14968 - Keep found column from reserve');
599 ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
600 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines after cancelling reserve with no charge configured' );
602 # Then, test cancelling a reserve when there's no charge desired.
603 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
605 $bz14464_reserve = AddReserve(
619 ok( $bz14464_reserve, 'Bug 14464 - 2nd reserve correctly created' );
621 CancelReserve({ reserve_id => $bz14464_reserve });
623 ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
624 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines after cancelling reserve with no charge desired' );
626 # Finally, test cancelling a reserve when there's a charge desired and configured.
627 $bz14464_reserve = AddReserve(
641 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
643 CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
645 ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
646 is( int( $bz14464_fines ), 42, 'Bug 14464 - Fine applied after cancelling reserve with charge desired and configured' );
648 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
649 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
650 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
651 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
652 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
653 AddReserve($branch_1, $borrowernumber, $item_bibnum,
654 $bibitems, 1, undef, $expdate, $notes, $title, $checkitem, '');
655 MoveReserve( $itemnumber, $borrowernumber );
656 ($status)=CheckReserves( $itemnumber );
657 is( $status, '', 'MoveReserve filled hold');
658 # hold from A waiting, today, no fut holds: MoveReserve should fill it
659 AddReserve($branch_1, $borrowernumber, $item_bibnum,
660 $bibitems, 1, undef, $expdate, $notes, $title, $checkitem, 'W');
661 MoveReserve( $itemnumber, $borrowernumber );
662 ($status)=CheckReserves( $itemnumber );
663 is( $status, '', 'MoveReserve filled waiting hold');
664 # hold from A pos 1, tomorrow, no fut holds: not filled
665 $resdate= dt_from_string();
666 $resdate->add_duration(DateTime::Duration->new(days => 1));
667 $resdate=output_pref($resdate);
668 AddReserve($branch_1, $borrowernumber, $item_bibnum,
669 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
670 MoveReserve( $itemnumber, $borrowernumber );
671 ($status)=CheckReserves( $itemnumber, undef, 1 );
672 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
673 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
674 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
675 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
676 AddReserve($branch_1, $borrowernumber, $item_bibnum,
677 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
678 MoveReserve( $itemnumber, $borrowernumber );
679 ($status)=CheckReserves( $itemnumber, undef, 2 );
680 is( $status, '', 'MoveReserve filled future hold now');
681 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
682 AddReserve($branch_1, $borrowernumber, $item_bibnum,
683 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, 'W');
684 MoveReserve( $itemnumber, $borrowernumber );
685 ($status)=CheckReserves( $itemnumber, undef, 2 );
686 is( $status, '', 'MoveReserve filled future waiting hold now');
687 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
688 $resdate= dt_from_string();
689 $resdate->add_duration(DateTime::Duration->new(days => 3));
690 $resdate=output_pref($resdate);
691 AddReserve($branch_1, $borrowernumber, $item_bibnum,
692 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
693 MoveReserve( $itemnumber, $borrowernumber );
694 ($status)=CheckReserves( $itemnumber, undef, 3 );
695 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
696 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
698 # we reached the finish
699 $schema->storage->txn_rollback();
701 sub count_hold_print_messages {
702 my $message_count = $dbh->selectall_arrayref(q{
705 WHERE letter_code = 'HOLD'
706 AND message_transport_type = 'print'
708 return $message_count->[0]->[0];