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
=> 64;
25 use t
::lib
::TestBuilder
;
28 use DateTime
::Duration
;
40 use Koha
::Notice
::Templates
;
42 use Koha
::Patron
::Categories
;
43 use Koha
::CirculationRules
;
46 require_ok
('C4::Reserves');
50 my $database = Koha
::Database
->new();
51 my $schema = $database->schema();
52 $schema->storage->txn_begin();
53 my $dbh = C4
::Context
->dbh;
54 $dbh->do('DELETE FROM circulation_rules');
56 my $builder = t
::lib
::TestBuilder
->new;
58 my $frameworkcode = q
//;
61 t
::lib
::Mocks
::mock_preference
('ReservesNeedReturns', 1);
63 # Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
64 $dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
65 my $cache = Koha
::Caches
->get_instance;
66 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
67 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
68 $cache->clear_from_cache("default_value_for_mod_marc-");
69 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
73 my $branch_1 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
74 my $branch_2 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
75 my $branch_3 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
77 my $category_1 = $builder->build({ source
=> 'Category' })->{ categorycode
};
78 my $category_2 = $builder->build({ source
=> 'Category' })->{ categorycode
};
80 my $itemtype = $builder->build(
81 { source
=> 'Itemtype', value
=> { notforloan
=> undef } } )->{itemtype
};
83 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $branch_1 });
85 my $bibnum = $builder->build_sample_biblio({frameworkcode
=> $frameworkcode})->biblionumber;
87 # Create a helper item instance for testing
88 my $item = $builder->build_sample_item({ biblionumber
=> $bibnum, library
=> $branch_1, itype
=> $itemtype });
90 my $biblio_with_no_item = $builder->build_sample_biblio;
92 # Modify item; setting barcode.
93 my $testbarcode = '97531';
94 $item->barcode($testbarcode)->store; # FIXME We should not hardcode a barcode! Also, what's the purpose of this?
98 firstname
=> 'my firstname',
99 surname
=> 'my surname',
100 categorycode
=> $category_1,
101 branchcode
=> $branch_1,
103 Koha
::Patron
::Categories
->find($category_1)->set({ enrolmentfee
=> 0})->store;
104 my $borrowernumber = Koha
::Patron
->new(\
%data)->store->borrowernumber;
105 my $patron = Koha
::Patrons
->find( $borrowernumber );
106 my $borrower = $patron->unblessed;
107 my $biblionumber = $bibnum;
108 my $barcode = $testbarcode;
110 my $branchcode = Koha
::Libraries
->search->next->branchcode;
114 branchcode
=> $branchcode,
115 borrowernumber
=> $borrowernumber,
116 biblionumber
=> $biblionumber,
121 my ($status, $reserve, $all_reserves) = CheckReserves
($item->itemnumber, $barcode);
123 is
($status, "Reserved", "CheckReserves Test 1");
125 ok
(exists($reserve->{reserve_id
}), 'CheckReserves() include reserve_id in its response');
127 ($status, $reserve, $all_reserves) = CheckReserves
($item->itemnumber);
128 is
($status, "Reserved", "CheckReserves Test 2");
130 ($status, $reserve, $all_reserves) = CheckReserves
(undef, $barcode);
131 is
($status, "Reserved", "CheckReserves Test 3");
133 my $ReservesControlBranch = C4
::Context
->preference('ReservesControlBranch');
134 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'ItemHomeLibrary' );
136 'ItemHomeLib' eq GetReservesControlBranch
(
137 { homebranch
=> 'ItemHomeLib' },
138 { branchcode
=> 'PatronHomeLib' }
139 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
141 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
143 'PatronHomeLib' eq GetReservesControlBranch
(
144 { homebranch
=> 'ItemHomeLib' },
145 { branchcode
=> 'PatronHomeLib' }
146 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
148 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', $ReservesControlBranch );
151 ### Regression test for bug 10272
154 $requesters{$branch_1} = Koha
::Patron
->new({
155 branchcode
=> $branch_1,
156 categorycode
=> $category_2,
157 surname
=> "borrower from $branch_1",
158 })->store->borrowernumber;
159 for my $i ( 2 .. 5 ) {
160 $requesters{"CPL$i"} = Koha
::Patron
->new({
161 branchcode
=> $branch_1,
162 categorycode
=> $category_2,
163 surname
=> "borrower $i from $branch_1",
164 })->store->borrowernumber;
166 $requesters{$branch_2} = Koha
::Patron
->new({
167 branchcode
=> $branch_2,
168 categorycode
=> $category_2,
169 surname
=> "borrower from $branch_2",
170 })->store->borrowernumber;
171 $requesters{$branch_3} = Koha
::Patron
->new({
172 branchcode
=> $branch_3,
173 categorycode
=> $category_2,
174 surname
=> "borrower from $branch_3",
175 })->store->borrowernumber;
177 # Configure rules so that $branch_1 allows only $branch_1 patrons
178 # to request its items, while $branch_2 will allow its items
179 # to fill holds from anywhere.
181 $dbh->do('DELETE FROM circulation_rules');
182 Koha
::CirculationRules
->set_rules(
185 categorycode
=> undef,
188 reservesallowed
=> 25,
189 holds_per_record
=> 1,
194 # CPL allows only its own patrons to request its items
195 Koha
::CirculationRules
->set_rules(
197 branchcode
=> $branch_1,
201 returnbranch
=> 'homebranch',
206 # ... while FPL allows anybody to request its items
207 Koha
::CirculationRules
->set_rules(
209 branchcode
=> $branch_2,
213 returnbranch
=> 'homebranch',
218 my $bibnum2 = $builder->build_sample_biblio({frameworkcode
=> $frameworkcode})->biblionumber;
220 my ($itemnum_cpl, $itemnum_fpl);
221 $itemnum_cpl = $builder->build_sample_item(
223 biblionumber
=> $bibnum2,
224 library
=> $branch_1,
225 barcode
=> 'bug10272_CPL',
229 $itemnum_fpl = $builder->build_sample_item(
231 biblionumber
=> $bibnum2,
232 library
=> $branch_2,
233 barcode
=> 'bug10272_FPL',
238 # Ensure that priorities are numbered correcly when a hold is moved to waiting
240 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
243 branchcode
=> $branch_3,
244 borrowernumber
=> $requesters{$branch_3},
245 biblionumber
=> $bibnum2,
251 branchcode
=> $branch_2,
252 borrowernumber
=> $requesters{$branch_2},
253 biblionumber
=> $bibnum2,
259 branchcode
=> $branch_1,
260 borrowernumber
=> $requesters{$branch_1},
261 biblionumber
=> $bibnum2,
265 ModReserveAffect
($itemnum_cpl, $requesters{$branch_3}, 0);
267 # Now it should have different priorities.
268 my $biblio = Koha
::Biblios
->find( $bibnum2 );
269 my $holds = $biblio->holds({}, { order_by
=> 'reserve_id' });;
270 is
($holds->next->priority, 0, 'Item is correctly waiting');
271 is
($holds->next->priority, 1, 'Item is correctly priority 1');
272 is
($holds->next->priority, 2, 'Item is correctly priority 2');
274 my @reserves = Koha
::Holds
->search({ borrowernumber
=> $requesters{$branch_3} })->waiting();
275 is
( @reserves, 1, 'GetWaiting got only the waiting reserve' );
276 is
( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' );
279 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum2));
282 branchcode
=> $branch_3,
283 borrowernumber
=> $requesters{$branch_3},
284 biblionumber
=> $bibnum2,
290 branchcode
=> $branch_2,
291 borrowernumber
=> $requesters{$branch_2},
292 biblionumber
=> $bibnum2,
299 branchcode
=> $branch_1,
300 borrowernumber
=> $requesters{$branch_1},
301 biblionumber
=> $bibnum2,
306 # Ensure that the item's home library controls hold policy lookup
307 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'ItemHomeLibrary' );
310 # Return the CPL item at FPL. The hold that should be triggered is
311 # the one placed by the CPL patron, as the other two patron's hold
312 # requests cannot be filled by that item per policy.
313 (undef, $messages, undef, undef) = AddReturn
('bug10272_CPL', $branch_2);
314 is
( $messages->{ResFound
}->{borrowernumber
},
315 $requesters{$branch_1},
316 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
318 # Return the FPL item at FPL. The hold that should be triggered is
319 # the one placed by the RPL patron, as that patron is first in line
320 # and RPL imposes no restrictions on whose holds its items can fill.
322 # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
323 t
::lib
::Mocks
::mock_preference
( 'LocalHoldsPriority', '' );
325 (undef, $messages, undef, undef) = AddReturn
('bug10272_FPL', $branch_2);
326 is
( $messages->{ResFound
}->{borrowernumber
},
327 $requesters{$branch_3},
328 'for generous library, its items fill first hold request in line (bug 10272)');
330 $biblio = Koha
::Biblios
->find( $biblionumber );
331 $holds = $biblio->holds;
332 is
($holds->count, 1, "Only one reserves for this biblio");
333 $holds->next->reserve_id;
335 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
336 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
337 # Test 9761a: Add a reserve without date, CheckReserve should return it
338 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
341 branchcode
=> $branch_1,
342 borrowernumber
=> $requesters{$branch_1},
343 biblionumber
=> $bibnum,
347 ($status)=CheckReserves
($item->itemnumber,undef,undef);
348 is
( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
349 ($status)=CheckReserves
($item->itemnumber,undef,7);
350 is
( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
352 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
353 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
354 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
355 my $resdate= dt_from_string
();
356 $resdate->add_duration(DateTime
::Duration
->new(days
=> 4));
357 $resdate=output_pref
($resdate);
358 my $reserve_id = AddReserve
(
360 branchcode
=> $branch_1,
361 borrowernumber
=> $requesters{$branch_1},
362 biblionumber
=> $bibnum,
364 reservation_date
=> $resdate,
367 ($status)=CheckReserves
($item->itemnumber,undef,undef);
368 is
( $status, '', 'CheckReserves returns no future reserve without lookahead');
370 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
371 ($status)=CheckReserves
($item->itemnumber,undef,3);
372 is
( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
373 ($status)=CheckReserves
($item->itemnumber,undef,4);
374 is
( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
376 # Test 9761d: Check ResFound message of AddReturn for future hold
377 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
378 # In this test we do not need an issued item; it is just a 'checkin'
379 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 0);
380 (my $doreturn, $messages)= AddReturn
('97531',$branch_1);
381 is
($messages->{ResFound
}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
382 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 3);
383 ($doreturn, $messages)= AddReturn
('97531',$branch_1);
384 is
(exists $messages->{ResFound
}?
1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
385 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 7);
386 ($doreturn, $messages)= AddReturn
('97531',$branch_1);
387 is
(exists $messages->{ResFound
}?
1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
389 # End of tests for bug 9761 (ConfirmFutureHolds)
391 # test marking a hold as captured
392 my $hold_notice_count = count_hold_print_messages
();
393 ModReserveAffect
($item->itemnumber, $requesters{$branch_1}, 0);
394 my $new_count = count_hold_print_messages
();
395 is
($new_count, $hold_notice_count + 1, 'patron notified when item set to waiting');
397 # test that duplicate notices aren't generated
398 ModReserveAffect
($item->itemnumber, $requesters{$branch_1}, 0);
399 $new_count = count_hold_print_messages
();
400 is
($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 11445)');
402 # avoiding the not_same_branch error
403 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
404 $item = Koha
::Items
->find($item->itemnumber);
408 'item that is captured to fill a hold cannot be deleted',
411 my $letter = ReserveSlip
( { branchcode
=> $branch_1, reserve_id
=> $reserve_id } );
412 ok
(defined($letter), 'can successfully generate hold slip (bug 10949)');
414 # Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
415 # 9788a: current_holds does not return future next available hold
416 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
417 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 2);
418 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
419 $resdate= dt_from_string
();
420 $resdate->add_duration(DateTime
::Duration
->new(days
=> 2));
421 $resdate=output_pref
($resdate);
424 branchcode
=> $branch_1,
425 borrowernumber
=> $requesters{$branch_1},
426 biblionumber
=> $bibnum,
428 reservation_date
=> $resdate,
432 $holds = $item->current_holds;
433 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
434 my $future_holds = $holds->search({ reservedate
=> { '>' => $dtf->format_date( dt_from_string
) } } );
435 is
( $future_holds->count, 0, 'current_holds does not return a future next available hold');
436 # 9788b: current_holds does not return future item level hold
437 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
440 branchcode
=> $branch_1,
441 borrowernumber
=> $requesters{$branch_1},
442 biblionumber
=> $bibnum,
444 reservation_date
=> $resdate,
445 itemnumber
=> $item->itemnumber,
448 $future_holds = $holds->search({ reservedate
=> { '>' => $dtf->format_date( dt_from_string
) } } );
449 is
( $future_holds->count, 0, 'current_holds does not return a future item level hold' );
450 # 9788c: current_holds returns future wait (confirmed future hold)
451 ModReserveAffect
( $item->itemnumber, $requesters{$branch_1} , 0); #confirm hold
452 $future_holds = $holds->search({ reservedate
=> { '>' => $dtf->format_date( dt_from_string
) } } );
453 is
( $future_holds->count, 1, 'current_holds returns a future wait (confirmed future hold)' );
454 # End of tests for bug 9788
456 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
457 # Tests for CalculatePriority (bug 8918)
458 my $p = C4
::Reserves
::CalculatePriority
($bibnum2);
459 is
($p, 4, 'CalculatePriority should now return priority 4');
462 branchcode
=> $branch_1,
463 borrowernumber
=> $requesters{'CPL2'},
464 biblionumber
=> $bibnum2,
468 $p = C4
::Reserves
::CalculatePriority
($bibnum2);
469 is
($p, 5, 'CalculatePriority should now return priority 5');
470 #some tests on bibnum
471 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
472 $p = C4
::Reserves
::CalculatePriority
($bibnum);
473 is
($p, 1, 'CalculatePriority should now return priority 1');
474 #add a new reserve and confirm it to waiting
477 branchcode
=> $branch_1,
478 borrowernumber
=> $requesters{$branch_1},
479 biblionumber
=> $bibnum,
481 itemnumber
=> $item->itemnumber,
484 $p = C4
::Reserves
::CalculatePriority
($bibnum);
485 is
($p, 2, 'CalculatePriority should now return priority 2');
486 ModReserveAffect
( $item->itemnumber, $requesters{$branch_1} , 0);
487 $p = C4
::Reserves
::CalculatePriority
($bibnum);
488 is
($p, 1, 'CalculatePriority should now return priority 1');
489 #add another biblio hold, no resdate
492 branchcode
=> $branch_1,
493 borrowernumber
=> $requesters{'CPL2'},
494 biblionumber
=> $bibnum,
498 $p = C4
::Reserves
::CalculatePriority
($bibnum);
499 is
($p, 2, 'CalculatePriority should now return priority 2');
500 #add another future hold
501 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
502 $resdate= dt_from_string
();
503 $resdate->add_duration(DateTime
::Duration
->new(days
=> 1));
506 branchcode
=> $branch_1,
507 borrowernumber
=> $requesters{'CPL2'},
508 biblionumber
=> $bibnum,
510 reservation_date
=> output_pref
($resdate),
513 $p = C4
::Reserves
::CalculatePriority
($bibnum);
514 is
($p, 2, 'CalculatePriority should now still return priority 2');
515 #calc priority with future resdate
516 $p = C4
::Reserves
::CalculatePriority
($bibnum, $resdate);
517 is
($p, 3, 'CalculatePriority should now return priority 3');
518 # End of tests for bug 8918
520 # Tests for cancel reserves by users from OPAC.
521 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
524 branchcode
=> $branch_1,
525 borrowernumber
=> $requesters{$branch_1},
526 biblionumber
=> $bibnum,
530 my (undef, $canres, undef) = CheckReserves
($item->itemnumber);
532 is
( CanReserveBeCanceledFromOpac
(), undef,
533 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
536 CanReserveBeCanceledFromOpac
( $canres->{resserve_id
} ),
538 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
541 CanReserveBeCanceledFromOpac
( undef, $requesters{CPL
} ),
543 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
546 my $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_1});
547 is
($cancancel, 1, 'Can user cancel its own reserve');
549 $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_2});
550 is
($cancancel, 0, 'Other user cant cancel reserve');
552 ModReserveAffect
($item->itemnumber, $requesters{$branch_1}, 1);
553 $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_1});
554 is
($cancancel, 0, 'Reserve in transfer status cant be canceled');
556 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
559 branchcode
=> $branch_1,
560 borrowernumber
=> $requesters{$branch_1},
561 biblionumber
=> $bibnum,
565 (undef, $canres, undef) = CheckReserves
($item->itemnumber);
567 ModReserveAffect
($item->itemnumber, $requesters{$branch_1}, 0);
568 $cancancel = CanReserveBeCanceledFromOpac
($canres->{reserve_id
}, $requesters{$branch_1});
569 is
($cancancel, 0, 'Reserve in waiting status cant be canceled');
571 # End of tests for bug 12876
574 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
577 t
::lib
::Mocks
::mock_preference
( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
579 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
581 #Set the ageRestriction for the Biblio
582 my $record = GetMarcBiblio
({ biblionumber
=> $bibnum });
583 my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField
( "biblioitems.agerestriction" );
584 $record->append_fields( MARC
::Field
->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
585 C4
::Biblio
::ModBiblio
( $record, $bibnum, $frameworkcode );
587 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblionumber)->{status
} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
589 #Set the dateofbirth for the Borrower making them "too young".
590 $borrower->{dateofbirth
} = DateTime
->now->add( years
=> -15 );
591 Koha
::Patrons
->find( $borrowernumber )->set({ dateofbirth
=> $borrower->{dateofbirth
} })->store;
593 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblionumber)->{status
} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
595 #Set the dateofbirth for the Borrower making them "too old".
596 $borrower->{dateofbirth
} = DateTime
->now->add( years
=> -30 );
597 Koha
::Patrons
->find( $borrowernumber )->set({ dateofbirth
=> $borrower->{dateofbirth
} })->store;
599 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblionumber)->{status
} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
601 is
( C4
::Reserves
::CanBookBeReserved
($borrowernumber, $biblio_with_no_item->biblionumber)->{status
} , '', "Biblio with no item. Status is empty");
603 ####### EO Bug 13113 <<<
606 ok
( C4
::Reserves
::IsAvailableForItemLevelRequest
($item, $patron), "Reserving a book on item level" );
608 my $pickup_branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
609 t
::lib
::Mocks
::mock_preference
( 'UseBranchTransferLimits', '1' );
610 t
::lib
::Mocks
::mock_preference
( 'BranchTransferLimitsType', 'itemtype' );
611 my $limit = Koha
::Item
::Transfer
::Limit
->new(
613 toBranch
=> $pickup_branch,
614 fromBranch
=> $item->holdingbranch,
615 itemtype
=> $item->effective_itemtype,
618 is
( C4
::Reserves
::IsAvailableForItemLevelRequest
($item, $patron, $pickup_branch), 0, "Item level request not available due to transfer limit" );
619 t
::lib
::Mocks
::mock_preference
( 'UseBranchTransferLimits', '0' );
621 my $categorycode = $borrower->{categorycode
};
622 my $holdingbranch = $item->{holdingbranch
};
623 Koha
::CirculationRules
->set_rules(
625 categorycode
=> $categorycode,
626 itemtype
=> $item->effective_itemtype,
627 branchcode
=> $holdingbranch,
634 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
635 # hold from A pos 1, today, no fut holds: MoveReserve should fill it
636 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
637 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 0);
638 t
::lib
::Mocks
::mock_preference
('AllowHoldDateInFuture', 1);
641 branchcode
=> $branch_1,
642 borrowernumber
=> $borrowernumber,
643 biblionumber
=> $bibnum,
647 MoveReserve
( $item->itemnumber, $borrowernumber );
648 ($status)=CheckReserves
( $item->itemnumber );
649 is
( $status, '', 'MoveReserve filled hold');
650 # hold from A waiting, today, no fut holds: MoveReserve should fill it
653 branchcode
=> $branch_1,
654 borrowernumber
=> $borrowernumber,
655 biblionumber
=> $bibnum,
660 MoveReserve
( $item->itemnumber, $borrowernumber );
661 ($status)=CheckReserves
( $item->itemnumber );
662 is
( $status, '', 'MoveReserve filled waiting hold');
663 # hold from A pos 1, tomorrow, no fut holds: not filled
664 $resdate= dt_from_string
();
665 $resdate->add_duration(DateTime
::Duration
->new(days
=> 1));
666 $resdate=output_pref
($resdate);
669 branchcode
=> $branch_1,
670 borrowernumber
=> $borrowernumber,
671 biblionumber
=> $bibnum,
673 reservation_date
=> $resdate,
676 MoveReserve
( $item->itemnumber, $borrowernumber );
677 ($status)=CheckReserves
( $item->itemnumber, undef, 1 );
678 is
( $status, 'Reserved', 'MoveReserve did not fill future hold');
679 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
680 # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
681 t
::lib
::Mocks
::mock_preference
('ConfirmFutureHolds', 2);
684 branchcode
=> $branch_1,
685 borrowernumber
=> $borrowernumber,
686 biblionumber
=> $bibnum,
688 reservation_date
=> $resdate,
691 MoveReserve
( $item->itemnumber, $borrowernumber );
692 ($status)=CheckReserves
( $item->itemnumber, undef, 2 );
693 is
( $status, '', 'MoveReserve filled future hold now');
694 # hold from A waiting, tomorrow, fut holds=2: MoveReserve should fill it
697 branchcode
=> $branch_1,
698 borrowernumber
=> $borrowernumber,
699 biblionumber
=> $bibnum,
701 reservation_date
=> $resdate,
704 MoveReserve
( $item->itemnumber, $borrowernumber );
705 ($status)=CheckReserves
( $item->itemnumber, undef, 2 );
706 is
( $status, '', 'MoveReserve filled future waiting hold now');
707 # hold from A pos 1, today+3, fut holds=2: MoveReserve should not fill it
708 $resdate= dt_from_string
();
709 $resdate->add_duration(DateTime
::Duration
->new(days
=> 3));
710 $resdate=output_pref
($resdate);
713 branchcode
=> $branch_1,
714 borrowernumber
=> $borrowernumber,
715 biblionumber
=> $bibnum,
717 reservation_date
=> $resdate,
720 MoveReserve
( $item->itemnumber, $borrowernumber );
721 ($status)=CheckReserves
( $item->itemnumber, undef, 3 );
722 is
( $status, 'Reserved', 'MoveReserve did not fill future hold of 3 days');
723 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
725 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
726 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
727 $cache->clear_from_cache("default_value_for_mod_marc-");
728 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
730 subtest
'_koha_notify_reserve() tests' => sub {
734 my $wants_hold_and_email = {
740 letter_code
=> 'HOLD'
743 my $mp = Test
::MockModule
->new( 'C4::Members::Messaging' );
745 $mp->mock("GetMessagingPreferences",$wants_hold_and_email);
747 $dbh->do('DELETE FROM letter');
749 my $email_hold_notice = $builder->build({
752 message_transport_type
=> 'email',
755 module
=> 'reserves',
760 my $sms_hold_notice = $builder->build({
763 message_transport_type
=> 'sms',
766 module
=> 'reserves',
771 my $hold_borrower = $builder->build({
772 source
=> 'Borrower',
774 smsalertnumber
=>'5555555555',
777 })->{borrowernumber
};
779 C4
::Reserves
::AddReserve
(
781 branchcode
=> $item->homebranch,
782 borrowernumber
=> $hold_borrower,
783 biblionumber
=> $item->biblionumber,
787 ModReserveAffect
($item->itemnumber, $hold_borrower, 0);
788 my $sms_message_address = $schema->resultset('MessageQueue')->search({
789 letter_code
=> 'HOLD',
790 message_transport_type
=> 'sms',
791 borrowernumber
=> $hold_borrower,
792 })->next()->to_address();
793 is
($sms_message_address, undef ,"We should not populate the sms message with the sms number, sending will do so");
795 my $email_message_address = $schema->resultset('MessageQueue')->search({
796 letter_code
=> 'HOLD',
797 message_transport_type
=> 'email',
798 borrowernumber
=> $hold_borrower,
799 })->next()->to_address();
800 is
($email_message_address, undef ,"We should not populate the hold message with the email address, sending will do so");
804 subtest
'ReservesNeedReturns' => sub {
807 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
809 homebranch
=> $library->branchcode,
810 holdingbranch
=> $library->branchcode,
812 my $item = $builder->build_sample_item($item_info);
813 my $patron = $builder->build_object(
815 class => 'Koha::Patrons',
816 value
=> { branchcode
=> $library->branchcode, }
819 my $patron_2 = $builder->build_object(
821 class => 'Koha::Patrons',
822 value
=> { branchcode
=> $library->branchcode, }
828 t
::lib
::Mocks
::mock_preference
('ReservesNeedReturns', 1); # Test with feature disabled
829 my $hold = place_item_hold
( $patron, $item, $library, $priority );
830 is
( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
831 is
( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
834 t
::lib
::Mocks
::mock_preference
('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
835 $hold = place_item_hold
( $patron, $item, $library, $priority );
836 is
( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
837 is
( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
840 $item->onloan('2010-01-01')->store;
841 $hold = place_item_hold
( $patron, $item, $library, $priority );
842 is
( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item onloan priority must be set to 1' );
845 t
::lib
::Mocks
::mock_preference
('AllowHoldsOnDamagedItems', 0); # '0' means damaged holds not allowed
846 $item->onloan(undef)->damaged(1)->store;
847 $hold = place_item_hold
( $patron, $item, $library, $priority );
848 is
( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item damaged and not allowed holds on damaged items priority must be set to 1' );
850 t
::lib
::Mocks
::mock_preference
('AllowHoldsOnDamagedItems', 1); # '0' means damaged holds not allowed
851 $hold = place_item_hold
( $patron, $item, $library, $priority );
852 is
( $hold->priority, 0, 'If ReservesNeedReturns is 0 and damaged holds allowed, priority must have been set to 0' );
853 is
( $hold->found, 'W', 'If ReservesNeedReturns is 0 and damaged holds allowed, found must have been set waiting' );
856 my $hold_1 = place_item_hold
( $patron, $item, $library, $priority );
857 is
( $hold_1->found, 'W', 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
858 is
( $hold_1->priority, 0, 'First hold on item is set to waiting with ReservesNeedReturns set to 0' );
859 $hold = place_item_hold
( $patron_2, $item, $library, $priority );
860 is
( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item already on hold priority must be set to 1' );
864 my $transfer = $builder->build_object({
865 class => "Koha::Item::Transfers",
867 itemnumber
=> $item->itemnumber,
868 datearrived
=> undef,
871 $item->damaged(0)->store;
872 $hold = place_item_hold
( $patron, $item, $library, $priority );
873 is
( $hold->found, undef, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
874 is
( $hold->priority, 1, 'If ReservesNeedReturns is 0 but item in transit the hold must not be set to waiting' );
878 $hold = place_item_hold
( $patron, $item, $library, $priority );
879 is
( $hold->priority, 0, 'If ReservesNeedReturns is 0 and no other status, priority must have been set to 0' );
880 is
( $hold->found, 'W', 'If ReservesNeedReturns is 0 and no other status, found must have been set waiting' );
881 $hold_1 = place_item_hold
( $patron, $item, $library, $priority );
882 is
( $hold_1->priority, 1, 'If ReservesNeedReturns is 0 but item has a hold priority is 1' );
883 $hold_1->suspend(1)->store; # We suspend the hold
884 $hold->delete; # Delete the waiting hold
885 $hold = place_item_hold
( $patron, $item, $library, $priority );
886 is
( $hold->priority, 0, 'If ReservesNeedReturns is 0 and other hold(s) suspended, priority must have been set to 0' );
887 is
( $hold->found, 'W', 'If ReservesNeedReturns is 0 and other hold(s) suspended, found must have been set waiting' );
892 t
::lib
::Mocks
::mock_preference
('ReservesNeedReturns', 1); # Don't affect other tests
895 subtest
'ChargeReserveFee tests' => sub {
899 my $library = $builder->build_object({ class => 'Koha::Libraries' });
900 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
903 my $title = 'A title';
905 my $context = Test
::MockModule
->new('C4::Context');
906 $context->mock( userenv
=> { branch
=> $library->id } );
908 my $line = C4
::Reserves
::ChargeReserveFee
( $patron->id, $fee, $title );
910 is
( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
911 ok
( $line->is_debit, 'Generates a debit line' );
912 is
( $line->debit_type_code, 'RESERVE' , 'generates RESERVE debit_type');
913 is
( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
914 is
( $line->amount, $fee , 'amount set correctly');
915 is
( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
916 is
( $line->description, "$title" , 'description is title of reserved item');
917 is
( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
920 subtest
'reserves.item_level_hold' => sub {
923 my $item = $builder->build_sample_item;
924 my $patron = $builder->build_object(
926 class => 'Koha::Patrons',
927 value
=> { branchcode
=> $item->homebranch }
931 subtest
'item level hold' => sub {
933 my $reserve_id = AddReserve
(
935 branchcode
=> $item->homebranch,
936 borrowernumber
=> $patron->borrowernumber,
937 biblionumber
=> $item->biblionumber,
939 itemnumber
=> $item->itemnumber,
943 my $hold = Koha
::Holds
->find($reserve_id);
944 is
( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' );
947 ModReserveAffect
( $item->itemnumber, $patron->borrowernumber, 1 );
949 # Revert the waiting status
950 C4
::Reserves
::RevertWaitingStatus
(
951 { itemnumber
=> $item->itemnumber } );
953 $hold = Koha
::Holds
->find($reserve_id);
955 is
( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' );
957 $hold->delete; # cleanup
960 subtest
'biblio level hold' => sub {
962 my $reserve_id = AddReserve
(
964 branchcode
=> $item->homebranch,
965 borrowernumber
=> $patron->borrowernumber,
966 biblionumber
=> $item->biblionumber,
971 my $hold = Koha
::Holds
->find($reserve_id);
972 is
( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' );
975 ModReserveAffect
( $item->itemnumber, $patron->borrowernumber, 1 );
977 $hold = Koha
::Holds
->find($reserve_id);
978 is
( $hold->itemnumber, $item->itemnumber, 'Itemnumber should be set on hold confirmation' );
980 # Revert the waiting status
981 C4
::Reserves
::RevertWaitingStatus
( { itemnumber
=> $item->itemnumber } );
983 $hold = Koha
::Holds
->find($reserve_id);
984 is
( $hold->itemnumber, undef, 'Itemnumber should be removed when the waiting status is revert' );
991 subtest
'MoveReserve additional test' => sub {
995 # Create the items and patrons we need
996 my $biblio = $builder->build_sample_biblio();
997 my $itype = $builder->build_object({ class => "Koha::ItemTypes", value
=> { notforloan
=> 0 } });
998 my $item_1 = $builder->build_sample_item({ biblionumber
=> $biblio->biblionumber,notforloan
=> 0, itype
=> $itype->itemtype });
999 my $item_2 = $builder->build_sample_item({ biblionumber
=> $biblio->biblionumber, notforloan
=> 0, itype
=> $itype->itemtype });
1000 my $patron_1 = $builder->build_object({ class => "Koha::Patrons" });
1001 my $patron_2 = $builder->build_object({ class => "Koha::Patrons" });
1003 # Place a hold on the title for both patrons
1004 my $reserve_1 = AddReserve
(
1006 branchcode
=> $item_1->homebranch,
1007 borrowernumber
=> $patron_1->borrowernumber,
1008 biblionumber
=> $biblio->biblionumber,
1010 itemnumber
=> $item_1->itemnumber,
1013 my $reserve_2 = AddReserve
(
1015 branchcode
=> $item_2->homebranch,
1016 borrowernumber
=> $patron_2->borrowernumber,
1017 biblionumber
=> $biblio->biblionumber,
1019 itemnumber
=> $item_1->itemnumber,
1022 is
($patron_1->holds->next()->reserve_id, $reserve_1, "The 1st patron has a hold");
1023 is
($patron_2->holds->next()->reserve_id, $reserve_2, "The 2nd patron has a hold");
1025 # Fake the holds queue
1026 $dbh->do(q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?,?)},undef,($patron_1->borrowernumber,$biblio->biblionumber,$item_1->itemnumber,$item_1->homebranch,0,$reserve_1));
1028 # The 2nd hold should be filed even if the item is preselected for the first hold
1029 MoveReserve
($item_1->itemnumber,$patron_2->borrowernumber);
1030 is
($patron_2->holds->count, 0, "The 2nd patrons no longer has a hold");
1031 is
($patron_2->old_holds->next()->reserve_id, $reserve_2, "The 2nd patrons hold was filled and moved to old holds");
1035 subtest
'RevertWaitingStatus' => sub {
1039 # Create the items and patrons we need
1040 my $biblio = $builder->build_sample_biblio();
1041 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1042 my $itype = $builder->build_object(
1043 { class => "Koha::ItemTypes", value
=> { notforloan
=> 0 } } );
1044 my $item_1 = $builder->build_sample_item(
1046 biblionumber
=> $biblio->biblionumber,
1047 itype
=> $itype->itemtype,
1048 library
=> $library->branchcode
1051 my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } );
1052 my $patron_2 = $builder->build_object( { class => "Koha::Patrons" } );
1053 my $patron_3 = $builder->build_object( { class => "Koha::Patrons" } );
1054 my $patron_4 = $builder->build_object( { class => "Koha::Patrons" } );
1056 # Place a hold on the title for both patrons
1058 my $hold_1 = place_item_hold
( $patron_1, $item_1, $library, $priority );
1059 my $hold_2 = place_item_hold
( $patron_2, $item_1, $library, $priority );
1060 my $hold_3 = place_item_hold
( $patron_3, $item_1, $library, $priority );
1061 my $hold_4 = place_item_hold
( $patron_4, $item_1, $library, $priority );
1063 $hold_1->set_waiting;
1064 AddIssue
( $patron_3->unblessed, $item_1->barcode, undef, 'revert' );
1066 my $holds = $biblio->holds;
1067 is
( $holds->count, 3, 'One hold has been deleted' );
1070 $holds->next->priority, $holds->next->priority,
1071 $holds->next->priority
1074 'priorities have been reordered'
1078 subtest
'CheckReserves additional test' => sub {
1082 my $item = $builder->build_sample_item;
1083 my $reserve1 = $builder->build_object(
1085 class => "Koha::Holds",
1089 itemnumber
=> undef,
1090 biblionumber
=> $item->biblionumber,
1091 waitingdate
=> undef,
1092 cancellationdate
=> undef,
1093 item_level_hold
=> 0,
1094 lowestPriority
=> 0,
1095 expirationdate
=> undef,
1096 suspend_until
=> undef,
1102 my $reserve2 = $builder->build_object(
1104 class => "Koha::Holds",
1108 biblionumber
=> $item->biblionumber,
1109 borrowernumber
=> $reserve1->borrowernumber,
1110 itemnumber
=> undef,
1111 waitingdate
=> undef,
1112 cancellationdate
=> undef,
1113 item_level_hold
=> 0,
1114 lowestPriority
=> 0,
1115 expirationdate
=> undef,
1116 suspend_until
=> undef,
1123 my $tmp_holdsqueue = $builder->build(
1125 source
=> 'TmpHoldsqueue',
1127 borrowernumber
=> $reserve1->borrowernumber,
1128 biblionumber
=> $reserve1->biblionumber,
1132 my $fill_target = $builder->build(
1134 source
=> 'HoldFillTarget',
1136 borrowernumber
=> $reserve1->borrowernumber,
1137 biblionumber
=> $reserve1->biblionumber,
1138 itemnumber
=> $item->itemnumber,
1139 item_level_request
=> 0,
1144 ModReserveAffect
( $item->itemnumber, $reserve1->borrowernumber, 1,
1145 $reserve1->reserve_id );
1146 my ( $status, $matched_reserve, $possible_reserves ) =
1147 CheckReserves
( $item->itemnumber );
1149 is
( $status, 'Reserved', "We found a reserve" );
1150 is
( $matched_reserve->{reserve_id
},
1151 $reserve1->reserve_id, "We got the Transit reserve" );
1152 is
( scalar @
$possible_reserves, 2, 'We do get both reserves' );
1156 sub count_hold_print_messages
{
1157 my $message_count = $dbh->selectall_arrayref(q{
1160 WHERE letter_code = 'HOLD'
1161 AND message_transport_type = 'print'
1163 return $message_count->[0]->[0];
1166 sub place_item_hold
{
1167 my ($patron,$item,$library,$priority) = @_;
1169 my $hold_id = C4
::Reserves
::AddReserve
(
1171 branchcode
=> $library->branchcode,
1172 borrowernumber
=> $patron->borrowernumber,
1173 biblionumber
=> $item->biblionumber,
1174 priority
=> $priority,
1175 title
=> "title for fee",
1176 itemnumber
=> $item->itemnumber,
1180 my $hold = Koha
::Holds
->find($hold_id);
1184 # we reached the finish
1185 $schema->storage->txn_rollback();