Bug 9809: Update unit tests
[srvgit] / 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 => 65;
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 $bibitems       = '';
104 my $priority       = '1';
105 my $resdate        = undef;
106 my $expdate        = undef;
107 my $notes          = '';
108 my $checkitem      = undef;
109 my $found          = undef;
110
111 my @branches = GetBranchesLoop();
112 my $branch = $branches[0][0]{value};
113
114 AddReserve($branch,    $borrowernumber, $biblionumber,
115         $bibitems,  $priority, $resdate, $expdate, $notes,
116         $title,      $checkitem, $found);
117
118 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
119
120 is($status, "Reserved", "CheckReserves Test 1");
121
122 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
123
124 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
125 is($status, "Reserved", "CheckReserves Test 2");
126
127 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
128 is($status, "Reserved", "CheckReserves Test 3");
129
130 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
131 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
132 ok(
133     'ItemHomeLib' eq GetReservesControlBranch(
134         { homebranch => 'ItemHomeLib' },
135         { branchcode => 'PatronHomeLib' }
136     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
137 );
138 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
139 ok(
140     'PatronHomeLib' eq GetReservesControlBranch(
141         { homebranch => 'ItemHomeLib' },
142         { branchcode => 'PatronHomeLib' }
143     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
144 );
145 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
146
147 ###
148 ### Regression test for bug 10272
149 ###
150 my %requesters = ();
151 $requesters{'CPL'} = AddMember(
152     branchcode   => 'CPL',
153     categorycode => 'PT',
154     surname      => 'borrower from CPL',
155 );
156 $requesters{'FPL'} = AddMember(
157     branchcode   => 'FPL',
158     categorycode => 'PT',
159     surname      => 'borrower from FPL',
160 );
161 $requesters{'RPL'} = AddMember(
162     branchcode   => 'RPL',
163     categorycode => 'PT',
164     surname      => 'borrower from RPL',
165 );
166
167 # Configure rules so that CPL allows only CPL patrons
168 # to request its items, while FPL will allow its items
169 # to fill holds from anywhere.
170
171 $dbh->do('DELETE FROM issuingrules');
172 $dbh->do('DELETE FROM branch_item_rules');
173 $dbh->do('DELETE FROM branch_borrower_circ_rules');
174 $dbh->do('DELETE FROM default_borrower_circ_rules');
175 $dbh->do('DELETE FROM default_branch_item_rules');
176 $dbh->do('DELETE FROM default_branch_circ_rules');
177 $dbh->do('DELETE FROM default_circ_rules');
178 $dbh->do(
179     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
180       VALUES (?, ?, ?, ?)},
181     {},
182     '*', '*', '*', 25
183 );
184
185 # CPL allows only its own patrons to request its items
186 $dbh->do(
187     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
188       VALUES (?, ?, ?, ?)},
189     {},
190     'CPL', 10, 1, 'homebranch',
191 );
192
193 # ... while FPL allows anybody to request its items
194 $dbh->do(
195     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
196       VALUES (?, ?, ?, ?)},
197     {},
198     'FPL', 10, 2, 'homebranch',
199 );
200
201 # helper biblio for the bug 10272 regression test
202 my $bib2 = MARC::Record->new();
203 $bib2->append_fields(
204     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
205     MARC::Field->new('245', ' ', ' ', a => $title),
206 );
207
208 # create one item belonging to FPL and one belonging to CPL
209 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
210 my ($itemnum_cpl, $itemnum_fpl);
211 (undef, undef, $itemnum_cpl) = AddItem({
212         homebranch => 'CPL',
213         holdingbranch => 'CPL',
214         barcode => 'bug10272_CPL'
215     } , $bibnum2);
216 (undef, undef, $itemnum_fpl) = AddItem({
217         homebranch => 'FPL',
218         holdingbranch => 'FPL',
219         barcode => 'bug10272_FPL'
220     } , $bibnum2);
221
222 # Ensure that priorities are numbered correcly when a hold is moved to waiting
223 # (bug 11947)
224 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
225 AddReserve('RPL',  $requesters{'RPL'}, $bibnum2,
226            $bibitems,  1, $resdate, $expdate, $notes,
227            $title,      $checkitem, $found);
228 AddReserve('FPL',  $requesters{'FPL'}, $bibnum2,
229            $bibitems,  2, $resdate, $expdate, $notes,
230            $title,      $checkitem, $found);
231 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
232            $bibitems,  3, $resdate, $expdate, $notes,
233            $title,      $checkitem, $found);
234 ModReserveAffect($itemnum_cpl, $requesters{'RPL'}, 0);
235
236 # Now it should have different priorities.
237 my $title_reserves = GetReservesFromBiblionumber({biblionumber => $bibnum2});
238 # Sort by reserve number in case the database gives us oddly ordered results
239 my @reserves = sort { $a->{reserve_id} <=> $b->{reserve_id} } @$title_reserves;
240 is($reserves[0]{priority}, 0, 'Item is correctly waiting');
241 is($reserves[1]{priority}, 1, 'Item is correctly priority 1');
242 is($reserves[2]{priority}, 2, 'Item is correctly priority 2');
243
244 @reserves = Koha::Holds->search({ borrowernumber => $requesters{'RPL'} })->waiting();
245 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
246 is( $reserves[0]->borrowernumber(), $requesters{'RPL'}, 'GetWaiting got the reserve for the correct borrower' );
247
248
249 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
250 AddReserve('RPL',  $requesters{'RPL'}, $bibnum2,
251            $bibitems,  1, $resdate, $expdate, $notes,
252            $title,      $checkitem, $found);
253 AddReserve('FPL',  $requesters{'FPL'}, $bibnum2,
254            $bibitems,  2, $resdate, $expdate, $notes,
255            $title,      $checkitem, $found);
256 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
257            $bibitems,  3, $resdate, $expdate, $notes,
258            $title,      $checkitem, $found);
259
260 # Ensure that the item's home library controls hold policy lookup
261 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
262
263 my $messages;
264 # Return the CPL item at FPL.  The hold that should be triggered is
265 # the one placed by the CPL patron, as the other two patron's hold
266 # requests cannot be filled by that item per policy.
267 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
268 is( $messages->{ResFound}->{borrowernumber},
269     $requesters{'CPL'},
270     'restrictive library\'s items only fill requests by own patrons (bug 10272)');
271
272 # Return the FPL item at FPL.  The hold that should be triggered is
273 # the one placed by the RPL patron, as that patron is first in line
274 # and RPL imposes no restrictions on whose holds its items can fill.
275 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
276 is( $messages->{ResFound}->{borrowernumber},
277     $requesters{'RPL'},
278     'for generous library, its items fill first hold request in line (bug 10272)');
279
280 my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber});
281 isa_ok($reserves, 'ARRAY');
282 is(scalar @$reserves, 1, "Only one reserves for this biblio");
283 my $reserve_id = $reserves->[0]->{reserve_id};
284
285 $reserve = GetReserve($reserve_id);
286 isa_ok($reserve, 'HASH', "GetReserve return");
287 is($reserve->{biblionumber}, $biblionumber);
288
289 $reserve = CancelReserve({reserve_id => $reserve_id});
290 isa_ok($reserve, 'HASH', "CancelReserve return");
291 is($reserve->{biblionumber}, $biblionumber);
292
293 $reserve = GetReserve($reserve_id);
294 is($reserve, undef, "GetReserve returns undef after deletion");
295
296 $reserve = CancelReserve({reserve_id => $reserve_id});
297 is($reserve, undef, "CancelReserve return undef if reserve does not exist");
298
299
300 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
301 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
302 # Test 9761a: Add a reserve without date, CheckReserve should return it
303 $resdate= undef; #defaults to today in AddReserve
304 $expdate= undef; #no expdate
305 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
306 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
307            $bibitems,  1, $resdate, $expdate, $notes,
308            $title,      $checkitem, $found);
309 ($status)=CheckReserves($itemnumber,undef,undef);
310 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
311 ($status)=CheckReserves($itemnumber,undef,7);
312 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
313
314 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
315 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
316 C4::Context->set_preference('AllowHoldDateInFuture', 1);
317 $resdate= dt_from_string();
318 $resdate->add_duration(DateTime::Duration->new(days => 4));
319 $resdate=output_pref($resdate);
320 $expdate= undef; #no expdate
321 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
322            $bibitems,  1, $resdate, $expdate, $notes,
323            $title,      $checkitem, $found);
324 ($status)=CheckReserves($itemnumber,undef,undef);
325 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
326
327 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
328 ($status)=CheckReserves($itemnumber,undef,3);
329 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
330 ($status)=CheckReserves($itemnumber,undef,4);
331 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
332
333 # Test 9761d: Check ResFound message of AddReturn for future hold
334 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
335 # In this test we do not need an issued item; it is just a 'checkin'
336 C4::Context->set_preference('ConfirmFutureHolds', 0);
337 (my $doreturn, $messages)= AddReturn('97531','CPL');
338 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
339 C4::Context->set_preference('ConfirmFutureHolds', 3);
340 ($doreturn, $messages)= AddReturn('97531','CPL');
341 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
342 C4::Context->set_preference('ConfirmFutureHolds', 7);
343 ($doreturn, $messages)= AddReturn('97531','CPL');
344 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
345
346 # End of tests for bug 9761 (ConfirmFutureHolds)
347
348 # test marking a hold as captured
349 my $hold_notice_count = count_hold_print_messages();
350 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
351 my $new_count = count_hold_print_messages();
352 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
353
354 # test that duplicate notices aren't generated
355 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
356 $new_count = count_hold_print_messages();
357 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
358
359 # avoiding the not_same_branch error
360 t::lib::Mocks::mock_preference('IndependentBranches', 0);
361 is(
362     DelItemCheck($dbh, $bibnum, $itemnumber),
363     'book_reserved',
364     'item that is captured to fill a hold cannot be deleted',
365 );
366
367 my $letter = ReserveSlip('CPL', $requesters{'CPL'}, $bibnum);
368 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
369
370 # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait?
371 # 9788a: GetReservesFromItemnumber does not return future next available hold
372 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
373 C4::Context->set_preference('ConfirmFutureHolds', 2);
374 C4::Context->set_preference('AllowHoldDateInFuture', 1);
375 $resdate= dt_from_string();
376 $resdate->add_duration(DateTime::Duration->new(days => 2));
377 $resdate=output_pref($resdate);
378 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
379            $bibitems,  1, $resdate, $expdate, $notes,
380            $title,      $checkitem, $found);
381 my @results= GetReservesFromItemnumber($itemnumber);
382 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future next available hold');
383 # 9788b: GetReservesFromItemnumber does not return future item level hold
384 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
385 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
386            $bibitems,  1, $resdate, $expdate, $notes,
387            $title,      $itemnumber, $found); #item level hold
388 @results= GetReservesFromItemnumber($itemnumber);
389 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future item level hold');
390 # 9788c: GetReservesFromItemnumber returns future wait (confirmed future hold)
391 ModReserveAffect( $itemnumber,  $requesters{'CPL'} , 0); #confirm hold
392 @results= GetReservesFromItemnumber($itemnumber);
393 is(defined $results[3]?1:0, 1, 'GetReservesFromItemnumber returns a future wait (confirmed future hold)');
394 # End of tests for bug 9788
395
396 # Tests for CalculatePriority (bug 8918)
397 my $p = C4::Reserves::CalculatePriority($bibnum2);
398 is($p, 4, 'CalculatePriority should now return priority 4');
399 $resdate=undef;
400 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
401            $bibitems,  $p, $resdate, $expdate, $notes,
402            $title,      $checkitem, $found);
403 $p = C4::Reserves::CalculatePriority($bibnum2);
404 is($p, 5, 'CalculatePriority should now return priority 5');
405 #some tests on bibnum
406 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
407 $p = C4::Reserves::CalculatePriority($bibnum);
408 is($p, 1, 'CalculatePriority should now return priority 1');
409 #add a new reserve and confirm it to waiting
410 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
411            $bibitems,  $p, $resdate, $expdate, $notes,
412            $title,      $itemnumber, $found);
413 $p = C4::Reserves::CalculatePriority($bibnum);
414 is($p, 2, 'CalculatePriority should now return priority 2');
415 ModReserveAffect( $itemnumber,  $requesters{'CPL'} , 0);
416 $p = C4::Reserves::CalculatePriority($bibnum);
417 is($p, 1, 'CalculatePriority should now return priority 1');
418 #add another biblio hold, no resdate
419 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
420            $bibitems,  $p, $resdate, $expdate, $notes,
421            $title,      $checkitem, $found);
422 $p = C4::Reserves::CalculatePriority($bibnum);
423 is($p, 2, 'CalculatePriority should now return priority 2');
424 #add another future hold
425 C4::Context->set_preference('AllowHoldDateInFuture', 1);
426 $resdate= dt_from_string();
427 $resdate->add_duration(DateTime::Duration->new(days => 1));
428 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
429            $bibitems,  $p, output_pref($resdate), $expdate, $notes,
430            $title,      $checkitem, $found);
431 $p = C4::Reserves::CalculatePriority($bibnum);
432 is($p, 2, 'CalculatePriority should now still return priority 2');
433 #calc priority with future resdate
434 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
435 is($p, 3, 'CalculatePriority should now return priority 3');
436 # End of tests for bug 8918
437
438 # Tests for cancel reserves by users from OPAC.
439 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
440 AddReserve('CPL',  $requesters{'CPL'}, $item_bibnum,
441            $bibitems,  1, undef, $expdate, $notes,
442            $title,      $checkitem, '');
443 my (undef, $canres, undef) = CheckReserves($itemnumber);
444
445 is( CanReserveBeCanceledFromOpac(), undef,
446     'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
447 );
448 is(
449     CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
450     undef,
451     'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
452 );
453 is(
454     CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
455     undef,
456     'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
457 );
458
459 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
460 is($cancancel, 1, 'Can user cancel its own reserve');
461
462 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'FPL'});
463 is($cancancel, 0, 'Other user cant cancel reserve');
464
465 ModReserveAffect($itemnumber, $requesters{'CPL'}, 1);
466 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
467 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
468
469 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
470 AddReserve('CPL',  $requesters{'CPL'}, $item_bibnum,
471            $bibitems,  1, undef, $expdate, $notes,
472            $title,      $checkitem, '');
473 (undef, $canres, undef) = CheckReserves($itemnumber);
474
475 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
476 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
477 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
478
479 # End of tests for bug 12876
480
481        ####
482 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
483        ####
484
485 C4::Context->set_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
486
487 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
488
489 #Set the ageRestriction for the Biblio
490 my $record = GetMarcBiblio( $bibnum );
491 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
492 $record->append_fields(  MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16')  );
493 C4::Biblio::ModBiblio( $record, $bibnum, '' );
494
495 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
496
497 #Set the dateofbirth for the Borrower making him "too young".
498 my $now = DateTime->now();
499 C4::Members::SetAge( $borrower, '0015-00-00' );
500 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
501
502 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
503
504 #Set the dateofbirth for the Borrower making him "too old".
505 C4::Members::SetAge( $borrower, '0030-00-00' );
506 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
507
508 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
509        ####
510 ####### EO Bug 13113 <<<
511        ####
512
513 my $item = GetItem($itemnumber);
514
515 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $borrower), "Reserving a book on item level" );
516
517 my $itype = C4::Reserves::_get_itype($item);
518 my $categorycode = $borrower->{categorycode};
519 my $holdingbranch = $item->{holdingbranch};
520 my $rule = C4::Circulation::GetIssuingRule($categorycode, $itype, $holdingbranch);
521
522 $dbh->do(
523     "UPDATE issuingrules SET onshelfholds = 1 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
524     undef,
525     $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
526 );
527 ok( C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() allowed" );
528 $dbh->do(
529     "UPDATE issuingrules SET onshelfholds = 0 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
530     undef,
531     $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
532 );
533 ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
534
535 # Tests for bug 14464
536
537 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
538 my ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
539 ok( !$bz14464_fines, 'Bug 14464 - No fines at beginning' );
540
541 # First, test cancelling a reserve when there's no charge configured.
542 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
543
544 my $bz14464_reserve = AddReserve(
545     'CPL',
546     $borrowernumber,
547     $bibnum,
548     'a',
549     undef,
550     '1',
551     undef,
552     undef,
553     '',
554     $title,
555     $itemnumber,
556     'W'
557 );
558
559 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
560
561 CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
562
563 ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
564 ok( !$bz14464_fines, 'Bug 14464 - No fines after cancelling reserve with no charge configured' );
565
566 # Then, test cancelling a reserve when there's no charge desired.
567 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
568
569 $bz14464_reserve = AddReserve(
570     'CPL',
571     $borrowernumber,
572     $bibnum,
573     'a',
574     undef,
575     '1',
576     undef,
577     undef,
578     '',
579     $title,
580     $itemnumber,
581     'W'
582 );
583
584 ok( $bz14464_reserve, 'Bug 14464 - 2nd reserve correctly created' );
585
586 CancelReserve({ reserve_id => $bz14464_reserve });
587
588 ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
589 ok( !$bz14464_fines, 'Bug 14464 - No fines after cancelling reserve with no charge desired' );
590
591 # Finally, test cancelling a reserve when there's a charge desired and configured.
592 $bz14464_reserve = AddReserve(
593     'CPL',
594     $borrowernumber,
595     $bibnum,
596     'a',
597     undef,
598     '1',
599     undef,
600     undef,
601     '',
602     $title,
603     $itemnumber,
604     'W'
605 );
606
607 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
608
609 CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
610
611 ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
612 is( int( $bz14464_fines ), 42, 'Bug 14464 - Fine applied after cancelling reserve with charge desired and configured' );
613
614 $dbh->rollback;
615
616 sub count_hold_print_messages {
617     my $message_count = $dbh->selectall_arrayref(q{
618         SELECT COUNT(*)
619         FROM message_queue
620         WHERE letter_code = 'HOLD' 
621         AND   message_transport_type = 'print'
622     });
623     return $message_count->[0]->[0];
624 }