Bug 9761: Unit tests for ConfirmFutureHolds changes
[koha.git] / t / db_dependent / Reserves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 16;
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 my $dbh = C4::Context->dbh;
22
23 # Start transaction
24 $dbh->{AutoCommit} = 0;
25 $dbh->{RaiseError} = 1;
26
27 # Setup Test------------------------
28
29 # Add branches if not existing
30 foreach my $addbra ('CPL', 'FPL', 'RPL') {
31     $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES (?,?)", undef, ($addbra,"$addbra branch")) unless GetBranchName($addbra);
32 }
33
34 # Add categories if not existing
35 foreach my $addcat ('S', 'PT') {
36     $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
37 }
38
39 # Helper biblio.
40 diag("\nCreating biblio instance for testing.");
41 my $bib = MARC::Record->new();
42 my $title = 'Silence in the library';
43 $bib->append_fields(
44     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
45     MARC::Field->new('245', ' ', ' ', a => $title),
46 );
47 my ($bibnum, $bibitemnum);
48 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
49
50 # Helper item for that biblio.
51 diag("Creating item instance for testing.");
52 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
53
54 # Modify item; setting barcode.
55 my $testbarcode = '97531';
56 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
57
58 # Create a borrower
59 my %data = (
60     firstname =>  'my firstname',
61     surname => 'my surname',
62     categorycode => 'S',
63     branchcode => 'CPL',
64 );
65 my $borrowernumber = AddMember(%data);
66 my $borrower = GetMember( borrowernumber => $borrowernumber );
67 my $biblionumber   = $bibnum;
68 my $barcode        = $testbarcode;
69
70 my $constraint     = 'a';
71 my $bibitems       = '';
72 my $priority       = '1';
73 my $resdate        = undef;
74 my $expdate        = undef;
75 my $notes          = '';
76 my $checkitem      = undef;
77 my $found          = undef;
78
79 my @branches = GetBranchesLoop();
80 my $branch = $branches[0][0]{value};
81
82 AddReserve($branch,    $borrowernumber, $biblionumber,
83         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
84         $title,      $checkitem, $found);
85
86 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
87
88 is($status, "Reserved", "CheckReserves Test 1");
89
90 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
91 is($status, "Reserved", "CheckReserves Test 2");
92
93 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
94 is($status, "Reserved", "CheckReserves Test 3");
95
96 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
97 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
98 ok(
99     'ItemHomeLib' eq GetReservesControlBranch(
100         { homebranch => 'ItemHomeLib' },
101         { branchcode => 'PatronHomeLib' }
102     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
103 );
104 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
105 ok(
106     'PatronHomeLib' eq GetReservesControlBranch(
107         { homebranch => 'ItemHomeLib' },
108         { branchcode => 'PatronHomeLib' }
109     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
110 );
111 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
112
113 ###
114 ### Regression test for bug 10272
115 ###
116 my %requesters = ();
117 $requesters{'CPL'} = AddMember(
118     branchcode   => 'CPL',
119     categorycode => 'PT',
120     surname      => 'borrower from CPL',
121 );
122 $requesters{'FPL'} = AddMember(
123     branchcode   => 'FPL',
124     categorycode => 'PT',
125     surname      => 'borrower from FPL',
126 );
127 $requesters{'RPL'} = AddMember(
128     branchcode   => 'RPL',
129     categorycode => 'PT',
130     surname      => 'borrower from RPL',
131 );
132
133 # Configure rules so that CPL allows only CPL patrons
134 # to request its items, while FPL will allow its items
135 # to fill holds from anywhere.
136
137 $dbh->do('DELETE FROM issuingrules');
138 $dbh->do('DELETE FROM branch_item_rules');
139 $dbh->do('DELETE FROM branch_borrower_circ_rules');
140 $dbh->do('DELETE FROM default_borrower_circ_rules');
141 $dbh->do('DELETE FROM default_branch_item_rules');
142 $dbh->do('DELETE FROM default_branch_circ_rules');
143 $dbh->do('DELETE FROM default_circ_rules');
144 $dbh->do(
145     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
146       VALUES (?, ?, ?, ?)},
147     {},
148     '*', '*', '*', 25
149 );
150
151 # CPL allows only its own patrons to request its items
152 $dbh->do(
153     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
154       VALUES (?, ?, ?, ?)},
155     {},
156     'CPL', 10, 1, 'homebranch',
157 );
158
159 # ... while FPL allows anybody to request its items
160 $dbh->do(
161     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
162       VALUES (?, ?, ?, ?)},
163     {},
164     'FPL', 10, 2, 'homebranch',
165 );
166
167 # helper biblio for the bug 10272 regression test
168 my $bib2 = MARC::Record->new();
169 $bib2->append_fields(
170     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
171     MARC::Field->new('245', ' ', ' ', a => $title),
172 );
173
174 # create one item belonging to FPL and one belonging to CPL
175 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
176 my ($itemnum_cpl, $itemnum_fpl);
177 (undef, undef, $itemnum_cpl) = AddItem({
178         homebranch => 'CPL',
179         holdingbranch => 'CPL',
180         barcode => 'bug10272_CPL'
181     } , $bibnum2);
182 (undef, undef, $itemnum_fpl) = AddItem({
183         homebranch => 'FPL',
184         holdingbranch => 'FPL',
185         barcode => 'bug10272_FPL'
186     } , $bibnum2);
187
188 AddReserve('RPL',  $requesters{'RPL'}, $bibnum2,
189            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
190            $title,      $checkitem, $found);
191 AddReserve('FPL',  $requesters{'FPL'}, $bibnum2,
192            $constraint, $bibitems,  2, $resdate, $expdate, $notes,
193            $title,      $checkitem, $found);
194 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
195            $constraint, $bibitems,  3, $resdate, $expdate, $notes,
196            $title,      $checkitem, $found);
197
198 # Ensure that the item's home library controls hold policy lookup
199 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
200
201 my $messages;
202 # Return the CPL item at FPL.  The hold that should be triggered is
203 # the one placed by the CPL patron, as the other two patron's hold
204 # requests cannot be filled by that item per policy.
205 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
206 is( $messages->{ResFound}->{borrowernumber},
207     $requesters{'CPL'},
208     'restrictive library\'s items only fill requests by own patrons (bug 10272)');
209
210 # Return the FPL item at FPL.  The hold that should be triggered is
211 # the one placed by the RPL patron, as that patron is first in line
212 # and RPL imposes no restrictions on whose holds its items can fill.
213 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
214 is( $messages->{ResFound}->{borrowernumber},
215     $requesters{'RPL'},
216     'for generous library, its items fill first hold request in line (bug 10272)');
217
218 # Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
219 # Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
220 # Test 9761a: Add a reserve without date, CheckReserve should return it
221 $resdate= undef; #defaults to today in AddReserve
222 $expdate= undef; #no expdate
223 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
224 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
225            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
226            $title,      $checkitem, $found);
227 ($status)=CheckReserves($itemnumber,undef,undef);
228 is( $status, 'Reserved', 'CheckReserves returns reserve without lookahead');
229 ($status)=CheckReserves($itemnumber,undef,7);
230 is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
231
232 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
233 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
234 C4::Context->set_preference('AllowHoldDateInFuture', 1);
235 $resdate= dt_from_string();
236 $resdate->add_duration(DateTime::Duration->new(days => 4));
237 $resdate=output_pref($resdate);
238 $expdate= undef; #no expdate
239 AddReserve('CPL',  $requesters{'CPL'}, $bibnum,
240            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
241            $title,      $checkitem, $found);
242 ($status)=CheckReserves($itemnumber,undef,undef);
243 is( $status, '', 'CheckReserves returns no future reserve without lookahead');
244
245 # Test 9761c: Add a reserve with future date, CheckReserve should return it if lookahead is high enough
246 ($status)=CheckReserves($itemnumber,undef,3);
247 is( $status, '', 'CheckReserves returns no future reserve with insufficient lookahead');
248 ($status)=CheckReserves($itemnumber,undef,4);
249 is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient lookahead');
250
251 # Test 9761d: Check ResFound message of AddReturn for future hold
252 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
253 # In this test we do not need an issued item; it is just a 'checkin'
254 C4::Context->set_preference('ConfirmFutureHolds', 0);
255 (my $doreturn, $messages)= AddReturn('97531','CPL');
256 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
257 C4::Context->set_preference('ConfirmFutureHolds', 3);
258 ($doreturn, $messages)= AddReturn('97531','CPL');
259 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
260 C4::Context->set_preference('ConfirmFutureHolds', 7);
261 ($doreturn, $messages)= AddReturn('97531','CPL');
262 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
263
264 # End of tests for bug 9761 (ConfirmFutureHolds)
265
266 $dbh->rollback;