Bug 21716: Escape publisher in item search
[koha.git] / t / db_dependent / Reserves.t
blobcdc5f066338f01740e2be4fe0cc09a42e3b8d914
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 61;
21 use Test::MockModule;
22 use Test::Warn;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
27 use MARC::Record;
28 use DateTime::Duration;
30 use C4::Circulation;
31 use C4::Items;
32 use C4::Biblio;
33 use C4::Members;
34 use C4::Reserves;
35 use Koha::Caches;
36 use Koha::DateUtils;
37 use Koha::Holds;
38 use Koha::Items;
39 use Koha::Libraries;
40 use Koha::Notice::Templates;
41 use Koha::Patrons;
42 use Koha::Patron::Categories;
43 use Koha::CirculationRules;
45 BEGIN {
46 require_ok('C4::Reserves');
49 # Start transaction
50 my $database = Koha::Database->new();
51 my $schema = $database->schema();
52 $schema->storage->txn_begin();
53 my $dbh = C4::Context->dbh;
54 $dbh->do('DELETE FROM circulation_rules');
56 my $builder = t::lib::TestBuilder->new;
58 my $frameworkcode = q//;
61 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1);
63 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
64 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
65 my $cache = Koha::Caches->get_instance;
66 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
67 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
68 $cache->clear_from_cache("default_value_for_mod_marc-");
69 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
71 ## Setup Test
72 # Add branches
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 };
76 # Add categories
77 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
78 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
79 # Add an item type
80 my $itemtype = $builder->build(
81 { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
83 t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
85 my $bibnum = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
87 # Create a helper item instance for testing
88 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
89 { homebranch => $branch_1,
90 holdingbranch => $branch_1,
91 itype => $itemtype
93 $bibnum
96 my $biblio_with_no_item = $builder->build({
97 source => 'Biblio'
98 });
101 # Modify item; setting barcode.
102 my $testbarcode = '97531';
103 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
105 # Create a borrower
106 my %data = (
107 firstname => 'my firstname',
108 surname => 'my surname',
109 categorycode => $category_1,
110 branchcode => $branch_1,
112 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
113 my $borrowernumber = Koha::Patron->new(\%data)->store->borrowernumber;
114 my $patron = Koha::Patrons->find( $borrowernumber );
115 my $borrower = $patron->unblessed;
116 my $biblionumber = $bibnum;
117 my $barcode = $testbarcode;
119 my $bibitems = '';
120 my $priority = '1';
121 my $resdate = undef;
122 my $expdate = undef;
123 my $notes = '';
124 my $checkitem = undef;
125 my $found = undef;
127 my $branchcode = Koha::Libraries->search->next->branchcode;
129 AddReserve($branchcode, $borrowernumber, $biblionumber,
130 $bibitems, $priority, $resdate, $expdate, $notes,
131 'a title', $checkitem, $found);
133 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
135 is($status, "Reserved", "CheckReserves Test 1");
137 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
139 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
140 is($status, "Reserved", "CheckReserves Test 2");
142 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
143 is($status, "Reserved", "CheckReserves Test 3");
145 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
146 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
148 'ItemHomeLib' eq GetReservesControlBranch(
149 { homebranch => 'ItemHomeLib' },
150 { branchcode => 'PatronHomeLib' }
151 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
153 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
155 'PatronHomeLib' eq GetReservesControlBranch(
156 { homebranch => 'ItemHomeLib' },
157 { branchcode => 'PatronHomeLib' }
158 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
160 t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
163 ### Regression test for bug 10272
165 my %requesters = ();
166 $requesters{$branch_1} = Koha::Patron->new({
167 branchcode => $branch_1,
168 categorycode => $category_2,
169 surname => "borrower from $branch_1",
170 })->store->borrowernumber;
171 for my $i ( 2 .. 5 ) {
172 $requesters{"CPL$i"} = Koha::Patron->new({
173 branchcode => $branch_1,
174 categorycode => $category_2,
175 surname => "borrower $i from $branch_1",
176 })->store->borrowernumber;
178 $requesters{$branch_2} = Koha::Patron->new({
179 branchcode => $branch_2,
180 categorycode => $category_2,
181 surname => "borrower from $branch_2",
182 })->store->borrowernumber;
183 $requesters{$branch_3} = Koha::Patron->new({
184 branchcode => $branch_3,
185 categorycode => $category_2,
186 surname => "borrower from $branch_3",
187 })->store->borrowernumber;
189 # Configure rules so that $branch_1 allows only $branch_1 patrons
190 # to request its items, while $branch_2 will allow its items
191 # to fill holds from anywhere.
193 $dbh->do('DELETE FROM issuingrules');
194 $dbh->do(
195 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
196 VALUES (?, ?, ?, ?)},
198 '*', '*', '*', 25
201 # CPL allows only its own patrons to request its items
202 Koha::CirculationRules->set_rules(
204 branchcode => $branch_1,
205 categorycode => undef,
206 itemtype => undef,
207 rules => {
208 holdallowed => 1,
209 returnbranch => 'homebranch',
214 # ... while FPL allows anybody to request its items
215 Koha::CirculationRules->set_rules(
217 branchcode => $branch_2,
218 categorycode => undef,
219 itemtype => undef,
220 rules => {
221 holdallowed => 2,
222 returnbranch => 'homebranch',
227 my $bibnum2 = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber;
229 my ($itemnum_cpl, $itemnum_fpl);
230 ( undef, undef, $itemnum_cpl ) = AddItem(
231 { homebranch => $branch_1,
232 holdingbranch => $branch_1,
233 barcode => 'bug10272_CPL',
234 itype => $itemtype
236 $bibnum2
238 ( undef, undef, $itemnum_fpl ) = AddItem(
239 { homebranch => $branch_2,
240 holdingbranch => $branch_2,
241 barcode => 'bug10272_FPL',
242 itype => $itemtype
244 $bibnum2
248 # Ensure that priorities are numbered correcly when a hold is moved to waiting
249 # (bug 11947)
250 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
251 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
252 $bibitems, 1, $resdate, $expdate, $notes,
253 'a title', $checkitem, $found);
254 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
255 $bibitems, 2, $resdate, $expdate, $notes,
256 'a title', $checkitem, $found);
257 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
258 $bibitems, 3, $resdate, $expdate, $notes,
259 'a title', $checkitem, $found);
260 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
262 # Now it should have different priorities.
263 my $biblio = Koha::Biblios->find( $bibnum2 );
264 my $holds = $biblio->holds({}, { order_by => 'reserve_id' });;
265 is($holds->next->priority, 0, 'Item is correctly waiting');
266 is($holds->next->priority, 1, 'Item is correctly priority 1');
267 is($holds->next->priority, 2, 'Item is correctly priority 2');
269 my @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting();
270 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
271 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
274 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
275 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
276 $bibitems, 1, $resdate, $expdate, $notes,
277 'a title', $checkitem, $found);
278 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
279 $bibitems, 2, $resdate, $expdate, $notes,
280 'a title', $checkitem, $found);
281 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
282 $bibitems, 3, $resdate, $expdate, $notes,
283 'a title', $checkitem, $found);
285 # Ensure that the item's home library controls hold policy lookup
286 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
288 my $messages;
289 # Return the CPL item at FPL. The hold that should be triggered is
290 # the one placed by the CPL patron, as the other two patron's hold
291 # requests cannot be filled by that item per policy.
292 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
293 is( $messages->{ResFound}->{borrowernumber},
294 $requesters{$branch_1},
295 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
297 # Return the FPL item at FPL. The hold that should be triggered is
298 # the one placed by the RPL patron, as that patron is first in line
299 # and RPL imposes no restrictions on whose holds its items can fill.
301 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
302 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
304 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
305 is( $messages->{ResFound}->{borrowernumber},
306 $requesters{$branch_3},
307 'for generous library, its items fill first hold request in line (bug 10272)');
309 $biblio = Koha::Biblios->find( $biblionumber );
310 $holds = $biblio->holds;
311 is($holds->count, 1, "Only one reserves for this biblio");
312 my $reserve_id = $holds->next->reserve_id;
314 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
315 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
316 # Test 9761a: Add a reserve without date, CheckReserve should return it
317 $resdate= undef; #defaults to today in AddReserve
318 $expdate= undef; #no expdate
319 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
320 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
321 $bibitems, 1, $resdate, $expdate, $notes,
322 'a title', $checkitem, $found);
323 ($status)=CheckReserves($itemnumber,undef,undef);
324 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
325 ($status)=CheckReserves($itemnumber,undef,7);
326 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
328 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
329 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
330 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
331 $resdate= dt_from_string();
332 $resdate->add_duration(DateTime::Duration->new(days => 4));
333 $resdate=output_pref($resdate);
334 $expdate= undef; #no expdate
335 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
336 $bibitems, 1, $resdate, $expdate, $notes,
337 'a title', $checkitem, $found);
338 ($status)=CheckReserves($itemnumber,undef,undef);
339 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
341 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
342 ($status)=CheckReserves($itemnumber,undef,3);
343 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
344 ($status)=CheckReserves($itemnumber,undef,4);
345 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
347 # Test 9761d: Check ResFound message of AddReturn for future hold
348 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
349 # In this test we do not need an issued item; it is just a 'checkin'
350 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
351 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
352 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
353 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
354 ($doreturn, $messages)= AddReturn('97531',$branch_1);
355 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
356 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
357 ($doreturn, $messages)= AddReturn('97531',$branch_1);
358 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
360 # End of tests for bug 9761 (ConfirmFutureHolds)
362 # test marking a hold as captured
363 my $hold_notice_count = count_hold_print_messages();
364 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
365 my $new_count = count_hold_print_messages();
366 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
368 # test that duplicate notices aren't generated
369 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
370 $new_count = count_hold_print_messages();
371 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
373 # avoiding the not_same_branch error
374 t::lib::Mocks::mock_preference('IndependentBranches', 0);
376 DelItemCheck( $bibnum, $itemnumber),
377 'book_reserved',
378 'item that is captured to fill a hold cannot be deleted',
381 my $letter = ReserveSlip( { branchcode => $branch_1, borrowernumber => $requesters{$branch_1}, biblionumber => $bibnum } );
382 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
384 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
385 # 9788a: current_holds does not return future next available hold
386 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
387 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
388 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
389 $resdate= dt_from_string();
390 $resdate->add_duration(DateTime::Duration->new(days => 2));
391 $resdate=output_pref($resdate);
392 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
393 $bibitems, 1, $resdate, $expdate, $notes,
394 'a title', $checkitem, $found);
395 my $item = Koha::Items->find( $itemnumber );
396 $holds = $item->current_holds;
397 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
398 my $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
399 is( $future_holds->count, 0, 'current_holds does not return a future next available hold');
400 # 9788b: current_holds does not return future item level hold
401 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
402 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
403 $bibitems, 1, $resdate, $expdate, $notes,
404 'a title', $itemnumber, $found); #item level hold
405 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
406 is( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
407 # 9788c: current_holds returns future wait (confirmed future hold)
408 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0); #confirm hold
409 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
410 is( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
411 # End of tests for bug 9788
413 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
414 # Tests for CalculatePriority (bug 8918)
415 my $p = C4::Reserves::CalculatePriority($bibnum2);
416 is($p, 4, 'CalculatePriority should now return priority 4');
417 $resdate=undef;
418 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum2,
419 $bibitems, $p, $resdate, $expdate, $notes,
420 'a title', $checkitem, $found);
421 $p = C4::Reserves::CalculatePriority($bibnum2);
422 is($p, 5, 'CalculatePriority should now return priority 5');
423 #some tests on bibnum
424 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
425 $p = C4::Reserves::CalculatePriority($bibnum);
426 is($p, 1, 'CalculatePriority should now return priority 1');
427 #add a new reserve and confirm it to waiting
428 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
429 $bibitems, $p, $resdate, $expdate, $notes,
430 'a title', $itemnumber, $found);
431 $p = C4::Reserves::CalculatePriority($bibnum);
432 is($p, 2, 'CalculatePriority should now return priority 2');
433 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0);
434 $p = C4::Reserves::CalculatePriority($bibnum);
435 is($p, 1, 'CalculatePriority should now return priority 1');
436 #add another biblio hold, no resdate
437 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum,
438 $bibitems, $p, $resdate, $expdate, $notes,
439 'a title', $checkitem, $found);
440 $p = C4::Reserves::CalculatePriority($bibnum);
441 is($p, 2, 'CalculatePriority should now return priority 2');
442 #add another future hold
443 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
444 $resdate= dt_from_string();
445 $resdate->add_duration(DateTime::Duration->new(days => 1));
446 AddReserve($branch_1, $requesters{'CPL3'}, $bibnum,
447 $bibitems, $p, output_pref($resdate), $expdate, $notes,
448 'a title', $checkitem, $found);
449 $p = C4::Reserves::CalculatePriority($bibnum);
450 is($p, 2, 'CalculatePriority should now still return priority 2');
451 #calc priority with future resdate
452 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
453 is($p, 3, 'CalculatePriority should now return priority 3');
454 # End of tests for bug 8918
456 # Tests for cancel reserves by users from OPAC.
457 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
458 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
459 $bibitems, 1, undef, $expdate, $notes,
460 'a title', $checkitem, '');
461 my (undef, $canres, undef) = CheckReserves($itemnumber);
463 is( CanReserveBeCanceledFromOpac(), undef,
464 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
467 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
468 undef,
469 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
472 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
473 undef,
474 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
477 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
478 is($cancancel, 1, 'Can user cancel its own reserve');
480 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
481 is($cancancel, 0, 'Other user cant cancel reserve');
483 ModReserveAffect($itemnumber, $requesters{$branch_1}, 1);
484 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
485 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
487 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
488 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
489 $bibitems, 1, undef, $expdate, $notes,
490 'a title', $checkitem, '');
491 (undef, $canres, undef) = CheckReserves($itemnumber);
493 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
494 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
495 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
497 # End of tests for bug 12876
499 ####
500 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
501 ####
503 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
505 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
507 #Set the ageRestriction for the Biblio
508 my $record = GetMarcBiblio({ biblionumber => $bibnum });
509 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
510 $record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
511 C4::Biblio::ModBiblio( $record, $bibnum, $frameworkcode );
513 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
515 #Set the dateofbirth for the Borrower making them "too young".
516 $borrower->{dateofbirth} = DateTime->now->add( years => -15 );
517 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
519 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
521 #Set the dateofbirth for the Borrower making them "too old".
522 $borrower->{dateofbirth} = DateTime->now->add( years => -30 );
523 Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
525 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
527 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblio_with_no_item->{biblionumber})->{status} , '', "Biblio with no item. Status is empty");
528 ####
529 ####### EO Bug 13113 <<<
530 ####
532 $item = Koha::Items->find($itemnumber);
534 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron), "Reserving a book on item level" );
536 my $pickup_branch = $builder->build({ source => 'Branch' })->{ branchcode };
537 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
538 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
539 my $limit = Koha::Item::Transfer::Limit->new(
541 toBranch => $pickup_branch,
542 fromBranch => $item->holdingbranch,
543 itemtype => $item->effective_itemtype,
545 )->store();
546 is( C4::Reserves::IsAvailableForItemLevelRequest($item, $patron, $pickup_branch), 0, "Item level request not available due to transfer limit" );
547 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
549 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
550 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
551 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
552 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
553 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
554 AddReserve($branch_1, $borrowernumber, $item_bibnum,
555 $bibitems, 1, undef, $expdate, $notes, 'a title', $checkitem, '');
556 MoveReserve( $itemnumber, $borrowernumber );
557 ($status)=CheckReserves( $itemnumber );
558 is( $status, '', 'MoveReserve filled hold');
559 # hold from A waiting, today, no fut holds: MoveReserve should fill it
560 AddReserve($branch_1, $borrowernumber, $item_bibnum,
561 $bibitems, 1, undef, $expdate, $notes, 'a title', $checkitem, 'W');
562 MoveReserve( $itemnumber, $borrowernumber );
563 ($status)=CheckReserves( $itemnumber );
564 is( $status, '', 'MoveReserve filled waiting hold');
565 # hold from A pos 1, tomorrow, no fut holds: not filled
566 $resdate= dt_from_string();
567 $resdate->add_duration(DateTime::Duration->new(days => 1));
568 $resdate=output_pref($resdate);
569 AddReserve($branch_1, $borrowernumber, $item_bibnum,
570 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
571 MoveReserve( $itemnumber, $borrowernumber );
572 ($status)=CheckReserves( $itemnumber, undef, 1 );
573 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
574 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
575 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
576 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
577 AddReserve($branch_1, $borrowernumber, $item_bibnum,
578 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
579 MoveReserve( $itemnumber, $borrowernumber );
580 ($status)=CheckReserves( $itemnumber, undef, 2 );
581 is( $status, '', 'MoveReserve filled future hold now');
582 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
583 AddReserve($branch_1, $borrowernumber, $item_bibnum,
584 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, 'W');
585 MoveReserve( $itemnumber, $borrowernumber );
586 ($status)=CheckReserves( $itemnumber, undef, 2 );
587 is( $status, '', 'MoveReserve filled future waiting hold now');
588 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
589 $resdate= dt_from_string();
590 $resdate->add_duration(DateTime::Duration->new(days => 3));
591 $resdate=output_pref($resdate);
592 AddReserve($branch_1, $borrowernumber, $item_bibnum,
593 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
594 MoveReserve( $itemnumber, $borrowernumber );
595 ($status)=CheckReserves( $itemnumber, undef, 3 );
596 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
597 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
599 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
600 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
601 $cache->clear_from_cache("default_value_for_mod_marc-");
602 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
604 subtest '_koha_notify_reserve() tests' => sub {
606 plan tests => 2;
608 my $wants_hold_and_email = {
609 wants_digest => '0',
610 transports => {
611 sms => 'HOLD',
612 email => 'HOLD',
614 letter_code => 'HOLD'
617 my $mp = Test::MockModule->new( 'C4::Members::Messaging' );
619 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
621 $dbh->do('DELETE FROM letter');
623 my $email_hold_notice = $builder->build({
624 source => 'Letter',
625 value => {
626 message_transport_type => 'email',
627 branchcode => '',
628 code => 'HOLD',
629 module => 'reserves',
630 lang => 'default',
634 my $sms_hold_notice = $builder->build({
635 source => 'Letter',
636 value => {
637 message_transport_type => 'sms',
638 branchcode => '',
639 code => 'HOLD',
640 module => 'reserves',
641 lang=>'default',
645 my $hold_borrower = $builder->build({
646 source => 'Borrower',
647 value => {
648 smsalertnumber=>'5555555555',
649 email=>'a@b.com',
651 })->{borrowernumber};
653 C4::Reserves::AddReserve(
654 $item->homebranch, $hold_borrower,
655 $item->biblionumber );
657 ModReserveAffect($item->itemnumber, $hold_borrower, 0);
658 my $sms_message_address = $schema->resultset('MessageQueue')->search({
659 letter_code => 'HOLD',
660 message_transport_type => 'sms',
661 borrowernumber => $hold_borrower,
662 })->next()->to_address();
663 is($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
665 my $email_message_address = $schema->resultset('MessageQueue')->search({
666 letter_code => 'HOLD',
667 message_transport_type => 'email',
668 borrowernumber => $hold_borrower,
669 })->next()->to_address();
670 is($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
674 subtest 'ReservesNeedReturns' => sub {
675 plan tests => 4;
677 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
678 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
679 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
680 my $item_info = {
681 biblionumber => $biblioitem->biblionumber,
682 biblioitemnumber => $biblioitem->biblioitemnumber,
683 homebranch => $library->branchcode,
684 holdingbranch => $library->branchcode,
685 itype => $itemtype->itemtype,
687 my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
688 my $patron = $builder->build_object(
690 class => 'Koha::Patrons',
691 value => { branchcode => $library->branchcode, }
695 my $priority = 1;
696 my ( $hold_id, $hold );
698 t::lib::Mocks::mock_preference('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
699 $hold_id = C4::Reserves::AddReserve(
700 $library->branchcode, $patron->borrowernumber,
701 $item->biblionumber, '',
702 $priority, undef,
703 undef, '',
704 "title for fee", $item->itemnumber,
706 $hold = Koha::Holds->find($hold_id);
707 is( $hold->priority, 0, 'If ReservesNeedReturns is 0, priority must have been set to 0' );
708 is( $hold->found, 'W', 'If ReservesNeedReturns is 0, found must have been set waiting' );
710 $hold->delete; # cleanup
712 t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # '0' means "Don't automatically mark a hold as found and waiting"
713 $hold_id = C4::Reserves::AddReserve(
714 $library->branchcode, $patron->borrowernumber,
715 $item->biblionumber, '',
716 $priority, undef,
717 undef, '',
718 "title for fee", $item->itemnumber,
720 $hold = Koha::Holds->find($hold_id);
721 is( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
722 is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
725 subtest 'ChargeReserveFee tests' => sub {
727 plan tests => 8;
729 my $library = $builder->build_object({ class => 'Koha::Libraries' });
730 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
732 my $fee = 20;
733 my $title = 'A title';
735 my $context = Test::MockModule->new('C4::Context');
736 $context->mock( userenv => { branch => $library->id } );
738 my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title );
740 is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
741 ok( $line->is_debit, 'Generates a debit line' );
742 is( $line->accounttype, 'Res' , 'generates Res accounttype');
743 is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
744 is( $line->amount, $fee , 'amount set correctly');
745 is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
746 is( $line->description, "$title" , 'description is title of reserved item');
747 is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
750 subtest 'reserves.item_level_hold' => sub {
751 plan tests => 2;
753 my $item = $builder->build_sample_item;
754 my $patron = $builder->build_object(
756 class => 'Koha::Patrons',
757 value => { branchcode => $item->homebranch }
761 subtest 'item level hold' => sub {
762 plan tests => 2;
763 my $reserve_id =
764 AddReserve( $item->homebranch, $patron->borrowernumber,
765 $item->biblionumber, undef, 1, undef, undef, '', '',
766 $item->itemnumber );
768 my $hold = Koha::Holds->find($reserve_id);
769 is( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' );
771 # Mark it waiting
772 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
774 # Revert the waiting status
775 C4::Reserves::RevertWaitingStatus(
776 { itemnumber => $item->itemnumber } );
778 $hold = Koha::Holds->find($reserve_id);
780 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' );
782 $hold->delete; # cleanup
785 subtest 'biblio level hold' => sub {
786 plan tests => 3;
787 my $reserve_id = AddReserve( $item->homebranch, $patron->borrowernumber,
788 $item->biblionumber, undef, 1 );
790 my $hold = Koha::Holds->find($reserve_id);
791 is( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' );
793 # Mark it waiting
794 ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 );
796 $hold = Koha::Holds->find($reserve_id);
797 is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should be set on hold confirmation' );
799 # Revert the waiting status
800 C4::Reserves::RevertWaitingStatus( { itemnumber => $item->itemnumber } );
802 $hold = Koha::Holds->find($reserve_id);
803 is( $hold->itemnumber, undef, 'Itemnumber should be removed when the waiting status is revert' );
805 $hold->delete;
810 sub count_hold_print_messages {
811 my $message_count = $dbh->selectall_arrayref(q{
812 SELECT COUNT(*)
813 FROM message_queue
814 WHERE letter_code = 'HOLD'
815 AND message_transport_type = 'print'
817 return $message_count->[0]->[0];
820 # we reached the finish
821 $schema->storage->txn_rollback();