Bug 13851: Replace waiting holds logic in circulation.pl with Koha Objects
[koha.git] / t / db_dependent / Reserves.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 58;
21
22 use MARC::Record;
23 use DateTime::Duration;
24
25 use C4::Branch;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Members;
29 use C4::Circulation;
30 use Koha::Holds;
31 use t::lib::Mocks;
32
33 use Koha::DateUtils;
34
35 use Data::Dumper;
36 BEGIN {
37     use_ok('C4::Reserves');
38 }
39
40 # a very minimal mack of userenv for use by the test of DelItemCheck
41 *C4::Context::userenv = sub {
42     return {};
43 };
44
45 my $dbh = C4::Context->dbh;
46
47 # Start transaction
48 $dbh->{AutoCommit} = 0;
49 $dbh->{RaiseError} = 1;
50
51 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
52 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a'");
53
54 # Setup Test------------------------
55
56 # Add branches if not existing
57 foreach my $addbra ('CPL', 'FPL', 'RPL') {
58     $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES (?,?)", undef, ($addbra,"$addbra branch")) unless GetBranchName($addbra);
59 }
60
61 # Add categories if not existing
62 foreach my $addcat ('S', 'PT') {
63     $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
64 }
65
66 # Create a helper biblio
67 my $bib = MARC::Record->new();
68 my $title = 'Silence in the library';
69 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
70     $bib->append_fields(
71         MARC::Field->new('600', '', '1', a => 'Moffat, Steven'),
72         MARC::Field->new('200', '', '', a => $title),
73     );
74 }
75 else {
76     $bib->append_fields(
77         MARC::Field->new('100', '', '', a => 'Moffat, Steven'),
78         MARC::Field->new('245', '', '', a => $title),
79     );
80 }
81 my ($bibnum, $bibitemnum);
82 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
83
84 # Create a helper item instance for testing
85 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
86
87 # Modify item; setting barcode.
88 my $testbarcode = '97531';
89 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
90
91 # Create a borrower
92 my %data = (
93     firstname =>  'my firstname',
94     surname => 'my surname',
95     categorycode => 'S',
96     branchcode => 'CPL',
97 );
98 my $borrowernumber = AddMember(%data);
99 my $borrower = GetMember( borrowernumber => $borrowernumber );
100 my $biblionumber   = $bibnum;
101 my $barcode        = $testbarcode;
102
103 my $constraint     = 'a';
104 my $bibitems       = '';
105 my $priority       = '1';
106 my $resdate        = undef;
107 my $expdate        = undef;
108 my $notes          = '';
109 my $checkitem      = undef;
110 my $found          = undef;
111
112 my @branches = GetBranchesLoop();
113 my $branch = $branches[0][0]{value};
114
115 AddReserve($branch,    $borrowernumber, $biblionumber,
116         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
117         $title,      $checkitem, $found);
118
119 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
120
121 is($status, "Reserved", "CheckReserves Test 1");
122
123 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
124
125 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
126 is($status, "Reserved", "CheckReserves Test 2");
127
128 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
129 is($status, "Reserved", "CheckReserves Test 3");
130
131 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
132 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
133 ok(
134     'ItemHomeLib' eq GetReservesControlBranch(
135         { homebranch => 'ItemHomeLib' },
136         { branchcode => 'PatronHomeLib' }
137     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
138 );
139 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
140 ok(
141     'PatronHomeLib' eq GetReservesControlBranch(
142         { homebranch => 'ItemHomeLib' },
143         { branchcode => 'PatronHomeLib' }
144     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
145 );
146 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
147
148 ###
149 ### Regression test for bug 10272
150 ###
151 my %requesters = ();
152 $requesters{'CPL'} = AddMember(
153     branchcode   => 'CPL',
154     categorycode => 'PT',
155     surname      => 'borrower from CPL',
156 );
157 $requesters{'FPL'} = AddMember(
158     branchcode   => 'FPL',
159     categorycode => 'PT',
160     surname      => 'borrower from FPL',
161 );
162 $requesters{'RPL'} = AddMember(
163     branchcode   => 'RPL',
164     categorycode => 'PT',
165     surname      => 'borrower from RPL',
166 );
167
168 # Configure rules so that CPL allows only CPL patrons
169 # to request its items, while FPL will allow its items
170 # to fill holds from anywhere.
171
172 $dbh->do('DELETE FROM issuingrules');
173 $dbh->do('DELETE FROM branch_item_rules');
174 $dbh->do('DELETE FROM branch_borrower_circ_rules');
175 $dbh->do('DELETE FROM default_borrower_circ_rules');
176 $dbh->do('DELETE FROM default_branch_item_rules');
177 $dbh->do('DELETE FROM default_branch_circ_rules');
178 $dbh->do('DELETE FROM default_circ_rules');
179 $dbh->do(
180     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
181       VALUES (?, ?, ?, ?)},
182     {},
183     '*', '*', '*', 25
184 );
185
186 # CPL allows only its own patrons to request its items
187 $dbh->do(
188     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
189       VALUES (?, ?, ?, ?)},
190     {},
191     'CPL', 10, 1, 'homebranch',
192 );
193
194 # ... while FPL allows anybody to request its items
195 $dbh->do(
196     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
197       VALUES (?, ?, ?, ?)},
198     {},
199     'FPL', 10, 2, 'homebranch',
200 );
201
202 # helper biblio for the bug 10272 regression test
203 my $bib2 = MARC::Record->new();
204 $bib2->append_fields(
205     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
206     MARC::Field->new('245', ' ', ' ', a => $title),
207 );
208
209 # create one item belonging to FPL and one belonging to CPL
210 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
211 my ($itemnum_cpl, $itemnum_fpl);
212 (undef, undef, $itemnum_cpl) = AddItem({
213         homebranch => 'CPL',
214         holdingbranch => 'CPL',
215         barcode => 'bug10272_CPL'
216     } , $bibnum2);
217 (undef, undef, $itemnum_fpl) = AddItem({
218         homebranch => 'FPL',
219         holdingbranch => 'FPL',
220         barcode => 'bug10272_FPL'
221     } , $bibnum2);
222
223 # Ensure that priorities are numbered correcly when a hold is moved to waiting
224 # (bug 11947)
225 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
226 AddReserve('RPL',  $requesters{'RPL'}, $bibnum2,
227            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
228            $title,      $checkitem, $found);
229 AddReserve('FPL',  $requesters{'FPL'}, $bibnum2,
230            $constraint, $bibitems,  2, $resdate, $expdate, $notes,
231            $title,      $checkitem, $found);
232 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
233            $constraint, $bibitems,  3, $resdate, $expdate, $notes,
234            $title,      $checkitem, $found);
235 ModReserveAffect($itemnum_cpl, $requesters{'RPL'}, 0);
236
237 # Now it should have different priorities.
238 my $title_reserves = GetReservesFromBiblionumber({biblionumber => $bibnum2});
239 # Sort by reserve number in case the database gives us oddly ordered results
240 my @reserves = sort { $a->{reserve_id} <=> $b->{reserve_id} } @$title_reserves;
241 is($reserves[0]{priority}, 0, 'Item is correctly waiting');
242 is($reserves[1]{priority}, 1, 'Item is correctly priority 1');
243 is($reserves[2]{priority}, 2, 'Item is correctly priority 2');
244
245 @reserves = Koha::Holds->search({ borrowernumber => $requesters{'RPL'} })->waiting();
246 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
247 is( $reserves[0]->borrowernumber(), $requesters{'RPL'}, 'GetWaiting got the reserve for the correct borrower' );
248
249
250 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
251 AddReserve('RPL',  $requesters{'RPL'}, $bibnum2,
252            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
253            $title,      $checkitem, $found);
254 AddReserve('FPL',  $requesters{'FPL'}, $bibnum2,
255            $constraint, $bibitems,  2, $resdate, $expdate, $notes,
256            $title,      $checkitem, $found);
257 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
258            $constraint, $bibitems,  3, $resdate, $expdate, $notes,
259            $title,      $checkitem, $found);
260
261 # Ensure that the item's home library controls hold policy lookup
262 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
263
264 my $messages;
265 # Return the CPL item at FPL.  The hold that should be triggered is
266 # the one placed by the CPL patron, as the other two patron's hold
267 # requests cannot be filled by that item per policy.
268 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
269 is( $messages->{ResFound}->{borrowernumber},
270     $requesters{'CPL'},
271     'restrictive library\'s items only fill requests by own patrons (bug 10272)');
272
273 # Return the FPL item at FPL.  The hold that should be triggered is
274 # the one placed by the RPL patron, as that patron is first in line
275 # and RPL imposes no restrictions on whose holds its items can fill.
276 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
277 is( $messages->{ResFound}->{borrowernumber},
278     $requesters{'RPL'},
279     'for generous library, its items fill first hold request in line (bug 10272)');
280
281 my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber});
282 isa_ok($reserves, 'ARRAY');
283 is(scalar @$reserves, 1, "Only one reserves for this biblio");
284 my $reserve_id = $reserves->[0]->{reserve_id};
285
286 $reserve = GetReserve($reserve_id);
287 isa_ok($reserve, 'HASH', "GetReserve return");
288 is($reserve->{biblionumber}, $biblionumber);
289
290 $reserve = CancelReserve({reserve_id => $reserve_id});
291 isa_ok($reserve, 'HASH', "CancelReserve return");
292 is($reserve->{biblionumber}, $biblionumber);
293
294 $reserve = GetReserve($reserve_id);
295 is($reserve, undef, "GetReserve returns undef after deletion");
296
297 $reserve = CancelReserve({reserve_id => $reserve_id});
298 is($reserve, undef, "CancelReserve return undef if reserve does not exist");
299
300
301 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
302 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
303 # Test 9761a: Add a reserve without date, CheckReserve should return it
304 $resdate= undef; #defaults to today in AddReserve
305 $expdate= undef; #no expdate
306 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
307 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
308            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
309            $title,      $checkitem, $found);
310 ($status)=CheckReserves($itemnumber,undef,undef);
311 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
312 ($status)=CheckReserves($itemnumber,undef,7);
313 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
314
315 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
316 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
317 C4::Context->set_preference('AllowHoldDateInFuture', 1);
318 $resdate= dt_from_string();
319 $resdate->add_duration(DateTime::Duration->new(days => 4));
320 $resdate=output_pref($resdate);
321 $expdate= undef; #no expdate
322 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
323            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
324            $title,      $checkitem, $found);
325 ($status)=CheckReserves($itemnumber,undef,undef);
326 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
327
328 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
329 ($status)=CheckReserves($itemnumber,undef,3);
330 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
331 ($status)=CheckReserves($itemnumber,undef,4);
332 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
333
334 # Test 9761d: Check ResFound message of AddReturn for future hold
335 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
336 # In this test we do not need an issued item; it is just a 'checkin'
337 C4::Context->set_preference('ConfirmFutureHolds', 0);
338 (my $doreturn, $messages)= AddReturn('97531','CPL');
339 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
340 C4::Context->set_preference('ConfirmFutureHolds', 3);
341 ($doreturn, $messages)= AddReturn('97531','CPL');
342 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
343 C4::Context->set_preference('ConfirmFutureHolds', 7);
344 ($doreturn, $messages)= AddReturn('97531','CPL');
345 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
346
347 # End of tests for bug 9761 (ConfirmFutureHolds)
348
349 # test marking a hold as captured
350 my $hold_notice_count = count_hold_print_messages();
351 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
352 my $new_count = count_hold_print_messages();
353 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
354
355 # test that duplicate notices aren't generated
356 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
357 $new_count = count_hold_print_messages();
358 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
359
360 # avoiding the not_same_branch error
361 t::lib::Mocks::mock_preference('IndependentBranches', 0);
362 is(
363     DelItemCheck($dbh, $bibnum, $itemnumber),
364     'book_reserved',
365     'item that is captured to fill a hold cannot be deleted',
366 );
367
368 my $letter = ReserveSlip('CPL', $requesters{'CPL'}, $bibnum);
369 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
370
371 # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait?
372 # 9788a: GetReservesFromItemnumber does not return future next available hold
373 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
374 C4::Context->set_preference('ConfirmFutureHolds', 2);
375 C4::Context->set_preference('AllowHoldDateInFuture', 1);
376 $resdate= dt_from_string();
377 $resdate->add_duration(DateTime::Duration->new(days => 2));
378 $resdate=output_pref($resdate);
379 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
380            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
381            $title,      $checkitem, $found);
382 my @results= GetReservesFromItemnumber($itemnumber);
383 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future next available hold');
384 # 9788b: GetReservesFromItemnumber does not return future item level hold
385 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
386 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
387            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
388            $title,      $itemnumber, $found); #item level hold
389 @results= GetReservesFromItemnumber($itemnumber);
390 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future item level hold');
391 # 9788c: GetReservesFromItemnumber returns future wait (confirmed future hold)
392 ModReserveAffect( $itemnumber,  $requesters{'CPL'} , 0); #confirm hold
393 @results= GetReservesFromItemnumber($itemnumber);
394 is(defined $results[3]?1:0, 1, 'GetReservesFromItemnumber returns a future wait (confirmed future hold)');
395 # End of tests for bug 9788
396
397 # Tests for CalculatePriority (bug 8918)
398 my $p = C4::Reserves::CalculatePriority($bibnum2);
399 is($p, 4, 'CalculatePriority should now return priority 4');
400 $resdate=undef;
401 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
402            $constraint, $bibitems,  $p, $resdate, $expdate, $notes,
403            $title,      $checkitem, $found);
404 $p = C4::Reserves::CalculatePriority($bibnum2);
405 is($p, 5, 'CalculatePriority should now return priority 5');
406 #some tests on bibnum
407 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
408 $p = C4::Reserves::CalculatePriority($bibnum);
409 is($p, 1, 'CalculatePriority should now return priority 1');
410 #add a new reserve and confirm it to waiting
411 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
412            $constraint, $bibitems,  $p, $resdate, $expdate, $notes,
413            $title,      $itemnumber, $found);
414 $p = C4::Reserves::CalculatePriority($bibnum);
415 is($p, 2, 'CalculatePriority should now return priority 2');
416 ModReserveAffect( $itemnumber,  $requesters{'CPL'} , 0);
417 $p = C4::Reserves::CalculatePriority($bibnum);
418 is($p, 1, 'CalculatePriority should now return priority 1');
419 #add another biblio hold, no resdate
420 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
421            $constraint, $bibitems,  $p, $resdate, $expdate, $notes,
422            $title,      $checkitem, $found);
423 $p = C4::Reserves::CalculatePriority($bibnum);
424 is($p, 2, 'CalculatePriority should now return priority 2');
425 #add another future hold
426 C4::Context->set_preference('AllowHoldDateInFuture', 1);
427 $resdate= dt_from_string();
428 $resdate->add_duration(DateTime::Duration->new(days => 1));
429 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
430            $constraint, $bibitems,  $p, output_pref($resdate), $expdate, $notes,
431            $title,      $checkitem, $found);
432 $p = C4::Reserves::CalculatePriority($bibnum);
433 is($p, 2, 'CalculatePriority should now still return priority 2');
434 #calc priority with future resdate
435 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
436 is($p, 3, 'CalculatePriority should now return priority 3');
437 # End of tests for bug 8918
438
439 # Tests for cancel reserves by users from OPAC.
440 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
441 AddReserve('CPL',  $requesters{'CPL'}, $item_bibnum,
442            $constraint, $bibitems,  1, undef, $expdate, $notes,
443            $title,      $checkitem, '');
444 my (undef, $canres, undef) = CheckReserves($itemnumber);
445
446 is( CanReserveBeCanceledFromOpac(), undef,
447     'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
448 );
449 is(
450     CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
451     undef,
452     'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
453 );
454 is(
455     CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
456     undef,
457     'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
458 );
459
460 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
461 is($cancancel, 1, 'Can user cancel its own reserve');
462
463 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'FPL'});
464 is($cancancel, 0, 'Other user cant cancel reserve');
465
466 ModReserveAffect($itemnumber, $requesters{'CPL'}, 1);
467 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
468 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
469
470 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
471 AddReserve('CPL',  $requesters{'CPL'}, $item_bibnum,
472            $constraint, $bibitems,  1, undef, $expdate, $notes,
473            $title,      $checkitem, '');
474 (undef, $canres, undef) = CheckReserves($itemnumber);
475
476 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
477 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
478 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
479
480 # End of tests for bug 12876
481
482        ####
483 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
484        ####
485
486 C4::Context->set_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
487
488 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
489
490 #Set the ageRestriction for the Biblio
491 my $record = GetMarcBiblio( $bibnum );
492 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
493 $record->append_fields(  MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16')  );
494 C4::Biblio::ModBiblio( $record, $bibnum, '' );
495
496 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
497
498 #Set the dateofbirth for the Borrower making him "too young".
499 my $now = DateTime->now();
500 C4::Members::SetAge( $borrower, '0015-00-00' );
501 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
502
503 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
504
505 #Set the dateofbirth for the Borrower making him "too old".
506 C4::Members::SetAge( $borrower, '0030-00-00' );
507 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
508
509 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
510        ####
511 ####### EO Bug 13113 <<<
512        ####
513
514 my $item = GetItem($itemnumber);
515
516 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $borrower), "Reserving a book on item level" );
517
518 my $itype = C4::Reserves::_get_itype($item);
519 my $categorycode = $borrower->{categorycode};
520 my $holdingbranch = $item->{holdingbranch};
521 my $rule = C4::Circulation::GetIssuingRule($categorycode, $itype, $holdingbranch);
522
523 $dbh->do(
524     "UPDATE issuingrules SET onshelfholds = 1 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
525     undef,
526     $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
527 );
528 ok( C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() allowed" );
529 $dbh->do(
530     "UPDATE issuingrules SET onshelfholds = 0 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
531     undef,
532     $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
533 );
534 ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
535
536 $dbh->rollback;
537
538 sub count_hold_print_messages {
539     my $message_count = $dbh->selectall_arrayref(q{
540         SELECT COUNT(*)
541         FROM message_queue
542         WHERE letter_code = 'HOLD' 
543         AND   message_transport_type = 'print'
544     });
545     return $message_count->[0]->[0];
546 }