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 => 58;
25 use t::lib::TestBuilder;
28 use DateTime::Duration;
39 use Koha::Notice::Templates;
41 use Koha::Patron::Categories;
44 require_ok('C4::Reserves');
48 my $database = Koha::Database->new();
49 my $schema = $database->schema();
50 $schema->storage->txn_begin();
51 my $dbh = C4::Context->dbh;
53 my $builder = t::lib::TestBuilder->new;
55 my $frameworkcode = q//;
58 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1);
60 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
61 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
62 my $cache = Koha::Caches->get_instance;
63 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
64 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
65 $cache->clear_from_cache("default_value_for_mod_marc-");
66 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
70 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
71 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
72 my $branch_3 = $builder->build({ source => 'Branch' })->{ branchcode };
74 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
75 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
77 my $itemtype = $builder->build(
78 { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
80 t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
82 # Create a helper biblio
83 my $bib = MARC::Record->new();
84 my $title = 'Silence in the library';
85 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
87 MARC::Field->new('600', '', '1', a => 'Moffat, Steven'),
88 MARC::Field->new('200', '', '', a => $title),
93 MARC::Field->new('100', '', '', a => 'Moffat, Steven'),
94 MARC::Field->new('245', '', '', a => $title),
97 my ( $bibnum ) = AddBiblio($bib, $frameworkcode);
99 # Create a helper item instance for testing
100 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
101 { homebranch => $branch_1,
102 holdingbranch => $branch_1,
109 # Modify item; setting barcode.
110 my $testbarcode = '97531';
111 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
115 firstname => 'my firstname',
116 surname => 'my surname',
117 categorycode => $category_1,
118 branchcode => $branch_1,
120 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
121 my $borrowernumber = Koha::Patron->new(\%data)->store->borrowernumber;
122 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
123 my $biblionumber = $bibnum;
124 my $barcode = $testbarcode;
131 my $checkitem = undef;
134 my $branchcode = Koha::Libraries->search->next->branchcode;
136 AddReserve($branchcode, $borrowernumber, $biblionumber,
137 $bibitems, $priority, $resdate, $expdate, $notes,
138 $title, $checkitem, $found);
140 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
142 is($status, "Reserved", "CheckReserves Test 1");
144 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
146 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
147 is($status, "Reserved", "CheckReserves Test 2");
149 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
150 is($status, "Reserved", "CheckReserves Test 3");
152 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
153 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
155 'ItemHomeLib' eq GetReservesControlBranch(
156 { homebranch => 'ItemHomeLib' },
157 { branchcode => 'PatronHomeLib' }
158 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
160 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
162 'PatronHomeLib' eq GetReservesControlBranch(
163 { homebranch => 'ItemHomeLib' },
164 { branchcode => 'PatronHomeLib' }
165 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
167 t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
170 ### Regression test for bug 10272
173 $requesters{$branch_1} = Koha::Patron->new({
174 branchcode => $branch_1,
175 categorycode => $category_2,
176 surname => "borrower from $branch_1",
177 })->store->borrowernumber;
178 for my $i ( 2 .. 5 ) {
179 $requesters{"CPL$i"} = Koha::Patron->new({
180 branchcode => $branch_1,
181 categorycode => $category_2,
182 surname => "borrower $i from $branch_1",
183 })->store->borrowernumber;
185 $requesters{$branch_2} = Koha::Patron->new({
186 branchcode => $branch_2,
187 categorycode => $category_2,
188 surname => "borrower from $branch_2",
189 })->store->borrowernumber;
190 $requesters{$branch_3} = Koha::Patron->new({
191 branchcode => $branch_3,
192 categorycode => $category_2,
193 surname => "borrower from $branch_3",
194 })->store->borrowernumber;
196 # Configure rules so that $branch_1 allows only $branch_1 patrons
197 # to request its items, while $branch_2 will allow its items
198 # to fill holds from anywhere.
200 $dbh->do('DELETE FROM issuingrules');
201 $dbh->do('DELETE FROM branch_item_rules');
202 $dbh->do('DELETE FROM branch_borrower_circ_rules');
203 $dbh->do('DELETE FROM default_borrower_circ_rules');
204 $dbh->do('DELETE FROM default_branch_item_rules');
205 $dbh->do('DELETE FROM default_branch_circ_rules');
206 $dbh->do('DELETE FROM default_circ_rules');
208 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
209 VALUES (?, ?, ?, ?)},
214 # CPL allows only its own patrons to request its items
216 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
217 VALUES (?, ?, ?, ?)},
219 $branch_1, 10, 1, 'homebranch',
222 # ... while FPL allows anybody to request its items
224 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
225 VALUES (?, ?, ?, ?)},
227 $branch_2, 10, 2, 'homebranch',
230 # helper biblio for the bug 10272 regression test
231 my $bib2 = MARC::Record->new();
232 $bib2->append_fields(
233 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
234 MARC::Field->new('245', ' ', ' ', a => $title),
237 # create one item belonging to FPL and one belonging to CPL
238 my ( $bibnum2 ) = AddBiblio($bib, $frameworkcode);
239 my ($itemnum_cpl, $itemnum_fpl);
240 ( undef, undef, $itemnum_cpl ) = AddItem(
241 { homebranch => $branch_1,
242 holdingbranch => $branch_1,
243 barcode => 'bug10272_CPL',
248 ( undef, undef, $itemnum_fpl ) = AddItem(
249 { homebranch => $branch_2,
250 holdingbranch => $branch_2,
251 barcode => 'bug10272_FPL',
258 # Ensure that priorities are numbered correcly when a hold is moved to waiting
260 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
261 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
262 $bibitems, 1, $resdate, $expdate, $notes,
263 $title, $checkitem, $found);
264 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
265 $bibitems, 2, $resdate, $expdate, $notes,
266 $title, $checkitem, $found);
267 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
268 $bibitems, 3, $resdate, $expdate, $notes,
269 $title, $checkitem, $found);
270 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
272 # Now it should have different priorities.
273 my $biblio = Koha::Biblios->find( $bibnum2 );
274 my $holds = $biblio->holds({}, { order_by => 'reserve_id' });;
275 is($holds->next->priority, 0, 'Item is correctly waiting');
276 is($holds->next->priority, 1, 'Item is correctly priority 1');
277 is($holds->next->priority, 2, 'Item is correctly priority 2');
279 my @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting();
280 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
281 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
284 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
285 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
286 $bibitems, 1, $resdate, $expdate, $notes,
287 $title, $checkitem, $found);
288 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
289 $bibitems, 2, $resdate, $expdate, $notes,
290 $title, $checkitem, $found);
291 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
292 $bibitems, 3, $resdate, $expdate, $notes,
293 $title, $checkitem, $found);
295 # Ensure that the item's home library controls hold policy lookup
296 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
299 # Return the CPL item at FPL. The hold that should be triggered is
300 # the one placed by the CPL patron, as the other two patron's hold
301 # requests cannot be filled by that item per policy.
302 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
303 is( $messages->{ResFound}->{borrowernumber},
304 $requesters{$branch_1},
305 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
307 # Return the FPL item at FPL. The hold that should be triggered is
308 # the one placed by the RPL patron, as that patron is first in line
309 # and RPL imposes no restrictions on whose holds its items can fill.
311 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
312 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
314 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
315 is( $messages->{ResFound}->{borrowernumber},
316 $requesters{$branch_3},
317 'for generous library, its items fill first hold request in line (bug 10272)');
319 $biblio = Koha::Biblios->find( $biblionumber );
320 $holds = $biblio->holds;
321 is($holds->count, 1, "Only one reserves for this biblio");
322 my $reserve_id = $holds->next->reserve_id;
324 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
325 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
326 # Test 9761a: Add a reserve without date, CheckReserve should return it
327 $resdate= undef; #defaults to today in AddReserve
328 $expdate= undef; #no expdate
329 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
330 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
331 $bibitems, 1, $resdate, $expdate, $notes,
332 $title, $checkitem, $found);
333 ($status)=CheckReserves($itemnumber,undef,undef);
334 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
335 ($status)=CheckReserves($itemnumber,undef,7);
336 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
338 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
339 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
340 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
341 $resdate= dt_from_string();
342 $resdate->add_duration(DateTime::Duration->new(days => 4));
343 $resdate=output_pref($resdate);
344 $expdate= undef; #no expdate
345 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
346 $bibitems, 1, $resdate, $expdate, $notes,
347 $title, $checkitem, $found);
348 ($status)=CheckReserves($itemnumber,undef,undef);
349 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
351 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
352 ($status)=CheckReserves($itemnumber,undef,3);
353 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
354 ($status)=CheckReserves($itemnumber,undef,4);
355 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
357 # Test 9761d: Check ResFound message of AddReturn for future hold
358 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
359 # In this test we do not need an issued item; it is just a 'checkin'
360 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
361 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
362 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
363 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
364 ($doreturn, $messages)= AddReturn('97531',$branch_1);
365 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
366 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
367 ($doreturn, $messages)= AddReturn('97531',$branch_1);
368 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
370 # End of tests for bug 9761 (ConfirmFutureHolds)
372 # test marking a hold as captured
373 my $hold_notice_count = count_hold_print_messages();
374 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
375 my $new_count = count_hold_print_messages();
376 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
378 # test that duplicate notices aren't generated
379 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
380 $new_count = count_hold_print_messages();
381 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
383 # avoiding the not_same_branch error
384 t::lib::Mocks::mock_preference('IndependentBranches', 0);
386 DelItemCheck( $bibnum, $itemnumber),
388 'item that is captured to fill a hold cannot be deleted',
391 my $letter = ReserveSlip( { branchcode => $branch_1, borrowernumber => $requesters{$branch_1}, biblionumber => $bibnum } );
392 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
394 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
395 # 9788a: current_holds does not return future next available hold
396 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
397 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
398 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
399 $resdate= dt_from_string();
400 $resdate->add_duration(DateTime::Duration->new(days => 2));
401 $resdate=output_pref($resdate);
402 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
403 $bibitems, 1, $resdate, $expdate, $notes,
404 $title, $checkitem, $found);
405 my $item = Koha::Items->find( $itemnumber );
406 $holds = $item->current_holds;
407 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
408 my $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
409 is( $future_holds->count, 0, 'current_holds does not return a future next available hold');
410 # 9788b: current_holds does not return future item level hold
411 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
412 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
413 $bibitems, 1, $resdate, $expdate, $notes,
414 $title, $itemnumber, $found); #item level hold
415 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
416 is( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
417 # 9788c: current_holds returns future wait (confirmed future hold)
418 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0); #confirm hold
419 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
420 is( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
421 # End of tests for bug 9788
423 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
424 # Tests for CalculatePriority (bug 8918)
425 my $p = C4::Reserves::CalculatePriority($bibnum2);
426 is($p, 4, 'CalculatePriority should now return priority 4');
428 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum2,
429 $bibitems, $p, $resdate, $expdate, $notes,
430 $title, $checkitem, $found);
431 $p = C4::Reserves::CalculatePriority($bibnum2);
432 is($p, 5, 'CalculatePriority should now return priority 5');
433 #some tests on bibnum
434 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
435 $p = C4::Reserves::CalculatePriority($bibnum);
436 is($p, 1, 'CalculatePriority should now return priority 1');
437 #add a new reserve and confirm it to waiting
438 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
439 $bibitems, $p, $resdate, $expdate, $notes,
440 $title, $itemnumber, $found);
441 $p = C4::Reserves::CalculatePriority($bibnum);
442 is($p, 2, 'CalculatePriority should now return priority 2');
443 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0);
444 $p = C4::Reserves::CalculatePriority($bibnum);
445 is($p, 1, 'CalculatePriority should now return priority 1');
446 #add another biblio hold, no resdate
447 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum,
448 $bibitems, $p, $resdate, $expdate, $notes,
449 $title, $checkitem, $found);
450 $p = C4::Reserves::CalculatePriority($bibnum);
451 is($p, 2, 'CalculatePriority should now return priority 2');
452 #add another future hold
453 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
454 $resdate= dt_from_string();
455 $resdate->add_duration(DateTime::Duration->new(days => 1));
456 AddReserve($branch_1, $requesters{'CPL3'}, $bibnum,
457 $bibitems, $p, output_pref($resdate), $expdate, $notes,
458 $title, $checkitem, $found);
459 $p = C4::Reserves::CalculatePriority($bibnum);
460 is($p, 2, 'CalculatePriority should now still return priority 2');
461 #calc priority with future resdate
462 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
463 is($p, 3, 'CalculatePriority should now return priority 3');
464 # End of tests for bug 8918
466 # Tests for cancel reserves by users from OPAC.
467 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
468 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
469 $bibitems, 1, undef, $expdate, $notes,
470 $title, $checkitem, '');
471 my (undef, $canres, undef) = CheckReserves($itemnumber);
473 is( CanReserveBeCanceledFromOpac(), undef,
474 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
477 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
479 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
482 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
484 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
487 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
488 is($cancancel, 1, 'Can user cancel its own reserve');
490 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
491 is($cancancel, 0, 'Other user cant cancel reserve');
493 ModReserveAffect($itemnumber, $requesters{$branch_1}, 1);
494 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
495 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
497 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
498 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
499 $bibitems, 1, undef, $expdate, $notes,
500 $title, $checkitem, '');
501 (undef, $canres, undef) = CheckReserves($itemnumber);
503 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
504 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
505 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
507 # End of tests for bug 12876
510 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
513 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
515 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
517 #Set the ageRestriction for the Biblio
518 my $record = GetMarcBiblio({ biblionumber => $bibnum });
519 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
520 $record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
521 C4::Biblio::ModBiblio( $record, $bibnum, $frameworkcode );
523 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
525 #Set the dateofbirth for the Borrower making them "too young".
526 $borrower->{dateofbirth} = DateTime->now->add( years => -15 );
527 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
529 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
531 #Set the dateofbirth for the Borrower making them "too old".
532 $borrower->{dateofbirth} = DateTime->now->add( years => -30 );
533 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
535 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
537 ####### EO Bug 13113 <<<
540 $item = GetItem($itemnumber);
542 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $borrower), "Reserving a book on item level" );
544 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
545 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
546 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
547 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
548 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
549 AddReserve($branch_1, $borrowernumber, $item_bibnum,
550 $bibitems, 1, undef, $expdate, $notes, $title, $checkitem, '');
551 MoveReserve( $itemnumber, $borrowernumber );
552 ($status)=CheckReserves( $itemnumber );
553 is( $status, '', 'MoveReserve filled hold');
554 # hold from A waiting, today, no fut holds: MoveReserve should fill it
555 AddReserve($branch_1, $borrowernumber, $item_bibnum,
556 $bibitems, 1, undef, $expdate, $notes, $title, $checkitem, 'W');
557 MoveReserve( $itemnumber, $borrowernumber );
558 ($status)=CheckReserves( $itemnumber );
559 is( $status, '', 'MoveReserve filled waiting hold');
560 # hold from A pos 1, tomorrow, no fut holds: not filled
561 $resdate= dt_from_string();
562 $resdate->add_duration(DateTime::Duration->new(days => 1));
563 $resdate=output_pref($resdate);
564 AddReserve($branch_1, $borrowernumber, $item_bibnum,
565 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
566 MoveReserve( $itemnumber, $borrowernumber );
567 ($status)=CheckReserves( $itemnumber, undef, 1 );
568 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
569 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
570 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
571 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
572 AddReserve($branch_1, $borrowernumber, $item_bibnum,
573 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
574 MoveReserve( $itemnumber, $borrowernumber );
575 ($status)=CheckReserves( $itemnumber, undef, 2 );
576 is( $status, '', 'MoveReserve filled future hold now');
577 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
578 AddReserve($branch_1, $borrowernumber, $item_bibnum,
579 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, 'W');
580 MoveReserve( $itemnumber, $borrowernumber );
581 ($status)=CheckReserves( $itemnumber, undef, 2 );
582 is( $status, '', 'MoveReserve filled future waiting hold now');
583 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
584 $resdate= dt_from_string();
585 $resdate->add_duration(DateTime::Duration->new(days => 3));
586 $resdate=output_pref($resdate);
587 AddReserve($branch_1, $borrowernumber, $item_bibnum,
588 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
589 MoveReserve( $itemnumber, $borrowernumber );
590 ($status)=CheckReserves( $itemnumber, undef, 3 );
591 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
592 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
594 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
595 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
596 $cache->clear_from_cache("default_value_for_mod_marc-");
597 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
599 subtest '_koha_notify_reserve() tests' => sub {
603 my $wants_hold_and_email = {
609 letter_code => 'HOLD'
612 my $mp = Test::MockModule->new( 'C4::Members::Messaging' );
614 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
616 $dbh->do('DELETE FROM letter');
618 my $email_hold_notice = $builder->build({
621 message_transport_type => 'email',
624 module => 'reserves',
629 my $sms_hold_notice = $builder->build({
632 message_transport_type => 'sms',
635 module => 'reserves',
640 my $hold_borrower = $builder->build({
641 source => 'Borrower',
643 smsalertnumber=>'5555555555',
646 })->{borrowernumber};
648 C4::Reserves::AddReserve(
649 $item->{homebranch}, $hold_borrower,
650 $item->{biblionumber} );
652 ModReserveAffect($item->{itemnumber}, $hold_borrower, 0);
653 my $sms_message_address = $schema->resultset('MessageQueue')->search({
654 letter_code => 'HOLD',
655 message_transport_type => 'sms',
656 borrowernumber => $hold_borrower,
657 })->next()->to_address();
658 is($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
660 my $email_message_address = $schema->resultset('MessageQueue')->search({
661 letter_code => 'HOLD',
662 message_transport_type => 'email',
663 borrowernumber => $hold_borrower,
664 })->next()->to_address();
665 is($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
669 subtest 'ReservesNeedReturns' => sub {
672 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
673 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
674 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
676 biblionumber => $biblioitem->biblionumber,
677 biblioitemnumber => $biblioitem->biblioitemnumber,
678 homebranch => $library->branchcode,
679 holdingbranch => $library->branchcode,
680 itype => $itemtype->itemtype,
682 my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
683 my $patron = $builder->build_object(
685 class => 'Koha::Patrons',
686 value => { branchcode => $library->branchcode, }
691 my ( $hold_id, $hold );
693 t::lib::Mocks::mock_preference('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
694 $hold_id = C4::Reserves::AddReserve(
695 $library->branchcode, $patron->borrowernumber,
696 $item->biblionumber, '',
699 "title for fee", $item->itemnumber,
701 $hold = Koha::Holds->find($hold_id);
702 is( $hold->priority, 0, 'If ReservesNeedReturns is 0, priority must have been set to 0' );
703 is( $hold->found, 'W', 'If ReservesNeedReturns is 0, found must have been set waiting' );
705 $hold->delete; # cleanup
707 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # '0' means "Don't automatically mark a hold as found and waiting"
708 $hold_id = C4::Reserves::AddReserve(
709 $library->branchcode, $patron->borrowernumber,
710 $item->biblionumber, '',
713 "title for fee", $item->itemnumber,
715 $hold = Koha::Holds->find($hold_id);
716 is( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
717 is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
720 subtest 'ChargeReserveFee tests' => sub {
724 my $library = $builder->build_object({ class => 'Koha::Libraries' });
725 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
728 my $title = 'A title';
730 my $context = Test::MockModule->new('C4::Context');
731 $context->mock( userenv => { branch => $library->id } );
733 my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title );
735 is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
736 ok( $line->is_debit, 'Generates a debit line' );
737 is( $line->accounttype, 'Res' , 'generates Res accounttype');
738 is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
739 is( $line->amount, $fee , 'amount set correctly');
740 is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
741 is( $line->description, "Reserve Charge - $title" , 'Hardcoded description is generated');
742 is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
745 sub count_hold_print_messages {
746 my $message_count = $dbh->selectall_arrayref(q{
749 WHERE letter_code = 'HOLD'
750 AND message_transport_type = 'print'
752 return $message_count->[0]->[0];
755 # we reached the finish
756 $schema->storage->txn_rollback();