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