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 => 62;
25 use t::lib::TestBuilder;
28 use DateTime::Duration;
40 use Koha::Notice::Templates;
42 use Koha::Patron::Categories;
43 use Koha::CirculationRules;
46 require_ok('C4::Reserves');
50 my $database = Koha::Database->new();
51 my $schema = $database->schema();
52 $schema->storage->txn_begin();
53 my $dbh = C4::Context->dbh;
54 $dbh->do('DELETE FROM circulation_rules');
56 my $builder = t::lib::TestBuilder->new;
58 my $frameworkcode = q//;
61 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1);
63 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
64 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
65 my $cache = Koha::Caches->get_instance;
66 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
67 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
68 $cache->clear_from_cache("default_value_for_mod_marc-");
69 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
73 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
74 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
75 my $branch_3 = $builder->build({ source => 'Branch' })->{ branchcode };
77 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
78 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
80 my $itemtype = $builder->build(
81 { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
83 t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
85 my $bibnum = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
87 # Create a helper item instance for testing
88 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
89 { homebranch => $branch_1,
90 holdingbranch => $branch_1,
96 my $biblio_with_no_item = $builder->build({
101 # Modify item; setting barcode.
102 my $testbarcode = '97531';
103 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
107 firstname => 'my firstname',
108 surname => 'my surname',
109 categorycode => $category_1,
110 branchcode => $branch_1,
112 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
113 my $borrowernumber = Koha::Patron->new(\%data)->store->borrowernumber;
114 my $patron = Koha::Patrons->find( $borrowernumber );
115 my $borrower = $patron->unblessed;
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 'a 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} = Koha::Patron->new({
167 branchcode => $branch_1,
168 categorycode => $category_2,
169 surname => "borrower from $branch_1",
170 })->store->borrowernumber;
171 for my $i ( 2 .. 5 ) {
172 $requesters{"CPL$i"} = Koha::Patron->new({
173 branchcode => $branch_1,
174 categorycode => $category_2,
175 surname => "borrower $i from $branch_1",
176 })->store->borrowernumber;
178 $requesters{$branch_2} = Koha::Patron->new({
179 branchcode => $branch_2,
180 categorycode => $category_2,
181 surname => "borrower from $branch_2",
182 })->store->borrowernumber;
183 $requesters{$branch_3} = Koha::Patron->new({
184 branchcode => $branch_3,
185 categorycode => $category_2,
186 surname => "borrower from $branch_3",
187 })->store->borrowernumber;
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 circulation_rules');
194 Koha::CirculationRules->set_rules(
197 categorycode => undef,
200 reservesallowed => 25,
201 holds_per_record => 1,
206 # CPL allows only its own patrons to request its items
207 Koha::CirculationRules->set_rules(
209 branchcode => $branch_1,
213 returnbranch => 'homebranch',
218 # ... while FPL allows anybody to request its items
219 Koha::CirculationRules->set_rules(
221 branchcode => $branch_2,
225 returnbranch => 'homebranch',
230 my $bibnum2 = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
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 'a title', $checkitem, $found);
257 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
258 $bibitems, 2, $resdate, $expdate, $notes,
259 'a title', $checkitem, $found);
260 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
261 $bibitems, 3, $resdate, $expdate, $notes,
262 'a title', $checkitem, $found);
263 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
265 # Now it should have different priorities.
266 my $biblio = Koha::Biblios->find( $bibnum2 );
267 my $holds = $biblio->holds({}, { order_by => 'reserve_id' });;
268 is($holds->next->priority, 0, 'Item is correctly waiting');
269 is($holds->next->priority, 1, 'Item is correctly priority 1');
270 is($holds->next->priority, 2, 'Item is correctly priority 2');
272 my @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting();
273 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
274 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
277 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
278 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
279 $bibitems, 1, $resdate, $expdate, $notes,
280 'a title', $checkitem, $found);
281 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
282 $bibitems, 2, $resdate, $expdate, $notes,
283 'a title', $checkitem, $found);
284 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
285 $bibitems, 3, $resdate, $expdate, $notes,
286 'a title', $checkitem, $found);
288 # Ensure that the item's home library controls hold policy lookup
289 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
292 # Return the CPL item at FPL. The hold that should be triggered is
293 # the one placed by the CPL patron, as the other two patron's hold
294 # requests cannot be filled by that item per policy.
295 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
296 is( $messages->{ResFound}->{borrowernumber},
297 $requesters{$branch_1},
298 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
300 # Return the FPL item at FPL. The hold that should be triggered is
301 # the one placed by the RPL patron, as that patron is first in line
302 # and RPL imposes no restrictions on whose holds its items can fill.
304 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
305 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
307 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
308 is( $messages->{ResFound}->{borrowernumber},
309 $requesters{$branch_3},
310 'for generous library, its items fill first hold request in line (bug 10272)');
312 $biblio = Koha::Biblios->find( $biblionumber );
313 $holds = $biblio->holds;
314 is($holds->count, 1, "Only one reserves for this biblio");
315 my $reserve_id = $holds->next->reserve_id;
317 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
318 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
319 # Test 9761a: Add a reserve without date, CheckReserve should return it
320 $resdate= undef; #defaults to today in AddReserve
321 $expdate= undef; #no expdate
322 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
323 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
324 $bibitems, 1, $resdate, $expdate, $notes,
325 'a title', $checkitem, $found);
326 ($status)=CheckReserves($itemnumber,undef,undef);
327 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
328 ($status)=CheckReserves($itemnumber,undef,7);
329 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
331 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
332 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
333 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
334 $resdate= dt_from_string();
335 $resdate->add_duration(DateTime::Duration->new(days => 4));
336 $resdate=output_pref($resdate);
337 $expdate= undef; #no expdate
338 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
339 $bibitems, 1, $resdate, $expdate, $notes,
340 'a title', $checkitem, $found);
341 ($status)=CheckReserves($itemnumber,undef,undef);
342 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
344 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
345 ($status)=CheckReserves($itemnumber,undef,3);
346 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
347 ($status)=CheckReserves($itemnumber,undef,4);
348 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
350 # Test 9761d: Check ResFound message of AddReturn for future hold
351 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
352 # In this test we do not need an issued item; it is just a 'checkin'
353 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
354 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
355 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
356 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
357 ($doreturn, $messages)= AddReturn('97531',$branch_1);
358 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
359 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
360 ($doreturn, $messages)= AddReturn('97531',$branch_1);
361 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
363 # End of tests for bug 9761 (ConfirmFutureHolds)
365 # test marking a hold as captured
366 my $hold_notice_count = count_hold_print_messages();
367 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
368 my $new_count = count_hold_print_messages();
369 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
371 # test that duplicate notices aren't generated
372 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
373 $new_count = count_hold_print_messages();
374 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
376 # avoiding the not_same_branch error
377 t::lib::Mocks::mock_preference('IndependentBranches', 0);
379 DelItemCheck( $bibnum, $itemnumber),
381 'item that is captured to fill a hold cannot be deleted',
384 my $letter = ReserveSlip( { branchcode => $branch_1, borrowernumber => $requesters{$branch_1}, biblionumber => $bibnum } );
385 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
387 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
388 # 9788a: current_holds does not return future next available hold
389 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
390 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
391 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
392 $resdate= dt_from_string();
393 $resdate->add_duration(DateTime::Duration->new(days => 2));
394 $resdate=output_pref($resdate);
395 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
396 $bibitems, 1, $resdate, $expdate, $notes,
397 'a title', $checkitem, $found);
398 my $item = Koha::Items->find( $itemnumber );
399 $holds = $item->current_holds;
400 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
401 my $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
402 is( $future_holds->count, 0, 'current_holds does not return a future next available hold');
403 # 9788b: current_holds does not return future item level hold
404 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
405 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
406 $bibitems, 1, $resdate, $expdate, $notes,
407 'a title', $itemnumber, $found); #item level hold
408 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
409 is( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
410 # 9788c: current_holds returns future wait (confirmed future hold)
411 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0); #confirm hold
412 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
413 is( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
414 # End of tests for bug 9788
416 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
417 # Tests for CalculatePriority (bug 8918)
418 my $p = C4::Reserves::CalculatePriority($bibnum2);
419 is($p, 4, 'CalculatePriority should now return priority 4');
421 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum2,
422 $bibitems, $p, $resdate, $expdate, $notes,
423 'a title', $checkitem, $found);
424 $p = C4::Reserves::CalculatePriority($bibnum2);
425 is($p, 5, 'CalculatePriority should now return priority 5');
426 #some tests on bibnum
427 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
428 $p = C4::Reserves::CalculatePriority($bibnum);
429 is($p, 1, 'CalculatePriority should now return priority 1');
430 #add a new reserve and confirm it to waiting
431 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
432 $bibitems, $p, $resdate, $expdate, $notes,
433 'a title', $itemnumber, $found);
434 $p = C4::Reserves::CalculatePriority($bibnum);
435 is($p, 2, 'CalculatePriority should now return priority 2');
436 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0);
437 $p = C4::Reserves::CalculatePriority($bibnum);
438 is($p, 1, 'CalculatePriority should now return priority 1');
439 #add another biblio hold, no resdate
440 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum,
441 $bibitems, $p, $resdate, $expdate, $notes,
442 'a title', $checkitem, $found);
443 $p = C4::Reserves::CalculatePriority($bibnum);
444 is($p, 2, 'CalculatePriority should now return priority 2');
445 #add another future hold
446 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
447 $resdate= dt_from_string();
448 $resdate->add_duration(DateTime::Duration->new(days => 1));
449 AddReserve($branch_1, $requesters{'CPL3'}, $bibnum,
450 $bibitems, $p, output_pref($resdate), $expdate, $notes,
451 'a title', $checkitem, $found);
452 $p = C4::Reserves::CalculatePriority($bibnum);
453 is($p, 2, 'CalculatePriority should now still return priority 2');
454 #calc priority with future resdate
455 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
456 is($p, 3, 'CalculatePriority should now return priority 3');
457 # End of tests for bug 8918
459 # Tests for cancel reserves by users from OPAC.
460 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
461 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
462 $bibitems, 1, undef, $expdate, $notes,
463 'a title', $checkitem, '');
464 my (undef, $canres, undef) = CheckReserves($itemnumber);
466 is( CanReserveBeCanceledFromOpac(), undef,
467 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
470 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
472 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
475 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
477 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
480 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
481 is($cancancel, 1, 'Can user cancel its own reserve');
483 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
484 is($cancancel, 0, 'Other user cant cancel reserve');
486 ModReserveAffect($itemnumber, $requesters{$branch_1}, 1);
487 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
488 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
490 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
491 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
492 $bibitems, 1, undef, $expdate, $notes,
493 'a title', $checkitem, '');
494 (undef, $canres, undef) = CheckReserves($itemnumber);
496 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
497 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
498 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
500 # End of tests for bug 12876
503 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
506 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
508 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
510 #Set the ageRestriction for the Biblio
511 my $record = GetMarcBiblio({ biblionumber => $bibnum });
512 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
513 $record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
514 C4::Biblio::ModBiblio( $record, $bibnum, $frameworkcode );
516 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
518 #Set the dateofbirth for the Borrower making them "too young".
519 $borrower->{dateofbirth} = DateTime->now->add( years => -15 );
520 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
522 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
524 #Set the dateofbirth for the Borrower making them "too old".
525 $borrower->{dateofbirth} = DateTime->now->add( years => -30 );
526 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
528 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
530 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblio_with_no_item->{biblionumber})->{status} , '', "Biblio with no item. Status is empty");
532 ####### EO Bug 13113 <<<
535 $item = Koha::Items->find($itemnumber);
537 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron), "Reserving a book on item level" );
539 my $pickup_branch = $builder->build({ source => 'Branch' })->{ branchcode };
540 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
541 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
542 my $limit = Koha::Item::Transfer::Limit->new(
544 toBranch => $pickup_branch,
545 fromBranch => $item->holdingbranch,
546 itemtype => $item->effective_itemtype,
549 is( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron, $pickup_branch), 0, "Item level request not available due to transfer limit" );
550 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
552 my $itype = C4::Reserves::_get_itype($item);
553 my $categorycode = $borrower->{categorycode};
554 my $holdingbranch = $item->{holdingbranch};
555 Koha::CirculationRules->set_rules(
557 categorycode => $categorycode,
559 branchcode => $holdingbranch,
566 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
567 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
568 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
569 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
570 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
571 AddReserve($branch_1, $borrowernumber, $item_bibnum,
572 $bibitems, 1, undef, $expdate, $notes, 'a title', $checkitem, '');
573 MoveReserve( $itemnumber, $borrowernumber );
574 ($status)=CheckReserves( $itemnumber );
575 is( $status, '', 'MoveReserve filled hold');
576 # hold from A waiting, today, no fut holds: MoveReserve should fill it
577 AddReserve($branch_1, $borrowernumber, $item_bibnum,
578 $bibitems, 1, undef, $expdate, $notes, 'a title', $checkitem, 'W');
579 MoveReserve( $itemnumber, $borrowernumber );
580 ($status)=CheckReserves( $itemnumber );
581 is( $status, '', 'MoveReserve filled waiting hold');
582 # hold from A pos 1, tomorrow, no fut holds: not filled
583 $resdate= dt_from_string();
584 $resdate->add_duration(DateTime::Duration->new(days => 1));
585 $resdate=output_pref($resdate);
586 AddReserve($branch_1, $borrowernumber, $item_bibnum,
587 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
588 MoveReserve( $itemnumber, $borrowernumber );
589 ($status)=CheckReserves( $itemnumber, undef, 1 );
590 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
591 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
592 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
593 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
594 AddReserve($branch_1, $borrowernumber, $item_bibnum,
595 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
596 MoveReserve( $itemnumber, $borrowernumber );
597 ($status)=CheckReserves( $itemnumber, undef, 2 );
598 is( $status, '', 'MoveReserve filled future hold now');
599 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
600 AddReserve($branch_1, $borrowernumber, $item_bibnum,
601 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, 'W');
602 MoveReserve( $itemnumber, $borrowernumber );
603 ($status)=CheckReserves( $itemnumber, undef, 2 );
604 is( $status, '', 'MoveReserve filled future waiting hold now');
605 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
606 $resdate= dt_from_string();
607 $resdate->add_duration(DateTime::Duration->new(days => 3));
608 $resdate=output_pref($resdate);
609 AddReserve($branch_1, $borrowernumber, $item_bibnum,
610 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
611 MoveReserve( $itemnumber, $borrowernumber );
612 ($status)=CheckReserves( $itemnumber, undef, 3 );
613 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
614 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
616 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
617 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
618 $cache->clear_from_cache("default_value_for_mod_marc-");
619 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
621 subtest '_koha_notify_reserve() tests' => sub {
625 my $wants_hold_and_email = {
631 letter_code => 'HOLD'
634 my $mp = Test::MockModule->new( 'C4::Members::Messaging' );
636 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
638 $dbh->do('DELETE FROM letter');
640 my $email_hold_notice = $builder->build({
643 message_transport_type => 'email',
646 module => 'reserves',
651 my $sms_hold_notice = $builder->build({
654 message_transport_type => 'sms',
657 module => 'reserves',
662 my $hold_borrower = $builder->build({
663 source => 'Borrower',
665 smsalertnumber=>'5555555555',
668 })->{borrowernumber};
670 C4::Reserves::AddReserve(
671 $item->homebranch, $hold_borrower,
672 $item->biblionumber );
674 ModReserveAffect($item->itemnumber, $hold_borrower, 0);
675 my $sms_message_address = $schema->resultset('MessageQueue')->search({
676 letter_code => 'HOLD',
677 message_transport_type => 'sms',
678 borrowernumber => $hold_borrower,
679 })->next()->to_address();
680 is($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
682 my $email_message_address = $schema->resultset('MessageQueue')->search({
683 letter_code => 'HOLD',
684 message_transport_type => 'email',
685 borrowernumber => $hold_borrower,
686 })->next()->to_address();
687 is($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
691 subtest 'ReservesNeedReturns' => sub {
694 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
696 homebranch => $library->branchcode,
697 holdingbranch => $library->branchcode,
699 my $item = $builder->build_sample_item($item_info);
700 my $patron = $builder->build_object(
702 class => 'Koha::Patrons',
703 value => { branchcode => $library->branchcode, }
706 my $patron_2 = $builder->build_object(
708 class => 'Koha::Patrons',
709 value => { branchcode => $library->branchcode, }
715 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Test with feature disabled
716 my $hold = place_item_hold( $patron, $item, $library, $priority );
717 is( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
718 is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
721 t::lib::Mocks::mock_preference('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
722 $hold = place_item_hold( $patron, $item, $library, $priority );
723 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
724 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
727 $item->onloan('2010-01-01')->store;
728 $hold = place_item_hold( $patron, $item, $library, $priority );
729 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item onloan priority must be set to 1' );
732 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); # '0' means damaged holds not allowed
733 $item->onloan(undef)->damaged(1)->store;
734 $hold = place_item_hold( $patron, $item, $library, $priority );
735 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item damaged and not allowed holds on damaged items priority must be set to 1' );
737 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); # '0' means damaged holds not allowed
738 $hold = place_item_hold( $patron, $item, $library, $priority );
739 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and damaged holds allowed, priority must have been set to 0' );
740 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and damaged holds allowed, found must have been set waiting' );
743 my $hold_1 = place_item_hold( $patron, $item, $library, $priority );
744 is( $hold_1->found, 'W', 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
745 is( $hold_1->priority, 0, 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
746 $hold = place_item_hold( $patron_2, $item, $library, $priority );
747 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item already on hold priority must be set to 1' );
751 my $transfer = $builder->build_object({
752 class => "Koha::Item::Transfers",
754 itemnumber => $item->itemnumber,
755 datearrived => undef,
758 $item->damaged(0)->store;
759 $hold = place_item_hold( $patron, $item, $library, $priority );
760 is( $hold->found, undef, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
761 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
765 $hold = place_item_hold( $patron, $item, $library, $priority );
766 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
767 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
768 $hold_1 = place_item_hold( $patron, $item, $library, $priority );
769 is( $hold_1->priority, 1, 'If ReservesNeedReturns is 0 but item has a hold priority is 1' );
770 $hold_1->suspend(1)->store; # We suspend the hold
771 $hold->delete; # Delete the waiting hold
772 $hold = place_item_hold( $patron, $item, $library, $priority );
773 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and other hold(s) suspended, priority must have been set to 0' );
774 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and other hold(s) suspended, found must have been set waiting' );
779 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Don't affect other tests
782 subtest 'ChargeReserveFee tests' => sub {
786 my $library = $builder->build_object({ class => 'Koha::Libraries' });
787 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
790 my $title = 'A title';
792 my $context = Test::MockModule->new('C4::Context');
793 $context->mock( userenv => { branch => $library->id } );
795 my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title );
797 is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
798 ok( $line->is_debit, 'Generates a debit line' );
799 is( $line->debit_type_code, 'RESERVE' , 'generates RESERVE debit_type');
800 is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
801 is( $line->amount, $fee , 'amount set correctly');
802 is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
803 is( $line->description, "$title" , 'description is title of reserved item');
804 is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
807 subtest 'reserves.item_level_hold' => sub {
810 my $item = $builder->build_sample_item;
811 my $patron = $builder->build_object(
813 class => 'Koha::Patrons',
814 value => { branchcode => $item->homebranch }
818 subtest 'item level hold' => sub {
821 AddReserve( $item->homebranch, $patron->borrowernumber,
822 $item->biblionumber, undef, 1, undef, undef, '', '',
825 my $hold = Koha::Holds->find($reserve_id);
826 is( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' );
829 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
831 # Revert the waiting status
832 C4::Reserves::RevertWaitingStatus(
833 { itemnumber => $item->itemnumber } );
835 $hold = Koha::Holds->find($reserve_id);
837 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' );
839 $hold->delete; # cleanup
842 subtest 'biblio level hold' => sub {
844 my $reserve_id = AddReserve( $item->homebranch, $patron->borrowernumber,
845 $item->biblionumber, undef, 1 );
847 my $hold = Koha::Holds->find($reserve_id);
848 is( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' );
851 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
853 $hold = Koha::Holds->find($reserve_id);
854 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should be set on hold confirmation' );
856 # Revert the waiting status
857 C4::Reserves::RevertWaitingStatus( { itemnumber => $item->itemnumber } );
859 $hold = Koha::Holds->find($reserve_id);
860 is( $hold->itemnumber, undef, 'Itemnumber should be removed when the waiting status is revert' );
867 subtest 'MoveReserve additional test' => sub {
871 # Create the items and patrons we need
872 my $biblio = $builder->build_sample_biblio();
873 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
874 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
875 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, notforloan => 0, itype => $itype->itemtype });
876 my $patron_1 = $builder->build_object({ class => "Koha::Patrons" });
877 my $patron_2 = $builder->build_object({ class => "Koha::Patrons" });
879 # Place a hold on the title for both patrons
880 my $reserve_1 = AddReserve( $item_1->homebranch, $patron_1->borrowernumber, $biblio->biblionumber, undef, 1 );
881 my $reserve_2 = AddReserve( $item_2->homebranch, $patron_2->borrowernumber, $biblio->biblionumber, undef, 1 );
882 is($patron_1->holds->next()->reserve_id, $reserve_1, "The 1st patron has a hold");
883 is($patron_2->holds->next()->reserve_id, $reserve_2, "The 2nd patron has a hold");
885 # Fake the holds queue
886 $dbh->do(q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?)},undef,($patron_1->borrowernumber,$biblio->biblionumber,$item_1->itemnumber,$item_1->homebranch,0));
888 # The 2nd hold should be filed even if the item is preselected for the first hold
889 MoveReserve($item_1->itemnumber,$patron_2->borrowernumber);
890 is($patron_2->holds->count, 0, "The 2nd patrons no longer has a hold");
891 is($patron_2->old_holds->next()->reserve_id, $reserve_2, "The 2nd patrons hold was filled and moved to old holds");
895 sub count_hold_print_messages {
896 my $message_count = $dbh->selectall_arrayref(q{
899 WHERE letter_code = 'HOLD'
900 AND message_transport_type = 'print'
902 return $message_count->[0]->[0];
905 sub place_item_hold {
906 my ($patron,$item,$library,$priority) = @_;
908 my $hold_id = C4::Reserves::AddReserve(
909 $library->branchcode, $patron->borrowernumber,
910 $item->biblionumber, '',
913 "title for fee", $item->itemnumber,
915 my $hold = Koha::Holds->find($hold_id);
919 # we reached the finish
920 $schema->storage->txn_rollback();