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