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