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
=> 61;
25 use t
::lib
::TestBuilder
;
28 use DateTime
::Duration
;
40 use Koha
::Notice
::Templates
;
42 use Koha
::Patron
::Categories
;
45 require_ok
('C4::Reserves');
49 my $database = Koha
::Database
->new();
50 my $schema = $database->schema();
51 $schema->storage->txn_begin();
52 my $dbh = C4
::Context
->dbh;
54 my $builder = t
::lib
::TestBuilder
->new;
56 my $frameworkcode = q
//;
59 t
::lib
::Mocks
::mock_preference
('ReservesNeedReturns', 1);
61 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
62 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
63 my $cache = Koha
::Caches
->get_instance;
64 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
65 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
66 $cache->clear_from_cache("default_value_for_mod_marc-");
67 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
71 my $branch_1 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
72 my $branch_2 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
73 my $branch_3 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
75 my $category_1 = $builder->build({ source
=> 'Category' })->{ categorycode
};
76 my $category_2 = $builder->build({ source
=> 'Category' })->{ categorycode
};
78 my $itemtype = $builder->build(
79 { source
=> 'Itemtype', value
=> { notforloan
=> undef } } )->{itemtype
};
81 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $branch_1 });
83 my $bibnum = $builder->build_sample_biblio({frameworkcode
=> $frameworkcode})->biblionumber;
85 # Create a helper item instance for testing
86 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem
(
87 { homebranch
=> $branch_1,
88 holdingbranch
=> $branch_1,
94 my $biblio_with_no_item = $builder->build({
99 # Modify item; setting barcode.
100 my $testbarcode = '97531';
101 ModItem
({ barcode
=> $testbarcode }, $bibnum, $itemnumber);
105 firstname
=> 'my firstname',
106 surname
=> 'my surname',
107 categorycode
=> $category_1,
108 branchcode
=> $branch_1,
110 Koha
::Patron
::Categories
->find($category_1)->set({ enrolmentfee
=> 0})->store;
111 my $borrowernumber = Koha
::Patron
->new(\
%data)->store->borrowernumber;
112 my $patron = Koha
::Patrons
->find( $borrowernumber );
113 my $borrower = $patron->unblessed;
114 my $biblionumber = $bibnum;
115 my $barcode = $testbarcode;
122 my $checkitem = undef;
125 my $branchcode = Koha
::Libraries
->search->next->branchcode;
127 AddReserve
($branchcode, $borrowernumber, $biblionumber,
128 $bibitems, $priority, $resdate, $expdate, $notes,
129 'a title', $checkitem, $found);
131 my ($status, $reserve, $all_reserves) = CheckReserves
($itemnumber, $barcode);
133 is
($status, "Reserved", "CheckReserves Test 1");
135 ok
(exists($reserve->{reserve_id
}), 'CheckReserves() include reserve_id in its response');
137 ($status, $reserve, $all_reserves) = CheckReserves
($itemnumber);
138 is
($status, "Reserved", "CheckReserves Test 2");
140 ($status, $reserve, $all_reserves) = CheckReserves
(undef, $barcode);
141 is
($status, "Reserved", "CheckReserves Test 3");
143 my $ReservesControlBranch = C4
::Context
->preference('ReservesControlBranch');
144 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'ItemHomeLibrary' );
146 'ItemHomeLib' eq GetReservesControlBranch
(
147 { homebranch
=> 'ItemHomeLib' },
148 { branchcode
=> 'PatronHomeLib' }
149 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
151 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
153 'PatronHomeLib' eq GetReservesControlBranch
(
154 { homebranch
=> 'ItemHomeLib' },
155 { branchcode
=> 'PatronHomeLib' }
156 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
158 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', $ReservesControlBranch );
161 ### Regression test for bug 10272
164 $requesters{$branch_1} = Koha
::Patron
->new({
165 branchcode
=> $branch_1,
166 categorycode
=> $category_2,
167 surname
=> "borrower from $branch_1",
168 })->store->borrowernumber;
169 for my $i ( 2 .. 5 ) {
170 $requesters{"CPL$i"} = Koha
::Patron
->new({
171 branchcode
=> $branch_1,
172 categorycode
=> $category_2,
173 surname
=> "borrower $i from $branch_1",
174 })->store->borrowernumber;
176 $requesters{$branch_2} = Koha
::Patron
->new({
177 branchcode
=> $branch_2,
178 categorycode
=> $category_2,
179 surname
=> "borrower from $branch_2",
180 })->store->borrowernumber;
181 $requesters{$branch_3} = Koha
::Patron
->new({
182 branchcode
=> $branch_3,
183 categorycode
=> $category_2,
184 surname
=> "borrower from $branch_3",
185 })->store->borrowernumber;
187 # Configure rules so that $branch_1 allows only $branch_1 patrons
188 # to request its items, while $branch_2 will allow its items
189 # to fill holds from anywhere.
191 $dbh->do('DELETE FROM issuingrules');
192 $dbh->do('DELETE FROM branch_item_rules');
193 $dbh->do('DELETE FROM default_branch_item_rules');
194 $dbh->do('DELETE FROM default_branch_circ_rules');
195 $dbh->do('DELETE FROM default_circ_rules');
197 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
198 VALUES (?, ?, ?, ?)},
203 # CPL allows only its own patrons to request its items
205 q{INSERT INTO default_branch_circ_rules (branchcode, holdallowed, returnbranch)
208 $branch_1, 1, 'homebranch',
211 # ... while FPL allows anybody to request its items
213 q{INSERT INTO default_branch_circ_rules (branchcode, holdallowed, returnbranch)
216 $branch_2, 2, 'homebranch',
219 my $bibnum2 = $builder->build_sample_biblio({frameworkcode
=> $frameworkcode})->biblionumber;
221 my ($itemnum_cpl, $itemnum_fpl);
222 ( undef, undef, $itemnum_cpl ) = AddItem
(
223 { homebranch
=> $branch_1,
224 holdingbranch
=> $branch_1,
225 barcode
=> 'bug10272_CPL',
230 ( undef, undef, $itemnum_fpl ) = AddItem
(
231 { homebranch
=> $branch_2,
232 holdingbranch
=> $branch_2,
233 barcode
=> 'bug10272_FPL',
240 # Ensure that priorities are numbered correcly when a hold is moved to waiting
242 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
243 AddReserve
($branch_3, $requesters{$branch_3}, $bibnum2,
244 $bibitems, 1, $resdate, $expdate, $notes,
245 'a title', $checkitem, $found);
246 AddReserve
($branch_2, $requesters{$branch_2}, $bibnum2,
247 $bibitems, 2, $resdate, $expdate, $notes,
248 'a title', $checkitem, $found);
249 AddReserve
($branch_1, $requesters{$branch_1}, $bibnum2,
250 $bibitems, 3, $resdate, $expdate, $notes,
251 'a title', $checkitem, $found);
252 ModReserveAffect
($itemnum_cpl, $requesters{$branch_3}, 0);
254 # Now it should have different priorities.
255 my $biblio = Koha
::Biblios
->find( $bibnum2 );
256 my $holds = $biblio->holds({}, { order_by
=> 'reserve_id' });;
257 is
($holds->next->priority, 0, 'Item is correctly waiting');
258 is
($holds->next->priority, 1, 'Item is correctly priority 1');
259 is
($holds->next->priority, 2, 'Item is correctly priority 2');
261 my @reserves = Koha
::Holds
->search({ borrowernumber
=> $requesters{$branch_3} })->waiting();
262 is
( @reserves, 1, 'GetWaiting got only the waiting reserve' );
263 is
( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
266 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
267 AddReserve
($branch_3, $requesters{$branch_3}, $bibnum2,
268 $bibitems, 1, $resdate, $expdate, $notes,
269 'a title', $checkitem, $found);
270 AddReserve
($branch_2, $requesters{$branch_2}, $bibnum2,
271 $bibitems, 2, $resdate, $expdate, $notes,
272 'a title', $checkitem, $found);
273 AddReserve
($branch_1, $requesters{$branch_1}, $bibnum2,
274 $bibitems, 3, $resdate, $expdate, $notes,
275 'a title', $checkitem, $found);
277 # Ensure that the item's home library controls hold policy lookup
278 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'ItemHomeLibrary' );
281 # Return the CPL item at FPL. The hold that should be triggered is
282 # the one placed by the CPL patron, as the other two patron's hold
283 # requests cannot be filled by that item per policy.
284 (undef, $messages, undef, undef) = AddReturn
('bug10272_CPL', $branch_2);
285 is
( $messages->{ResFound
}->{borrowernumber
},
286 $requesters{$branch_1},
287 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
289 # Return the FPL item at FPL. The hold that should be triggered is
290 # the one placed by the RPL patron, as that patron is first in line
291 # and RPL imposes no restrictions on whose holds its items can fill.
293 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
294 t
::lib
::Mocks
::mock_preference
( 'LocalHoldsPriority', '' );
296 (undef, $messages, undef, undef) = AddReturn
('bug10272_FPL', $branch_2);
297 is
( $messages->{ResFound
}->{borrowernumber
},
298 $requesters{$branch_3},
299 'for generous library, its items fill first hold request in line (bug 10272)');
301 $biblio = Koha
::Biblios
->find( $biblionumber );
302 $holds = $biblio->holds;
303 is
($holds->count, 1, "Only one reserves for this biblio");
304 my $reserve_id = $holds->next->reserve_id;
306 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
307 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
308 # Test 9761a: Add a reserve without date, CheckReserve should return it
309 $resdate= undef; #defaults to today in AddReserve
310 $expdate= undef; #no expdate
311 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
312 AddReserve
($branch_1, $requesters{$branch_1}, $bibnum,
313 $bibitems, 1, $resdate, $expdate, $notes,
314 'a title', $checkitem, $found);
315 ($status)=CheckReserves
($itemnumber,undef,undef);
316 is
( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
317 ($status)=CheckReserves
($itemnumber,undef,7);
318 is
( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
320 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
321 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
322 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
323 $resdate= dt_from_string
();
324 $resdate->add_duration(DateTime
::Duration
->new(days
=> 4));
325 $resdate=output_pref
($resdate);
326 $expdate= undef; #no expdate
327 AddReserve
($branch_1, $requesters{$branch_1}, $bibnum,
328 $bibitems, 1, $resdate, $expdate, $notes,
329 'a title', $checkitem, $found);
330 ($status)=CheckReserves
($itemnumber,undef,undef);
331 is
( $status, '', 'CheckReserves returns no future reserve without lookahead');
333 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
334 ($status)=CheckReserves
($itemnumber,undef,3);
335 is
( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
336 ($status)=CheckReserves
($itemnumber,undef,4);
337 is
( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
339 # Test 9761d: Check ResFound message of AddReturn for future hold
340 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
341 # In this test we do not need an issued item; it is just a 'checkin'
342 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 0);
343 (my $doreturn, $messages)= AddReturn
('97531',$branch_1);
344 is
($messages->{ResFound
}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
345 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 3);
346 ($doreturn, $messages)= AddReturn
('97531',$branch_1);
347 is
(exists $messages->{ResFound
}?
1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
348 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 7);
349 ($doreturn, $messages)= AddReturn
('97531',$branch_1);
350 is
(exists $messages->{ResFound
}?
1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
352 # End of tests for bug 9761 (ConfirmFutureHolds)
354 # test marking a hold as captured
355 my $hold_notice_count = count_hold_print_messages
();
356 ModReserveAffect
($itemnumber, $requesters{$branch_1}, 0);
357 my $new_count = count_hold_print_messages
();
358 is
($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
360 # test that duplicate notices aren't generated
361 ModReserveAffect
($itemnumber, $requesters{$branch_1}, 0);
362 $new_count = count_hold_print_messages
();
363 is
($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
365 # avoiding the not_same_branch error
366 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
368 DelItemCheck
( $bibnum, $itemnumber),
370 'item that is captured to fill a hold cannot be deleted',
373 my $letter = ReserveSlip
( { branchcode
=> $branch_1, borrowernumber
=> $requesters{$branch_1}, biblionumber
=> $bibnum } );
374 ok
(defined($letter), 'can successfully generate hold slip (bug 10949)');
376 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
377 # 9788a: current_holds does not return future next available hold
378 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
379 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 2);
380 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
381 $resdate= dt_from_string
();
382 $resdate->add_duration(DateTime
::Duration
->new(days
=> 2));
383 $resdate=output_pref
($resdate);
384 AddReserve
($branch_1, $requesters{$branch_1}, $bibnum,
385 $bibitems, 1, $resdate, $expdate, $notes,
386 'a title', $checkitem, $found);
387 my $item = Koha
::Items
->find( $itemnumber );
388 $holds = $item->current_holds;
389 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
390 my $future_holds = $holds->search({ reservedate
=> { '>' => $dtf->format_date( dt_from_string
) } } );
391 is
( $future_holds->count, 0, 'current_holds does not return a future next available hold');
392 # 9788b: current_holds does not return future item level hold
393 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
394 AddReserve
($branch_1, $requesters{$branch_1}, $bibnum,
395 $bibitems, 1, $resdate, $expdate, $notes,
396 'a title', $itemnumber, $found); #item level hold
397 $future_holds = $holds->search({ reservedate
=> { '>' => $dtf->format_date( dt_from_string
) } } );
398 is
( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
399 # 9788c: current_holds returns future wait (confirmed future hold)
400 ModReserveAffect
( $itemnumber, $requesters{$branch_1} , 0); #confirm hold
401 $future_holds = $holds->search({ reservedate
=> { '>' => $dtf->format_date( dt_from_string
) } } );
402 is
( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
403 # End of tests for bug 9788
405 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
406 # Tests for CalculatePriority (bug 8918)
407 my $p = C4
::Reserves
::CalculatePriority
($bibnum2);
408 is
($p, 4, 'CalculatePriority should now return priority 4');
410 AddReserve
($branch_1, $requesters{'CPL2'}, $bibnum2,
411 $bibitems, $p, $resdate, $expdate, $notes,
412 'a title', $checkitem, $found);
413 $p = C4
::Reserves
::CalculatePriority
($bibnum2);
414 is
($p, 5, 'CalculatePriority should now return priority 5');
415 #some tests on bibnum
416 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
417 $p = C4
::Reserves
::CalculatePriority
($bibnum);
418 is
($p, 1, 'CalculatePriority should now return priority 1');
419 #add a new reserve and confirm it to waiting
420 AddReserve
($branch_1, $requesters{$branch_1}, $bibnum,
421 $bibitems, $p, $resdate, $expdate, $notes,
422 'a title', $itemnumber, $found);
423 $p = C4
::Reserves
::CalculatePriority
($bibnum);
424 is
($p, 2, 'CalculatePriority should now return priority 2');
425 ModReserveAffect
( $itemnumber, $requesters{$branch_1} , 0);
426 $p = C4
::Reserves
::CalculatePriority
($bibnum);
427 is
($p, 1, 'CalculatePriority should now return priority 1');
428 #add another biblio hold, no resdate
429 AddReserve
($branch_1, $requesters{'CPL2'}, $bibnum,
430 $bibitems, $p, $resdate, $expdate, $notes,
431 'a title', $checkitem, $found);
432 $p = C4
::Reserves
::CalculatePriority
($bibnum);
433 is
($p, 2, 'CalculatePriority should now return priority 2');
434 #add another future hold
435 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
436 $resdate= dt_from_string
();
437 $resdate->add_duration(DateTime
::Duration
->new(days
=> 1));
438 AddReserve
($branch_1, $requesters{'CPL3'}, $bibnum,
439 $bibitems, $p, output_pref
($resdate), $expdate, $notes,
440 'a title', $checkitem, $found);
441 $p = C4
::Reserves
::CalculatePriority
($bibnum);
442 is
($p, 2, 'CalculatePriority should now still return priority 2');
443 #calc priority with future resdate
444 $p = C4
::Reserves
::CalculatePriority
($bibnum, $resdate);
445 is
($p, 3, 'CalculatePriority should now return priority 3');
446 # End of tests for bug 8918
448 # Tests for cancel reserves by users from OPAC.
449 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
450 AddReserve
($branch_1, $requesters{$branch_1}, $item_bibnum,
451 $bibitems, 1, undef, $expdate, $notes,
452 'a title', $checkitem, '');
453 my (undef, $canres, undef) = CheckReserves
($itemnumber);
455 is
( CanReserveBeCanceledFromOpac
(), undef,
456 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
459 CanReserveBeCanceledFromOpac
( $canres->{resserve_id
} ),
461 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
464 CanReserveBeCanceledFromOpac
( undef, $requesters{CPL
} ),
466 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
469 my $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_1});
470 is
($cancancel, 1, 'Can user cancel its own reserve');
472 $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_2});
473 is
($cancancel, 0, 'Other user cant cancel reserve');
475 ModReserveAffect
($itemnumber, $requesters{$branch_1}, 1);
476 $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_1});
477 is
($cancancel, 0, 'Reserve in transfer status cant be canceled');
479 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
480 AddReserve
($branch_1, $requesters{$branch_1}, $item_bibnum,
481 $bibitems, 1, undef, $expdate, $notes,
482 'a title', $checkitem, '');
483 (undef, $canres, undef) = CheckReserves
($itemnumber);
485 ModReserveAffect
($itemnumber, $requesters{$branch_1}, 0);
486 $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_1});
487 is
($cancancel, 0, 'Reserve in waiting status cant be canceled');
489 # End of tests for bug 12876
492 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
495 t
::lib
::Mocks
::mock_preference
( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
497 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
499 #Set the ageRestriction for the Biblio
500 my $record = GetMarcBiblio
({ biblionumber
=> $bibnum });
501 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField
( "biblioitems.agerestriction" );
502 $record->append_fields( MARC
::Field
->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
503 C4
::Biblio
::ModBiblio
( $record, $bibnum, $frameworkcode );
505 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblionumber)->{status
} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
507 #Set the dateofbirth for the Borrower making them "too young".
508 $borrower->{dateofbirth
} = DateTime
->now->add( years
=> -15 );
509 Koha
::Patrons
->find( $borrowernumber )->set({ dateofbirth
=> $borrower->{dateofbirth
} })->store;
511 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblionumber)->{status
} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
513 #Set the dateofbirth for the Borrower making them "too old".
514 $borrower->{dateofbirth
} = DateTime
->now->add( years
=> -30 );
515 Koha
::Patrons
->find( $borrowernumber )->set({ dateofbirth
=> $borrower->{dateofbirth
} })->store;
517 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblionumber)->{status
} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
519 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblio_with_no_item->{biblionumber
})->{status
} , '', "Biblio with no item. Status is empty");
521 ####### EO Bug 13113 <<<
524 $item = Koha
::Items
->find($itemnumber);
526 ok
( C4
::Reserves
::IsAvailableForItemLevelRequest
($item, $patron), "Reserving a book on item level" );
528 my $pickup_branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
529 t
::lib
::Mocks
::mock_preference
( 'UseBranchTransferLimits', '1' );
530 t
::lib
::Mocks
::mock_preference
( 'BranchTransferLimitsType', 'itemtype' );
531 my $limit = Koha
::Item
::Transfer
::Limit
->new(
533 toBranch
=> $pickup_branch,
534 fromBranch
=> $item->holdingbranch,
535 itemtype
=> $item->effective_itemtype,
538 is
( C4
::Reserves
::IsAvailableForItemLevelRequest
($item, $patron, $pickup_branch), 0, "Item level request not available due to transfer limit" );
539 t
::lib
::Mocks
::mock_preference
( 'UseBranchTransferLimits', '0' );
541 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
542 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
543 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
544 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 0);
545 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
546 AddReserve
($branch_1, $borrowernumber, $item_bibnum,
547 $bibitems, 1, undef, $expdate, $notes, 'a title', $checkitem, '');
548 MoveReserve
( $itemnumber, $borrowernumber );
549 ($status)=CheckReserves
( $itemnumber );
550 is
( $status, '', 'MoveReserve filled hold');
551 # hold from A waiting, today, no fut holds: MoveReserve should fill it
552 AddReserve
($branch_1, $borrowernumber, $item_bibnum,
553 $bibitems, 1, undef, $expdate, $notes, 'a title', $checkitem, 'W');
554 MoveReserve
( $itemnumber, $borrowernumber );
555 ($status)=CheckReserves
( $itemnumber );
556 is
( $status, '', 'MoveReserve filled waiting hold');
557 # hold from A pos 1, tomorrow, no fut holds: not filled
558 $resdate= dt_from_string
();
559 $resdate->add_duration(DateTime
::Duration
->new(days
=> 1));
560 $resdate=output_pref
($resdate);
561 AddReserve
($branch_1, $borrowernumber, $item_bibnum,
562 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
563 MoveReserve
( $itemnumber, $borrowernumber );
564 ($status)=CheckReserves
( $itemnumber, undef, 1 );
565 is
( $status, 'Reserved', 'MoveReserve did not fill future hold');
566 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
567 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
568 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 2);
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, 2 );
573 is
( $status, '', 'MoveReserve filled future hold now');
574 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
575 AddReserve
($branch_1, $borrowernumber, $item_bibnum,
576 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, 'W');
577 MoveReserve
( $itemnumber, $borrowernumber );
578 ($status)=CheckReserves
( $itemnumber, undef, 2 );
579 is
( $status, '', 'MoveReserve filled future waiting hold now');
580 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
581 $resdate= dt_from_string
();
582 $resdate->add_duration(DateTime
::Duration
->new(days
=> 3));
583 $resdate=output_pref
($resdate);
584 AddReserve
($branch_1, $borrowernumber, $item_bibnum,
585 $bibitems, 1, $resdate, $expdate, $notes, 'a title', $checkitem, '');
586 MoveReserve
( $itemnumber, $borrowernumber );
587 ($status)=CheckReserves
( $itemnumber, undef, 3 );
588 is
( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
589 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
591 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
592 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
593 $cache->clear_from_cache("default_value_for_mod_marc-");
594 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
596 subtest
'_koha_notify_reserve() tests' => sub {
600 my $wants_hold_and_email = {
606 letter_code
=> 'HOLD'
609 my $mp = Test
::MockModule
->new( 'C4::Members::Messaging' );
611 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
613 $dbh->do('DELETE FROM letter');
615 my $email_hold_notice = $builder->build({
618 message_transport_type
=> 'email',
621 module
=> 'reserves',
626 my $sms_hold_notice = $builder->build({
629 message_transport_type
=> 'sms',
632 module
=> 'reserves',
637 my $hold_borrower = $builder->build({
638 source
=> 'Borrower',
640 smsalertnumber
=>'5555555555',
643 })->{borrowernumber
};
645 C4
::Reserves
::AddReserve
(
646 $item->homebranch, $hold_borrower,
647 $item->biblionumber );
649 ModReserveAffect
($item->itemnumber, $hold_borrower, 0);
650 my $sms_message_address = $schema->resultset('MessageQueue')->search({
651 letter_code
=> 'HOLD',
652 message_transport_type
=> 'sms',
653 borrowernumber
=> $hold_borrower,
654 })->next()->to_address();
655 is
($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
657 my $email_message_address = $schema->resultset('MessageQueue')->search({
658 letter_code
=> 'HOLD',
659 message_transport_type
=> 'email',
660 borrowernumber
=> $hold_borrower,
661 })->next()->to_address();
662 is
($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
666 subtest
'ReservesNeedReturns' => sub {
669 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
670 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
671 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value
=> { rentalcharge
=> 0 } } );
673 biblionumber
=> $biblioitem->biblionumber,
674 biblioitemnumber
=> $biblioitem->biblioitemnumber,
675 homebranch
=> $library->branchcode,
676 holdingbranch
=> $library->branchcode,
677 itype
=> $itemtype->itemtype,
679 my $item = $builder->build_object( { class => 'Koha::Items', value
=> $item_info } );
680 my $patron = $builder->build_object(
682 class => 'Koha::Patrons',
683 value
=> { branchcode
=> $library->branchcode, }
688 my ( $hold_id, $hold );
690 t
::lib
::Mocks
::mock_preference
('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
691 $hold_id = C4
::Reserves
::AddReserve
(
692 $library->branchcode, $patron->borrowernumber,
693 $item->biblionumber, '',
696 "title for fee", $item->itemnumber,
698 $hold = Koha
::Holds
->find($hold_id);
699 is
( $hold->priority, 0, 'If ReservesNeedReturns is 0, priority must have been set to 0' );
700 is
( $hold->found, 'W', 'If ReservesNeedReturns is 0, found must have been set waiting' );
702 $hold->delete; # cleanup
704 t
::lib
::Mocks
::mock_preference
('ReservesNeedReturns', 1); # '0' means "Don't automatically mark a hold as found and waiting"
705 $hold_id = C4
::Reserves
::AddReserve
(
706 $library->branchcode, $patron->borrowernumber,
707 $item->biblionumber, '',
710 "title for fee", $item->itemnumber,
712 $hold = Koha
::Holds
->find($hold_id);
713 is
( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
714 is
( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
717 subtest
'ChargeReserveFee tests' => sub {
721 my $library = $builder->build_object({ class => 'Koha::Libraries' });
722 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
725 my $title = 'A title';
727 my $context = Test
::MockModule
->new('C4::Context');
728 $context->mock( userenv
=> { branch
=> $library->id } );
730 my $line = C4
::Reserves
::ChargeReserveFee
( $patron->id, $fee, $title );
732 is
( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
733 ok
( $line->is_debit, 'Generates a debit line' );
734 is
( $line->accounttype, 'Res' , 'generates Res accounttype');
735 is
( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
736 is
( $line->amount, $fee , 'amount set correctly');
737 is
( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
738 is
( $line->description, "$title" , 'description is title of reserved item');
739 is
( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
742 subtest
'reserves.item_level_hold' => sub {
745 my $item = $builder->build_sample_item;
746 my $patron = $builder->build_object(
748 class => 'Koha::Patrons',
749 value
=> { branchcode
=> $item->homebranch }
753 subtest
'item level hold' => sub {
756 AddReserve
( $item->homebranch, $patron->borrowernumber,
757 $item->biblionumber, undef, 1, undef, undef, '', '',
760 my $hold = Koha
::Holds
->find($reserve_id);
761 is
( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' );
764 ModReserveAffect
( $item->itemnumber, $patron->borrowernumber, 1 );
766 # Revert the waiting status
767 C4
::Reserves
::RevertWaitingStatus
(
768 { itemnumber
=> $item->itemnumber } );
770 $hold = Koha
::Holds
->find($reserve_id);
772 is
( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' );
774 $hold->delete; # cleanup
777 subtest
'biblio level hold' => sub {
779 my $reserve_id = AddReserve
( $item->homebranch, $patron->borrowernumber,
780 $item->biblionumber, undef, 1 );
782 my $hold = Koha
::Holds
->find($reserve_id);
783 is
( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' );
786 ModReserveAffect
( $item->itemnumber, $patron->borrowernumber, 1 );
788 $hold = Koha
::Holds
->find($reserve_id);
789 is
( $hold->itemnumber, $item->itemnumber, 'Itemnumber should be set on hold confirmation' );
791 # Revert the waiting status
792 C4
::Reserves
::RevertWaitingStatus
( { itemnumber
=> $item->itemnumber } );
794 $hold = Koha
::Holds
->find($reserve_id);
795 is
( $hold->itemnumber, undef, 'Itemnumber should be removed when the waiting status is revert' );
802 sub count_hold_print_messages
{
803 my $message_count = $dbh->selectall_arrayref(q{
806 WHERE letter_code = 'HOLD'
807 AND message_transport_type = 'print'
809 return $message_count->[0]->[0];
812 # we reached the finish
813 $schema->storage->txn_rollback();