Bug 18982: Use KOHA_INTRANET_URL instead of syspref staffClientBaseURL
[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 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
215 my $reserve2 = GetReserveInfo( $reserve->{'reserve_id'} );
216 ok( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" );
217
218
219 $holds = $biblio->holds;
220 $hold = $holds->next;
221 AlterPriority( 'top', $hold->reserve_id );
222 $reserve = GetReserve( $reserve->{'reserve_id'} );
223 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" );
224
225 AlterPriority( 'down', $reserve->{'reserve_id'} );
226 $reserve = GetReserve( $reserve->{'reserve_id'} );
227 is( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
228
229 AlterPriority( 'up', $reserve->{'reserve_id'} );
230 $reserve = GetReserve( $reserve->{'reserve_id'} );
231 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
232
233 AlterPriority( 'bottom', $reserve->{'reserve_id'} );
234 $reserve = GetReserve( $reserve->{'reserve_id'} );
235 is( $reserve->{'priority'}, '5', "Test AlterPriority(), move to bottom" );
236
237 # Regression test for bug 2394
238 #
239 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
240 # a patron is not permittedo to request an item whose homebranch (i.e.,
241 # owner of the item) is different from the patron's own library.
242 # However, if canreservefromotherbranches is turned ON, the patron can
243 # create such hold requests.
244 #
245 # Note that canreservefromotherbranches has no effect if
246 # IndependentBranches is OFF.
247
248 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
249 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
250   = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
251 $dbh->do('DELETE FROM issuingrules');
252 $dbh->do(
253     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
254       VALUES (?, ?, ?, ?, ?)},
255     {},
256     '*', '*', '*', 25, 99
257 );
258 $dbh->do(
259     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
260       VALUES (?, ?, ?, ?, ?)},
261     {},
262     '*', '*', 'CANNOT', 0, 99
263 );
264
265 # make sure some basic sysprefs are set
266 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
267 t::lib::Mocks::mock_preference('item-level_itypes', 1);
268
269 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
270 t::lib::Mocks::mock_preference('IndependentBranches', 0);
271 ok(
272     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
273     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
274 );
275
276 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
277 t::lib::Mocks::mock_preference('IndependentBranches', 1);
278 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
279 ok(
280     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
281     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
282 );
283
284 # ... unless canreservefromotherbranches is ON
285 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
286 ok(
287     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
288     '... unless canreservefromotherbranches is ON (bug 2394)'
289 );
290
291 # Regression test for bug 11336
292 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
293 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
294 AddReserve(
295     $branch_1,
296     $borrowernumbers[0],
297     $bibnum,
298     '',
299     1,
300 );
301
302 my $reserveid1 = C4::Reserves::GetReserveId(
303     {
304         biblionumber => $bibnum,
305         borrowernumber => $borrowernumbers[0]
306     }
307 );
308
309 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
310 AddReserve(
311     $branch_1,
312     $borrowernumbers[1],
313     $bibnum,
314     '',
315     2,
316 );
317 my $reserveid2 = C4::Reserves::GetReserveId(
318     {
319         biblionumber => $bibnum,
320         borrowernumber => $borrowernumbers[1]
321     }
322 );
323
324 CancelReserve({ reserve_id => $reserveid1 });
325
326 $reserve2 = GetReserve( $reserveid2 );
327 is( $reserve2->{priority}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
328
329 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
330 AddReserve(
331     $branch_1,
332     $borrowernumbers[0],
333     $bibnum,
334     '',
335     2,
336 );
337 my $reserveid3 = C4::Reserves::GetReserveId(
338     {
339         biblionumber => $bibnum,
340         borrowernumber => $borrowernumbers[0]
341     }
342 );
343
344 my $reserve3 = GetReserve( $reserveid3 );
345 is( $reserve3->{priority}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
346
347 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
348 $reserve3 = GetReserve( $reserveid3 );
349 is( $reserve3->{priority}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
350
351 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
352 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
353 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
354 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
355
356 $hold = Koha::Hold->new(
357     {
358         borrowernumber => $borrowernumbers[0],
359         itemnumber     => $itemnumber,
360         biblionumber   => $item_bibnum,
361     }
362 )->store();
363 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
364     'itemAlreadyOnHold',
365     "Patron cannot place a second item level hold for a given item" );
366 $hold->delete();
367
368 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
369 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
370 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
371
372 # Regression test for bug 9532
373 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
374 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
375 AddReserve(
376     $branch_1,
377     $borrowernumbers[0],
378     $bibnum,
379     '',
380     1,
381 );
382 is(
383     CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
384     "cannot request item if policy that matches on item-level item type forbids it"
385 );
386 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
387 ok(
388     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
389     "can request item if policy that matches on item type allows it"
390 );
391
392 t::lib::Mocks::mock_preference('item-level_itypes', 0);
393 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
394 ok(
395     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
396     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
397 );
398
399
400 # Test branch item rules
401
402 $dbh->do('DELETE FROM issuingrules');
403 $dbh->do(
404     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
405       VALUES (?, ?, ?, ?)},
406     {},
407     '*', '*', '*', 25
408 );
409 $dbh->do('DELETE FROM branch_item_rules');
410 $dbh->do('DELETE FROM default_branch_circ_rules');
411 $dbh->do('DELETE FROM default_branch_item_rules');
412 $dbh->do('DELETE FROM default_circ_rules');
413 $dbh->do(q{
414     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
415     VALUES (?, ?, ?, ?)
416 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
417 $dbh->do(q{
418     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
419     VALUES (?, ?, ?, ?)
420 }, {}, $branch_1, 'CAN', 1, 'homebranch');
421 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
422 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
423     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
424 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
425     "CanItemBeReserved should returns 'notReservable'");
426
427 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
428     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
429 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
430     'cannotReserveFromOtherBranches',
431     "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
432
433 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
434     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
435 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
436     "CanItemBeReserved should returns 'OK'");
437
438 # Bug 12632
439 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
440 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
441
442 $dbh->do('DELETE FROM reserves');
443 $dbh->do('DELETE FROM issues');
444 $dbh->do('DELETE FROM items');
445 $dbh->do('DELETE FROM biblio');
446
447 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
448 ( $item_bibnum, $item_bibitemnum, $itemnumber )
449     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
450
451 $dbh->do(
452     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
453       VALUES (?, ?, ?, ?, ?)},
454     {},
455     '*', '*', 'ONLY1', 1, 99
456 );
457 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
458     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
459
460 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
461
462 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
463     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
464
465
466 # Helper method to set up a Biblio.
467 sub create_helper_biblio {
468     my $itemtype = shift;
469     my $bib = MARC::Record->new();
470     my $title = 'Silence in the library';
471     $bib->append_fields(
472         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
473         MARC::Field->new('245', ' ', ' ', a => $title),
474         MARC::Field->new('942', ' ', ' ', c => $itemtype),
475     );
476     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
477 }