584acb281d2e1acc43bf7951ed5e9cbe84e1c30a
[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 => 57;
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
125 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
126 $holds = $patron->holds;
127 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
128
129
130 ok( GetReserveCount( $borrowernumbers[0] ), "Test GetReserveCount()" );
131
132
133 CancelReserve({ 'reserve_id' => $reserve_id });
134 $holds = $biblio->holds;
135 is( $holds->count, $borrowers_count - 1, "Test CancelReserve()" );
136
137 $holds = $item->current_holds;
138 $first_hold = $holds->next;
139 $borrowernumber = $first_hold->borrowernumber;
140 $branch_1code = $first_hold->branchcode;
141 $reserve_id = $first_hold->reserve_id;
142
143 ModReserve({
144     reserve_id    => $reserve_id,
145     rank          => '4',
146     branchcode    => $branch_1,
147     itemnumber    => $itemnumber,
148     suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
149 });
150
151 my $reserve = GetReserve( $reserve_id );
152 ok( $reserve->{'priority'} eq '4', "Test GetReserve(), priority changed correctly" );
153 ok( $reserve->{'suspend'}, "Test GetReserve(), suspend hold" );
154 is( $reserve->{'suspend_until'}, '2013-01-01 00:00:00', "Test GetReserve(), suspend until date" );
155
156 ToggleSuspend( $reserve_id );
157 $reserve = GetReserve( $reserve_id );
158 ok( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
159
160 ToggleSuspend( $reserve_id, '2012-01-01' );
161 $reserve = GetReserve( $reserve_id );
162 is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
163
164 AutoUnsuspendReserves();
165 $reserve = GetReserve( $reserve_id );
166 ok( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
167
168 SuspendAll(
169     borrowernumber => $borrowernumber,
170     biblionumber   => $biblionumber,
171     suspend => 1,
172     suspend_until => '2012-01-01',
173 );
174 $reserve = GetReserve( $reserve_id );
175 is( $reserve->{'suspend'}, 1, "Test SuspendAll()" );
176 is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
177
178 SuspendAll(
179     borrowernumber => $borrowernumber,
180     biblionumber   => $biblionumber,
181     suspend => 0,
182 );
183 $reserve = GetReserve( $reserve_id );
184 is( $reserve->{'suspend'}, 0, "Test resuming with SuspendAll()" );
185 is( $reserve->{'suspend_until'}, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
186
187 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
188 AddReserve(
189     $branch_1,
190     $borrowernumbers[0],
191     $biblionumber,
192     my $bibitems = q{},
193     my $priority,
194     my $resdate,
195     my $expdate,
196     my $notes = q{},
197     $title,
198     my $checkitem,
199     my $found,
200 );
201 $patron = Koha::Patrons->find( $borrowernumber );
202 $holds = $patron->holds;
203 my $reserveid = C4::Reserves::GetReserveId(
204     {
205         biblionumber => $biblionumber,
206         borrowernumber => $borrowernumber
207     }
208 );
209 is( $reserveid, $holds->next->reserve_id, "Test GetReserveId" );
210 ModReserveMinusPriority( $itemnumber, $reserve->{'reserve_id'} );
211 $holds = $patron->holds;
212 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
213
214 $holds = $biblio->holds;
215 $hold = $holds->next;
216 AlterPriority( 'top', $hold->reserve_id );
217 $reserve = GetReserve( $reserve->{'reserve_id'} );
218 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" );
219
220 AlterPriority( 'down', $reserve->{'reserve_id'} );
221 $reserve = GetReserve( $reserve->{'reserve_id'} );
222 is( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
223
224 AlterPriority( 'up', $reserve->{'reserve_id'} );
225 $reserve = GetReserve( $reserve->{'reserve_id'} );
226 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
227
228 AlterPriority( 'bottom', $reserve->{'reserve_id'} );
229 $reserve = GetReserve( $reserve->{'reserve_id'} );
230 is( $reserve->{'priority'}, '5', "Test AlterPriority(), move to bottom" );
231
232 # Regression test for bug 2394
233 #
234 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
235 # a patron is not permittedo to request an item whose homebranch (i.e.,
236 # owner of the item) is different from the patron's own library.
237 # However, if canreservefromotherbranches is turned ON, the patron can
238 # create such hold requests.
239 #
240 # Note that canreservefromotherbranches has no effect if
241 # IndependentBranches is OFF.
242
243 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
244 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
245   = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
246 $dbh->do('DELETE FROM issuingrules');
247 $dbh->do(
248     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
249       VALUES (?, ?, ?, ?, ?)},
250     {},
251     '*', '*', '*', 25, 99
252 );
253 $dbh->do(
254     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
255       VALUES (?, ?, ?, ?, ?)},
256     {},
257     '*', '*', 'CANNOT', 0, 99
258 );
259
260 # make sure some basic sysprefs are set
261 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
262 t::lib::Mocks::mock_preference('item-level_itypes', 1);
263
264 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
265 t::lib::Mocks::mock_preference('IndependentBranches', 0);
266 ok(
267     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
268     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
269 );
270
271 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
272 t::lib::Mocks::mock_preference('IndependentBranches', 1);
273 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
274 ok(
275     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
276     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
277 );
278
279 # ... unless canreservefromotherbranches is ON
280 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
281 ok(
282     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
283     '... unless canreservefromotherbranches is ON (bug 2394)'
284 );
285
286 # Regression test for bug 11336
287 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
288 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
289 AddReserve(
290     $branch_1,
291     $borrowernumbers[0],
292     $bibnum,
293     '',
294     1,
295 );
296
297 my $reserveid1 = C4::Reserves::GetReserveId(
298     {
299         biblionumber => $bibnum,
300         borrowernumber => $borrowernumbers[0]
301     }
302 );
303
304 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
305 AddReserve(
306     $branch_1,
307     $borrowernumbers[1],
308     $bibnum,
309     '',
310     2,
311 );
312 my $reserveid2 = C4::Reserves::GetReserveId(
313     {
314         biblionumber => $bibnum,
315         borrowernumber => $borrowernumbers[1]
316     }
317 );
318
319 CancelReserve({ reserve_id => $reserveid1 });
320
321 my $reserve2 = GetReserve( $reserveid2 );
322 is( $reserve2->{priority}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
323
324 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
325 AddReserve(
326     $branch_1,
327     $borrowernumbers[0],
328     $bibnum,
329     '',
330     2,
331 );
332 my $reserveid3 = C4::Reserves::GetReserveId(
333     {
334         biblionumber => $bibnum,
335         borrowernumber => $borrowernumbers[0]
336     }
337 );
338
339 my $reserve3 = GetReserve( $reserveid3 );
340 is( $reserve3->{priority}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
341
342 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
343 $reserve3 = GetReserve( $reserveid3 );
344 is( $reserve3->{priority}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
345
346 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
347 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
348 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
349 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
350
351 $hold = Koha::Hold->new(
352     {
353         borrowernumber => $borrowernumbers[0],
354         itemnumber     => $itemnumber,
355         biblionumber   => $item_bibnum,
356     }
357 )->store();
358 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
359     'itemAlreadyOnHold',
360     "Patron cannot place a second item level hold for a given item" );
361 $hold->delete();
362
363 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
364 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
365 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
366
367 # Regression test for bug 9532
368 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
369 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
370 AddReserve(
371     $branch_1,
372     $borrowernumbers[0],
373     $bibnum,
374     '',
375     1,
376 );
377 is(
378     CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
379     "cannot request item if policy that matches on item-level item type forbids it"
380 );
381 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
382 ok(
383     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
384     "can request item if policy that matches on item type allows it"
385 );
386
387 t::lib::Mocks::mock_preference('item-level_itypes', 0);
388 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
389 ok(
390     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
391     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
392 );
393
394
395 # Test branch item rules
396
397 $dbh->do('DELETE FROM issuingrules');
398 $dbh->do(
399     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
400       VALUES (?, ?, ?, ?)},
401     {},
402     '*', '*', '*', 25
403 );
404 $dbh->do('DELETE FROM branch_item_rules');
405 $dbh->do('DELETE FROM default_branch_circ_rules');
406 $dbh->do('DELETE FROM default_branch_item_rules');
407 $dbh->do('DELETE FROM default_circ_rules');
408 $dbh->do(q{
409     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
410     VALUES (?, ?, ?, ?)
411 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
412 $dbh->do(q{
413     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
414     VALUES (?, ?, ?, ?)
415 }, {}, $branch_1, 'CAN', 1, 'homebranch');
416 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
417 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
418     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
419 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
420     "CanItemBeReserved should returns 'notReservable'");
421
422 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
423     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
424 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
425     'cannotReserveFromOtherBranches',
426     "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
427
428 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
429     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
430 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
431     "CanItemBeReserved should returns 'OK'");
432
433 # Bug 12632
434 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
435 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
436
437 $dbh->do('DELETE FROM reserves');
438 $dbh->do('DELETE FROM issues');
439 $dbh->do('DELETE FROM items');
440 $dbh->do('DELETE FROM biblio');
441
442 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
443 ( $item_bibnum, $item_bibitemnum, $itemnumber )
444     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
445
446 $dbh->do(
447     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
448       VALUES (?, ?, ?, ?, ?)},
449     {},
450     '*', '*', 'ONLY1', 1, 99
451 );
452 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
453     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
454
455 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
456
457 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
458     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
459
460
461 # Helper method to set up a Biblio.
462 sub create_helper_biblio {
463     my $itemtype = shift;
464     my $bib = MARC::Record->new();
465     my $title = 'Silence in the library';
466     $bib->append_fields(
467         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
468         MARC::Field->new('245', ' ', ' ', a => $title),
469         MARC::Field->new('942', ' ', ' ', c => $itemtype),
470     );
471     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
472 }