Bug 16187: Add a script to cancel unfilled holds after a specified number of days
[koha.git] / t / db_dependent / Holds.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
7
8 use C4::Context;
9
10 use Test::More tests => 54;
11 use MARC::Record;
12 use C4::Biblio;
13 use C4::Items;
14 use C4::Members;
15 use C4::Calendar;
16 use Koha::Database;
17 use Koha::DateUtils qw( dt_from_string output_pref );
18 use Koha::Biblios;
19 use Koha::Holds;
20 use Koha::Patrons;
21
22 BEGIN {
23     use FindBin;
24     use lib $FindBin::Bin;
25     use_ok('C4::Reserves');
26 }
27
28 my $schema  = Koha::Database->new->schema;
29 $schema->storage->txn_begin;
30
31 my $builder = t::lib::TestBuilder->new();
32 my $dbh     = C4::Context->dbh;
33
34 # Create two random branches
35 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
36 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
37
38 my $category = $builder->build({ source => 'Category' });
39
40 my $borrowers_count = 5;
41
42 $dbh->do('DELETE FROM itemtypes');
43 $dbh->do('DELETE FROM reserves');
44 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
45 $insert_sth->execute('CAN');
46 $insert_sth->execute('CANNOT');
47 $insert_sth->execute('DUMMY');
48 $insert_sth->execute('ONLY1');
49
50 # Setup Test------------------------
51 # Create a biblio instance for testing
52 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
53
54 # Create item instance for testing.
55 my ($item_bibnum, $item_bibitemnum, $itemnumber)
56     = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
57
58 # Create some borrowers
59 my @borrowernumbers;
60 foreach (1..$borrowers_count) {
61     my $borrowernumber = AddMember(
62         firstname =>  'my firstname',
63         surname => 'my surname ' . $_,
64         categorycode => $category->{categorycode},
65         branchcode => $branch_1,
66     );
67     push @borrowernumbers, $borrowernumber;
68 }
69
70 my $biblionumber = $bibnum;
71
72 # Create five item level holds
73 foreach my $borrowernumber ( @borrowernumbers ) {
74     AddReserve(
75         $branch_1,
76         $borrowernumber,
77         $biblionumber,
78         my $bibitems = q{},
79         my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
80         my $resdate,
81         my $expdate,
82         my $notes = q{},
83         $title,
84         my $checkitem = $itemnumber,
85         my $found,
86     );
87 }
88
89 my $biblio = Koha::Biblios->find( $biblionumber );
90 my $holds = $biblio->holds;
91 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
92 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
93 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
94 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
95 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
96 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
97
98 my $item = Koha::Items->find( $itemnumber );
99 $holds = $item->current_holds;
100 my $first_hold = $holds->next;
101 my $reservedate = $first_hold->reservedate;
102 my $borrowernumber = $first_hold->borrowernumber;
103 my $branch_1code = $first_hold->branchcode;
104 my $reserve_id = $first_hold->reserve_id;
105 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
106 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
107 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
108 ok($reserve_id, "Test holds_placed_today()");
109
110 my $hold = Koha::Holds->find( $reserve_id );
111 ok( $hold, "Koha::Holds found the hold" );
112 my $hold_biblio = $hold->biblio();
113 ok( $hold_biblio, "Got biblio using biblio() method" );
114 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
115 my $hold_item = $hold->item();
116 ok( $hold_item, "Got item using item() method" );
117 ok( $hold_item == $hold->item(), "item method returns stashed item" );
118 my $hold_branch = $hold->branch();
119 ok( $hold_branch, "Got branch using branch() method" );
120 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
121 my $hold_found = $hold->found();
122 $hold->set({ found => 'W'})->store();
123 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
124 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
125
126 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
127 $holds = $patron->holds;
128 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
129
130
131 $holds = $item->current_holds;
132 $first_hold = $holds->next;
133 $borrowernumber = $first_hold->borrowernumber;
134 $branch_1code = $first_hold->branchcode;
135 $reserve_id = $first_hold->reserve_id;
136
137 ModReserve({
138     reserve_id    => $reserve_id,
139     rank          => '4',
140     branchcode    => $branch_1,
141     itemnumber    => $itemnumber,
142     suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
143 });
144
145 $hold = Koha::Holds->find( $reserve_id );
146 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
147 ok( $hold->suspend, "Test ModReserve, suspend hold" );
148 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
149
150 ToggleSuspend( $reserve_id );
151 $hold = Koha::Holds->find( $reserve_id );
152 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
153
154 ToggleSuspend( $reserve_id, '2012-01-01' );
155 $hold = Koha::Holds->find( $reserve_id );
156 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
157
158 AutoUnsuspendReserves();
159 $hold = Koha::Holds->find( $reserve_id );
160 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
161
162 SuspendAll(
163     borrowernumber => $borrowernumber,
164     biblionumber   => $biblionumber,
165     suspend => 1,
166     suspend_until => '2012-01-01',
167 );
168 $hold = Koha::Holds->find( $reserve_id );
169 is( $hold->suspend, 1, "Test SuspendAll()" );
170 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
171
172 SuspendAll(
173     borrowernumber => $borrowernumber,
174     biblionumber   => $biblionumber,
175     suspend => 0,
176 );
177 $hold = Koha::Holds->find( $reserve_id );
178 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
179 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
180
181 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
182 AddReserve(
183     $branch_1,
184     $borrowernumbers[0],
185     $biblionumber,
186     my $bibitems = q{},
187     my $priority,
188     my $resdate,
189     my $expdate,
190     my $notes = q{},
191     $title,
192     my $checkitem,
193     my $found,
194 );
195 $patron = Koha::Patrons->find( $borrowernumber );
196 $holds = $patron->holds;
197 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
198 ModReserveMinusPriority( $itemnumber, $reserveid );
199 $holds = $patron->holds;
200 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
201
202 $holds = $biblio->holds;
203 $hold = $holds->next;
204 AlterPriority( 'top', $hold->reserve_id );
205 $hold = Koha::Holds->find( $reserveid );
206 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
207
208 AlterPriority( 'down', $hold->reserve_id );
209 $hold = Koha::Holds->find( $reserveid );
210 is( $hold->priority, '2', "Test AlterPriority(), move down" );
211
212 AlterPriority( 'up', $hold->reserve_id );
213 $hold = Koha::Holds->find( $reserveid );
214 is( $hold->priority, '1', "Test AlterPriority(), move up" );
215
216 AlterPriority( 'bottom', $hold->reserve_id );
217 $hold = Koha::Holds->find( $reserveid );
218 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
219
220 # Regression test for bug 2394
221 #
222 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
223 # a patron is not permittedo to request an item whose homebranch (i.e.,
224 # owner of the item) is different from the patron's own library.
225 # However, if canreservefromotherbranches is turned ON, the patron can
226 # create such hold requests.
227 #
228 # Note that canreservefromotherbranches has no effect if
229 # IndependentBranches is OFF.
230
231 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
232 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
233   = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
234 $dbh->do('DELETE FROM issuingrules');
235 $dbh->do(
236     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
237       VALUES (?, ?, ?, ?, ?)},
238     {},
239     '*', '*', '*', 25, 99
240 );
241 $dbh->do(
242     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
243       VALUES (?, ?, ?, ?, ?)},
244     {},
245     '*', '*', 'CANNOT', 0, 99
246 );
247
248 # make sure some basic sysprefs are set
249 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
250 t::lib::Mocks::mock_preference('item-level_itypes', 1);
251
252 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
253 t::lib::Mocks::mock_preference('IndependentBranches', 0);
254 ok(
255     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
256     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
257 );
258
259 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
260 t::lib::Mocks::mock_preference('IndependentBranches', 1);
261 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
262 ok(
263     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
264     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
265 );
266
267 # ... unless canreservefromotherbranches is ON
268 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
269 ok(
270     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
271     '... unless canreservefromotherbranches is ON (bug 2394)'
272 );
273
274 {
275     # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
276     ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
277     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
278     my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
279     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
280     my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
281     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
282     my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
283     my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
284     my $hold3 = Koha::Holds->find( $reserveid3 );
285     is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
286     ModReserve({ reserve_id => $reserveid1, rank => 'del' });
287     ModReserve({ reserve_id => $reserveid2, rank => 'del' });
288     is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
289 }
290
291 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
292 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
293 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
294 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
295
296 $hold = Koha::Hold->new(
297     {
298         borrowernumber => $borrowernumbers[0],
299         itemnumber     => $itemnumber,
300         biblionumber   => $item_bibnum,
301     }
302 )->store();
303 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
304     'itemAlreadyOnHold',
305     "Patron cannot place a second item level hold for a given item" );
306 $hold->delete();
307
308 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
309 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
310 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
311
312 # Regression test for bug 9532
313 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
314 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
315 AddReserve(
316     $branch_1,
317     $borrowernumbers[0],
318     $bibnum,
319     '',
320     1,
321 );
322 is(
323     CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
324     "cannot request item if policy that matches on item-level item type forbids it"
325 );
326 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
327 ok(
328     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
329     "can request item if policy that matches on item type allows it"
330 );
331
332 t::lib::Mocks::mock_preference('item-level_itypes', 0);
333 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
334 ok(
335     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
336     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
337 );
338
339
340 # Test branch item rules
341
342 $dbh->do('DELETE FROM issuingrules');
343 $dbh->do(
344     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
345       VALUES (?, ?, ?, ?)},
346     {},
347     '*', '*', '*', 25
348 );
349 $dbh->do('DELETE FROM branch_item_rules');
350 $dbh->do('DELETE FROM default_branch_circ_rules');
351 $dbh->do('DELETE FROM default_branch_item_rules');
352 $dbh->do('DELETE FROM default_circ_rules');
353 $dbh->do(q{
354     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
355     VALUES (?, ?, ?, ?)
356 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
357 $dbh->do(q{
358     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
359     VALUES (?, ?, ?, ?)
360 }, {}, $branch_1, 'CAN', 1, 'homebranch');
361 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
362 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
363     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
364 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
365     "CanItemBeReserved should returns 'notReservable'");
366
367 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
368     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
369 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
370     'cannotReserveFromOtherBranches',
371     "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
372
373 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
374     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
375 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
376     "CanItemBeReserved should returns 'OK'");
377
378 # Bug 12632
379 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
380 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
381
382 $dbh->do('DELETE FROM reserves');
383 $dbh->do('DELETE FROM issues');
384 $dbh->do('DELETE FROM items');
385 $dbh->do('DELETE FROM biblio');
386
387 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
388 ( $item_bibnum, $item_bibitemnum, $itemnumber )
389     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
390
391 $dbh->do(
392     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
393       VALUES (?, ?, ?, ?, ?)},
394     {},
395     '*', '*', 'ONLY1', 1, 99
396 );
397 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
398     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
399
400 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
401
402 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
403     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
404
405
406 # Helper method to set up a Biblio.
407 sub create_helper_biblio {
408     my $itemtype = shift;
409     my $bib = MARC::Record->new();
410     my $title = 'Silence in the library';
411     $bib->append_fields(
412         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
413         MARC::Field->new('245', ' ', ' ', a => $title),
414         MARC::Field->new('942', ' ', ' ', c => $itemtype),
415     );
416     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
417 }