5 use Test::More tests => 18;
7 use DateTime::Duration;
18 use_ok('C4::Reserves');
21 # a very minimal mack of userenv for use by the test of DelItemCheck
22 *C4::Context::userenv = sub {
26 my $dbh = C4::Context->dbh;
29 $dbh->{AutoCommit} = 0;
30 $dbh->{RaiseError} = 1;
32 # Setup Test------------------------
34 # Add branches if not existing
35 foreach my $addbra ('CPL', 'FPL', 'RPL') {
36 $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES (?,?)", undef, ($addbra,"$addbra branch")) unless GetBranchName($addbra);
39 # Add categories if not existing
40 foreach my $addcat ('S', 'PT') {
41 $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
45 diag("\nCreating biblio instance for testing.");
46 my $bib = MARC::Record->new();
47 my $title = 'Silence in the library';
49 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
50 MARC::Field->new('245', ' ', ' ', a => $title),
52 my ($bibnum, $bibitemnum);
53 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
55 # Helper item for that biblio.
56 diag("Creating item instance for testing.");
57 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
59 # Modify item; setting barcode.
60 my $testbarcode = '97531';
61 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
65 firstname => 'my firstname',
66 surname => 'my surname',
70 my $borrowernumber = AddMember(%data);
71 my $borrower = GetMember( borrowernumber => $borrowernumber );
72 my $biblionumber = $bibnum;
73 my $barcode = $testbarcode;
81 my $checkitem = undef;
84 my @branches = GetBranchesLoop();
85 my $branch = $branches[0][0]{value};
87 AddReserve($branch, $borrowernumber, $biblionumber,
88 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
89 $title, $checkitem, $found);
91 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
93 is($status, "Reserved", "CheckReserves Test 1");
95 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
96 is($status, "Reserved", "CheckReserves Test 2");
98 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
99 is($status, "Reserved", "CheckReserves Test 3");
101 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
102 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
104 'ItemHomeLib' eq GetReservesControlBranch(
105 { homebranch => 'ItemHomeLib' },
106 { branchcode => 'PatronHomeLib' }
107 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
109 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
111 'PatronHomeLib' eq GetReservesControlBranch(
112 { homebranch => 'ItemHomeLib' },
113 { branchcode => 'PatronHomeLib' }
114 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
116 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
119 ### Regression test for bug 10272
122 $requesters{'CPL'} = AddMember(
124 categorycode => 'PT',
125 surname => 'borrower from CPL',
127 $requesters{'FPL'} = AddMember(
129 categorycode => 'PT',
130 surname => 'borrower from FPL',
132 $requesters{'RPL'} = AddMember(
134 categorycode => 'PT',
135 surname => 'borrower from RPL',
138 # Configure rules so that CPL allows only CPL patrons
139 # to request its items, while FPL will allow its items
140 # to fill holds from anywhere.
142 $dbh->do('DELETE FROM issuingrules');
143 $dbh->do('DELETE FROM branch_item_rules');
144 $dbh->do('DELETE FROM branch_borrower_circ_rules');
145 $dbh->do('DELETE FROM default_borrower_circ_rules');
146 $dbh->do('DELETE FROM default_branch_item_rules');
147 $dbh->do('DELETE FROM default_branch_circ_rules');
148 $dbh->do('DELETE FROM default_circ_rules');
150 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
151 VALUES (?, ?, ?, ?)},
156 # CPL allows only its own patrons to request its items
158 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
159 VALUES (?, ?, ?, ?)},
161 'CPL', 10, 1, 'homebranch',
164 # ... while FPL allows anybody to request its items
166 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
167 VALUES (?, ?, ?, ?)},
169 'FPL', 10, 2, 'homebranch',
172 # helper biblio for the bug 10272 regression test
173 my $bib2 = MARC::Record->new();
174 $bib2->append_fields(
175 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
176 MARC::Field->new('245', ' ', ' ', a => $title),
179 # create one item belonging to FPL and one belonging to CPL
180 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
181 my ($itemnum_cpl, $itemnum_fpl);
182 (undef, undef, $itemnum_cpl) = AddItem({
184 holdingbranch => 'CPL',
185 barcode => 'bug10272_CPL'
187 (undef, undef, $itemnum_fpl) = AddItem({
189 holdingbranch => 'FPL',
190 barcode => 'bug10272_FPL'
193 AddReserve('RPL', $requesters{'RPL'}, $bibnum2,
194 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
195 $title, $checkitem, $found);
196 AddReserve('FPL', $requesters{'FPL'}, $bibnum2,
197 $constraint, $bibitems, 2, $resdate, $expdate, $notes,
198 $title, $checkitem, $found);
199 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
200 $constraint, $bibitems, 3, $resdate, $expdate, $notes,
201 $title, $checkitem, $found);
203 # Ensure that the item's home library controls hold policy lookup
204 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
207 # Return the CPL item at FPL. The hold that should be triggered is
208 # the one placed by the CPL patron, as the other two patron's hold
209 # requests cannot be filled by that item per policy.
210 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
211 is( $messages->{ResFound}->{borrowernumber},
213 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
215 # Return the FPL item at FPL. The hold that should be triggered is
216 # the one placed by the RPL patron, as that patron is first in line
217 # and RPL imposes no restrictions on whose holds its items can fill.
218 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
219 is( $messages->{ResFound}->{borrowernumber},
221 'for generous library, its items fill first hold request in line (bug 10272)');
223 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
224 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
225 # Test 9761a: Add a reserve without date, CheckReserve should return it
226 $resdate= undef; #defaults to today in AddReserve
227 $expdate= undef; #no expdate
228 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
229 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
230 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
231 $title, $checkitem, $found);
232 ($status)=CheckReserves($itemnumber,undef,undef);
233 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
234 ($status)=CheckReserves($itemnumber,undef,7);
235 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
237 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
238 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
239 C4::Context->set_preference('AllowHoldDateInFuture', 1);
240 $resdate= dt_from_string();
241 $resdate->add_duration(DateTime::Duration->new(days => 4));
242 $resdate=output_pref($resdate);
243 $expdate= undef; #no expdate
244 AddReserve('CPL', $requesters{'CPL'}, $bibnum,
245 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
246 $title, $checkitem, $found);
247 ($status)=CheckReserves($itemnumber,undef,undef);
248 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
250 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
251 ($status)=CheckReserves($itemnumber,undef,3);
252 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
253 ($status)=CheckReserves($itemnumber,undef,4);
254 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
256 # Test 9761d: Check ResFound message of AddReturn for future hold
257 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
258 # In this test we do not need an issued item; it is just a 'checkin'
259 C4::Context->set_preference('ConfirmFutureHolds', 0);
260 (my $doreturn, $messages)= AddReturn('97531','CPL');
261 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
262 C4::Context->set_preference('ConfirmFutureHolds', 3);
263 ($doreturn, $messages)= AddReturn('97531','CPL');
264 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
265 C4::Context->set_preference('ConfirmFutureHolds', 7);
266 ($doreturn, $messages)= AddReturn('97531','CPL');
267 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
269 # End of tests for bug 9761 (ConfirmFutureHolds)
271 # test marking a hold as captured
272 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
275 DelItemCheck($dbh, $bibnum, $itemnumber),
277 'item that is capture to fill a hold cannot be deleted',
280 my $letter = ReserveSlip('CPL', $requesters{'CPL'}, $bibnum);
281 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');