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( GetMarcBiblio GetMarcFromKohaField ModBiblio );
34 use C4::Reserves qw( AddReserve AlterPriority CheckReserves GetReservesControlBranch ModReserve ModReserveAffect ReserveSlip CalculatePriority CanReserveBeCanceledFromOpac CanBookBeReserved IsAvailableForItemLevelRequest MoveReserve ChargeReserveFee RevertWaitingStatus CanItemBeReserved MergeHolds );
37 use Koha::DateUtils qw( dt_from_string output_pref );
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("MarcSubfieldStructure-$frameworkcode");
73 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
74 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
75 my $branch_3 = $builder->build({ source => 'Branch' })->{ branchcode };
77 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
78 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
80 my $itemtype = $builder->build(
81 { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
83 t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
85 my $bibnum = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
87 # Create a helper item instance for testing
88 my $item = $builder->build_sample_item({ biblionumber => $bibnum, library => $branch_1, itype => $itemtype });
90 my $biblio_with_no_item = $builder->build_sample_biblio;
92 # Modify item; setting barcode.
93 my $testbarcode = '97531';
94 $item->barcode($testbarcode)->store; # FIXME We should not hardcode a barcode! Also, what's the purpose of this?
98 firstname => 'my firstname',
99 surname => 'my surname',
100 categorycode => $category_1,
101 branchcode => $branch_1,
103 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
104 my $borrowernumber = Koha::Patron->new(\%data)->store->borrowernumber;
105 my $patron = Koha::Patrons->find( $borrowernumber );
106 my $borrower = $patron->unblessed;
107 my $biblionumber = $bibnum;
108 my $barcode = $testbarcode;
110 my $branchcode = Koha::Libraries->search->next->branchcode;
114 branchcode => $branchcode,
115 borrowernumber => $borrowernumber,
116 biblionumber => $biblionumber,
121 my ($status, $reserve, $all_reserves) = CheckReserves($item->itemnumber, $barcode);
123 is($status, "Reserved", "CheckReserves Test 1");
125 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
127 ($status, $reserve, $all_reserves) = CheckReserves($item->itemnumber);
128 is($status, "Reserved", "CheckReserves Test 2");
130 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
131 is($status, "Reserved", "CheckReserves Test 3");
133 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
134 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
136 'ItemHomeLib' eq GetReservesControlBranch(
137 { homebranch => 'ItemHomeLib' },
138 { branchcode => 'PatronHomeLib' }
139 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
141 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
143 'PatronHomeLib' eq GetReservesControlBranch(
144 { homebranch => 'ItemHomeLib' },
145 { branchcode => 'PatronHomeLib' }
146 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
148 t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
151 ### Regression test for bug 10272
154 $requesters{$branch_1} = Koha::Patron->new({
155 branchcode => $branch_1,
156 categorycode => $category_2,
157 surname => "borrower from $branch_1",
158 })->store->borrowernumber;
159 for my $i ( 2 .. 5 ) {
160 $requesters{"CPL$i"} = Koha::Patron->new({
161 branchcode => $branch_1,
162 categorycode => $category_2,
163 surname => "borrower $i from $branch_1",
164 })->store->borrowernumber;
166 $requesters{$branch_2} = Koha::Patron->new({
167 branchcode => $branch_2,
168 categorycode => $category_2,
169 surname => "borrower from $branch_2",
170 })->store->borrowernumber;
171 $requesters{$branch_3} = Koha::Patron->new({
172 branchcode => $branch_3,
173 categorycode => $category_2,
174 surname => "borrower from $branch_3",
175 })->store->borrowernumber;
177 # Configure rules so that $branch_1 allows only $branch_1 patrons
178 # to request its items, while $branch_2 will allow its items
179 # to fill holds from anywhere.
181 $dbh->do('DELETE FROM circulation_rules');
182 Koha::CirculationRules->set_rules(
185 categorycode => undef,
188 reservesallowed => 25,
189 holds_per_record => 1,
194 # CPL allows only its own patrons to request its items
195 Koha::CirculationRules->set_rules(
197 branchcode => $branch_1,
200 holdallowed => 'from_home_library',
201 returnbranch => 'homebranch',
206 # ... while FPL allows anybody to request its items
207 Koha::CirculationRules->set_rules(
209 branchcode => $branch_2,
212 holdallowed => 'from_any_library',
213 returnbranch => 'homebranch',
218 my $bibnum2 = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
220 my ($itemnum_cpl, $itemnum_fpl);
221 $itemnum_cpl = $builder->build_sample_item(
223 biblionumber => $bibnum2,
224 library => $branch_1,
225 barcode => 'bug10272_CPL',
229 $itemnum_fpl = $builder->build_sample_item(
231 biblionumber => $bibnum2,
232 library => $branch_2,
233 barcode => 'bug10272_FPL',
238 # Ensure that priorities are numbered correcly when a hold is moved to waiting
240 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
243 branchcode => $branch_3,
244 borrowernumber => $requesters{$branch_3},
245 biblionumber => $bibnum2,
251 branchcode => $branch_2,
252 borrowernumber => $requesters{$branch_2},
253 biblionumber => $bibnum2,
259 branchcode => $branch_1,
260 borrowernumber => $requesters{$branch_1},
261 biblionumber => $bibnum2,
265 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
267 # Now it should have different priorities.
268 my $biblio = Koha::Biblios->find( $bibnum2 );
269 my $holds = $biblio->holds({}, { order_by => 'reserve_id' });;
270 is($holds->next->priority, 0, 'Item is correctly waiting');
271 is($holds->next->priority, 1, 'Item is correctly priority 1');
272 is($holds->next->priority, 2, 'Item is correctly priority 2');
274 my @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting->as_list;
275 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
276 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
279 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
282 branchcode => $branch_3,
283 borrowernumber => $requesters{$branch_3},
284 biblionumber => $bibnum2,
290 branchcode => $branch_2,
291 borrowernumber => $requesters{$branch_2},
292 biblionumber => $bibnum2,
299 branchcode => $branch_1,
300 borrowernumber => $requesters{$branch_1},
301 biblionumber => $bibnum2,
306 # Ensure that the item's home library controls hold policy lookup
307 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
310 # Return the CPL item at FPL. The hold that should be triggered is
311 # the one placed by the CPL patron, as the other two patron's hold
312 # requests cannot be filled by that item per policy.
313 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
314 is( $messages->{ResFound}->{borrowernumber},
315 $requesters{$branch_1},
316 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
318 # Return the FPL item at FPL. The hold that should be triggered is
319 # the one placed by the RPL patron, as that patron is first in line
320 # and RPL imposes no restrictions on whose holds its items can fill.
322 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
323 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
325 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
326 is( $messages->{ResFound}->{borrowernumber},
327 $requesters{$branch_3},
328 'for generous library, its items fill first hold request in line (bug 10272)');
330 $biblio = Koha::Biblios->find( $biblionumber );
331 $holds = $biblio->holds;
332 is($holds->count, 1, "Only one reserves for this biblio");
333 $holds->next->reserve_id;
335 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
336 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
337 # Test 9761a: Add a reserve without date, CheckReserve should return it
338 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
341 branchcode => $branch_1,
342 borrowernumber => $requesters{$branch_1},
343 biblionumber => $bibnum,
347 ($status)=CheckReserves($item->itemnumber,undef,undef);
348 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
349 ($status)=CheckReserves($item->itemnumber,undef,7);
350 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
352 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
353 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
354 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
355 my $resdate= dt_from_string();
356 $resdate->add_duration(DateTime::Duration->new(days => 4));
357 $resdate=output_pref($resdate);
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 => output_pref(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));
442 $resdate=output_pref($resdate);
445 branchcode => $branch_1,
446 borrowernumber => $requesters{$branch_1},
447 biblionumber => $bibnum,
449 reservation_date => $resdate,
453 $holds = $item->current_holds;
454 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
455 my $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
456 is( $future_holds->count, 0, 'current_holds does not return a future next available hold');
457 # 9788b: current_holds does not return future item level hold
458 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
461 branchcode => $branch_1,
462 borrowernumber => $requesters{$branch_1},
463 biblionumber => $bibnum,
465 reservation_date => $resdate,
466 itemnumber => $item->itemnumber,
469 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
470 is( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
471 # 9788c: current_holds returns future wait (confirmed future hold)
472 ModReserveAffect( $item->itemnumber, $requesters{$branch_1} , 0); #confirm hold
473 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
474 is( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
475 # End of tests for bug 9788
477 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
478 # Tests for CalculatePriority (bug 8918)
479 my $p = C4::Reserves::CalculatePriority($bibnum2);
480 is($p, 4, 'CalculatePriority should now return priority 4');
483 branchcode => $branch_1,
484 borrowernumber => $requesters{'CPL2'},
485 biblionumber => $bibnum2,
489 $p = C4::Reserves::CalculatePriority($bibnum2);
490 is($p, 5, 'CalculatePriority should now return priority 5');
491 #some tests on bibnum
492 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
493 $p = C4::Reserves::CalculatePriority($bibnum);
494 is($p, 1, 'CalculatePriority should now return priority 1');
495 #add a new reserve and confirm it to waiting
498 branchcode => $branch_1,
499 borrowernumber => $requesters{$branch_1},
500 biblionumber => $bibnum,
502 itemnumber => $item->itemnumber,
505 $p = C4::Reserves::CalculatePriority($bibnum);
506 is($p, 2, 'CalculatePriority should now return priority 2');
507 ModReserveAffect( $item->itemnumber, $requesters{$branch_1} , 0);
508 $p = C4::Reserves::CalculatePriority($bibnum);
509 is($p, 1, 'CalculatePriority should now return priority 1');
510 #add another biblio hold, no resdate
513 branchcode => $branch_1,
514 borrowernumber => $requesters{'CPL2'},
515 biblionumber => $bibnum,
519 $p = C4::Reserves::CalculatePriority($bibnum);
520 is($p, 2, 'CalculatePriority should now return priority 2');
521 #add another future hold
522 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
523 $resdate= dt_from_string();
524 $resdate->add_duration(DateTime::Duration->new(days => 1));
527 branchcode => $branch_1,
528 borrowernumber => $requesters{'CPL2'},
529 biblionumber => $bibnum,
531 reservation_date => output_pref($resdate),
534 $p = C4::Reserves::CalculatePriority($bibnum);
535 is($p, 2, 'CalculatePriority should now still return priority 2');
536 #calc priority with future resdate
537 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
538 is($p, 3, 'CalculatePriority should now return priority 3');
539 # End of tests for bug 8918
541 # regression test for bug 12630
542 # Now there are 2 reserves on $bibnum
543 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
544 my $bor_tmp_1 = $builder->build_object({ class => 'Koha::Patrons',value =>{
545 firstname => 'my firstname tmp 1',
546 surname => 'my surname tmp 1',
550 my $bor_tmp_2 = $builder->build_object({ class => 'Koha::Patrons',value =>{
551 firstname => 'my firstname tmp 2',
552 surname => 'my surname tmp 2',
556 my $borrowernumber_tmp_1 = $bor_tmp_1->borrowernumber;
557 my $borrowernumber_tmp_2 = $bor_tmp_2->borrowernumber;
558 my $date_in_future = dt_from_string();
559 $date_in_future = $date_in_future->add_duration(DateTime::Duration->new(days => 1));
562 borrowernumber => $borrowernumber_tmp_1,
563 biblionumber => $bibnum,
565 reservation_date => output_pref($date_in_future)
569 borrowernumber => $borrowernumber_tmp_2,
570 biblionumber => $bibnum,
572 reservation_date => output_pref($date_in_future)
574 my @r1 = Koha::Holds->search({ borrowernumber => $borrowernumber_tmp_1 })->as_list;
575 my @r2 = Koha::Holds->search({ borrowernumber => $borrowernumber_tmp_2 })->as_list;
576 is( $r1[0]->priority, 3, 'priority for hold in future should be correct');
577 is( $r2[0]->priority, 4, 'priority for hold not in future should be correct');
578 # end of tests for bug 12630
580 # Tests for cancel reserves by users from OPAC.
581 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
584 branchcode => $branch_1,
585 borrowernumber => $requesters{$branch_1},
586 biblionumber => $bibnum,
590 my (undef, $canres, undef) = CheckReserves($item->itemnumber);
592 is( CanReserveBeCanceledFromOpac(), undef,
593 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
596 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
598 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
601 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
603 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
606 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
607 is($cancancel, 1, 'Can user cancel its own reserve');
609 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
610 is($cancancel, 0, 'Other user cant cancel reserve');
612 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 1);
613 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
614 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
616 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
617 is( CanReserveBeCanceledFromOpac($canres->{resserve_id}, $requesters{$branch_1}), undef,
618 'Cannot cancel a deleted hold' );
622 branchcode => $branch_1,
623 borrowernumber => $requesters{$branch_1},
624 biblionumber => $bibnum,
628 (undef, $canres, undef) = CheckReserves($item->itemnumber);
630 ModReserveAffect($item->itemnumber, $requesters{$branch_1}, 0);
631 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
632 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
634 # End of tests for bug 12876
637 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
640 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
642 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
644 #Set the ageRestriction for the Biblio
645 my $record = GetMarcBiblio({ biblionumber => $bibnum });
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));
729 $resdate=output_pref($resdate);
732 branchcode => $branch_1,
733 borrowernumber => $borrowernumber,
734 biblionumber => $bibnum,
736 reservation_date => $resdate,
739 MoveReserve( $item->itemnumber, $borrowernumber );
740 ($status)=CheckReserves( $item->itemnumber, undef, 1 );
741 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
742 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
743 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
744 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
747 branchcode => $branch_1,
748 borrowernumber => $borrowernumber,
749 biblionumber => $bibnum,
751 reservation_date => $resdate,
754 MoveReserve( $item->itemnumber, $borrowernumber );
755 ($status)=CheckReserves( $item->itemnumber, undef, 2 );
756 is( $status, '', 'MoveReserve filled future hold now');
757 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
760 branchcode => $branch_1,
761 borrowernumber => $borrowernumber,
762 biblionumber => $bibnum,
764 reservation_date => $resdate,
767 MoveReserve( $item->itemnumber, $borrowernumber );
768 ($status)=CheckReserves( $item->itemnumber, undef, 2 );
769 is( $status, '', 'MoveReserve filled future waiting hold now');
770 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
771 $resdate= dt_from_string();
772 $resdate->add_duration(DateTime::Duration->new(days => 3));
773 $resdate=output_pref($resdate);
776 branchcode => $branch_1,
777 borrowernumber => $borrowernumber,
778 biblionumber => $bibnum,
780 reservation_date => $resdate,
783 MoveReserve( $item->itemnumber, $borrowernumber );
784 ($status)=CheckReserves( $item->itemnumber, undef, 3 );
785 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
786 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
788 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
789 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
790 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
792 subtest '_koha_notify_reserve() tests' => sub {
796 my $wants_hold_and_email = {
802 letter_code => 'HOLD'
805 my $mp = Test::MockModule->new( 'C4::Members::Messaging' );
807 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
809 $dbh->do('DELETE FROM letter');
811 my $email_hold_notice = $builder->build({
814 message_transport_type => 'email',
817 module => 'reserves',
822 my $sms_hold_notice = $builder->build({
825 message_transport_type => 'sms',
828 module => 'reserves',
833 my $hold_borrower = $builder->build({
834 source => 'Borrower',
836 smsalertnumber=>'5555555555',
839 })->{borrowernumber};
841 C4::Reserves::AddReserve(
843 branchcode => $item->homebranch,
844 borrowernumber => $hold_borrower,
845 biblionumber => $item->biblionumber,
849 ModReserveAffect($item->itemnumber, $hold_borrower, 0);
850 my $sms_message_address = $schema->resultset('MessageQueue')->search({
851 letter_code => 'HOLD',
852 message_transport_type => 'sms',
853 borrowernumber => $hold_borrower,
854 })->next()->to_address();
855 is($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
857 my $email_message_address = $schema->resultset('MessageQueue')->search({
858 letter_code => 'HOLD',
859 message_transport_type => 'email',
860 borrowernumber => $hold_borrower,
861 })->next()->to_address();
862 is($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
866 subtest 'ReservesNeedReturns' => sub {
869 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
871 homebranch => $library->branchcode,
872 holdingbranch => $library->branchcode,
874 my $item = $builder->build_sample_item($item_info);
875 my $patron = $builder->build_object(
877 class => 'Koha::Patrons',
878 value => { branchcode => $library->branchcode, }
881 my $patron_2 = $builder->build_object(
883 class => 'Koha::Patrons',
884 value => { branchcode => $library->branchcode, }
890 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Test with feature disabled
891 my $hold = place_item_hold( $patron, $item, $library, $priority );
892 is( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
893 is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
896 t::lib::Mocks::mock_preference('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
897 $hold = place_item_hold( $patron, $item, $library, $priority );
898 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
899 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
902 $item->onloan('2010-01-01')->store;
903 $hold = place_item_hold( $patron, $item, $library, $priority );
904 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item onloan priority must be set to 1' );
907 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); # '0' means damaged holds not allowed
908 $item->onloan(undef)->damaged(1)->store;
909 $hold = place_item_hold( $patron, $item, $library, $priority );
910 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item damaged and not allowed holds on damaged items priority must be set to 1' );
912 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); # '0' means damaged holds not allowed
913 $hold = place_item_hold( $patron, $item, $library, $priority );
914 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and damaged holds allowed, priority must have been set to 0' );
915 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and damaged holds allowed, found must have been set waiting' );
918 my $hold_1 = place_item_hold( $patron, $item, $library, $priority );
919 is( $hold_1->found, 'W', 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
920 is( $hold_1->priority, 0, 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
921 $hold = place_item_hold( $patron_2, $item, $library, $priority );
922 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item already on hold priority must be set to 1' );
926 my $transfer = $builder->build_object({
927 class => "Koha::Item::Transfers",
929 itemnumber => $item->itemnumber,
930 datearrived => undef,
931 datecancelled => undef
934 $item->damaged(0)->store;
935 $hold = place_item_hold( $patron, $item, $library, $priority );
936 is( $hold->found, undef, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
937 is( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
941 $hold = place_item_hold( $patron, $item, $library, $priority );
942 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
943 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
944 $hold_1 = place_item_hold( $patron, $item, $library, $priority );
945 is( $hold_1->priority, 1, 'If ReservesNeedReturns is 0 but item has a hold priority is 1' );
946 $hold_1->suspend(1)->store; # We suspend the hold
947 $hold->delete; # Delete the waiting hold
948 $hold = place_item_hold( $patron, $item, $library, $priority );
949 is( $hold->priority, 0, 'If ReservesNeedReturns is 0 and other hold(s) suspended, priority must have been set to 0' );
950 is( $hold->found, 'W', 'If ReservesNeedReturns is 0 and other hold(s) suspended, found must have been set waiting' );
955 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # Don't affect other tests
958 subtest 'ChargeReserveFee tests' => sub {
962 my $library = $builder->build_object({ class => 'Koha::Libraries' });
963 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
966 my $title = 'A title';
968 my $context = Test::MockModule->new('C4::Context');
969 $context->mock( userenv => { branch => $library->id } );
971 my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title );
973 is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
974 ok( $line->is_debit, 'Generates a debit line' );
975 is( $line->debit_type_code, 'RESERVE' , 'generates RESERVE debit_type');
976 is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
977 is( $line->amount, $fee , 'amount set correctly');
978 is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
979 is( $line->description, "$title" , 'description is title of reserved item');
980 is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
983 subtest 'reserves.item_level_hold' => sub {
986 my $item = $builder->build_sample_item;
987 my $patron = $builder->build_object(
989 class => 'Koha::Patrons',
990 value => { branchcode => $item->homebranch }
994 subtest 'item level hold' => sub {
996 my $reserve_id = AddReserve(
998 branchcode => $item->homebranch,
999 borrowernumber => $patron->borrowernumber,
1000 biblionumber => $item->biblionumber,
1002 itemnumber => $item->itemnumber,
1006 my $hold = Koha::Holds->find($reserve_id);
1007 is( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' );
1010 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
1012 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1013 $mock->mock( 'enqueue', sub {
1014 my ( $self, $args ) = @_;
1016 $args->{biblio_ids},
1017 [ $hold->biblionumber ],
1018 "AlterPriority triggers a holds queue update for the related biblio"
1022 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1024 # Revert the waiting status
1025 C4::Reserves::RevertWaitingStatus(
1026 { itemnumber => $item->itemnumber } );
1028 $hold = Koha::Holds->find($reserve_id);
1030 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' );
1032 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1036 # Revert the waiting status, RealTimeHoldsQueue => shouldn't add a test
1037 C4::Reserves::RevertWaitingStatus(
1038 { itemnumber => $item->itemnumber } );
1040 $hold->delete; # cleanup
1043 subtest 'biblio level hold' => sub {
1045 my $reserve_id = AddReserve(
1047 branchcode => $item->homebranch,
1048 borrowernumber => $patron->borrowernumber,
1049 biblionumber => $item->biblionumber,
1054 my $hold = Koha::Holds->find($reserve_id);
1055 is( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' );
1058 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
1060 $hold = Koha::Holds->find($reserve_id);
1061 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should be set on hold confirmation' );
1063 # Revert the waiting status
1064 C4::Reserves::RevertWaitingStatus( { itemnumber => $item->itemnumber } );
1066 $hold = Koha::Holds->find($reserve_id);
1067 is( $hold->itemnumber, undef, 'Itemnumber should be removed when the waiting status is revert' );
1074 subtest 'MoveReserve additional test' => sub {
1078 # Create the items and patrons we need
1079 my $biblio = $builder->build_sample_biblio();
1080 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
1081 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
1082 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, notforloan => 0, itype => $itype->itemtype });
1083 my $patron_1 = $builder->build_object({ class => "Koha::Patrons" });
1084 my $patron_2 = $builder->build_object({ class => "Koha::Patrons" });
1086 # Place a hold on the title for both patrons
1087 my $reserve_1 = AddReserve(
1089 branchcode => $item_1->homebranch,
1090 borrowernumber => $patron_1->borrowernumber,
1091 biblionumber => $biblio->biblionumber,
1093 itemnumber => $item_1->itemnumber,
1096 my $reserve_2 = AddReserve(
1098 branchcode => $item_2->homebranch,
1099 borrowernumber => $patron_2->borrowernumber,
1100 biblionumber => $biblio->biblionumber,
1102 itemnumber => $item_1->itemnumber,
1105 is($patron_1->holds->next()->reserve_id, $reserve_1, "The 1st patron has a hold");
1106 is($patron_2->holds->next()->reserve_id, $reserve_2, "The 2nd patron has a hold");
1108 # Fake the holds queue
1109 $dbh->do(q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?,?)},undef,($patron_1->borrowernumber,$biblio->biblionumber,$item_1->itemnumber,$item_1->homebranch,0,$reserve_1));
1111 # The 2nd hold should be filed even if the item is preselected for the first hold
1112 MoveReserve($item_1->itemnumber,$patron_2->borrowernumber);
1113 is($patron_2->holds->count, 0, "The 2nd patrons no longer has a hold");
1114 is($patron_2->old_holds->next()->reserve_id, $reserve_2, "The 2nd patrons hold was filled and moved to old holds");
1118 subtest 'RevertWaitingStatus' => sub {
1122 # Create the items and patrons we need
1123 my $biblio = $builder->build_sample_biblio();
1124 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1125 my $itype = $builder->build_object(
1126 { class => "Koha::ItemTypes", value => { notforloan => 0 } } );
1127 my $item_1 = $builder->build_sample_item(
1129 biblionumber => $biblio->biblionumber,
1130 itype => $itype->itemtype,
1131 library => $library->branchcode
1134 my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } );
1135 my $patron_2 = $builder->build_object( { class => "Koha::Patrons" } );
1136 my $patron_3 = $builder->build_object( { class => "Koha::Patrons" } );
1137 my $patron_4 = $builder->build_object( { class => "Koha::Patrons" } );
1139 # Place a hold on the title for both patrons
1141 my $hold_1 = place_item_hold( $patron_1, $item_1, $library, $priority );
1142 my $hold_2 = place_item_hold( $patron_2, $item_1, $library, $priority );
1143 my $hold_3 = place_item_hold( $patron_3, $item_1, $library, $priority );
1144 my $hold_4 = place_item_hold( $patron_4, $item_1, $library, $priority );
1146 $hold_1->set_waiting;
1147 AddIssue( $patron_3->unblessed, $item_1->barcode, undef, 'revert' );
1149 my $holds = $biblio->holds;
1150 is( $holds->count, 3, 'One hold has been deleted' );
1153 $holds->next->priority, $holds->next->priority,
1154 $holds->next->priority
1157 'priorities have been reordered'
1161 subtest 'CheckReserves additional tests' => sub {
1165 my $item = $builder->build_sample_item;
1166 my $reserve1 = $builder->build_object(
1168 class => "Koha::Holds",
1172 itemnumber => undef,
1173 biblionumber => $item->biblionumber,
1174 waitingdate => undef,
1175 cancellationdate => undef,
1176 item_level_hold => 0,
1177 lowestPriority => 0,
1178 expirationdate => undef,
1179 suspend_until => undef,
1185 my $reserve2 = $builder->build_object(
1187 class => "Koha::Holds",
1191 biblionumber => $item->biblionumber,
1192 borrowernumber => $reserve1->borrowernumber,
1193 itemnumber => undef,
1194 waitingdate => undef,
1195 cancellationdate => undef,
1196 item_level_hold => 0,
1197 lowestPriority => 0,
1198 expirationdate => undef,
1199 suspend_until => undef,
1206 my $tmp_holdsqueue = $builder->build(
1208 source => 'TmpHoldsqueue',
1210 borrowernumber => $reserve1->borrowernumber,
1211 biblionumber => $reserve1->biblionumber,
1215 my $fill_target = $builder->build(
1217 source => 'HoldFillTarget',
1219 borrowernumber => $reserve1->borrowernumber,
1220 biblionumber => $reserve1->biblionumber,
1221 itemnumber => $item->itemnumber,
1222 item_level_request => 0,
1227 ModReserveAffect( $item->itemnumber, $reserve1->borrowernumber, 1,
1228 $reserve1->reserve_id );
1229 my ( $status, $matched_reserve, $possible_reserves ) =
1230 CheckReserves( $item->itemnumber );
1232 is( $status, 'Transferred', "We found a reserve" );
1233 is( $matched_reserve->{reserve_id},
1234 $reserve1->reserve_id, "We got the Transit reserve" );
1235 is( scalar @$possible_reserves, 2, 'We do get both reserves' );
1237 my $patron_B = $builder->build_object({ class => "Koha::Patrons" });
1238 my $item_A = $builder->build_sample_item;
1239 my $item_B = $builder->build_sample_item({
1240 homebranch => $patron_B->branchcode,
1241 biblionumber => $item_A->biblionumber,
1242 itype => $item_A->itype
1244 Koha::CirculationRules->set_rules(
1246 branchcode => undef,
1247 categorycode => undef,
1248 itemtype => $item_A->itype,
1250 reservesallowed => 25,
1251 holds_per_record => 1,
1255 Koha::CirculationRules->set_rule({
1256 branchcode => undef,
1257 itemtype => $item_A->itype,
1258 rule_name => 'holdallowed',
1259 rule_value => 'from_home_library'
1261 my $reserve_id = AddReserve(
1263 branchcode => $patron_B->branchcode,
1264 borrowernumber => $patron_B->borrowernumber,
1265 biblionumber => $item_A->biblionumber,
1267 itemnumber => undef,
1271 ok( $reserve_id, "We can place a record level hold because one item is owned by patron's home library");
1272 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
1273 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1274 is( $status, "", "We do not fill the hold with item A because it is not from the patron's homebranch");
1275 Koha::CirculationRules->set_rule({
1276 branchcode => $item_A->homebranch,
1277 itemtype => $item_A->itype,
1278 rule_name => 'holdallowed',
1279 rule_value => 'from_any_library'
1281 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1282 is( $status, "Reserved", "We fill the hold with item A because item's branch rule says allow any");
1285 # Changing the control branch should change only the rule we get
1286 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
1287 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1288 is( $status, "", "We do not fill the hold with item A because it is not from the patron's homebranch");
1289 Koha::CirculationRules->set_rule({
1290 branchcode => $patron_B->branchcode,
1291 itemtype => $item_A->itype,
1292 rule_name => 'holdallowed',
1293 rule_value => 'from_any_library'
1295 ( $status, $matched_reserve, $possible_reserves ) = CheckReserves( $item_A->itemnumber );
1296 is( $status, "Reserved", "We fill the hold with item A because patron's branch rule says allow any");
1300 subtest 'AllowHoldOnPatronPossession test' => sub {
1304 # Create the items and patrons we need
1305 my $biblio = $builder->build_sample_biblio();
1306 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
1307 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
1308 my $patron = $builder->build_object({ class => "Koha::Patrons",
1309 value => { branchcode => $item->homebranch }});
1311 C4::Circulation::AddIssue($patron->unblessed,
1313 t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 0);
1315 is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
1316 $item->biblionumber)->{status},
1317 'alreadypossession',
1318 'Patron cannot place hold on a book loaned to itself');
1320 is(C4::Reserves::CanItemBeReserved( $patron, $item )->{status},
1321 'alreadypossession',
1322 'Patron cannot place hold on an item loaned to itself');
1324 t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 1);
1326 is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
1327 $item->biblionumber)->{status},
1329 'Patron can place hold on a book loaned to itself');
1331 is(C4::Reserves::CanItemBeReserved( $patron, $item )->{status},
1333 'Patron can place hold on an item loaned to itself');
1336 subtest 'MergeHolds' => sub {
1340 my $biblio_1 = $builder->build_sample_biblio();
1341 my $biblio_2 = $builder->build_sample_biblio();
1342 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1343 my $itype = $builder->build_object(
1344 { class => "Koha::ItemTypes", value => { notforloan => 0 } } );
1345 my $item_1 = $builder->build_sample_item(
1347 biblionumber => $biblio_1->biblionumber,
1348 itype => $itype->itemtype,
1349 library => $library->branchcode
1352 my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } );
1354 # Place a hold on $biblio_1
1356 place_item_hold( $patron_1, $item_1, $library, $priority );
1358 # Move and make sure hold is now on $biblio_2
1359 C4::Reserves::MergeHolds($dbh, $biblio_2->biblionumber, $biblio_1->biblionumber);
1360 is( $biblio_2->holds->count, 1, 'Hold has been transferred' );
1363 subtest 'ModReserveAffect logging' => sub {
1367 my $item = $builder->build_sample_item;
1368 my $patron = $builder->build_object(
1370 class => "Koha::Patrons",
1371 value => { branchcode => $item->homebranch }
1375 t::lib::Mocks::mock_userenv({ patron => $patron });
1376 t::lib::Mocks::mock_preference('HoldsLog', 1);
1378 my $reserve_id = AddReserve(
1380 branchcode => $item->homebranch,
1381 borrowernumber => $patron->borrowernumber,
1382 biblionumber => $item->biblionumber,
1384 itemnumber => $item->itemnumber,
1388 my $hold = Koha::Holds->find($reserve_id);
1389 my $previous_timestamp = '1970-01-01 12:34:56';
1390 $hold->timestamp($previous_timestamp)->store;
1392 $hold = Koha::Holds->find($reserve_id);
1393 is( $hold->timestamp, $previous_timestamp, 'Make sure the previous timestamp has been used' );
1396 my $reserve_mock = Test::MockModule->new('C4::Reserves');
1397 $reserve_mock->mock( '_koha_notify_reserve', undef );
1400 ModReserveAffect( $item->itemnumber, $patron->borrowernumber );
1402 $hold->discard_changes;
1403 ok( $hold->is_waiting, 'Hold has been set waiting' );
1404 isnt( $hold->timestamp, $previous_timestamp, 'The timestamp has been modified' );
1406 my $log = Koha::ActionLogs->search({ module => 'HOLDS', action => 'MODIFY', object => $hold->reserve_id })->next;
1407 my $expected = sprintf q{'timestamp' => '%s'}, $hold->timestamp;
1408 like( $log->info, qr{$expected}, 'Timestamp logged is the current one' );
1411 sub count_hold_print_messages {
1412 my $message_count = $dbh->selectall_arrayref(q{
1415 WHERE letter_code = 'HOLD'
1416 AND message_transport_type = 'print'
1418 return $message_count->[0]->[0];
1421 sub place_item_hold {
1422 my ($patron,$item,$library,$priority) = @_;
1424 my $hold_id = C4::Reserves::AddReserve(
1426 branchcode => $library->branchcode,
1427 borrowernumber => $patron->borrowernumber,
1428 biblionumber => $item->biblionumber,
1429 priority => $priority,
1430 title => "title for fee",
1431 itemnumber => $item->itemnumber,
1435 my $hold = Koha::Holds->find($hold_id);
1439 # we reached the finish
1440 $schema->storage->txn_rollback();
1442 subtest 'IsAvailableForItemLevelRequest() tests' => sub {
1446 $schema->storage->txn_begin;
1448 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1450 my $item_type = undef;
1452 my $item_mock = Test::MockModule->new('Koha::Item');
1453 $item_mock->mock( 'effective_itemtype', sub { return $item_type; } );
1455 my $item = $builder->build_sample_item;
1458 !C4::Reserves::IsAvailableForItemLevelRequest( $item, $patron ),
1459 "Item not available for item-level hold because no effective item type"
1462 # Weird use case to highlight issue
1464 Koha::ItemTypes->search( { itemtype => $item_type } )->delete;
1465 my $itemtype = $builder->build_object(
1467 class => 'Koha::ItemTypes',
1468 value => { itemtype => $item_type, notloan => 0 }
1472 C4::Reserves::IsAvailableForItemLevelRequest( $item, $patron ),
1473 "Item not available for item-level hold because no effective item type"
1476 $schema->storage->txn_rollback;
1479 subtest 'AddReserve() tests' => sub {
1483 $schema->storage->txn_begin;
1485 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1486 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1487 my $biblio = $builder->build_sample_biblio;
1489 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1490 $mock->mock( 'enqueue', sub {
1491 my ( $self, $args ) = @_;
1493 $args->{biblio_ids},
1495 "AddReserve triggers a holds queue update for the related biblio"
1499 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1503 branchcode => $library->branchcode,
1504 borrowernumber => $patron->id,
1505 biblionumber => $biblio->id,
1509 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1513 branchcode => $library->branchcode,
1514 borrowernumber => $patron->id,
1515 biblionumber => $biblio->id,
1519 $schema->storage->txn_rollback;
1522 subtest 'AlterPriorty() tests' => sub {
1526 $schema->storage->txn_begin;
1528 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1529 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1530 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1531 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1532 my $biblio = $builder->build_sample_biblio;
1534 my $reserve_id = AddReserve(
1536 branchcode => $library->branchcode,
1537 borrowernumber => $patron_1->id,
1538 biblionumber => $biblio->id,
1543 branchcode => $library->branchcode,
1544 borrowernumber => $patron_2->id,
1545 biblionumber => $biblio->id,
1550 branchcode => $library->branchcode,
1551 borrowernumber => $patron_3->id,
1552 biblionumber => $biblio->id,
1556 my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1557 $mock->mock( 'enqueue', sub {
1558 my ( $self, $args ) = @_;
1560 $args->{biblio_ids},
1562 "AlterPriority triggers a holds queue update for the related biblio"
1566 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1568 AlterPriority( "bottom", $reserve_id, 1, 2, 1, 3 );
1570 my $hold = Koha::Holds->find($reserve_id);
1572 is($hold->priority,3,'Successfully altered priority to bottom');
1574 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1576 AlterPriority( "bottom", $reserve_id, 1, 2, 1, 3 );
1578 $schema->storage->txn_rollback;
1581 subtest 'CanBookBeReserved() tests' => sub {
1585 $schema->storage->txn_begin;
1587 my $library = $builder->build_object(
1588 { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1589 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1590 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1592 my $biblio = $builder->build_sample_biblio();
1593 my $item_1 = $builder->build_sample_item(
1594 { biblionumber => $biblio->id, itype => $itype->id } );
1595 my $item_2 = $builder->build_sample_item(
1596 { biblionumber => $biblio->id, itype => $itype->id } );
1598 Koha::CirculationRules->delete;
1599 Koha::CirculationRules->set_rules(
1601 branchcode => undef,
1602 categorycode => undef,
1605 holds_per_record => 100,
1609 Koha::CirculationRules->set_rules(
1611 branchcode => undef,
1612 categorycode => undef,
1613 itemtype => $itype->id,
1615 reservesallowed => 2,
1620 C4::Reserves::AddReserve(
1622 branchcode => $library->id,
1623 borrowernumber => $patron->id,
1624 biblionumber => $biblio->id,
1625 title => $biblio->title,
1626 itemnumber => $item_1->id
1630 ## Limit on item type is 2, only one hold, success tests
1632 my $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1633 { itemtype => $itype->id } );
1634 is_deeply( $res, { status => 'OK' },
1635 'Holds on itemtype limit not reached' );
1637 # Add a second hold, biblio-level and item type-constrained
1638 C4::Reserves::AddReserve(
1640 branchcode => $library->id,
1641 borrowernumber => $patron->id,
1642 biblionumber => $biblio->id,
1643 title => $biblio->title,
1644 itemtype => $itype->id,
1648 ## Limit on item type is 2, two holds, one of them biblio-level/item type-constrained
1650 $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1651 { itemtype => $itype->id } );
1652 is_deeply( $res, { status => '' }, 'Holds on itemtype limit reached' );
1654 $schema->storage->txn_rollback;
1657 subtest 'CanItemBeReserved() tests' => sub {
1661 $schema->storage->txn_begin;
1663 my $library = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1664 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1665 my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
1667 my $biblio = $builder->build_sample_biblio();
1668 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->id, itype => $itype->id });
1669 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->id, itype => $itype->id });
1671 Koha::CirculationRules->delete;
1672 Koha::CirculationRules->set_rules(
1673 { branchcode => undef,
1674 categorycode => undef,
1677 holds_per_record => 100,
1681 Koha::CirculationRules->set_rules(
1682 { branchcode => undef,
1683 categorycode => undef,
1684 itemtype => $itype->id,
1686 reservesallowed => 2,
1691 C4::Reserves::AddReserve(
1693 branchcode => $library->id,
1694 borrowernumber => $patron->id,
1695 biblionumber => $biblio->id,
1696 title => $biblio->title,
1697 itemnumber => $item_1->id
1701 ## Limit on item type is 2, only one hold, success tests
1703 my $res = CanItemBeReserved( $patron, $item_2, $library->id );
1704 is_deeply( $res, { status => 'OK' }, 'Holds on itemtype limit not reached' );
1706 # Add a second hold, biblio-level and item type-constrained
1707 C4::Reserves::AddReserve(
1709 branchcode => $library->id,
1710 borrowernumber => $patron->id,
1711 biblionumber => $biblio->id,
1712 title => $biblio->title,
1713 itemtype => $itype->id,
1717 ## Limit on item type is 2, two holds, one of them biblio-level/item type-constrained
1719 $res = CanItemBeReserved( $patron, $item_2, $library->id );
1720 is_deeply( $res, { status => 'tooManyReserves', limit => 2 }, 'Holds on itemtype limit reached' );
1722 $schema->storage->txn_rollback;