Bug 11338: add unit tests for DelItemCheck
[koha.git] / t / db_dependent / Reserves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 18;
6 use MARC::Record;
7 use DateTime::Duration;
8
9 use C4::Branch;
10 use C4::Biblio;
11 use C4::Items;
12 use C4::Members;
13 use C4::Circulation;
14
15 use Koha::DateUtils;
16
17 BEGIN {
18     use_ok('C4::Reserves');
19 }
20
21 # a very minimal mack of userenv for use by the test of DelItemCheck
22 *C4::Context::userenv = sub {
23     return {};
24 };
25
26 my $dbh = C4::Context->dbh;
27
28 # Start transaction
29 $dbh->{AutoCommit} = 0;
30 $dbh->{RaiseError} = 1;
31
32 # Setup Test------------------------
33
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);
37 }
38
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);
42 }
43
44 # Helper biblio.
45 diag("\nCreating biblio instance for testing.");
46 my $bib = MARC::Record->new();
47 my $title = 'Silence in the library';
48 $bib->append_fields(
49     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
50     MARC::Field->new('245', ' ', ' ', a => $title),
51 );
52 my ($bibnum, $bibitemnum);
53 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
54
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);
58
59 # Modify item; setting barcode.
60 my $testbarcode = '97531';
61 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
62
63 # Create a borrower
64 my %data = (
65     firstname =>  'my firstname',
66     surname => 'my surname',
67     categorycode => 'S',
68     branchcode => 'CPL',
69 );
70 my $borrowernumber = AddMember(%data);
71 my $borrower = GetMember( borrowernumber => $borrowernumber );
72 my $biblionumber   = $bibnum;
73 my $barcode        = $testbarcode;
74
75 my $constraint     = 'a';
76 my $bibitems       = '';
77 my $priority       = '1';
78 my $resdate        = undef;
79 my $expdate        = undef;
80 my $notes          = '';
81 my $checkitem      = undef;
82 my $found          = undef;
83
84 my @branches = GetBranchesLoop();
85 my $branch = $branches[0][0]{value};
86
87 AddReserve($branch,    $borrowernumber, $biblionumber,
88         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
89         $title,      $checkitem, $found);
90
91 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
92
93 is($status, "Reserved", "CheckReserves Test 1");
94
95 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
96 is($status, "Reserved", "CheckReserves Test 2");
97
98 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
99 is($status, "Reserved", "CheckReserves Test 3");
100
101 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
102 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
103 ok(
104     'ItemHomeLib' eq GetReservesControlBranch(
105         { homebranch => 'ItemHomeLib' },
106         { branchcode => 'PatronHomeLib' }
107     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
108 );
109 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
110 ok(
111     'PatronHomeLib' eq GetReservesControlBranch(
112         { homebranch => 'ItemHomeLib' },
113         { branchcode => 'PatronHomeLib' }
114     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
115 );
116 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
117
118 ###
119 ### Regression test for bug 10272
120 ###
121 my %requesters = ();
122 $requesters{'CPL'} = AddMember(
123     branchcode   => 'CPL',
124     categorycode => 'PT',
125     surname      => 'borrower from CPL',
126 );
127 $requesters{'FPL'} = AddMember(
128     branchcode   => 'FPL',
129     categorycode => 'PT',
130     surname      => 'borrower from FPL',
131 );
132 $requesters{'RPL'} = AddMember(
133     branchcode   => 'RPL',
134     categorycode => 'PT',
135     surname      => 'borrower from RPL',
136 );
137
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.
141
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');
149 $dbh->do(
150     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
151       VALUES (?, ?, ?, ?)},
152     {},
153     '*', '*', '*', 25
154 );
155
156 # CPL allows only its own patrons to request its items
157 $dbh->do(
158     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
159       VALUES (?, ?, ?, ?)},
160     {},
161     'CPL', 10, 1, 'homebranch',
162 );
163
164 # ... while FPL allows anybody to request its items
165 $dbh->do(
166     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
167       VALUES (?, ?, ?, ?)},
168     {},
169     'FPL', 10, 2, 'homebranch',
170 );
171
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),
177 );
178
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({
183         homebranch => 'CPL',
184         holdingbranch => 'CPL',
185         barcode => 'bug10272_CPL'
186     } , $bibnum2);
187 (undef, undef, $itemnum_fpl) = AddItem({
188         homebranch => 'FPL',
189         holdingbranch => 'FPL',
190         barcode => 'bug10272_FPL'
191     } , $bibnum2);
192
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);
202
203 # Ensure that the item's home library controls hold policy lookup
204 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
205
206 my $messages;
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},
212     $requesters{'CPL'},
213     'restrictive library\'s items only fill requests by own patrons (bug 10272)');
214
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},
220     $requesters{'RPL'},
221     'for generous library, its items fill first hold request in line (bug 10272)');
222
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');
236
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');
249
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');
255
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');
268
269 # End of tests for bug 9761 (ConfirmFutureHolds)
270
271 # test marking a hold as captured
272 ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
273
274 is(
275     DelItemCheck($dbh, $bibnum, $itemnumber),
276     'book_reserved',
277     'item that is capture to fill a hold cannot be deleted',
278 );
279
280 my $letter = ReserveSlip('CPL', $requesters{'CPL'}, $bibnum);
281 ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
282
283 $dbh->rollback;