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 => 67;
25 use t::lib::TestBuilder;
28 use DateTime::Duration;
41 use Koha::Notice::Templates;
43 use Koha::Patron::Categories;
44 use Koha::CirculationRules;
47 require_ok('C4::Reserves');
51 my $database = Koha::Database->new();
52 my $schema = $database->schema();
53 $schema->storage->txn_begin();
54 my $dbh = C4::Context->dbh;
55 $dbh->do('DELETE FROM circulation_rules');
57 my $builder = t::lib::TestBuilder->new;
59 my $frameworkcode = q//;
62 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1);
64 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
65 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
66 my $cache = Koha::Caches->get_instance;
67 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
68 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
69 $cache->clear_from_cache("default_value_for_mod_marc-");
70 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
74 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
75 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
76 my $branch_3 = $builder->build({ source => 'Branch' })->{ branchcode };
78 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
79 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
81 my $itemtype = $builder->build(
82 { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
84 t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
86 my $bibnum = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
88 # Create a helper item instance for testing
89 my $item = $builder->build_sample_item({ biblionumber => $bibnum, library => $branch_1, itype => $itemtype });
91 my $biblio_with_no_item = $builder->build_sample_biblio;
93 # Modify item; setting barcode.
94 my $testbarcode = '97531';
95 $item->barcode($testbarcode)->store; # FIXME We should not hardcode a barcode! Also, what's the purpose of this?
99 firstname => 'my firstname',
100 surname => 'my surname',
101 categorycode => $category_1,
102 branchcode => $branch_1,
104 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
105 my $borrowernumber = Koha::Patron->new(\%data)->store->borrowernumber;
106 my $patron = Koha::Patrons->find( $borrowernumber );
107 my $borrower = $patron->unblessed;
108 my $biblionumber = $bibnum;
109 my $barcode = $testbarcode;
111 my $branchcode = Koha::Libraries->search->next->branchcode;
115 branchcode => $branchcode,
116 borrowernumber => $borrowernumber,
117 biblionumber => $biblionumber,
122 my ($status, $reserve, $all_reserves) = CheckReserves($item->itemnumber, $barcode);
124 is($status, "Reserved", "CheckReserves Test 1");
126 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
128 ($status, $reserve, $all_reserves) = CheckReserves($item->itemnumber);
129 is($status, "Reserved", "CheckReserves Test 2");
131 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
132 is($status, "Reserved", "CheckReserves Test 3");
134 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
135 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
137 'ItemHomeLib' eq GetReservesControlBranch(
138 { homebranch => 'ItemHomeLib' },
139 { branchcode => 'PatronHomeLib' }
140 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
142 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
144 'PatronHomeLib' eq GetReservesControlBranch(
145 { homebranch => 'ItemHomeLib' },
146 { branchcode => 'PatronHomeLib' }
147 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
149 t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
152 ### Regression test for bug 10272
155 $requesters{$branch_1} = Koha::Patron->new({
156 branchcode => $branch_1,
157 categorycode => $category_2,
158 surname => "borrower from $branch_1",
159 })->store->borrowernumber;
160 for my $i ( 2 .. 5 ) {
161 $requesters{"CPL$i"} = Koha::Patron->new({
162 branchcode => $branch_1,
163 categorycode => $category_2,
164 surname => "borrower $i from $branch_1",
165 })->store->borrowernumber;
167 $requesters{$branch_2} = Koha::Patron->new({
168 branchcode => $branch_2,
169 categorycode => $category_2,
170 surname => "borrower from $branch_2",
171 })->store->borrowernumber;
172 $requesters{$branch_3} = Koha::Patron->new({
173 branchcode => $branch_3,
174 categorycode => $category_2,
175 surname => "borrower from $branch_3",
176 })->store->borrowernumber;
178 # Configure rules so that $branch_1 allows only $branch_1 patrons
179 # to request its items, while $branch_2 will allow its items
180 # to fill holds from anywhere.
182 $dbh->do('DELETE FROM circulation_rules');
183 Koha::CirculationRules->set_rules(
186 categorycode => undef,
189 reservesallowed => 25,
190 holds_per_record => 1,
195 # CPL allows only its own patrons to request its items
196 Koha::CirculationRules->set_rules(
198 branchcode => $branch_1,
202 returnbranch => 'homebranch',
207 # ... while FPL allows anybody to request its items
208 Koha::CirculationRules->set_rules(
210 branchcode => $branch_2,
214 returnbranch => 'homebranch',
219 my $bibnum2 = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
221 my ($itemnum_cpl, $itemnum_fpl);
222 $itemnum_cpl = $builder->build_sample_item(
224 biblionumber => $bibnum2,
225 library => $branch_1,
226 barcode => 'bug10272_CPL',
230 $itemnum_fpl = $builder->build_sample_item(
232 biblionumber => $bibnum2,
233 library => $branch_2,
234 barcode => 'bug10272_FPL',
239 # Ensure that priorities are numbered correcly when a hold is moved to waiting
241 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
244 branchcode => $branch_3,
245 borrowernumber => $requesters{$branch_3},
246 biblionumber => $bibnum2,
252 branchcode => $branch_2,
253 borrowernumber => $requesters{$branch_2},
254 biblionumber => $bibnum2,
260 branchcode => $branch_1,
261 borrowernumber => $requesters{$branch_1},
262 biblionumber => $bibnum2,
266 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
268 # Now it should have different priorities.
269 my $biblio = Koha::Biblios->find( $bibnum2 );
270 my $holds = $biblio->holds({}, { order_by => 'reserve_id' });;
271 is($holds->next->priority, 0, 'Item is correctly waiting');
272 is($holds->next->priority, 1, 'Item is correctly priority 1');
273 is($holds->next->priority, 2, 'Item is correctly priority 2');
275 my @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting();
276 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
277 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
280 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
283 branchcode => $branch_3,
284 borrowernumber => $requesters{$branch_3},
285 biblionumber => $bibnum2,
291 branchcode => $branch_2,
292 borrowernumber => $requesters{$branch_2},
293 biblionumber => $bibnum2,
300 branchcode => $branch_1,
301 borrowernumber => $requesters{$branch_1},
302 biblionumber => $bibnum2,
307 # Ensure that the item's home library controls hold policy lookup
308 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
311 # Return the CPL item at FPL. The hold that should be triggered is
312 # the one placed by the CPL patron, as the other two patron's hold
313 # requests cannot be filled by that item per policy.
314 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
315 is( $messages->{ResFound}->{borrowernumber},
316 $requesters{$branch_1},
317 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
319 # Return the FPL item at FPL. The hold that should be triggered is
320 # the one placed by the RPL patron, as that patron is first in line
321 # and RPL imposes no restrictions on whose holds its items can fill.
323 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
324 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
326 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
327 is( $messages->{ResFound}->{borrowernumber},
328 $requesters{$branch_3},
329 'for generous library, its items fill first hold request in line (bug 10272)');
331 $biblio = Koha::Biblios->find( $biblionumber );
332 $holds = $biblio->holds;
333 is($holds->count, 1, "Only one reserves for this biblio");
334 $holds->next->reserve_id;
336 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
337 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
338 # Test 9761a: Add a reserve without date, CheckReserve should return it
339 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
342 branchcode => $branch_1,
343 borrowernumber => $requesters{$branch_1},
344 biblionumber => $bibnum,
348 ($status)=CheckReserves($item->itemnumber,undef,undef);
349 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
350 ($status)=CheckReserves($item->itemnumber,undef,7);
351 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
353 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
354 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
355 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
356 my $resdate= dt_from_string();
357 $resdate->add_duration(DateTime::Duration->new(days => 4));
358 $resdate=output_pref($resdate);
359 my $reserve_id = AddReserve(
361 branchcode => $branch_1,
362 borrowernumber => $requesters{$branch_1},
363 biblionumber => $bibnum,
365 reservation_date => $resdate,
368 ($status)=CheckReserves($item->itemnumber,undef,undef);
369 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
371 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
372 ($status)=CheckReserves($item->itemnumber,undef,3);
373 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
374 ($status)=CheckReserves($item->itemnumber,undef,4);
375 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
377 # Test 9761d: Check ResFound message of AddReturn for future hold
378 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
379 # In this test we do not need an issued item; it is just a 'checkin'
380 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
381 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
382 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
383 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
384 ($doreturn, $messages)= AddReturn('97531',$branch_1);
385 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
386 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
387 ($doreturn, $messages)= AddReturn('97531',$branch_1);
388 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
390 # End of tests for bug 9761 (ConfirmFutureHolds)
392 # test marking a hold as captured
393 my $hold_notice_count = count_hold_print_messages();
394 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 0);
395 my $new_count = count_hold_print_messages();
396 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
398 # test that duplicate notices aren't generated
399 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 0);
400 $new_count = count_hold_print_messages();
401 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
403 # avoiding the not_same_branch error
404 t::lib::Mocks::mock_preference('IndependentBranches', 0);
405 $item = Koha::Items->find($item->itemnumber);
409 'item that is captured to fill a hold cannot be deleted',
412 my $letter = ReserveSlip( { branchcode => $branch_1, reserve_id => $reserve_id } );
413 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
415 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
416 # 9788a: current_holds does not return future next available hold
417 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
418 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
419 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
420 $resdate= dt_from_string();
421 $resdate->add_duration(DateTime::Duration->new(days => 2));
422 $resdate=output_pref($resdate);
425 branchcode => $branch_1,
426 borrowernumber => $requesters{$branch_1},
427 biblionumber => $bibnum,
429 reservation_date => $resdate,
433 $holds = $item->current_holds;
434 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
435 my $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
436 is( $future_holds->count, 0, 'current_holds does not return a future next available hold');
437 # 9788b: current_holds does not return future item level hold
438 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
441 branchcode => $branch_1,
442 borrowernumber => $requesters{$branch_1},
443 biblionumber => $bibnum,
445 reservation_date => $resdate,
446 itemnumber => $item->itemnumber,
449 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
450 is( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
451 # 9788c: current_holds returns future wait (confirmed future hold)
452 ModReserveAffect( $item->itemnumber, $requesters{$branch_1} , 0); #confirm hold
453 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
454 is( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
455 # End of tests for bug 9788
457 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
458 # Tests for CalculatePriority (bug 8918)
459 my $p = C4::Reserves::CalculatePriority($bibnum2);
460 is($p, 4, 'CalculatePriority should now return priority 4');
463 branchcode => $branch_1,
464 borrowernumber => $requesters{'CPL2'},
465 biblionumber => $bibnum2,
469 $p = C4::Reserves::CalculatePriority($bibnum2);
470 is($p, 5, 'CalculatePriority should now return priority 5');
471 #some tests on bibnum
472 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
473 $p = C4::Reserves::CalculatePriority($bibnum);
474 is($p, 1, 'CalculatePriority should now return priority 1');
475 #add a new reserve and confirm it to waiting
478 branchcode => $branch_1,
479 borrowernumber => $requesters{$branch_1},
480 biblionumber => $bibnum,
482 itemnumber => $item->itemnumber,
485 $p = C4::Reserves::CalculatePriority($bibnum);
486 is($p, 2, 'CalculatePriority should now return priority 2');
487 ModReserveAffect( $item->itemnumber, $requesters{$branch_1} , 0);
488 $p = C4::Reserves::CalculatePriority($bibnum);
489 is($p, 1, 'CalculatePriority should now return priority 1');
490 #add another biblio hold, no resdate
493 branchcode => $branch_1,
494 borrowernumber => $requesters{'CPL2'},
495 biblionumber => $bibnum,
499 $p = C4::Reserves::CalculatePriority($bibnum);
500 is($p, 2, 'CalculatePriority should now return priority 2');
501 #add another future hold
502 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
503 $resdate= dt_from_string();
504 $resdate->add_duration(DateTime::Duration->new(days => 1));
507 branchcode => $branch_1,
508 borrowernumber => $requesters{'CPL2'},
509 biblionumber => $bibnum,
511 reservation_date => output_pref($resdate),
514 $p = C4::Reserves::CalculatePriority($bibnum);
515 is($p, 2, 'CalculatePriority should now still return priority 2');
516 #calc priority with future resdate
517 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
518 is($p, 3, 'CalculatePriority should now return priority 3');
519 # End of tests for bug 8918
521 # Tests for cancel reserves by users from OPAC.
522 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
525 branchcode => $branch_1,
526 borrowernumber => $requesters{$branch_1},
527 biblionumber => $bibnum,
531 my (undef, $canres, undef) = CheckReserves($item->itemnumber);
533 is( CanReserveBeCanceledFromOpac(), undef,
534 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
537 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
539 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
542 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
544 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
547 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
548 is($cancancel, 1, 'Can user cancel its own reserve');
550 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
551 is($cancancel, 0, 'Other user cant cancel reserve');
553 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 1);
554 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
555 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
557 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
560 branchcode => $branch_1,
561 borrowernumber => $requesters{$branch_1},
562 biblionumber => $bibnum,
566 (undef, $canres, undef) = CheckReserves($item->itemnumber);
568 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 0);
569 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
570 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
572 # End of tests for bug 12876
575 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
578 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
580 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
582 #Set the ageRestriction for the Biblio
583 my $record = GetMarcBiblio({ biblionumber => $bibnum });
584 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
585 $record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
586 C4::Biblio::ModBiblio( $record, $bibnum, $frameworkcode );
588 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
590 #Set the dateofbirth for the Borrower making them "too young".
591 $borrower->{dateofbirth} = DateTime->now->add( years => -15 );
592 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
594 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
596 #Set the dateofbirth for the Borrower making them "too old".
597 $borrower->{dateofbirth} = DateTime->now->add( years => -30 );
598 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
600 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
602 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblio_with_no_item->biblionumber)->{status} , '', "Biblio with no item. Status is empty");
604 ####### EO Bug 13113 <<<
607 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron), "Reserving a book on item level" );
609 my $pickup_branch = $builder->build({ source => 'Branch' })->{ branchcode };
610 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
611 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
612 my $limit = Koha::Item::Transfer::Limit->new(
614 toBranch => $pickup_branch,
615 fromBranch => $item->holdingbranch,
616 itemtype => $item->effective_itemtype,
619 is( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron, $pickup_branch), 0, "Item level request not available due to transfer limit" );
620 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
622 my $categorycode = $borrower->{categorycode};
623 my $holdingbranch = $item->{holdingbranch};
624 Koha::CirculationRules->set_rules(
626 categorycode => $categorycode,
627 itemtype => $item->effective_itemtype,
628 branchcode => $holdingbranch,
635 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
636 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
637 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
638 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
639 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
642 branchcode => $branch_1,
643 borrowernumber => $borrowernumber,
644 biblionumber => $bibnum,
648 MoveReserve( $item->itemnumber, $borrowernumber );
649 ($status)=CheckReserves( $item->itemnumber );
650 is( $status, '', 'MoveReserve filled hold');
651 # hold from A waiting, today, no fut holds: MoveReserve should fill it
654 branchcode => $branch_1,
655 borrowernumber => $borrowernumber,
656 biblionumber => $bibnum,
661 MoveReserve( $item->itemnumber, $borrowernumber );
662 ($status)=CheckReserves( $item->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);
670 branchcode => $branch_1,
671 borrowernumber => $borrowernumber,
672 biblionumber => $bibnum,
674 reservation_date => $resdate,
677 MoveReserve( $item->itemnumber, $borrowernumber );
678 ($status)=CheckReserves( $item->itemnumber, undef, 1 );
679 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
680 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
681 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
682 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
685 branchcode => $branch_1,
686 borrowernumber => $borrowernumber,
687 biblionumber => $bibnum,
689 reservation_date => $resdate,
692 MoveReserve( $item->itemnumber, $borrowernumber );
693 ($status)=CheckReserves( $item->itemnumber, undef, 2 );
694 is( $status, '', 'MoveReserve filled future hold now');
695 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
698 branchcode => $branch_1,
699 borrowernumber => $borrowernumber,
700 biblionumber => $bibnum,
702 reservation_date => $resdate,
705 MoveReserve( $item->itemnumber, $borrowernumber );
706 ($status)=CheckReserves( $item->itemnumber, undef, 2 );
707 is( $status, '', 'MoveReserve filled future waiting hold now');
708 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
709 $resdate= dt_from_string();
710 $resdate->add_duration(DateTime::Duration->new(days => 3));
711 $resdate=output_pref($resdate);
714 branchcode => $branch_1,
715 borrowernumber => $borrowernumber,
716 biblionumber => $bibnum,
718 reservation_date => $resdate,
721 MoveReserve( $item->itemnumber, $borrowernumber );
722 ($status)=CheckReserves( $item->itemnumber, undef, 3 );
723 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
724 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
726 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
727 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
728 $cache->clear_from_cache("default_value_for_mod_marc-");
729 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
731 subtest '_koha_notify_reserve() tests' => sub {
735 my $wants_hold_and_email = {
741 letter_code => 'HOLD'
744 my $mp = Test::MockModule->new( 'C4::Members::Messaging' );
746 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
748 $dbh->do('DELETE FROM letter');
750 my $email_hold_notice = $builder->build({
753 message_transport_type => 'email',
756 module => 'reserves',
761 my $sms_hold_notice = $builder->build({
764 message_transport_type => 'sms',
767 module => 'reserves',
772 my $hold_borrower = $builder->build({
773 source => 'Borrower',
775 smsalertnumber=>'5555555555',
778 })->{borrowernumber};
780 C4::Reserves::AddReserve(
782 branchcode => $item->homebranch,
783 borrowernumber => $hold_borrower,
784 biblionumber => $item->biblionumber,
788 ModReserveAffect($item->itemnumber, $hold_borrower, 0);
789 my $sms_message_address = $schema->resultset('MessageQueue')->search({
790 letter_code => 'HOLD',
791 message_transport_type => 'sms',
792 borrowernumber => $hold_borrower,
793 })->next()->to_address();
794 is($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
796 my $email_message_address = $schema->resultset('MessageQueue')->search({
797 letter_code => 'HOLD',
798 message_transport_type => 'email',
799 borrowernumber => $hold_borrower,
800 })->next()->to_address();
801 is($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
805 subtest 'ReservesNeedReturns' => sub {
808 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
810 homebranch => $library->branchcode,
811 holdingbranch => $library->branchcode,
813 my $item = $builder->build_sample_item($item_info);
814 my $patron = $builder->build_object(
816 class => 'Koha::Patrons',
817 value => { branchcode => $library->branchcode, }
820 my $patron_2 = $builder->build_object(
822 class => 'Koha::Patrons',
823 value => { branchcode => $library->branchcode, }
829 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Test with feature disabled
830 my $hold = place_item_hold( $patron, $item, $library, $priority );
831 is( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
832 is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
835 t::lib::Mocks::mock_preference('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
836 $hold = place_item_hold( $patron, $item, $library, $priority );
837 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
838 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
841 $item->onloan('2010-01-01')->store;
842 $hold = place_item_hold( $patron, $item, $library, $priority );
843 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item onloan priority must be set to 1' );
846 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); # '0' means damaged holds not allowed
847 $item->onloan(undef)->damaged(1)->store;
848 $hold = place_item_hold( $patron, $item, $library, $priority );
849 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item damaged and not allowed holds on damaged items priority must be set to 1' );
851 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); # '0' means damaged holds not allowed
852 $hold = place_item_hold( $patron, $item, $library, $priority );
853 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and damaged holds allowed, priority must have been set to 0' );
854 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and damaged holds allowed, found must have been set waiting' );
857 my $hold_1 = place_item_hold( $patron, $item, $library, $priority );
858 is( $hold_1->found, 'W', 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
859 is( $hold_1->priority, 0, 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
860 $hold = place_item_hold( $patron_2, $item, $library, $priority );
861 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item already on hold priority must be set to 1' );
865 my $transfer = $builder->build_object({
866 class => "Koha::Item::Transfers",
868 itemnumber => $item->itemnumber,
869 datearrived => undef,
870 datecancelled => undef
873 $item->damaged(0)->store;
874 $hold = place_item_hold( $patron, $item, $library, $priority );
875 is( $hold->found, undef, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
876 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
880 $hold = place_item_hold( $patron, $item, $library, $priority );
881 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
882 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
883 $hold_1 = place_item_hold( $patron, $item, $library, $priority );
884 is( $hold_1->priority, 1, 'If ReservesNeedReturns is 0 but item has a hold priority is 1' );
885 $hold_1->suspend(1)->store; # We suspend the hold
886 $hold->delete; # Delete the waiting hold
887 $hold = place_item_hold( $patron, $item, $library, $priority );
888 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and other hold(s) suspended, priority must have been set to 0' );
889 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and other hold(s) suspended, found must have been set waiting' );
894 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Don't affect other tests
897 subtest 'ChargeReserveFee tests' => sub {
901 my $library = $builder->build_object({ class => 'Koha::Libraries' });
902 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
905 my $title = 'A title';
907 my $context = Test::MockModule->new('C4::Context');
908 $context->mock( userenv => { branch => $library->id } );
910 my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title );
912 is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
913 ok( $line->is_debit, 'Generates a debit line' );
914 is( $line->debit_type_code, 'RESERVE' , 'generates RESERVE debit_type');
915 is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
916 is( $line->amount, $fee , 'amount set correctly');
917 is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
918 is( $line->description, "$title" , 'description is title of reserved item');
919 is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
922 subtest 'reserves.item_level_hold' => sub {
925 my $item = $builder->build_sample_item;
926 my $patron = $builder->build_object(
928 class => 'Koha::Patrons',
929 value => { branchcode => $item->homebranch }
933 subtest 'item level hold' => sub {
935 my $reserve_id = AddReserve(
937 branchcode => $item->homebranch,
938 borrowernumber => $patron->borrowernumber,
939 biblionumber => $item->biblionumber,
941 itemnumber => $item->itemnumber,
945 my $hold = Koha::Holds->find($reserve_id);
946 is( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' );
949 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
951 # Revert the waiting status
952 C4::Reserves::RevertWaitingStatus(
953 { itemnumber => $item->itemnumber } );
955 $hold = Koha::Holds->find($reserve_id);
957 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' );
959 $hold->delete; # cleanup
962 subtest 'biblio level hold' => sub {
964 my $reserve_id = AddReserve(
966 branchcode => $item->homebranch,
967 borrowernumber => $patron->borrowernumber,
968 biblionumber => $item->biblionumber,
973 my $hold = Koha::Holds->find($reserve_id);
974 is( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' );
977 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
979 $hold = Koha::Holds->find($reserve_id);
980 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should be set on hold confirmation' );
982 # Revert the waiting status
983 C4::Reserves::RevertWaitingStatus( { itemnumber => $item->itemnumber } );
985 $hold = Koha::Holds->find($reserve_id);
986 is( $hold->itemnumber, undef, 'Itemnumber should be removed when the waiting status is revert' );
993 subtest 'MoveReserve additional test' => sub {
997 # Create the items and patrons we need
998 my $biblio = $builder->build_sample_biblio();
999 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
1000 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
1001 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, notforloan => 0, itype => $itype->itemtype });
1002 my $patron_1 = $builder->build_object({ class => "Koha::Patrons" });
1003 my $patron_2 = $builder->build_object({ class => "Koha::Patrons" });
1005 # Place a hold on the title for both patrons
1006 my $reserve_1 = AddReserve(
1008 branchcode => $item_1->homebranch,
1009 borrowernumber => $patron_1->borrowernumber,
1010 biblionumber => $biblio->biblionumber,
1012 itemnumber => $item_1->itemnumber,
1015 my $reserve_2 = AddReserve(
1017 branchcode => $item_2->homebranch,
1018 borrowernumber => $patron_2->borrowernumber,
1019 biblionumber => $biblio->biblionumber,
1021 itemnumber => $item_1->itemnumber,
1024 is($patron_1->holds->next()->reserve_id, $reserve_1, "The 1st patron has a hold");
1025 is($patron_2->holds->next()->reserve_id, $reserve_2, "The 2nd patron has a hold");
1027 # Fake the holds queue
1028 $dbh->do(q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?,?)},undef,($patron_1->borrowernumber,$biblio->biblionumber,$item_1->itemnumber,$item_1->homebranch,0,$reserve_1));
1030 # The 2nd hold should be filed even if the item is preselected for the first hold
1031 MoveReserve($item_1->itemnumber,$patron_2->borrowernumber);
1032 is($patron_2->holds->count, 0, "The 2nd patrons no longer has a hold");
1033 is($patron_2->old_holds->next()->reserve_id, $reserve_2, "The 2nd patrons hold was filled and moved to old holds");
1037 subtest 'RevertWaitingStatus' => sub {
1041 # Create the items and patrons we need
1042 my $biblio = $builder->build_sample_biblio();
1043 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1044 my $itype = $builder->build_object(
1045 { class => "Koha::ItemTypes", value => { notforloan => 0 } } );
1046 my $item_1 = $builder->build_sample_item(
1048 biblionumber => $biblio->biblionumber,
1049 itype => $itype->itemtype,
1050 library => $library->branchcode
1053 my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } );
1054 my $patron_2 = $builder->build_object( { class => "Koha::Patrons" } );
1055 my $patron_3 = $builder->build_object( { class => "Koha::Patrons" } );
1056 my $patron_4 = $builder->build_object( { class => "Koha::Patrons" } );
1058 # Place a hold on the title for both patrons
1060 my $hold_1 = place_item_hold( $patron_1, $item_1, $library, $priority );
1061 my $hold_2 = place_item_hold( $patron_2, $item_1, $library, $priority );
1062 my $hold_3 = place_item_hold( $patron_3, $item_1, $library, $priority );
1063 my $hold_4 = place_item_hold( $patron_4, $item_1, $library, $priority );
1065 $hold_1->set_waiting;
1066 AddIssue( $patron_3->unblessed, $item_1->barcode, undef, 'revert' );
1068 my $holds = $biblio->holds;
1069 is( $holds->count, 3, 'One hold has been deleted' );
1072 $holds->next->priority, $holds->next->priority,
1073 $holds->next->priority
1076 'priorities have been reordered'
1080 subtest 'CheckReserves additional test' => sub {
1084 my $item = $builder->build_sample_item;
1085 my $reserve1 = $builder->build_object(
1087 class => "Koha::Holds",
1091 itemnumber => undef,
1092 biblionumber => $item->biblionumber,
1093 waitingdate => undef,
1094 cancellationdate => undef,
1095 item_level_hold => 0,
1096 lowestPriority => 0,
1097 expirationdate => undef,
1098 suspend_until => undef,
1104 my $reserve2 = $builder->build_object(
1106 class => "Koha::Holds",
1110 biblionumber => $item->biblionumber,
1111 borrowernumber => $reserve1->borrowernumber,
1112 itemnumber => undef,
1113 waitingdate => undef,
1114 cancellationdate => undef,
1115 item_level_hold => 0,
1116 lowestPriority => 0,
1117 expirationdate => undef,
1118 suspend_until => undef,
1125 my $tmp_holdsqueue = $builder->build(
1127 source => 'TmpHoldsqueue',
1129 borrowernumber => $reserve1->borrowernumber,
1130 biblionumber => $reserve1->biblionumber,
1134 my $fill_target = $builder->build(
1136 source => 'HoldFillTarget',
1138 borrowernumber => $reserve1->borrowernumber,
1139 biblionumber => $reserve1->biblionumber,
1140 itemnumber => $item->itemnumber,
1141 item_level_request => 0,
1146 ModReserveAffect( $item->itemnumber, $reserve1->borrowernumber, 1,
1147 $reserve1->reserve_id );
1148 my ( $status, $matched_reserve, $possible_reserves ) =
1149 CheckReserves( $item->itemnumber );
1151 is( $status, 'Transferred', "We found a reserve" );
1152 is( $matched_reserve->{reserve_id},
1153 $reserve1->reserve_id, "We got the Transit reserve" );
1154 is( scalar @$possible_reserves, 2, 'We do get both reserves' );
1157 subtest 'AllowHoldOnPatronPossession test' => sub {
1161 # Create the items and patrons we need
1162 my $biblio = $builder->build_sample_biblio();
1163 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
1164 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
1165 my $patron = $builder->build_object({ class => "Koha::Patrons",
1166 value => { branchcode => $item->homebranch }});
1168 C4::Circulation::AddIssue($patron->unblessed,
1170 t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 0);
1172 is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
1173 $item->biblionumber)->{status},
1174 'alreadypossession',
1175 'Patron cannot place hold on a book loaned to itself');
1177 is(C4::Reserves::CanItemBeReserved($patron->borrowernumber,
1178 $item->itemnumber)->{status},
1179 'alreadypossession',
1180 'Patron cannot place hold on an item loaned to itself');
1182 t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 1);
1184 is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
1185 $item->biblionumber)->{status},
1187 'Patron can place hold on a book loaned to itself');
1189 is(C4::Reserves::CanItemBeReserved($patron->borrowernumber,
1190 $item->itemnumber)->{status},
1192 'Patron can place hold on an item loaned to itself');
1195 subtest 'MergeHolds' => sub {
1199 my $biblio_1 = $builder->build_sample_biblio();
1200 my $biblio_2 = $builder->build_sample_biblio();
1201 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1202 my $itype = $builder->build_object(
1203 { class => "Koha::ItemTypes", value => { notforloan => 0 } } );
1204 my $item_1 = $builder->build_sample_item(
1206 biblionumber => $biblio_1->biblionumber,
1207 itype => $itype->itemtype,
1208 library => $library->branchcode
1211 my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } );
1213 # Place a hold on $biblio_1
1215 place_item_hold( $patron_1, $item_1, $library, $priority );
1217 # Move and make sure hold is now on $biblio_2
1218 C4::Reserves::MergeHolds($dbh, $biblio_2->biblionumber, $biblio_1->biblionumber);
1219 is( $biblio_2->holds->count, 1, 'Hold has been transferred' );
1222 subtest 'ModReserveAffect logging' => sub {
1226 my $item = $builder->build_sample_item;
1227 my $patron = $builder->build_object(
1229 class => "Koha::Patrons",
1230 value => { branchcode => $item->homebranch }
1234 t::lib::Mocks::mock_userenv({ patron => $patron });
1235 t::lib::Mocks::mock_preference('HoldsLog', 1);
1237 my $reserve_id = AddReserve(
1239 branchcode => $item->homebranch,
1240 borrowernumber => $patron->borrowernumber,
1241 biblionumber => $item->biblionumber,
1243 itemnumber => $item->itemnumber,
1247 my $hold = Koha::Holds->find($reserve_id);
1248 my $previous_timestamp = '1970-01-01 12:34:56';
1249 $hold->timestamp($previous_timestamp)->store;
1251 $hold = Koha::Holds->find($reserve_id);
1252 is( $hold->timestamp, $previous_timestamp, 'Make sure the previous timestamp has been used' );
1255 ModReserveAffect( $item->itemnumber, $patron->borrowernumber );
1257 $hold = Koha::Holds->find($reserve_id);
1258 is( $hold->found, 'W', 'Hold has been set waiting' );
1259 isnt( $hold->timestamp, $previous_timestamp, 'The timestamp has been modified' );
1261 my $log = Koha::ActionLogs->search({ module => 'HOLDS', action => 'MODIFY', object => $hold->reserve_id })->next;
1262 my $expected = sprintf q{'timestamp' => '%s'}, $hold->timestamp;
1263 like( $log->info, qr{$expected}, 'Timestamp logged is the current one' );
1266 sub count_hold_print_messages {
1267 my $message_count = $dbh->selectall_arrayref(q{
1270 WHERE letter_code = 'HOLD'
1271 AND message_transport_type = 'print'
1273 return $message_count->[0]->[0];
1276 sub place_item_hold {
1277 my ($patron,$item,$library,$priority) = @_;
1279 my $hold_id = C4::Reserves::AddReserve(
1281 branchcode => $library->branchcode,
1282 borrowernumber => $patron->borrowernumber,
1283 biblionumber => $item->biblionumber,
1284 priority => $priority,
1285 title => "title for fee",
1286 itemnumber => $item->itemnumber,
1290 my $hold = Koha::Holds->find($hold_id);
1294 # we reached the finish
1295 $schema->storage->txn_rollback();