Bug 17085 - Specify libmojolicious-perl min version
[koha.git] / t / db_dependent / Holds.t
blob61b9e6e751cf826d3ab7808b81ae228c5e4c1640
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
8 use C4::Context;
10 use Test::More tests => 61;
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::Holds;
20 BEGIN {
21 use FindBin;
22 use lib $FindBin::Bin;
23 use_ok('C4::Reserves');
26 my $schema = Koha::Database->new->schema;
27 $schema->storage->txn_begin;
29 my $builder = t::lib::TestBuilder->new();
30 my $dbh = C4::Context->dbh;
32 # Create two random branches
33 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
34 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
36 my $category = $builder->build({ source => 'Category' });
38 my $borrowers_count = 5;
40 $dbh->do('DELETE FROM itemtypes');
41 $dbh->do('DELETE FROM reserves');
42 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
43 $insert_sth->execute('CAN');
44 $insert_sth->execute('CANNOT');
45 $insert_sth->execute('DUMMY');
46 $insert_sth->execute('ONLY1');
48 # Setup Test------------------------
49 # Create a biblio instance for testing
50 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
52 # Create item instance for testing.
53 my ($item_bibnum, $item_bibitemnum, $itemnumber)
54 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
56 # Create some borrowers
57 my @borrowernumbers;
58 foreach (1..$borrowers_count) {
59 my $borrowernumber = AddMember(
60 firstname => 'my firstname',
61 surname => 'my surname ' . $_,
62 categorycode => $category->{categorycode},
63 branchcode => $branch_1,
65 push @borrowernumbers, $borrowernumber;
68 my $biblionumber = $bibnum;
70 # Create five item level holds
71 foreach my $borrowernumber ( @borrowernumbers ) {
72 AddReserve(
73 $branch_1,
74 $borrowernumber,
75 $biblionumber,
76 my $bibitems = q{},
77 my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
78 my $resdate,
79 my $expdate,
80 my $notes = q{},
81 $title,
82 my $checkitem = $itemnumber,
83 my $found,
87 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber });
88 is( scalar(@$reserves), $borrowers_count, "Test GetReserves()" );
90 is( $reserves->[0]->{priority}, 1, "Reserve 1 has a priority of 1" );
91 is( $reserves->[1]->{priority}, 2, "Reserve 2 has a priority of 2" );
92 is( $reserves->[2]->{priority}, 3, "Reserve 3 has a priority of 3" );
93 is( $reserves->[3]->{priority}, 4, "Reserve 4 has a priority of 4" );
94 is( $reserves->[4]->{priority}, 5, "Reserve 5 has a priority of 5" );
96 my ( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber($itemnumber);
97 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "GetReservesFromItemnumber should return a valid reserve date");
98 is( $borrowernumber, $borrowernumbers[0], "GetReservesFromItemnumber should return a valid borrowernumber");
99 is( $branch_1code, $branch_1, "GetReservesFromItemnumber should return a valid branchcode");
100 ok($reserve_id, "Test GetReservesFromItemnumber()");
102 my $hold = Koha::Holds->find( $reserve_id );
103 ok( $hold, "Koha::Holds found the hold" );
104 my $hold_biblio = $hold->biblio();
105 ok( $hold_biblio, "Got biblio using biblio() method" );
106 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
107 my $hold_item = $hold->item();
108 ok( $hold_item, "Got item using item() method" );
109 ok( $hold_item == $hold->item(), "item method returns stashed item" );
110 my $hold_branch = $hold->branch();
111 ok( $hold_branch, "Got branch using branch() method" );
112 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
113 my $hold_found = $hold->found();
114 $hold->set({ found => 'W'})->store();
115 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
117 my ( $reserve ) = GetReservesFromBorrowernumber($borrowernumbers[0]);
118 ok( $reserve->{'borrowernumber'} eq $borrowernumbers[0], "Test GetReservesFromBorrowernumber()");
121 ok( GetReserveCount( $borrowernumbers[0] ), "Test GetReserveCount()" );
124 CancelReserve({ 'reserve_id' => $reserve_id });
125 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber });
126 is( scalar(@$reserves), $borrowers_count - 1, "Test CancelReserve()" );
129 ( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber($itemnumber);
130 ModReserve({
131 reserve_id => $reserve_id,
132 rank => '4',
133 branchcode => $branch_1,
134 itemnumber => $itemnumber,
135 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
138 $reserve = GetReserve( $reserve_id );
139 ok( $reserve->{'priority'} eq '4', "Test GetReserve(), priority changed correctly" );
140 ok( $reserve->{'suspend'}, "Test GetReserve(), suspend hold" );
141 is( $reserve->{'suspend_until'}, '2013-01-01 00:00:00', "Test GetReserve(), suspend until date" );
143 ToggleSuspend( $reserve_id );
144 $reserve = GetReserve( $reserve_id );
145 ok( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
147 ToggleSuspend( $reserve_id, '2012-01-01' );
148 $reserve = GetReserve( $reserve_id );
149 is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
151 AutoUnsuspendReserves();
152 $reserve = GetReserve( $reserve_id );
153 ok( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
155 SuspendAll(
156 borrowernumber => $borrowernumber,
157 biblionumber => $biblionumber,
158 suspend => 1,
159 suspend_until => '2012-01-01',
161 $reserve = GetReserve( $reserve_id );
162 is( $reserve->{'suspend'}, 1, "Test SuspendAll()" );
163 is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
165 SuspendAll(
166 borrowernumber => $borrowernumber,
167 biblionumber => $biblionumber,
168 suspend => 0,
170 $reserve = GetReserve( $reserve_id );
171 is( $reserve->{'suspend'}, 0, "Test resuming with SuspendAll()" );
172 is( $reserve->{'suspend_until'}, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
174 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
175 AddReserve(
176 $branch_1,
177 $borrowernumbers[0],
178 $biblionumber,
179 my $bibitems = q{},
180 my $priority,
181 my $resdate,
182 my $expdate,
183 my $notes = q{},
184 $title,
185 my $checkitem,
186 my $found,
188 ( $reserve ) = GetReservesFromBorrowernumber($borrowernumber);
189 my $reserveid = C4::Reserves::GetReserveId(
191 biblionumber => $biblionumber,
192 borrowernumber => $borrowernumber
195 is( $reserveid, $reserve->{reserve_id}, "Test GetReserveId" );
196 ModReserveMinusPriority( $itemnumber, $reserve->{'reserve_id'} );
197 ( $reserve ) = GetReservesFromBorrowernumber($borrowernumber);
198 ok( $reserve->{'itemnumber'} eq $itemnumber, "Test ModReserveMinusPriority()" );
201 my $reserve2 = GetReserveInfo( $reserve->{'reserve_id'} );
202 ok( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" );
205 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
206 $reserve = $reserves->[1];
207 AlterPriority( 'top', $reserve->{'reserve_id'} );
208 $reserve = GetReserve( $reserve->{'reserve_id'} );
209 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" );
211 AlterPriority( 'down', $reserve->{'reserve_id'} );
212 $reserve = GetReserve( $reserve->{'reserve_id'} );
213 is( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
215 AlterPriority( 'up', $reserve->{'reserve_id'} );
216 $reserve = GetReserve( $reserve->{'reserve_id'} );
217 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
219 AlterPriority( 'bottom', $reserve->{'reserve_id'} );
220 $reserve = GetReserve( $reserve->{'reserve_id'} );
221 is( $reserve->{'priority'}, '5', "Test AlterPriority(), move to bottom" );
223 # Regression test for bug 2394
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.
231 # Note that canreservefromotherbranches has no effect if
232 # IndependentBranches is OFF.
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 (?, ?, ?, ?, ?)},
242 '*', '*', '*', 25, 99
244 $dbh->do(
245 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
246 VALUES (?, ?, ?, ?, ?)},
248 '*', '*', 'CANNOT', 0, 99
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);
255 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
256 t::lib::Mocks::mock_preference('IndependentBranches', 0);
258 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
259 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
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);
266 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
267 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
270 # ... unless canreservefromotherbranches is ON
271 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
273 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
274 '... unless canreservefromotherbranches is ON (bug 2394)'
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,
288 my $reserveid1 = C4::Reserves::GetReserveId(
290 biblionumber => $bibnum,
291 borrowernumber => $borrowernumbers[0]
295 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
296 AddReserve(
297 $branch_1,
298 $borrowernumbers[1],
299 $bibnum,
303 my $reserveid2 = C4::Reserves::GetReserveId(
305 biblionumber => $bibnum,
306 borrowernumber => $borrowernumbers[1]
310 CancelReserve({ reserve_id => $reserveid1 });
312 $reserve2 = GetReserve( $reserveid2 );
313 is( $reserve2->{priority}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
315 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
316 AddReserve(
317 $branch_1,
318 $borrowernumbers[0],
319 $bibnum,
323 my $reserveid3 = C4::Reserves::GetReserveId(
325 biblionumber => $bibnum,
326 borrowernumber => $borrowernumbers[0]
330 my $reserve3 = GetReserve( $reserveid3 );
331 is( $reserve3->{priority}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
333 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
334 $reserve3 = GetReserve( $reserveid3 );
335 is( $reserve3->{priority}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
337 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
338 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
339 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
340 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
342 $hold = Koha::Hold->new(
344 borrowernumber => $borrowernumbers[0],
345 itemnumber => $itemnumber,
346 biblionumber => $item_bibnum,
348 )->store();
349 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
350 'itemAlreadyOnHold',
351 "Patron cannot place a second item level hold for a given item" );
352 $hold->delete();
354 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
355 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
356 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
358 # Regression test for bug 9532
359 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
360 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
361 AddReserve(
362 $branch_1,
363 $borrowernumbers[0],
364 $bibnum,
369 CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
370 "cannot request item if policy that matches on item-level item type forbids it"
372 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
374 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
375 "can request item if policy that matches on item type allows it"
378 t::lib::Mocks::mock_preference('item-level_itypes', 0);
379 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
381 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
382 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
386 # Test branch item rules
388 $dbh->do('DELETE FROM issuingrules');
389 $dbh->do(
390 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
391 VALUES (?, ?, ?, ?)},
393 '*', '*', '*', 25
395 $dbh->do('DELETE FROM branch_item_rules');
396 $dbh->do('DELETE FROM default_branch_circ_rules');
397 $dbh->do('DELETE FROM default_branch_item_rules');
398 $dbh->do('DELETE FROM default_circ_rules');
399 $dbh->do(q{
400 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
401 VALUES (?, ?, ?, ?)
402 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
403 $dbh->do(q{
404 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
405 VALUES (?, ?, ?, ?)
406 }, {}, $branch_1, 'CAN', 1, 'homebranch');
407 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
408 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
409 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
410 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
411 "CanItemBeReserved should returns 'notReservable'");
413 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
414 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
415 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
416 'cannotReserveFromOtherBranches',
417 "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
419 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
420 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
421 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
422 "CanItemBeReserved should returns 'OK'");
425 # Test CancelExpiredReserves
426 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
427 t::lib::Mocks::mock_preference('ReservesMaxPickUpDelay', 1);
429 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
430 $year += 1900;
431 $mon += 1;
432 $reserves = $dbh->selectall_arrayref('SELECT * FROM reserves', { Slice => {} });
433 $reserve = $reserves->[0];
434 my $calendar = C4::Calendar->new(branchcode => $reserve->{branchcode});
435 $calendar->insert_single_holiday(
436 day => $mday,
437 month => $mon,
438 year => $year,
439 title => 'Test',
440 description => 'Test',
442 $reserve_id = $reserve->{reserve_id};
443 $dbh->do("UPDATE reserves SET waitingdate = DATE_SUB( NOW(), INTERVAL 5 DAY ), found = 'W', priority = 0 WHERE reserve_id = ?", undef, $reserve_id );
444 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0);
445 CancelExpiredReserves();
446 my $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
447 is( $count, 1, "Waiting reserve beyond max pickup delay *not* canceled on holiday" );
448 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
449 CancelExpiredReserves();
450 $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
451 is( $count, 0, "Waiting reserve beyond max pickup delay canceled on holiday" );
453 # Test expirationdate
454 $reserve = $reserves->[1];
455 $reserve_id = $reserve->{reserve_id};
456 $dbh->do("UPDATE reserves SET expirationdate = DATE_SUB( NOW(), INTERVAL 1 DAY ) WHERE reserve_id = ?", undef, $reserve_id );
457 CancelExpiredReserves();
458 $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
459 is( $count, 0, "Reserve with manual expiration date canceled correctly" );
461 # Bug 12632
462 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
463 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
465 $dbh->do('DELETE FROM reserves');
466 $dbh->do('DELETE FROM issues');
467 $dbh->do('DELETE FROM items');
468 $dbh->do('DELETE FROM biblio');
470 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
471 ( $item_bibnum, $item_bibitemnum, $itemnumber )
472 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
474 $dbh->do(
475 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
476 VALUES (?, ?, ?, ?, ?)},
478 '*', '*', 'ONLY1', 1, 99
480 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
481 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
483 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
485 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
486 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
489 # Helper method to set up a Biblio.
490 sub create_helper_biblio {
491 my $itemtype = shift;
492 my $bib = MARC::Record->new();
493 my $title = 'Silence in the library';
494 $bib->append_fields(
495 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
496 MARC::Field->new('245', ' ', ' ', a => $title),
497 MARC::Field->new('942', ' ', ' ', c => $itemtype),
499 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');