Bug 18624: Regression test for 'any' vs 'all'
[koha.git] / t / db_dependent / Holds.t
blobad87b2612902a2c6b98d623bfa0bc8f036e2978f
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 => 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;
22 BEGIN {
23 use FindBin;
24 use lib $FindBin::Bin;
25 use_ok('C4::Reserves');
28 my $schema = Koha::Database->new->schema;
29 $schema->storage->txn_begin;
31 my $builder = t::lib::TestBuilder->new();
32 my $dbh = C4::Context->dbh;
34 # Create two random branches
35 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
36 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
38 my $category = $builder->build({ source => 'Category' });
40 my $borrowers_count = 5;
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');
50 # Setup Test------------------------
51 # Create a biblio instance for testing
52 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
54 # Create item instance for testing.
55 my ($item_bibnum, $item_bibitemnum, $itemnumber)
56 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
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,
67 push @borrowernumbers, $borrowernumber;
70 my $biblionumber = $bibnum;
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,
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" );
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()");
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" );
125 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
126 $holds = $patron->holds;
127 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
130 ok( GetReserveCount( $borrowernumbers[0] ), "Test GetReserveCount()" );
133 CancelReserve({ 'reserve_id' => $reserve_id });
134 $holds = $biblio->holds;
135 is( $holds->count, $borrowers_count - 1, "Test CancelReserve()" );
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;
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 } ),
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" );
156 ToggleSuspend( $reserve_id );
157 $reserve = GetReserve( $reserve_id );
158 ok( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
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" );
164 AutoUnsuspendReserves();
165 $reserve = GetReserve( $reserve_id );
166 ok( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
168 SuspendAll(
169 borrowernumber => $borrowernumber,
170 biblionumber => $biblionumber,
171 suspend => 1,
172 suspend_until => '2012-01-01',
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" );
178 SuspendAll(
179 borrowernumber => $borrowernumber,
180 biblionumber => $biblionumber,
181 suspend => 0,
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" );
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,
201 $patron = Koha::Patrons->find( $borrowernumber );
202 $holds = $patron->holds;
203 my $reserveid = C4::Reserves::GetReserveId(
205 biblionumber => $biblionumber,
206 borrowernumber => $borrowernumber
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()" );
215 my $reserve2 = GetReserveInfo( $reserve->{'reserve_id'} );
216 ok( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" );
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" );
225 AlterPriority( 'down', $reserve->{'reserve_id'} );
226 $reserve = GetReserve( $reserve->{'reserve_id'} );
227 is( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
229 AlterPriority( 'up', $reserve->{'reserve_id'} );
230 $reserve = GetReserve( $reserve->{'reserve_id'} );
231 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
233 AlterPriority( 'bottom', $reserve->{'reserve_id'} );
234 $reserve = GetReserve( $reserve->{'reserve_id'} );
235 is( $reserve->{'priority'}, '5', "Test AlterPriority(), move to bottom" );
237 # Regression test for bug 2394
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.
245 # Note that canreservefromotherbranches has no effect if
246 # IndependentBranches is OFF.
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 (?, ?, ?, ?, ?)},
256 '*', '*', '*', 25, 99
258 $dbh->do(
259 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
260 VALUES (?, ?, ?, ?, ?)},
262 '*', '*', 'CANNOT', 0, 99
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);
269 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
270 t::lib::Mocks::mock_preference('IndependentBranches', 0);
272 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
273 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
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);
280 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
281 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
284 # ... unless canreservefromotherbranches is ON
285 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
287 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
288 '... unless canreservefromotherbranches is ON (bug 2394)'
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,
302 my $reserveid1 = C4::Reserves::GetReserveId(
304 biblionumber => $bibnum,
305 borrowernumber => $borrowernumbers[0]
309 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
310 AddReserve(
311 $branch_1,
312 $borrowernumbers[1],
313 $bibnum,
317 my $reserveid2 = C4::Reserves::GetReserveId(
319 biblionumber => $bibnum,
320 borrowernumber => $borrowernumbers[1]
324 CancelReserve({ reserve_id => $reserveid1 });
326 $reserve2 = GetReserve( $reserveid2 );
327 is( $reserve2->{priority}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
329 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
330 AddReserve(
331 $branch_1,
332 $borrowernumbers[0],
333 $bibnum,
337 my $reserveid3 = C4::Reserves::GetReserveId(
339 biblionumber => $bibnum,
340 borrowernumber => $borrowernumbers[0]
344 my $reserve3 = GetReserve( $reserveid3 );
345 is( $reserve3->{priority}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
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" );
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" );
356 $hold = Koha::Hold->new(
358 borrowernumber => $borrowernumbers[0],
359 itemnumber => $itemnumber,
360 biblionumber => $item_bibnum,
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();
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" );
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,
383 CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
384 "cannot request item if policy that matches on item-level item type forbids it"
386 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
388 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
389 "can request item if policy that matches on item type allows it"
392 t::lib::Mocks::mock_preference('item-level_itypes', 0);
393 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
395 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
396 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
400 # Test branch item rules
402 $dbh->do('DELETE FROM issuingrules');
403 $dbh->do(
404 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
405 VALUES (?, ?, ?, ?)},
407 '*', '*', '*', 25
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'");
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'");
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'");
438 # Bug 12632
439 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
440 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
442 $dbh->do('DELETE FROM reserves');
443 $dbh->do('DELETE FROM issues');
444 $dbh->do('DELETE FROM items');
445 $dbh->do('DELETE FROM biblio');
447 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
448 ( $item_bibnum, $item_bibitemnum, $itemnumber )
449 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
451 $dbh->do(
452 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
453 VALUES (?, ?, ?, ?, ?)},
455 '*', '*', 'ONLY1', 1, 99
457 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
458 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
460 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
462 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
463 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
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),
476 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');