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