Bug 19059: Move C4::Reserves::CancelReserve to Koha::Hold->cancel
[koha.git] / t / db_dependent / Reserves.t
blob2a927b360d74b7c4d9b73ef130bd30e6ac3a5f28
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 => 67;
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::Biblio;
31 use C4::Circulation;
32 use C4::Items;
33 use C4::Members;
34 use C4::Reserves;
35 use Koha::Caches;
36 use Koha::DateUtils;
37 use Koha::Holds;
38 use Koha::Libraries;
39 use Koha::Notice::Templates;
40 use Koha::Patrons;
41 use Koha::Patron::Categories;
43 BEGIN {
44 require_ok('C4::Reserves');
47 # Start transaction
48 my $database = Koha::Database->new();
49 my $schema = $database->schema();
50 $schema->storage->txn_begin();
51 my $dbh = C4::Context->dbh;
53 my $builder = t::lib::TestBuilder->new;
55 my $frameworkcode = q||;
57 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
58 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
59 my $cache = Koha::Caches->get_instance;
60 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
61 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
62 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
63 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
65 ## Setup Test
66 # Add branches
67 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
68 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
69 my $branch_3 = $builder->build({ source => 'Branch' })->{ branchcode };
70 # Add categories
71 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
72 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
73 # Add an item type
74 my $itemtype = $builder->build(
75 { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
77 C4::Context->set_userenv(
78 undef, undef, undef, undef, undef, undef, $branch_1
81 # Create a helper biblio
82 my $bib = MARC::Record->new();
83 my $title = 'Silence in the library';
84 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
85 $bib->append_fields(
86 MARC::Field->new('600', '', '1', a => 'Moffat, Steven'),
87 MARC::Field->new('200', '', '', a => $title),
90 else {
91 $bib->append_fields(
92 MARC::Field->new('100', '', '', a => 'Moffat, Steven'),
93 MARC::Field->new('245', '', '', a => $title),
96 my ( $bibnum ) = AddBiblio($bib, $frameworkcode);
98 # Create a helper item instance for testing
99 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
100 { homebranch => $branch_1,
101 holdingbranch => $branch_1,
102 itype => $itemtype
104 $bibnum
108 # Modify item; setting barcode.
109 my $testbarcode = '97531';
110 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
112 # Create a borrower
113 my %data = (
114 firstname => 'my firstname',
115 surname => 'my surname',
116 categorycode => $category_1,
117 branchcode => $branch_1,
119 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
120 my $borrowernumber = AddMember(%data);
121 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
122 my $biblionumber = $bibnum;
123 my $barcode = $testbarcode;
125 my $bibitems = '';
126 my $priority = '1';
127 my $resdate = undef;
128 my $expdate = undef;
129 my $notes = '';
130 my $checkitem = undef;
131 my $found = undef;
133 my $branchcode = Koha::Libraries->search->next->branchcode;
135 AddReserve($branchcode, $borrowernumber, $biblionumber,
136 $bibitems, $priority, $resdate, $expdate, $notes,
137 $title, $checkitem, $found);
139 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
141 is($status, "Reserved", "CheckReserves Test 1");
143 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
145 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
146 is($status, "Reserved", "CheckReserves Test 2");
148 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
149 is($status, "Reserved", "CheckReserves Test 3");
151 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
152 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
154 'ItemHomeLib' eq GetReservesControlBranch(
155 { homebranch => 'ItemHomeLib' },
156 { branchcode => 'PatronHomeLib' }
157 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
159 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
161 'PatronHomeLib' eq GetReservesControlBranch(
162 { homebranch => 'ItemHomeLib' },
163 { branchcode => 'PatronHomeLib' }
164 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
166 t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
169 ### Regression test for bug 10272
171 my %requesters = ();
172 $requesters{$branch_1} = AddMember(
173 branchcode => $branch_1,
174 categorycode => $category_2,
175 surname => "borrower from $branch_1",
177 for my $i ( 2 .. 5 ) {
178 $requesters{"CPL$i"} = AddMember(
179 branchcode => $branch_1,
180 categorycode => $category_2,
181 surname => "borrower $i from $branch_1",
184 $requesters{$branch_2} = AddMember(
185 branchcode => $branch_2,
186 categorycode => $category_2,
187 surname => "borrower from $branch_2",
189 $requesters{$branch_3} = AddMember(
190 branchcode => $branch_3,
191 categorycode => $category_2,
192 surname => "borrower from $branch_3",
195 # Configure rules so that $branch_1 allows only $branch_1 patrons
196 # to request its items, while $branch_2 will allow its items
197 # to fill holds from anywhere.
199 $dbh->do('DELETE FROM issuingrules');
200 $dbh->do('DELETE FROM branch_item_rules');
201 $dbh->do('DELETE FROM branch_borrower_circ_rules');
202 $dbh->do('DELETE FROM default_borrower_circ_rules');
203 $dbh->do('DELETE FROM default_branch_item_rules');
204 $dbh->do('DELETE FROM default_branch_circ_rules');
205 $dbh->do('DELETE FROM default_circ_rules');
206 $dbh->do(
207 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
208 VALUES (?, ?, ?, ?)},
210 '*', '*', '*', 25
213 # CPL allows only its own patrons to request its items
214 $dbh->do(
215 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
216 VALUES (?, ?, ?, ?)},
218 $branch_1, 10, 1, 'homebranch',
221 # ... while FPL allows anybody to request its items
222 $dbh->do(
223 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
224 VALUES (?, ?, ?, ?)},
226 $branch_2, 10, 2, 'homebranch',
229 # helper biblio for the bug 10272 regression test
230 my $bib2 = MARC::Record->new();
231 $bib2->append_fields(
232 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
233 MARC::Field->new('245', ' ', ' ', a => $title),
236 # create one item belonging to FPL and one belonging to CPL
237 my ( $bibnum2 ) = AddBiblio($bib, $frameworkcode);
238 my ($itemnum_cpl, $itemnum_fpl);
239 ( undef, undef, $itemnum_cpl ) = AddItem(
240 { homebranch => $branch_1,
241 holdingbranch => $branch_1,
242 barcode => 'bug10272_CPL',
243 itype => $itemtype
245 $bibnum2
247 ( undef, undef, $itemnum_fpl ) = AddItem(
248 { homebranch => $branch_2,
249 holdingbranch => $branch_2,
250 barcode => 'bug10272_FPL',
251 itype => $itemtype
253 $bibnum2
257 # Ensure that priorities are numbered correcly when a hold is moved to waiting
258 # (bug 11947)
259 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
260 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
261 $bibitems, 1, $resdate, $expdate, $notes,
262 $title, $checkitem, $found);
263 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
264 $bibitems, 2, $resdate, $expdate, $notes,
265 $title, $checkitem, $found);
266 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
267 $bibitems, 3, $resdate, $expdate, $notes,
268 $title, $checkitem, $found);
269 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
271 # Now it should have different priorities.
272 my $biblio = Koha::Biblios->find( $bibnum2 );
273 my $holds = $biblio->holds({}, { order_by => 'reserve_id' });;
274 is($holds->next->priority, 0, 'Item is correctly waiting');
275 is($holds->next->priority, 1, 'Item is correctly priority 1');
276 is($holds->next->priority, 2, 'Item is correctly priority 2');
278 my @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting();
279 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
280 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
283 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
284 AddReserve($branch_3, $requesters{$branch_3}, $bibnum2,
285 $bibitems, 1, $resdate, $expdate, $notes,
286 $title, $checkitem, $found);
287 AddReserve($branch_2, $requesters{$branch_2}, $bibnum2,
288 $bibitems, 2, $resdate, $expdate, $notes,
289 $title, $checkitem, $found);
290 AddReserve($branch_1, $requesters{$branch_1}, $bibnum2,
291 $bibitems, 3, $resdate, $expdate, $notes,
292 $title, $checkitem, $found);
294 # Ensure that the item's home library controls hold policy lookup
295 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
297 my $messages;
298 # Return the CPL item at FPL. The hold that should be triggered is
299 # the one placed by the CPL patron, as the other two patron's hold
300 # requests cannot be filled by that item per policy.
301 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
302 is( $messages->{ResFound}->{borrowernumber},
303 $requesters{$branch_1},
304 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
306 # Return the FPL item at FPL. The hold that should be triggered is
307 # the one placed by the RPL patron, as that patron is first in line
308 # and RPL imposes no restrictions on whose holds its items can fill.
310 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
311 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
313 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
314 is( $messages->{ResFound}->{borrowernumber},
315 $requesters{$branch_3},
316 'for generous library, its items fill first hold request in line (bug 10272)');
318 $biblio = Koha::Biblios->find( $biblionumber );
319 $holds = $biblio->holds;
320 is($holds->count, 1, "Only one reserves for this biblio");
321 my $reserve_id = $holds->next->reserve_id;
323 Koha::Holds->find( $reserve_id )->cancel;
325 my $hold = Koha::Holds->find( $reserve_id );
326 is($hold, undef, "Koha::Holds->cancel should have cancel the reserve");
328 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
329 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
330 # Test 9761a: Add a reserve without date, CheckReserve should return it
331 $resdate= undef; #defaults to today in AddReserve
332 $expdate= undef; #no expdate
333 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
334 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
335 $bibitems, 1, $resdate, $expdate, $notes,
336 $title, $checkitem, $found);
337 ($status)=CheckReserves($itemnumber,undef,undef);
338 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
339 ($status)=CheckReserves($itemnumber,undef,7);
340 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
342 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
343 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
344 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
345 $resdate= dt_from_string();
346 $resdate->add_duration(DateTime::Duration->new(days => 4));
347 $resdate=output_pref($resdate);
348 $expdate= undef; #no expdate
349 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
350 $bibitems, 1, $resdate, $expdate, $notes,
351 $title, $checkitem, $found);
352 ($status)=CheckReserves($itemnumber,undef,undef);
353 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
355 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
356 ($status)=CheckReserves($itemnumber,undef,3);
357 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
358 ($status)=CheckReserves($itemnumber,undef,4);
359 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
361 # Test 9761d: Check ResFound message of AddReturn for future hold
362 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
363 # In this test we do not need an issued item; it is just a 'checkin'
364 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
365 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
366 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
367 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
368 ($doreturn, $messages)= AddReturn('97531',$branch_1);
369 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
370 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
371 ($doreturn, $messages)= AddReturn('97531',$branch_1);
372 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
374 # End of tests for bug 9761 (ConfirmFutureHolds)
376 # test marking a hold as captured
377 my $hold_notice_count = count_hold_print_messages();
378 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
379 my $new_count = count_hold_print_messages();
380 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
382 # test that duplicate notices aren't generated
383 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
384 $new_count = count_hold_print_messages();
385 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
387 # avoiding the not_same_branch error
388 t::lib::Mocks::mock_preference('IndependentBranches', 0);
390 DelItemCheck( $bibnum, $itemnumber),
391 'book_reserved',
392 'item that is captured to fill a hold cannot be deleted',
395 my $letter = ReserveSlip($branch_1, $requesters{$branch_1}, $bibnum);
396 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
398 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
399 # 9788a: current_holds does not return future next available hold
400 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
401 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
402 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
403 $resdate= dt_from_string();
404 $resdate->add_duration(DateTime::Duration->new(days => 2));
405 $resdate=output_pref($resdate);
406 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
407 $bibitems, 1, $resdate, $expdate, $notes,
408 $title, $checkitem, $found);
409 my $item = Koha::Items->find( $itemnumber );
410 $holds = $item->current_holds;
411 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
412 my $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
413 is( $future_holds->count, 0, 'current_holds does not return a future next available hold');
414 # 9788b: current_holds does not return future item level hold
415 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
416 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
417 $bibitems, 1, $resdate, $expdate, $notes,
418 $title, $itemnumber, $found); #item level hold
419 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
420 is( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
421 # 9788c: current_holds returns future wait (confirmed future hold)
422 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0); #confirm hold
423 $future_holds = $holds->search({ reservedate => { '>' => $dtf->format_date( dt_from_string ) } } );
424 is( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
425 # End of tests for bug 9788
427 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
428 # Tests for CalculatePriority (bug 8918)
429 my $p = C4::Reserves::CalculatePriority($bibnum2);
430 is($p, 4, 'CalculatePriority should now return priority 4');
431 $resdate=undef;
432 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum2,
433 $bibitems, $p, $resdate, $expdate, $notes,
434 $title, $checkitem, $found);
435 $p = C4::Reserves::CalculatePriority($bibnum2);
436 is($p, 5, 'CalculatePriority should now return priority 5');
437 #some tests on bibnum
438 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
439 $p = C4::Reserves::CalculatePriority($bibnum);
440 is($p, 1, 'CalculatePriority should now return priority 1');
441 #add a new reserve and confirm it to waiting
442 AddReserve($branch_1, $requesters{$branch_1}, $bibnum,
443 $bibitems, $p, $resdate, $expdate, $notes,
444 $title, $itemnumber, $found);
445 $p = C4::Reserves::CalculatePriority($bibnum);
446 is($p, 2, 'CalculatePriority should now return priority 2');
447 ModReserveAffect( $itemnumber, $requesters{$branch_1} , 0);
448 $p = C4::Reserves::CalculatePriority($bibnum);
449 is($p, 1, 'CalculatePriority should now return priority 1');
450 #add another biblio hold, no resdate
451 AddReserve($branch_1, $requesters{'CPL2'}, $bibnum,
452 $bibitems, $p, $resdate, $expdate, $notes,
453 $title, $checkitem, $found);
454 $p = C4::Reserves::CalculatePriority($bibnum);
455 is($p, 2, 'CalculatePriority should now return priority 2');
456 #add another future hold
457 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
458 $resdate= dt_from_string();
459 $resdate->add_duration(DateTime::Duration->new(days => 1));
460 AddReserve($branch_1, $requesters{'CPL3'}, $bibnum,
461 $bibitems, $p, output_pref($resdate), $expdate, $notes,
462 $title, $checkitem, $found);
463 $p = C4::Reserves::CalculatePriority($bibnum);
464 is($p, 2, 'CalculatePriority should now still return priority 2');
465 #calc priority with future resdate
466 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
467 is($p, 3, 'CalculatePriority should now return priority 3');
468 # End of tests for bug 8918
470 # Tests for cancel reserves by users from OPAC.
471 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
472 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
473 $bibitems, 1, undef, $expdate, $notes,
474 $title, $checkitem, '');
475 my (undef, $canres, undef) = CheckReserves($itemnumber);
477 is( CanReserveBeCanceledFromOpac(), undef,
478 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
481 CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
482 undef,
483 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
486 CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
487 undef,
488 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
491 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
492 is($cancancel, 1, 'Can user cancel its own reserve');
494 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
495 is($cancancel, 0, 'Other user cant cancel reserve');
497 ModReserveAffect($itemnumber, $requesters{$branch_1}, 1);
498 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
499 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
501 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
502 AddReserve($branch_1, $requesters{$branch_1}, $item_bibnum,
503 $bibitems, 1, undef, $expdate, $notes,
504 $title, $checkitem, '');
505 (undef, $canres, undef) = CheckReserves($itemnumber);
507 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
508 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
509 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
511 # End of tests for bug 12876
513 ####
514 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
515 ####
517 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
519 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
521 #Set the ageRestriction for the Biblio
522 my $record = GetMarcBiblio({ biblionumber => $bibnum });
523 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
524 $record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
525 C4::Biblio::ModBiblio( $record, $bibnum, $frameworkcode );
527 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
529 #Set the dateofbirth for the Borrower making them "too young".
530 $borrower->{dateofbirth} = DateTime->now->add( years => -15 );
531 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
533 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
535 #Set the dateofbirth for the Borrower making them "too old".
536 $borrower->{dateofbirth} = DateTime->now->add( years => -30 );
537 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
539 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
540 ####
541 ####### EO Bug 13113 <<<
542 ####
544 $item = GetItem($itemnumber);
546 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $borrower), "Reserving a book on item level" );
548 my $itype = C4::Reserves::_get_itype($item);
549 my $categorycode = $borrower->{categorycode};
550 my $holdingbranch = $item->{holdingbranch};
551 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
553 categorycode => $categorycode,
554 itemtype => $itype,
555 branchcode => $holdingbranch
559 $dbh->do(
560 "UPDATE issuingrules SET onshelfholds = 1 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
561 undef,
562 $issuing_rule->categorycode, $issuing_rule->itemtype, $issuing_rule->branchcode
564 ok( C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() allowed" );
565 $dbh->do(
566 "UPDATE issuingrules SET onshelfholds = 0 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
567 undef,
568 $issuing_rule->categorycode, $issuing_rule->itemtype, $issuing_rule->branchcode
570 ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
572 # Tests for bug 14464
574 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
575 my $patron = Koha::Patrons->find( $borrowernumber );
576 my $bz14464_fines = $patron->account->balance;
577 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines at beginning' );
579 # First, test cancelling a reserve when there's no charge configured.
580 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
582 my $bz14464_reserve = AddReserve(
583 $branch_1,
584 $borrowernumber,
585 $bibnum,
586 undef,
587 '1',
588 undef,
589 undef,
591 $title,
592 $itemnumber,
596 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
598 Koha::Holds->find( $bz14464_reserve )->cancel( { charge_cancel_fee => 1 } );
600 my $old_reserve = Koha::Database->new()->schema()->resultset('OldReserve')->find( $bz14464_reserve );
601 is($old_reserve->get_column('found'), 'W', 'Bug 14968 - Keep found column from reserve');
603 $bz14464_fines = $patron->account->balance;
604 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines after cancelling reserve with no charge configured' );
606 # Then, test cancelling a reserve when there's no charge desired.
607 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
609 $bz14464_reserve = AddReserve(
610 $branch_1,
611 $borrowernumber,
612 $bibnum,
613 undef,
614 '1',
615 undef,
616 undef,
618 $title,
619 $itemnumber,
623 ok( $bz14464_reserve, 'Bug 14464 - 2nd reserve correctly created' );
625 Koha::Holds->find( $bz14464_reserve )->cancel();
627 $bz14464_fines = $patron->account->balance;
628 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines after cancelling reserve with no charge desired' );
630 # Finally, test cancelling a reserve when there's a charge desired and configured.
631 $bz14464_reserve = AddReserve(
632 $branch_1,
633 $borrowernumber,
634 $bibnum,
635 undef,
636 '1',
637 undef,
638 undef,
640 $title,
641 $itemnumber,
645 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
647 Koha::Holds->find( $bz14464_reserve )->cancel( { charge_cancel_fee => 1 } );
649 $bz14464_fines = $patron->account->balance;
650 is( int( $bz14464_fines ), 42, 'Bug 14464 - Fine applied after cancelling reserve with charge desired and configured' );
652 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
653 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
654 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
655 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
656 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
657 AddReserve($branch_1, $borrowernumber, $item_bibnum,
658 $bibitems, 1, undef, $expdate, $notes, $title, $checkitem, '');
659 MoveReserve( $itemnumber, $borrowernumber );
660 ($status)=CheckReserves( $itemnumber );
661 is( $status, '', 'MoveReserve filled hold');
662 # hold from A waiting, today, no fut holds: MoveReserve should fill it
663 AddReserve($branch_1, $borrowernumber, $item_bibnum,
664 $bibitems, 1, undef, $expdate, $notes, $title, $checkitem, 'W');
665 MoveReserve( $itemnumber, $borrowernumber );
666 ($status)=CheckReserves( $itemnumber );
667 is( $status, '', 'MoveReserve filled waiting hold');
668 # hold from A pos 1, tomorrow, no fut holds: not filled
669 $resdate= dt_from_string();
670 $resdate->add_duration(DateTime::Duration->new(days => 1));
671 $resdate=output_pref($resdate);
672 AddReserve($branch_1, $borrowernumber, $item_bibnum,
673 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
674 MoveReserve( $itemnumber, $borrowernumber );
675 ($status)=CheckReserves( $itemnumber, undef, 1 );
676 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
677 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
678 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
679 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
680 AddReserve($branch_1, $borrowernumber, $item_bibnum,
681 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
682 MoveReserve( $itemnumber, $borrowernumber );
683 ($status)=CheckReserves( $itemnumber, undef, 2 );
684 is( $status, '', 'MoveReserve filled future hold now');
685 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
686 AddReserve($branch_1, $borrowernumber, $item_bibnum,
687 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, 'W');
688 MoveReserve( $itemnumber, $borrowernumber );
689 ($status)=CheckReserves( $itemnumber, undef, 2 );
690 is( $status, '', 'MoveReserve filled future waiting hold now');
691 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
692 $resdate= dt_from_string();
693 $resdate->add_duration(DateTime::Duration->new(days => 3));
694 $resdate=output_pref($resdate);
695 AddReserve($branch_1, $borrowernumber, $item_bibnum,
696 $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, '');
697 MoveReserve( $itemnumber, $borrowernumber );
698 ($status)=CheckReserves( $itemnumber, undef, 3 );
699 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
700 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
702 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
703 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
704 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
705 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
707 subtest '_koha_notify_reserve() tests' => sub {
709 plan tests => 2;
711 my $wants_hold_and_email = {
712 wants_digest => '0',
713 transports => {
714 sms => 'HOLD',
715 email => 'HOLD',
717 letter_code => 'HOLD'
720 my $mp = Test::MockModule->new( 'C4::Members::Messaging' );
722 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
724 $dbh->do('DELETE FROM letter');
726 my $email_hold_notice = $builder->build({
727 source => 'Letter',
728 value => {
729 message_transport_type => 'email',
730 branchcode => '',
731 code => 'HOLD',
732 module => 'reserves',
733 lang => 'default',
737 my $sms_hold_notice = $builder->build({
738 source => 'Letter',
739 value => {
740 message_transport_type => 'sms',
741 branchcode => '',
742 code => 'HOLD',
743 module => 'reserves',
744 lang=>'default',
748 my $hold_borrower = $builder->build({
749 source => 'Borrower',
750 value => {
751 smsalertnumber=>'5555555555',
752 email=>'a@b.com',
754 })->{borrowernumber};
756 my $hold = $builder->build({
757 source => 'Reserve',
758 value => {
759 borrowernumber=>$hold_borrower
763 ModReserveAffect($hold->{itemnumber}, $hold->{borrowernumber}, 0);
764 my $sms_message_address = $schema->resultset('MessageQueue')->search({
765 letter_code => 'HOLD',
766 message_transport_type => 'sms',
767 borrowernumber => $hold_borrower,
768 })->next()->to_address();
769 is($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
771 my $email_message_address = $schema->resultset('MessageQueue')->search({
772 letter_code => 'HOLD',
773 message_transport_type => 'email',
774 borrowernumber => $hold_borrower,
775 })->next()->to_address();
776 is($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
780 sub count_hold_print_messages {
781 my $message_count = $dbh->selectall_arrayref(q{
782 SELECT COUNT(*)
783 FROM message_queue
784 WHERE letter_code = 'HOLD'
785 AND message_transport_type = 'print'
787 return $message_count->[0]->[0];
790 # we reached the finish
791 $schema->storage->txn_rollback();