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 => 77;
25 use t::lib::TestBuilder;
28 use DateTime::Duration;
30 use C4::Circulation qw( AddReturn AddIssue );
32 use C4::Biblio qw( GetMarcFromKohaField ModBiblio );
34 use C4::Reserves qw( AddReserve AlterPriority CheckReserves GetReservesControlBranch ModReserve ModReserveAffect ReserveSlip CalculatePriority CanReserveBeCanceledFromOpac CanBookBeReserved IsAvailableForItemLevelRequest MoveReserve ChargeReserveFee RevertWaitingStatus CanItemBeReserved MergeHolds );
38 use Koha::DateUtils qw( dt_from_string output_pref );
42 use Koha::Notice::Templates;
44 use Koha::Patron::Categories;
45 use Koha::CirculationRules;
48 require_ok('C4::Reserves');
52 my $database = Koha::Database->new();
53 my $schema = $database->schema();
54 $schema->storage->txn_begin();
55 my $dbh = C4::Context->dbh;
56 $dbh->do('DELETE FROM circulation_rules');
58 my $builder = t::lib::TestBuilder->new;
60 my $frameworkcode = q//;
63 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1);
65 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
66 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
67 my $cache = Koha::Caches->get_instance;
68 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
69 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
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,
201 holdallowed => 'from_home_library',
202 returnbranch => 'homebranch',
207 # ... while FPL allows anybody to request its items
208 Koha::CirculationRules->set_rules(
210 branchcode => $branch_2,
213 holdallowed => 'from_any_library',
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->as_list;
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 my $reserve_id = AddReserve(
360 branchcode => $branch_1,
361 borrowernumber => $requesters{$branch_1},
362 biblionumber => $bibnum,
364 reservation_date => $resdate,
367 ($status)=CheckReserves($item->itemnumber,undef,undef);
368 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
370 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
371 ($status)=CheckReserves($item->itemnumber,undef,3);
372 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
373 ($status)=CheckReserves($item->itemnumber,undef,4);
374 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
376 # Test 9761d: Check ResFound message of AddReturn for future hold
377 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
378 # In this test we do not need an issued item; it is just a 'checkin'
379 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
380 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
381 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
382 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
383 ($doreturn, $messages)= AddReturn('97531',$branch_1);
384 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
385 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
386 ($doreturn, $messages)= AddReturn('97531',$branch_1);
387 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
389 my $now_holder = $builder->build_object({ class => 'Koha::Patrons', value => {
390 branchcode => $branch_1,
392 my $now_reserve_id = AddReserve(
394 branchcode => $branch_1,
395 borrowernumber => $requesters{$branch_1},
396 biblionumber => $bibnum,
398 reservation_date => dt_from_string(),
402 ($status,$which_highest)=CheckReserves($item->itemnumber,undef,3);
403 is( $which_highest->{reserve_id}, $now_reserve_id, 'CheckReserves returns lower priority current reserve with insufficient lookahead');
404 ($status, $which_highest)=CheckReserves($item->itemnumber,undef,4);
405 is( $which_highest->{reserve_id}, $reserve_id, 'CheckReserves returns higher priority future reserve with sufficient lookahead');
406 ModReserve({ reserve_id => $now_reserve_id, rank => 'del', cancellation_reason => 'test reserve' });
409 # End of tests for bug 9761 (ConfirmFutureHolds)
412 # test marking a hold as captured
413 my $hold_notice_count = count_hold_print_messages();
414 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 0);
415 my $new_count = count_hold_print_messages();
416 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
418 # test that duplicate notices aren't generated
419 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 0);
420 $new_count = count_hold_print_messages();
421 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
423 # avoiding the not_same_branch error
424 t::lib::Mocks::mock_preference('IndependentBranches', 0);
425 $item = Koha::Items->find($item->itemnumber);
427 @{$item->safe_delete->messages}[0]->message,
429 'item that is captured to fill a hold cannot be deleted',
432 my $letter = ReserveSlip( { branchcode => $branch_1, reserve_id => $reserve_id } );
433 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
435 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
436 # 9788a: current_holds does not return future next available hold
437 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
438 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
439 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
440 $resdate= dt_from_string();
441 $resdate->add_duration(DateTime::Duration->new(days => 2));
444 branchcode => $branch_1,
445 borrowernumber => $requesters{$branch_1},
446 biblionumber => $bibnum,
448 reservation_date => $resdate,
452 $holds = $item->current_holds;
453 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
454 my $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
455 is( $future_holds->count, 0, 'current_holds does not return a future next available hold');
456 # 9788b: current_holds does not return future item level hold
457 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
460 branchcode => $branch_1,
461 borrowernumber => $requesters{$branch_1},
462 biblionumber => $bibnum,
464 reservation_date => $resdate,
465 itemnumber => $item->itemnumber,
468 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
469 is( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
470 # 9788c: current_holds returns future wait (confirmed future hold)
471 ModReserveAffect( $item->itemnumber, $requesters{$branch_1} , 0); #confirm hold
472 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
473 is( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
474 # End of tests for bug 9788
476 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
477 # Tests for CalculatePriority (bug 8918)
478 my $p = C4::Reserves::CalculatePriority($bibnum2);
479 is($p, 4, 'CalculatePriority should now return priority 4');
482 branchcode => $branch_1,
483 borrowernumber => $requesters{'CPL2'},
484 biblionumber => $bibnum2,
488 $p = C4::Reserves::CalculatePriority($bibnum2);
489 is($p, 5, 'CalculatePriority should now return priority 5');
490 #some tests on bibnum
491 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
492 $p = C4::Reserves::CalculatePriority($bibnum);
493 is($p, 1, 'CalculatePriority should now return priority 1');
494 #add a new reserve and confirm it to waiting
497 branchcode => $branch_1,
498 borrowernumber => $requesters{$branch_1},
499 biblionumber => $bibnum,
501 itemnumber => $item->itemnumber,
504 $p = C4::Reserves::CalculatePriority($bibnum);
505 is($p, 2, 'CalculatePriority should now return priority 2');
506 ModReserveAffect( $item->itemnumber, $requesters{$branch_1} , 0);
507 $p = C4::Reserves::CalculatePriority($bibnum);
508 is($p, 1, 'CalculatePriority should now return priority 1');
509 #add another biblio hold, no resdate
512 branchcode => $branch_1,
513 borrowernumber => $requesters{'CPL2'},
514 biblionumber => $bibnum,
518 $p = C4::Reserves::CalculatePriority($bibnum);
519 is($p, 2, 'CalculatePriority should now return priority 2');
520 #add another future hold
521 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
522 $resdate= dt_from_string();
523 $resdate->add_duration(DateTime::Duration->new(days => 1));
526 branchcode => $branch_1,
527 borrowernumber => $requesters{'CPL2'},
528 biblionumber => $bibnum,
530 reservation_date => $resdate,
533 $p = C4::Reserves::CalculatePriority($bibnum);
534 is($p, 2, 'CalculatePriority should now still return priority 2');
535 #calc priority with future resdate
536 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
537 is($p, 3, 'CalculatePriority should now return priority 3');
538 # End of tests for bug 8918
540 # regression test for bug 12630
541 # Now there are 2 reserves on $bibnum
542 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
543 my $bor_tmp_1 = $builder->build_object({ class => 'Koha::Patrons',value =>{
544 firstname => 'my firstname tmp 1',
545 surname => 'my surname tmp 1',
549 my $bor_tmp_2 = $builder->build_object({ class => 'Koha::Patrons',value =>{
550 firstname => 'my firstname tmp 2',
551 surname => 'my surname tmp 2',
555 my $borrowernumber_tmp_1 = $bor_tmp_1->borrowernumber;
556 my $borrowernumber_tmp_2 = $bor_tmp_2->borrowernumber;
557 my $date_in_future = dt_from_string();
558 $date_in_future = $date_in_future->add_duration(DateTime::Duration->new(days => 1));
561 borrowernumber => $borrowernumber_tmp_1,
562 biblionumber => $bibnum,
564 reservation_date => $date_in_future
568 borrowernumber => $borrowernumber_tmp_2,
569 biblionumber => $bibnum,
571 reservation_date => $date_in_future
573 my @r1 = Koha::Holds->search({ borrowernumber => $borrowernumber_tmp_1 })->as_list;
574 my @r2 = Koha::Holds->search({ borrowernumber => $borrowernumber_tmp_2 })->as_list;
575 is( $r1[0]->priority, 3, 'priority for hold in future should be correct');
576 is( $r2[0]->priority, 4, 'priority for hold not in future should be correct');
577 # end of tests for bug 12630
579 # Tests for cancel reserves by users from OPAC.
580 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
583 branchcode => $branch_1,
584 borrowernumber => $requesters{$branch_1},
585 biblionumber => $bibnum,
589 my (undef, $canres, undef) = CheckReserves($item->itemnumber);
591 is( CanReserveBeCanceledFromOpac(), undef,
592 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
595 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
597 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
600 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
602 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
605 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
606 is($cancancel, 1, 'Can user cancel its own reserve');
608 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
609 is($cancancel, 0, 'Other user cant cancel reserve');
611 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 1);
612 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
613 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
615 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
616 is( CanReserveBeCanceledFromOpac($canres->{resserve_id}, $requesters{$branch_1}), undef,
617 'Cannot cancel a deleted hold' );
621 branchcode => $branch_1,
622 borrowernumber => $requesters{$branch_1},
623 biblionumber => $bibnum,
627 (undef, $canres, undef) = CheckReserves($item->itemnumber);
629 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 0);
630 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
631 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
633 # End of tests for bug 12876
636 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
639 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
641 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
643 #Set the ageRestriction for the Biblio
644 $biblio = Koha::Biblios->find($bibnum);
645 my $record = $biblio->metadata->record;
646 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
647 $record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
648 C4::Biblio::ModBiblio( $record, $bibnum, $frameworkcode );
650 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
652 #Set the dateofbirth for the Borrower making them "too young".
653 $borrower->{dateofbirth} = DateTime->now->add( years => -15 );
654 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
656 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
658 #Set the dateofbirth for the Borrower making them "too old".
659 $borrower->{dateofbirth} = DateTime->now->add( years => -30 );
660 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
662 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
664 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblio_with_no_item->biblionumber)->{status} , '', "Biblio with no item. Status is empty");
666 ####### EO Bug 13113 <<<
669 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron), "Reserving a book on item level" );
671 my $pickup_branch = $builder->build({ source => 'Branch' })->{ branchcode };
672 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
673 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
674 my $limit = Koha::Item::Transfer::Limit->new(
676 toBranch => $pickup_branch,
677 fromBranch => $item->holdingbranch,
678 itemtype => $item->effective_itemtype,
681 is( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron, $pickup_branch), 0, "Item level request not available due to transfer limit" );
682 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
684 my $categorycode = $borrower->{categorycode};
685 my $holdingbranch = $item->{holdingbranch};
686 Koha::CirculationRules->set_rules(
688 categorycode => $categorycode,
689 itemtype => $item->effective_itemtype,
690 branchcode => $holdingbranch,
697 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
698 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
699 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
700 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
701 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
704 branchcode => $branch_1,
705 borrowernumber => $borrowernumber,
706 biblionumber => $bibnum,
710 MoveReserve( $item->itemnumber, $borrowernumber );
711 ($status)=CheckReserves( $item->itemnumber );
712 is( $status, '', 'MoveReserve filled hold');
713 # hold from A waiting, today, no fut holds: MoveReserve should fill it
716 branchcode => $branch_1,
717 borrowernumber => $borrowernumber,
718 biblionumber => $bibnum,
723 MoveReserve( $item->itemnumber, $borrowernumber );
724 ($status)=CheckReserves( $item->itemnumber );
725 is( $status, '', 'MoveReserve filled waiting hold');
726 # hold from A pos 1, tomorrow, no fut holds: not filled
727 $resdate= dt_from_string();
728 $resdate->add_duration(DateTime::Duration->new(days => 1));
731 branchcode => $branch_1,
732 borrowernumber => $borrowernumber,
733 biblionumber => $bibnum,
735 reservation_date => $resdate,
738 MoveReserve( $item->itemnumber, $borrowernumber );
739 ($status)=CheckReserves( $item->itemnumber, undef, 1 );
740 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
741 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
742 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
743 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
746 branchcode => $branch_1,
747 borrowernumber => $borrowernumber,
748 biblionumber => $bibnum,
750 reservation_date => $resdate,
753 MoveReserve( $item->itemnumber, $borrowernumber );
754 ($status)=CheckReserves( $item->itemnumber, undef, 2 );
755 is( $status, '', 'MoveReserve filled future hold now');
756 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
759 branchcode => $branch_1,
760 borrowernumber => $borrowernumber,
761 biblionumber => $bibnum,
763 reservation_date => $resdate,
766 MoveReserve( $item->itemnumber, $borrowernumber );
767 ($status)=CheckReserves( $item->itemnumber, undef, 2 );
768 is( $status, '', 'MoveReserve filled future waiting hold now');
769 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
770 $resdate= dt_from_string();
771 $resdate->add_duration(DateTime::Duration->new(days => 3));
774 branchcode => $branch_1,
775 borrowernumber => $borrowernumber,
776 biblionumber => $bibnum,
778 reservation_date => $resdate,
781 MoveReserve( $item->itemnumber, $borrowernumber );
782 ($status)=CheckReserves( $item->itemnumber, undef, 3 );
783 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
784 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
786 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
787 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
788 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
790 subtest '_koha_notify_reserve() tests' => sub {
794 my $wants_hold_and_email = {
800 letter_code => 'HOLD'
803 my $mp = Test::MockModule->new( 'C4::Members::Messaging' );
805 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
807 $dbh->do('DELETE FROM letter');
809 my $email_hold_notice = $builder->build({
812 message_transport_type => 'email',
815 module => 'reserves',
820 my $sms_hold_notice = $builder->build({
823 message_transport_type => 'sms',
826 module => 'reserves',
831 my $hold_borrower = $builder->build({
832 source => 'Borrower',
834 smsalertnumber=>'5555555555',
837 })->{borrowernumber};
839 C4::Reserves::AddReserve(
841 branchcode => $item->homebranch,
842 borrowernumber => $hold_borrower,
843 biblionumber => $item->biblionumber,
847 ModReserveAffect($item->itemnumber, $hold_borrower, 0);
848 my $sms_message_address = $schema->resultset('MessageQueue')->search({
849 letter_code => 'HOLD',
850 message_transport_type => 'sms',
851 borrowernumber => $hold_borrower,
852 })->next()->to_address();
853 is($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
855 my $email_message_address = $schema->resultset('MessageQueue')->search({
856 letter_code => 'HOLD',
857 message_transport_type => 'email',
858 borrowernumber => $hold_borrower,
859 })->next()->to_address();
860 is($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
864 subtest 'ReservesNeedReturns' => sub {
867 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
869 homebranch => $library->branchcode,
870 holdingbranch => $library->branchcode,
872 my $item = $builder->build_sample_item($item_info);
873 my $patron = $builder->build_object(
875 class => 'Koha::Patrons',
876 value => { branchcode => $library->branchcode, }
879 my $patron_2 = $builder->build_object(
881 class => 'Koha::Patrons',
882 value => { branchcode => $library->branchcode, }
888 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Test with feature disabled
889 my $hold = place_item_hold( $patron, $item, $library, $priority );
890 is( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
891 is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
894 t::lib::Mocks::mock_preference('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
895 $hold = place_item_hold( $patron, $item, $library, $priority );
896 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
897 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
900 $item->onloan('2010-01-01')->store;
901 $hold = place_item_hold( $patron, $item, $library, $priority );
902 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item onloan priority must be set to 1' );
905 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); # '0' means damaged holds not allowed
906 $item->onloan(undef)->damaged(1)->store;
907 $hold = place_item_hold( $patron, $item, $library, $priority );
908 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item damaged and not allowed holds on damaged items priority must be set to 1' );
910 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); # '0' means damaged holds not allowed
911 $hold = place_item_hold( $patron, $item, $library, $priority );
912 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and damaged holds allowed, priority must have been set to 0' );
913 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and damaged holds allowed, found must have been set waiting' );
916 my $hold_1 = place_item_hold( $patron, $item, $library, $priority );
917 is( $hold_1->found, 'W', 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
918 is( $hold_1->priority, 0, 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
919 $hold = place_item_hold( $patron_2, $item, $library, $priority );
920 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item already on hold priority must be set to 1' );
924 my $transfer = $builder->build_object({
925 class => "Koha::Item::Transfers",
927 itemnumber => $item->itemnumber,
928 datearrived => undef,
929 datecancelled => undef
932 $item->damaged(0)->store;
933 $hold = place_item_hold( $patron, $item, $library, $priority );
934 is( $hold->found, undef, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
935 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
939 $hold = place_item_hold( $patron, $item, $library, $priority );
940 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
941 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
942 $hold_1 = place_item_hold( $patron, $item, $library, $priority );
943 is( $hold_1->priority, 1, 'If ReservesNeedReturns is 0 but item has a hold priority is 1' );
944 $hold_1->suspend(1)->store; # We suspend the hold
945 $hold->delete; # Delete the waiting hold
946 $hold = place_item_hold( $patron, $item, $library, $priority );
947 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and other hold(s) suspended, priority must have been set to 0' );
948 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and other hold(s) suspended, found must have been set waiting' );
953 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Don't affect other tests
956 subtest 'ChargeReserveFee tests' => sub {
960 my $library = $builder->build_object({ class => 'Koha::Libraries' });
961 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
964 my $title = 'A title';
966 my $context = Test::MockModule->new('C4::Context');
967 $context->mock( userenv => { branch => $library->id } );
969 my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title );
971 is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
972 ok( $line->is_debit, 'Generates a debit line' );
973 is( $line->debit_type_code, 'RESERVE' , 'generates RESERVE debit_type');
974 is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
975 is( $line->amount, $fee , 'amount set correctly');
976 is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
977 is( $line->description, "$title" , 'description is title of reserved item');
978 is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
981 subtest 'reserves.item_level_hold' => sub {
984 my $item = $builder->build_sample_item;
985 my $patron = $builder->build_object(
987 class => 'Koha::Patrons',
988 value => { branchcode => $item->homebranch }
992 subtest 'item level hold' => sub {
994 my $reserve_id = AddReserve(
996 branchcode => $item->homebranch,
997 borrowernumber => $patron->borrowernumber,
998 biblionumber => $item->biblionumber,
1000 itemnumber => $item->itemnumber,
1004 my $hold = Koha::Holds->find($reserve_id);
1005 is( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' );
1008 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
1010 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1011 $mock->mock( 'enqueue', sub {
1012 my ( $self, $args ) = @_;
1014 $args->{biblio_ids},
1015 [ $hold->biblionumber ],
1016 "AlterPriority triggers a holds queue update for the related biblio"
1020 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1022 # Revert the waiting status
1023 C4::Reserves::RevertWaitingStatus(
1024 { itemnumber => $item->itemnumber } );
1026 $hold = Koha::Holds->find($reserve_id);
1028 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' );
1030 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1034 # Revert the waiting status, RealTimeHoldsQueue => shouldn't add a test
1035 C4::Reserves::RevertWaitingStatus(
1036 { itemnumber => $item->itemnumber } );
1038 $hold->delete; # cleanup
1041 subtest 'biblio level hold' => sub {
1043 my $reserve_id = AddReserve(
1045 branchcode => $item->homebranch,
1046 borrowernumber => $patron->borrowernumber,
1047 biblionumber => $item->biblionumber,
1052 my $hold = Koha::Holds->find($reserve_id);
1053 is( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' );
1056 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
1058 $hold = Koha::Holds->find($reserve_id);
1059 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should be set on hold confirmation' );
1061 # Revert the waiting status
1062 C4::Reserves::RevertWaitingStatus( { itemnumber => $item->itemnumber } );
1064 $hold = Koha::Holds->find($reserve_id);
1065 is( $hold->itemnumber, undef, 'Itemnumber should be removed when the waiting status is revert' );
1072 subtest 'MoveReserve additional test' => sub {
1076 # Create the items and patrons we need
1077 my $biblio = $builder->build_sample_biblio();
1078 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
1079 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
1080 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, notforloan => 0, itype => $itype->itemtype });
1081 my $patron_1 = $builder->build_object({ class => "Koha::Patrons" });
1082 my $patron_2 = $builder->build_object({ class => "Koha::Patrons" });
1084 # Place a hold on the title for both patrons
1085 my $reserve_1 = AddReserve(
1087 branchcode => $item_1->homebranch,
1088 borrowernumber => $patron_1->borrowernumber,
1089 biblionumber => $biblio->biblionumber,
1091 itemnumber => $item_1->itemnumber,
1094 my $reserve_2 = AddReserve(
1096 branchcode => $item_2->homebranch,
1097 borrowernumber => $patron_2->borrowernumber,
1098 biblionumber => $biblio->biblionumber,
1100 itemnumber => $item_1->itemnumber,
1103 is($patron_1->holds->next()->reserve_id, $reserve_1, "The 1st patron has a hold");
1104 is($patron_2->holds->next()->reserve_id, $reserve_2, "The 2nd patron has a hold");
1106 # Fake the holds queue
1107 $dbh->do(q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?,?)},undef,($patron_1->borrowernumber,$biblio->biblionumber,$item_1->itemnumber,$item_1->homebranch,0,$reserve_1));
1109 # The 2nd hold should be filed even if the item is preselected for the first hold
1110 MoveReserve($item_1->itemnumber,$patron_2->borrowernumber);
1111 is($patron_2->holds->count, 0, "The 2nd patrons no longer has a hold");
1112 is($patron_2->old_holds->next()->reserve_id, $reserve_2, "The 2nd patrons hold was filled and moved to old holds");
1116 subtest 'RevertWaitingStatus' => sub {
1120 # Create the items and patrons we need
1121 my $biblio = $builder->build_sample_biblio();
1122 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1123 my $itype = $builder->build_object(
1124 { class => "Koha::ItemTypes", value => { notforloan => 0 } } );
1125 my $item_1 = $builder->build_sample_item(
1127 biblionumber => $biblio->biblionumber,
1128 itype => $itype->itemtype,
1129 library => $library->branchcode
1132 my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } );
1133 my $patron_2 = $builder->build_object( { class => "Koha::Patrons" } );
1134 my $patron_3 = $builder->build_object( { class => "Koha::Patrons" } );
1135 my $patron_4 = $builder->build_object( { class => "Koha::Patrons" } );
1137 # Place a hold on the title for both patrons
1139 my $hold_1 = place_item_hold( $patron_1, $item_1, $library, $priority );
1140 my $hold_2 = place_item_hold( $patron_2, $item_1, $library, $priority );
1141 my $hold_3 = place_item_hold( $patron_3, $item_1, $library, $priority );
1142 my $hold_4 = place_item_hold( $patron_4, $item_1, $library, $priority );
1144 $hold_1->set_waiting;
1145 AddIssue( $patron_3->unblessed, $item_1->barcode, undef, 'revert' );
1147 my $holds = $biblio->holds;
1148 is( $holds->count, 3, 'One hold has been deleted' );
1151 $holds->next->priority, $holds->next->priority,
1152 $holds->next->priority
1155 'priorities have been reordered'
1159 subtest 'CheckReserves additional tests' => sub {
1163 my $item = $builder->build_sample_item;
1164 my $reserve1 = $builder->build_object(
1166 class => "Koha::Holds",
1170 itemnumber => undef,
1171 biblionumber => $item->biblionumber,
1172 waitingdate => undef,
1173 cancellationdate => undef,
1174 item_level_hold => 0,
1175 lowestPriority => 0,
1176 expirationdate => undef,
1177 suspend_until => undef,
1183 my $reserve2 = $builder->build_object(
1185 class => "Koha::Holds",
1189 biblionumber => $item->biblionumber,
1190 borrowernumber => $reserve1->borrowernumber,
1191 itemnumber => undef,
1192 waitingdate => undef,
1193 cancellationdate => undef,
1194 item_level_hold => 0,
1195 lowestPriority => 0,
1196 expirationdate => undef,
1197 suspend_until => undef,
1204 my $tmp_holdsqueue = $builder->build(
1206 source => 'TmpHoldsqueue',
1208 borrowernumber => $reserve1->borrowernumber,
1209 biblionumber => $reserve1->biblionumber,
1213 my $fill_target = $builder->build(
1215 source => 'HoldFillTarget',
1217 borrowernumber => $reserve1->borrowernumber,
1218 biblionumber => $reserve1->biblionumber,
1219 itemnumber => $item->itemnumber,
1220 item_level_request => 0,
1225 ModReserveAffect( $item->itemnumber, $reserve1->borrowernumber, 1,
1226 $reserve1->reserve_id );
1227 my ( $status, $matched_reserve, $possible_reserves ) =
1228 CheckReserves( $item->itemnumber );
1230 is( $status, 'Transferred', "We found a reserve" );
1231 is( $matched_reserve->{reserve_id},
1232 $reserve1->reserve_id, "We got the Transit reserve" );
1233 is( scalar @$possible_reserves, 2, 'We do get both reserves' );
1235 my $patron_B = $builder->build_object({ class => "Koha::Patrons" });
1236 my $item_A = $builder->build_sample_item;
1237 my $item_B = $builder->build_sample_item({
1238 homebranch => $patron_B->branchcode,
1239 biblionumber => $item_A->biblionumber,
1240 itype => $item_A->itype
1242 Koha::CirculationRules->set_rules(
1244 branchcode => undef,
1245 categorycode => undef,
1246 itemtype => $item_A->itype,
1248 reservesallowed => 25,
1249 holds_per_record => 1,
1253 Koha::CirculationRules->set_rule({
1254 branchcode => undef,
1255 itemtype => $item_A->itype,
1256 rule_name => 'holdallowed',
1257 rule_value => 'from_home_library'
1259 my $reserve_id = AddReserve(
1261 branchcode => $patron_B->branchcode,
1262 borrowernumber => $patron_B->borrowernumber,
1263 biblionumber => $item_A->biblionumber,
1265 itemnumber => undef,
1269 ok( $reserve_id, "We can place a record level hold because one item is owned by patron's home library");
1270 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1271 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1272 is( $status, "", "We do not fill the hold with item A because it is not from the patron's homebranch");
1273 Koha::CirculationRules->set_rule({
1274 branchcode => $item_A->homebranch,
1275 itemtype => $item_A->itype,
1276 rule_name => 'holdallowed',
1277 rule_value => 'from_any_library'
1279 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1280 is( $status, "Reserved", "We fill the hold with item A because item's branch rule says allow any");
1283 # Changing the control branch should change only the rule we get
1284 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
1285 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1286 is( $status, "", "We do not fill the hold with item A because it is not from the patron's homebranch");
1287 Koha::CirculationRules->set_rule({
1288 branchcode => $patron_B->branchcode,
1289 itemtype => $item_A->itype,
1290 rule_name => 'holdallowed',
1291 rule_value => 'from_any_library'
1293 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1294 is( $status, "Reserved", "We fill the hold with item A because patron's branch rule says allow any");
1298 subtest 'AllowHoldOnPatronPossession test' => sub {
1302 # Create the items and patrons we need
1303 my $biblio = $builder->build_sample_biblio();
1304 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
1305 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
1306 my $patron = $builder->build_object({ class => "Koha::Patrons",
1307 value => { branchcode => $item->homebranch }});
1309 C4::Circulation::AddIssue($patron->unblessed,
1311 t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 0);
1313 is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
1314 $item->biblionumber)->{status},
1315 'alreadypossession',
1316 'Patron cannot place hold on a book loaned to itself');
1318 is(C4::Reserves::CanItemBeReserved( $patron, $item )->{status},
1319 'alreadypossession',
1320 'Patron cannot place hold on an item loaned to itself');
1322 t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 1);
1324 is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
1325 $item->biblionumber)->{status},
1327 'Patron can place hold on a book loaned to itself');
1329 is(C4::Reserves::CanItemBeReserved( $patron, $item )->{status},
1331 'Patron can place hold on an item loaned to itself');
1334 subtest 'MergeHolds' => sub {
1338 my $biblio_1 = $builder->build_sample_biblio();
1339 my $biblio_2 = $builder->build_sample_biblio();
1340 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1341 my $itype = $builder->build_object(
1342 { class => "Koha::ItemTypes", value => { notforloan => 0 } } );
1343 my $item_1 = $builder->build_sample_item(
1345 biblionumber => $biblio_1->biblionumber,
1346 itype => $itype->itemtype,
1347 library => $library->branchcode
1350 my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } );
1352 # Place a hold on $biblio_1
1354 place_item_hold( $patron_1, $item_1, $library, $priority );
1356 # Move and make sure hold is now on $biblio_2
1357 C4::Reserves::MergeHolds($dbh, $biblio_2->biblionumber, $biblio_1->biblionumber);
1358 is( $biblio_2->holds->count, 1, 'Hold has been transferred' );
1361 subtest 'ModReserveAffect logging' => sub {
1365 my $item = $builder->build_sample_item;
1366 my $patron = $builder->build_object(
1368 class => "Koha::Patrons",
1369 value => { branchcode => $item->homebranch }
1373 t::lib::Mocks::mock_userenv({ patron => $patron });
1374 t::lib::Mocks::mock_preference('HoldsLog', 1);
1376 my $reserve_id = AddReserve(
1378 branchcode => $item->homebranch,
1379 borrowernumber => $patron->borrowernumber,
1380 biblionumber => $item->biblionumber,
1382 itemnumber => $item->itemnumber,
1386 my $hold = Koha::Holds->find($reserve_id);
1387 my $previous_timestamp = '1970-01-01 12:34:56';
1388 $hold->timestamp($previous_timestamp)->store;
1390 $hold = Koha::Holds->find($reserve_id);
1391 is( $hold->timestamp, $previous_timestamp, 'Make sure the previous timestamp has been used' );
1394 my $reserve_mock = Test::MockModule->new('C4::Reserves');
1395 $reserve_mock->mock( '_koha_notify_reserve', undef );
1398 ModReserveAffect( $item->itemnumber, $patron->borrowernumber );
1400 $hold->discard_changes;
1401 ok( $hold->is_waiting, 'Hold has been set waiting' );
1402 isnt( $hold->timestamp, $previous_timestamp, 'The timestamp has been modified' );
1404 my $log = Koha::ActionLogs->search({ module => 'HOLDS', action => 'MODIFY', object => $hold->reserve_id })->next;
1405 my $expected = sprintf q{'timestamp' => '%s'}, $hold->timestamp;
1406 like( $log->info, qr{$expected}, 'Timestamp logged is the current one' );
1409 sub count_hold_print_messages {
1410 my $message_count = $dbh->selectall_arrayref(q{
1413 WHERE letter_code = 'HOLD'
1414 AND message_transport_type = 'print'
1416 return $message_count->[0]->[0];
1419 sub place_item_hold {
1420 my ($patron,$item,$library,$priority) = @_;
1422 my $hold_id = C4::Reserves::AddReserve(
1424 branchcode => $library->branchcode,
1425 borrowernumber => $patron->borrowernumber,
1426 biblionumber => $item->biblionumber,
1427 priority => $priority,
1428 title => "title for fee",
1429 itemnumber => $item->itemnumber,
1433 my $hold = Koha::Holds->find($hold_id);
1437 # we reached the finish
1438 $schema->storage->txn_rollback();
1440 subtest 'IsAvailableForItemLevelRequest() tests' => sub {
1444 $schema->storage->txn_begin;
1446 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1448 my $item_type = undef;
1450 my $item_mock = Test::MockModule->new('Koha::Item');
1451 $item_mock->mock( 'effective_itemtype', sub { return $item_type; } );
1453 my $item = $builder->build_sample_item;
1456 !C4::Reserves::IsAvailableForItemLevelRequest( $item, $patron ),
1457 "Item not available for item-level hold because no effective item type"
1460 # Weird use case to highlight issue
1462 Koha::ItemTypes->search( { itemtype => $item_type } )->delete;
1463 my $itemtype = $builder->build_object(
1465 class => 'Koha::ItemTypes',
1466 value => { itemtype => $item_type, notloan => 0 }
1470 C4::Reserves::IsAvailableForItemLevelRequest( $item, $patron ),
1471 "Item not available for item-level hold because no effective item type"
1474 $schema->storage->txn_rollback;
1477 subtest 'AddReserve() tests' => sub {
1481 $schema->storage->txn_begin;
1483 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1484 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1485 my $biblio = $builder->build_sample_biblio;
1487 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1488 $mock->mock( 'enqueue', sub {
1489 my ( $self, $args ) = @_;
1491 $args->{biblio_ids},
1493 "AddReserve triggers a holds queue update for the related biblio"
1497 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1501 branchcode => $library->branchcode,
1502 borrowernumber => $patron->id,
1503 biblionumber => $biblio->id,
1507 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1511 branchcode => $library->branchcode,
1512 borrowernumber => $patron->id,
1513 biblionumber => $biblio->id,
1517 $schema->storage->txn_rollback;
1520 subtest 'AlterPriorty() tests' => sub {
1524 $schema->storage->txn_begin;
1526 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1527 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1528 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1529 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1530 my $biblio = $builder->build_sample_biblio;
1532 my $reserve_id = AddReserve(
1534 branchcode => $library->branchcode,
1535 borrowernumber => $patron_1->id,
1536 biblionumber => $biblio->id,
1541 branchcode => $library->branchcode,
1542 borrowernumber => $patron_2->id,
1543 biblionumber => $biblio->id,
1548 branchcode => $library->branchcode,
1549 borrowernumber => $patron_3->id,
1550 biblionumber => $biblio->id,
1554 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1555 $mock->mock( 'enqueue', sub {
1556 my ( $self, $args ) = @_;
1558 $args->{biblio_ids},
1560 "AlterPriority triggers a holds queue update for the related biblio"
1564 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1566 AlterPriority( "bottom", $reserve_id, 1, 2, 1, 3 );
1568 my $hold = Koha::Holds->find($reserve_id);
1570 is($hold->priority,3,'Successfully altered priority to bottom');
1572 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1574 AlterPriority( "bottom", $reserve_id, 1, 2, 1, 3 );
1576 $schema->storage->txn_rollback;
1579 subtest 'CanBookBeReserved() tests' => sub {
1583 $schema->storage->txn_begin;
1585 my $library = $builder->build_object(
1586 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1587 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1588 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1590 my $biblio = $builder->build_sample_biblio();
1591 my $item_1 = $builder->build_sample_item(
1592 { biblionumber => $biblio->id, itype => $itype->id } );
1593 my $item_2 = $builder->build_sample_item(
1594 { biblionumber => $biblio->id, itype => $itype->id } );
1596 Koha::CirculationRules->delete;
1597 Koha::CirculationRules->set_rules(
1599 branchcode => undef,
1600 categorycode => undef,
1603 holds_per_record => 100,
1607 Koha::CirculationRules->set_rules(
1609 branchcode => undef,
1610 categorycode => undef,
1611 itemtype => $itype->id,
1613 reservesallowed => 2,
1618 C4::Reserves::AddReserve(
1620 branchcode => $library->id,
1621 borrowernumber => $patron->id,
1622 biblionumber => $biblio->id,
1623 title => $biblio->title,
1624 itemnumber => $item_1->id
1628 ## Limit on item type is 2, only one hold, success tests
1630 my $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1631 { itemtype => $itype->id } );
1632 is_deeply( $res, { status => 'OK' },
1633 'Holds on itemtype limit not reached' );
1635 # Add a second hold, biblio-level and item type-constrained
1636 C4::Reserves::AddReserve(
1638 branchcode => $library->id,
1639 borrowernumber => $patron->id,
1640 biblionumber => $biblio->id,
1641 title => $biblio->title,
1642 itemtype => $itype->id,
1646 ## Limit on item type is 2, two holds, one of them biblio-level/item type-constrained
1648 $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1649 { itemtype => $itype->id } );
1650 is_deeply( $res, { status => '' }, 'Holds on itemtype limit reached' );
1652 $schema->storage->txn_rollback;
1655 subtest 'CanItemBeReserved() tests' => sub {
1659 $schema->storage->txn_begin;
1661 my $library = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1662 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1663 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1665 my $biblio = $builder->build_sample_biblio();
1666 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->id, itype => $itype->id });
1667 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->id, itype => $itype->id });
1669 Koha::CirculationRules->delete;
1670 Koha::CirculationRules->set_rules(
1671 { branchcode => undef,
1672 categorycode => undef,
1675 holds_per_record => 100,
1679 Koha::CirculationRules->set_rules(
1680 { branchcode => undef,
1681 categorycode => undef,
1682 itemtype => $itype->id,
1684 reservesallowed => 2,
1689 C4::Reserves::AddReserve(
1691 branchcode => $library->id,
1692 borrowernumber => $patron->id,
1693 biblionumber => $biblio->id,
1694 title => $biblio->title,
1695 itemnumber => $item_1->id
1699 ## Limit on item type is 2, only one hold, success tests
1701 my $res = CanItemBeReserved( $patron, $item_2, $library->id );
1702 is_deeply( $res, { status => 'OK' }, 'Holds on itemtype limit not reached' );
1704 # Add a second hold, biblio-level and item type-constrained
1705 C4::Reserves::AddReserve(
1707 branchcode => $library->id,
1708 borrowernumber => $patron->id,
1709 biblionumber => $biblio->id,
1710 title => $biblio->title,
1711 itemtype => $itype->id,
1715 ## Limit on item type is 2, two holds, one of them biblio-level/item type-constrained
1717 $res = CanItemBeReserved( $patron, $item_2, $library->id );
1718 is_deeply( $res, { status => 'tooManyReserves', limit => 2 }, 'Holds on itemtype limit reached' );
1720 $schema->storage->txn_rollback;