Bug 17588: Koha::Patrons - Move GetMemberIssuesAndFines
[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 => 72;
21 use Test::MockModule;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use MARC::Record;
28 use DateTime::Duration;
29
30 use C4::Biblio;
31 use C4::Circulation;
32 use C4::Items;
33 use C4::Members;
34 use C4::Reserves;
35 use Koha::DateUtils;
36 use Koha::Holds;
37 use Koha::Libraries;
38 use Koha::Patron::Categories;
39
40 BEGIN {
41     require_ok('C4::Reserves');
42 }
43
44 # Start transaction
45 my $database = Koha::Database->new();
46 my $schema = $database->schema();
47 $schema->storage->txn_begin();
48 my $dbh = C4::Context->dbh;
49
50 my $builder = t::lib::TestBuilder->new;
51
52 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
53 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a'");
54
55 ## Setup Test
56 # Add branches
57 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
58 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
59 my $branch_3 = $builder->build({ source => 'Branch' })->{ branchcode };
60 # Add categories
61 my $category_1 = $builder->build({ source => 'Category' })->{ categorycode };
62 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode };
63 # Add an item type
64 my $itemtype = $builder->build(
65     { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
66
67 C4::Context->set_userenv(
68     undef, undef, undef, undef, undef, undef, $branch_1
69 );
70
71 # Create a helper biblio
72 my $bib = MARC::Record->new();
73 my $title = 'Silence in the library';
74 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
75     $bib->append_fields(
76         MARC::Field->new('600', '', '1', a => 'Moffat, Steven'),
77         MARC::Field->new('200', '', '', a => $title),
78     );
79 }
80 else {
81     $bib->append_fields(
82         MARC::Field->new('100', '', '', a => 'Moffat, Steven'),
83         MARC::Field->new('245', '', '', a => $title),
84     );
85 }
86 my ($bibnum, $bibitemnum);
87 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
88
89 # Create a helper item instance for testing
90 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
91     {   homebranch    => $branch_1,
92         holdingbranch => $branch_1,
93         itype         => $itemtype
94     },
95     $bibnum
96 );
97
98
99 # Modify item; setting barcode.
100 my $testbarcode = '97531';
101 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
102
103 # Create a borrower
104 my %data = (
105     firstname =>  'my firstname',
106     surname => 'my surname',
107     categorycode => $category_1,
108     branchcode => $branch_1,
109 );
110 Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
111 my $borrowernumber = AddMember(%data);
112 my $borrower = GetMember( borrowernumber => $borrowernumber );
113 my $biblionumber   = $bibnum;
114 my $barcode        = $testbarcode;
115
116 my $bibitems       = '';
117 my $priority       = '1';
118 my $resdate        = undef;
119 my $expdate        = undef;
120 my $notes          = '';
121 my $checkitem      = undef;
122 my $found          = undef;
123
124 my $branchcode = Koha::Libraries->search->next->branchcode;
125
126 AddReserve($branchcode,    $borrowernumber, $biblionumber,
127         $bibitems,  $priority, $resdate, $expdate, $notes,
128         $title,      $checkitem, $found);
129
130 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
131
132 is($status, "Reserved", "CheckReserves Test 1");
133
134 ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
135
136 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
137 is($status, "Reserved", "CheckReserves Test 2");
138
139 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
140 is($status, "Reserved", "CheckReserves Test 3");
141
142 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
143 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
144 ok(
145     'ItemHomeLib' eq GetReservesControlBranch(
146         { homebranch => 'ItemHomeLib' },
147         { branchcode => 'PatronHomeLib' }
148     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
149 );
150 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
151 ok(
152     'PatronHomeLib' eq GetReservesControlBranch(
153         { homebranch => 'ItemHomeLib' },
154         { branchcode => 'PatronHomeLib' }
155     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
156 );
157 t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
158
159 ###
160 ### Regression test for bug 10272
161 ###
162 my %requesters = ();
163 $requesters{$branch_1} = AddMember(
164     branchcode   => $branch_1,
165     categorycode => $category_2,
166     surname      => "borrower from $branch_1",
167 );
168 for my $i ( 2 .. 5 ) {
169     $requesters{"CPL$i"} = AddMember(
170         branchcode   => $branch_1,
171         categorycode => $category_2,
172         surname      => "borrower $i from $branch_1",
173     );
174 }
175 $requesters{$branch_2} = AddMember(
176     branchcode   => $branch_2,
177     categorycode => $category_2,
178     surname      => "borrower from $branch_2",
179 );
180 $requesters{$branch_3} = AddMember(
181     branchcode   => $branch_3,
182     categorycode => $category_2,
183     surname      => "borrower from $branch_3",
184 );
185
186 # Configure rules so that $branch_1 allows only $branch_1 patrons
187 # to request its items, while $branch_2 will allow its items
188 # to fill holds from anywhere.
189
190 $dbh->do('DELETE FROM issuingrules');
191 $dbh->do('DELETE FROM branch_item_rules');
192 $dbh->do('DELETE FROM branch_borrower_circ_rules');
193 $dbh->do('DELETE FROM default_borrower_circ_rules');
194 $dbh->do('DELETE FROM default_branch_item_rules');
195 $dbh->do('DELETE FROM default_branch_circ_rules');
196 $dbh->do('DELETE FROM default_circ_rules');
197 $dbh->do(
198     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
199       VALUES (?, ?, ?, ?)},
200     {},
201     '*', '*', '*', 25
202 );
203
204 # CPL allows only its own patrons to request its items
205 $dbh->do(
206     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
207       VALUES (?, ?, ?, ?)},
208     {},
209     $branch_1, 10, 1, 'homebranch',
210 );
211
212 # ... while FPL allows anybody to request its items
213 $dbh->do(
214     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
215       VALUES (?, ?, ?, ?)},
216     {},
217     $branch_2, 10, 2, 'homebranch',
218 );
219
220 # helper biblio for the bug 10272 regression test
221 my $bib2 = MARC::Record->new();
222 $bib2->append_fields(
223     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
224     MARC::Field->new('245', ' ', ' ', a => $title),
225 );
226
227 # create one item belonging to FPL and one belonging to CPL
228 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
229 my ($itemnum_cpl, $itemnum_fpl);
230 ( undef, undef, $itemnum_cpl ) = AddItem(
231     {   homebranch    => $branch_1,
232         holdingbranch => $branch_1,
233         barcode       => 'bug10272_CPL',
234         itype         => $itemtype
235     },
236     $bibnum2
237 );
238 ( undef, undef, $itemnum_fpl ) = AddItem(
239     {   homebranch    => $branch_2,
240         holdingbranch => $branch_2,
241         barcode       => 'bug10272_FPL',
242         itype         => $itemtype
243     },
244     $bibnum2
245 );
246
247
248 # Ensure that priorities are numbered correcly when a hold is moved to waiting
249 # (bug 11947)
250 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
251 AddReserve($branch_3,  $requesters{$branch_3}, $bibnum2,
252            $bibitems,  1, $resdate, $expdate, $notes,
253            $title,      $checkitem, $found);
254 AddReserve($branch_2,  $requesters{$branch_2}, $bibnum2,
255            $bibitems,  2, $resdate, $expdate, $notes,
256            $title,      $checkitem, $found);
257 AddReserve($branch_1,  $requesters{$branch_1}, $bibnum2,
258            $bibitems,  3, $resdate, $expdate, $notes,
259            $title,      $checkitem, $found);
260 ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0);
261
262 # Now it should have different priorities.
263 my $title_reserves = GetReservesFromBiblionumber({biblionumber => $bibnum2});
264 # Sort by reserve number in case the database gives us oddly ordered results
265 my @reserves = sort { $a->{reserve_id} <=> $b->{reserve_id} } @$title_reserves;
266 is($reserves[0]{priority}, 0, 'Item is correctly waiting');
267 is($reserves[1]{priority}, 1, 'Item is correctly priority 1');
268 is($reserves[2]{priority}, 2, 'Item is correctly priority 2');
269
270 @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting();
271 is( @reserves, 1, 'GetWaiting got only the waiting reserve' );
272 is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
273
274
275 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
276 AddReserve($branch_3,  $requesters{$branch_3}, $bibnum2,
277            $bibitems,  1, $resdate, $expdate, $notes,
278            $title,      $checkitem, $found);
279 AddReserve($branch_2,  $requesters{$branch_2}, $bibnum2,
280            $bibitems,  2, $resdate, $expdate, $notes,
281            $title,      $checkitem, $found);
282 AddReserve($branch_1,  $requesters{$branch_1}, $bibnum2,
283            $bibitems,  3, $resdate, $expdate, $notes,
284            $title,      $checkitem, $found);
285
286 # Ensure that the item's home library controls hold policy lookup
287 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
288
289 my $messages;
290 # Return the CPL item at FPL.  The hold that should be triggered is
291 # the one placed by the CPL patron, as the other two patron's hold
292 # requests cannot be filled by that item per policy.
293 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', $branch_2);
294 is( $messages->{ResFound}->{borrowernumber},
295     $requesters{$branch_1},
296     'restrictive library\'s items only fill requests by own patrons (bug 10272)');
297
298 # Return the FPL item at FPL.  The hold that should be triggered is
299 # the one placed by the RPL patron, as that patron is first in line
300 # and RPL imposes no restrictions on whose holds its items can fill.
301
302 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
303 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
304
305 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', $branch_2);
306 is( $messages->{ResFound}->{borrowernumber},
307     $requesters{$branch_3},
308     'for generous library, its items fill first hold request in line (bug 10272)');
309
310 my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber});
311 isa_ok($reserves, 'ARRAY');
312 is(scalar @$reserves, 1, "Only one reserves for this biblio");
313 my $reserve_id = $reserves->[0]->{reserve_id};
314
315 $reserve = GetReserve($reserve_id);
316 isa_ok($reserve, 'HASH', "GetReserve return");
317 is($reserve->{biblionumber}, $biblionumber);
318
319 $reserve = CancelReserve({reserve_id => $reserve_id});
320 isa_ok($reserve, 'HASH', "CancelReserve return");
321 is($reserve->{biblionumber}, $biblionumber);
322
323 $reserve = GetReserve($reserve_id);
324 is($reserve, undef, "GetReserve returns undef after deletion");
325
326 $reserve = CancelReserve({reserve_id => $reserve_id});
327 is($reserve, undef, "CancelReserve return undef if reserve does not exist");
328
329
330 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
331 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
332 # Test 9761a: Add a reserve without date, CheckReserve should return it
333 $resdate= undef; #defaults to today in AddReserve
334 $expdate= undef; #no expdate
335 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
336 AddReserve($branch_1,  $requesters{$branch_1}, $bibnum,
337            $bibitems,  1, $resdate, $expdate, $notes,
338            $title,      $checkitem, $found);
339 ($status)=CheckReserves($itemnumber,undef,undef);
340 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
341 ($status)=CheckReserves($itemnumber,undef,7);
342 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
343
344 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
345 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
346 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
347 $resdate= dt_from_string();
348 $resdate->add_duration(DateTime::Duration->new(days => 4));
349 $resdate=output_pref($resdate);
350 $expdate= undef; #no expdate
351 AddReserve($branch_1,  $requesters{$branch_1}, $bibnum,
352            $bibitems,  1, $resdate, $expdate, $notes,
353            $title,      $checkitem, $found);
354 ($status)=CheckReserves($itemnumber,undef,undef);
355 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
356
357 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
358 ($status)=CheckReserves($itemnumber,undef,3);
359 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
360 ($status)=CheckReserves($itemnumber,undef,4);
361 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
362
363 # Test 9761d: Check ResFound message of AddReturn for future hold
364 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
365 # In this test we do not need an issued item; it is just a 'checkin'
366 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
367 (my $doreturn, $messages)= AddReturn('97531',$branch_1);
368 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
369 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
370 ($doreturn, $messages)= AddReturn('97531',$branch_1);
371 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
372 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
373 ($doreturn, $messages)= AddReturn('97531',$branch_1);
374 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
375
376 # End of tests for bug 9761 (ConfirmFutureHolds)
377
378 # test marking a hold as captured
379 my $hold_notice_count = count_hold_print_messages();
380 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
381 my $new_count = count_hold_print_messages();
382 is($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
383
384 # test that duplicate notices aren't generated
385 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
386 $new_count = count_hold_print_messages();
387 is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
388
389 # avoiding the not_same_branch error
390 t::lib::Mocks::mock_preference('IndependentBranches', 0);
391 is(
392     DelItemCheck( $bibnum, $itemnumber),
393     'book_reserved',
394     'item that is captured to fill a hold cannot be deleted',
395 );
396
397 my $letter = ReserveSlip($branch_1, $requesters{$branch_1}, $bibnum);
398 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
399
400 # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait?
401 # 9788a: GetReservesFromItemnumber does not return future next available hold
402 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
403 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
404 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
405 $resdate= dt_from_string();
406 $resdate->add_duration(DateTime::Duration->new(days => 2));
407 $resdate=output_pref($resdate);
408 AddReserve($branch_1,  $requesters{$branch_1}, $bibnum,
409            $bibitems,  1, $resdate, $expdate, $notes,
410            $title,      $checkitem, $found);
411 my @results= GetReservesFromItemnumber($itemnumber);
412 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future next available hold');
413 # 9788b: GetReservesFromItemnumber does not return future item level hold
414 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
415 AddReserve($branch_1,  $requesters{$branch_1}, $bibnum,
416            $bibitems,  1, $resdate, $expdate, $notes,
417            $title,      $itemnumber, $found); #item level hold
418 @results= GetReservesFromItemnumber($itemnumber);
419 is(defined $results[3]?1:0, 0, 'GetReservesFromItemnumber does not return a future item level hold');
420 # 9788c: GetReservesFromItemnumber returns future wait (confirmed future hold)
421 ModReserveAffect( $itemnumber,  $requesters{$branch_1} , 0); #confirm hold
422 @results= GetReservesFromItemnumber($itemnumber);
423 is(defined $results[3]?1:0, 1, 'GetReservesFromItemnumber returns a future wait (confirmed future hold)');
424 # End of tests for bug 9788
425
426 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
427 # Tests for CalculatePriority (bug 8918)
428 my $p = C4::Reserves::CalculatePriority($bibnum2);
429 is($p, 4, 'CalculatePriority should now return priority 4');
430 $resdate=undef;
431 AddReserve($branch_1,  $requesters{'CPL2'}, $bibnum2,
432            $bibitems,  $p, $resdate, $expdate, $notes,
433            $title,      $checkitem, $found);
434 $p = C4::Reserves::CalculatePriority($bibnum2);
435 is($p, 5, 'CalculatePriority should now return priority 5');
436 #some tests on bibnum
437 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
438 $p = C4::Reserves::CalculatePriority($bibnum);
439 is($p, 1, 'CalculatePriority should now return priority 1');
440 #add a new reserve and confirm it to waiting
441 AddReserve($branch_1,  $requesters{$branch_1}, $bibnum,
442            $bibitems,  $p, $resdate, $expdate, $notes,
443            $title,      $itemnumber, $found);
444 $p = C4::Reserves::CalculatePriority($bibnum);
445 is($p, 2, 'CalculatePriority should now return priority 2');
446 ModReserveAffect( $itemnumber,  $requesters{$branch_1} , 0);
447 $p = C4::Reserves::CalculatePriority($bibnum);
448 is($p, 1, 'CalculatePriority should now return priority 1');
449 #add another biblio hold, no resdate
450 AddReserve($branch_1,  $requesters{'CPL2'}, $bibnum,
451            $bibitems,  $p, $resdate, $expdate, $notes,
452            $title,      $checkitem, $found);
453 $p = C4::Reserves::CalculatePriority($bibnum);
454 is($p, 2, 'CalculatePriority should now return priority 2');
455 #add another future hold
456 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
457 $resdate= dt_from_string();
458 $resdate->add_duration(DateTime::Duration->new(days => 1));
459 AddReserve($branch_1,  $requesters{'CPL3'}, $bibnum,
460            $bibitems,  $p, output_pref($resdate), $expdate, $notes,
461            $title,      $checkitem, $found);
462 $p = C4::Reserves::CalculatePriority($bibnum);
463 is($p, 2, 'CalculatePriority should now still return priority 2');
464 #calc priority with future resdate
465 $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
466 is($p, 3, 'CalculatePriority should now return priority 3');
467 # End of tests for bug 8918
468
469 # Tests for cancel reserves by users from OPAC.
470 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
471 AddReserve($branch_1,  $requesters{$branch_1}, $item_bibnum,
472            $bibitems,  1, undef, $expdate, $notes,
473            $title,      $checkitem, '');
474 my (undef, $canres, undef) = CheckReserves($itemnumber);
475
476 is( CanReserveBeCanceledFromOpac(), undef,
477     'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
478 );
479 is(
480     CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
481     undef,
482     'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
483 );
484 is(
485     CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
486     undef,
487     'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
488 );
489
490 my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
491 is($cancancel, 1, 'Can user cancel its own reserve');
492
493 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_2});
494 is($cancancel, 0, 'Other user cant cancel reserve');
495
496 ModReserveAffect($itemnumber, $requesters{$branch_1}, 1);
497 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
498 is($cancancel, 0, 'Reserve in transfer status cant be canceled');
499
500 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
501 AddReserve($branch_1,  $requesters{$branch_1}, $item_bibnum,
502            $bibitems,  1, undef, $expdate, $notes,
503            $title,      $checkitem, '');
504 (undef, $canres, undef) = CheckReserves($itemnumber);
505
506 ModReserveAffect($itemnumber, $requesters{$branch_1}, 0);
507 $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{$branch_1});
508 is($cancancel, 0, 'Reserve in waiting status cant be canceled');
509
510 # End of tests for bug 12876
511
512        ####
513 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
514        ####
515
516 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
517
518 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
519
520 #Set the ageRestriction for the Biblio
521 my $record = GetMarcBiblio( $bibnum );
522 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
523 $record->append_fields(  MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16')  );
524 C4::Biblio::ModBiblio( $record, $bibnum, '' );
525
526 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
527
528 #Set the dateofbirth for the Borrower making him "too young".
529 $borrower->{dateofbirth} = DateTime->now->add( years => -15 );
530 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
531
532 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
533
534 #Set the dateofbirth for the Borrower making him "too old".
535 $borrower->{dateofbirth} = DateTime->now->add( years => -30 );
536 C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
537
538 is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
539        ####
540 ####### EO Bug 13113 <<<
541        ####
542
543 my $item = GetItem($itemnumber);
544
545 ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $borrower), "Reserving a book on item level" );
546
547 my $itype = C4::Reserves::_get_itype($item);
548 my $categorycode = $borrower->{categorycode};
549 my $holdingbranch = $item->{holdingbranch};
550 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
551     {
552         categorycode => $categorycode,
553         itemtype     => $itype,
554         branchcode   => $holdingbranch
555     }
556 );
557
558 $dbh->do(
559     "UPDATE issuingrules SET onshelfholds = 1 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
560     undef,
561     $issuing_rule->categorycode, $issuing_rule->itemtype, $issuing_rule->branchcode
562 );
563 ok( C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() allowed" );
564 $dbh->do(
565     "UPDATE issuingrules SET onshelfholds = 0 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
566     undef,
567     $issuing_rule->categorycode, $issuing_rule->itemtype, $issuing_rule->branchcode
568 );
569 ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
570
571 # Tests for bug 14464
572
573 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
574 my $patron = Koha::Patrons->find( $borrowernumber );
575 my $bz14464_fines = $patron->get_account_lines->get_balance;
576 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines at beginning' );
577
578 # First, test cancelling a reserve when there's no charge configured.
579 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
580
581 my $bz14464_reserve = AddReserve(
582     $branch_1,
583     $borrowernumber,
584     $bibnum,
585     undef,
586     '1',
587     undef,
588     undef,
589     '',
590     $title,
591     $itemnumber,
592     'W'
593 );
594
595 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
596
597 CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
598
599 my $old_reserve = Koha::Database->new()->schema()->resultset('OldReserve')->find( $bz14464_reserve );
600 is($old_reserve->get_column('found'), 'W', 'Bug 14968 - Keep found column from reserve');
601
602 $bz14464_fines = $patron->get_account_lines->get_balance;
603 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines after cancelling reserve with no charge configured' );
604
605 # Then, test cancelling a reserve when there's no charge desired.
606 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
607
608 $bz14464_reserve = AddReserve(
609     $branch_1,
610     $borrowernumber,
611     $bibnum,
612     undef,
613     '1',
614     undef,
615     undef,
616     '',
617     $title,
618     $itemnumber,
619     'W'
620 );
621
622 ok( $bz14464_reserve, 'Bug 14464 - 2nd reserve correctly created' );
623
624 CancelReserve({ reserve_id => $bz14464_reserve });
625
626 $bz14464_fines = $patron->get_account_lines->get_balance;
627 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines after cancelling reserve with no charge desired' );
628
629 # Finally, test cancelling a reserve when there's a charge desired and configured.
630 $bz14464_reserve = AddReserve(
631     $branch_1,
632     $borrowernumber,
633     $bibnum,
634     undef,
635     '1',
636     undef,
637     undef,
638     '',
639     $title,
640     $itemnumber,
641     'W'
642 );
643
644 ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
645
646 CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
647
648 $bz14464_fines = $patron->get_account_lines->get_balance;
649 is( int( $bz14464_fines ), 42, 'Bug 14464 - Fine applied after cancelling reserve with charge desired and configured' );
650
651 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
652 #   hold from A pos 1, today, no fut holds: MoveReserve should fill it
653 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
654 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
655 t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
656 AddReserve($branch_1,  $borrowernumber, $item_bibnum,
657     $bibitems,  1, undef, $expdate, $notes, $title, $checkitem, '');
658 MoveReserve( $itemnumber, $borrowernumber );
659 ($status)=CheckReserves( $itemnumber );
660 is( $status, '', 'MoveReserve filled hold');
661 #   hold from A waiting, today, no fut holds: MoveReserve should fill it
662 AddReserve($branch_1,  $borrowernumber, $item_bibnum,
663    $bibitems,  1, undef, $expdate, $notes, $title, $checkitem, 'W');
664 MoveReserve( $itemnumber, $borrowernumber );
665 ($status)=CheckReserves( $itemnumber );
666 is( $status, '', 'MoveReserve filled waiting hold');
667 #   hold from A pos 1, tomorrow, no fut holds: not filled
668 $resdate= dt_from_string();
669 $resdate->add_duration(DateTime::Duration->new(days => 1));
670 $resdate=output_pref($resdate);
671 AddReserve($branch_1,  $borrowernumber, $item_bibnum,
672     $bibitems,  1, $resdate, $expdate, $notes, $title, $checkitem, '');
673 MoveReserve( $itemnumber, $borrowernumber );
674 ($status)=CheckReserves( $itemnumber, undef, 1 );
675 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
676 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
677 #   hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
678 t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
679 AddReserve($branch_1,  $borrowernumber, $item_bibnum,
680     $bibitems,  1, $resdate, $expdate, $notes, $title, $checkitem, '');
681 MoveReserve( $itemnumber, $borrowernumber );
682 ($status)=CheckReserves( $itemnumber, undef, 2 );
683 is( $status, '', 'MoveReserve filled future hold now');
684 #   hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
685 AddReserve($branch_1,  $borrowernumber, $item_bibnum,
686     $bibitems,  1, $resdate, $expdate, $notes, $title, $checkitem, 'W');
687 MoveReserve( $itemnumber, $borrowernumber );
688 ($status)=CheckReserves( $itemnumber, undef, 2 );
689 is( $status, '', 'MoveReserve filled future waiting hold now');
690 #   hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
691 $resdate= dt_from_string();
692 $resdate->add_duration(DateTime::Duration->new(days => 3));
693 $resdate=output_pref($resdate);
694 AddReserve($branch_1,  $borrowernumber, $item_bibnum,
695     $bibitems,  1, $resdate, $expdate, $notes, $title, $checkitem, '');
696 MoveReserve( $itemnumber, $borrowernumber );
697 ($status)=CheckReserves( $itemnumber, undef, 3 );
698 is( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
699 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
700
701 # we reached the finish
702 $schema->storage->txn_rollback();
703
704 sub count_hold_print_messages {
705     my $message_count = $dbh->selectall_arrayref(q{
706         SELECT COUNT(*)
707         FROM message_queue
708         WHERE letter_code = 'HOLD' 
709         AND   message_transport_type = 'print'
710     });
711     return $message_count->[0]->[0];
712 }