Bug 19059: Move C4::Reserves::CancelReserve to Koha::Hold->cancel
[koha.git] / t / db_dependent / Holds.t
blob241d0247a41839da318cbf4f549abf75a59fa925
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 => 55;
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 Koha::Holds->find( $reserve_id )->cancel;
132 $holds = $biblio->holds;
133 is( $holds->count, $borrowers_count - 1, "Koha::Hold->cancel" );
135 $holds = $item->current_holds;
136 $first_hold = $holds->next;
137 $borrowernumber = $first_hold->borrowernumber;
138 $branch_1code = $first_hold->branchcode;
139 $reserve_id = $first_hold->reserve_id;
141 ModReserve({
142 reserve_id => $reserve_id,
143 rank => '4',
144 branchcode => $branch_1,
145 itemnumber => $itemnumber,
146 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
149 $hold = Koha::Holds->find( $reserve_id );
150 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
151 ok( $hold->suspend, "Test ModReserve, suspend hold" );
152 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
154 ToggleSuspend( $reserve_id );
155 $hold = Koha::Holds->find( $reserve_id );
156 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
158 ToggleSuspend( $reserve_id, '2012-01-01' );
159 $hold = Koha::Holds->find( $reserve_id );
160 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
162 AutoUnsuspendReserves();
163 $hold = Koha::Holds->find( $reserve_id );
164 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
166 SuspendAll(
167 borrowernumber => $borrowernumber,
168 biblionumber => $biblionumber,
169 suspend => 1,
170 suspend_until => '2012-01-01',
172 $hold = Koha::Holds->find( $reserve_id );
173 is( $hold->suspend, 1, "Test SuspendAll()" );
174 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
176 SuspendAll(
177 borrowernumber => $borrowernumber,
178 biblionumber => $biblionumber,
179 suspend => 0,
181 $hold = Koha::Holds->find( $reserve_id );
182 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
183 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
185 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
186 AddReserve(
187 $branch_1,
188 $borrowernumbers[0],
189 $biblionumber,
190 my $bibitems = q{},
191 my $priority,
192 my $resdate,
193 my $expdate,
194 my $notes = q{},
195 $title,
196 my $checkitem,
197 my $found,
199 $patron = Koha::Patrons->find( $borrowernumber );
200 $holds = $patron->holds;
201 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
202 ModReserveMinusPriority( $itemnumber, $reserveid );
203 $holds = $patron->holds;
204 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
206 $holds = $biblio->holds;
207 $hold = $holds->next;
208 AlterPriority( 'top', $hold->reserve_id );
209 $hold = Koha::Holds->find( $reserveid );
210 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
212 AlterPriority( 'down', $hold->reserve_id );
213 $hold = Koha::Holds->find( $reserveid );
214 is( $hold->priority, '2', "Test AlterPriority(), move down" );
216 AlterPriority( 'up', $hold->reserve_id );
217 $hold = Koha::Holds->find( $reserveid );
218 is( $hold->priority, '1', "Test AlterPriority(), move up" );
220 AlterPriority( 'bottom', $hold->reserve_id );
221 $hold = Koha::Holds->find( $reserveid );
222 is( $hold->priority, '5', "Test AlterPriority(), move to bottom" );
224 # Regression test for bug 2394
226 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
227 # a patron is not permittedo to request an item whose homebranch (i.e.,
228 # owner of the item) is different from the patron's own library.
229 # However, if canreservefromotherbranches is turned ON, the patron can
230 # create such hold requests.
232 # Note that canreservefromotherbranches has no effect if
233 # IndependentBranches is OFF.
235 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
236 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
237 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
238 $dbh->do('DELETE FROM issuingrules');
239 $dbh->do(
240 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
241 VALUES (?, ?, ?, ?, ?)},
243 '*', '*', '*', 25, 99
245 $dbh->do(
246 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
247 VALUES (?, ?, ?, ?, ?)},
249 '*', '*', 'CANNOT', 0, 99
252 # make sure some basic sysprefs are set
253 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
254 t::lib::Mocks::mock_preference('item-level_itypes', 1);
256 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
257 t::lib::Mocks::mock_preference('IndependentBranches', 0);
259 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
260 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
263 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
264 t::lib::Mocks::mock_preference('IndependentBranches', 1);
265 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
267 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
268 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
271 # ... unless canreservefromotherbranches is ON
272 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
274 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
275 '... unless canreservefromotherbranches is ON (bug 2394)'
278 # Regression test for bug 11336
279 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
280 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
281 AddReserve(
282 $branch_1,
283 $borrowernumbers[0],
284 $bibnum,
289 my $reserveid1 = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
291 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
292 my $reserveid2 = AddReserve(
293 $branch_1,
294 $borrowernumbers[1],
295 $bibnum,
300 my $hold1 = Koha::Holds->find( $reserveid1 );
301 $hold1->cancel;
303 my $hold2 = Koha::Holds->find( $reserveid2 );
304 is( $hold2->priority, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
306 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
307 my $reserveid3 = AddReserve(
308 $branch_1,
309 $borrowernumbers[0],
310 $bibnum,
315 my $hold3 = Koha::Holds->find( $reserveid3 );
316 is( $hold3->priority, 2, "New reserve for patron 0, the reserve has a priority = 2" );
318 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
319 $hold3 = Koha::Holds->find( $reserveid3 );
320 is( $hold3->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
322 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
323 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
324 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
325 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
327 $hold = Koha::Hold->new(
329 borrowernumber => $borrowernumbers[0],
330 itemnumber => $itemnumber,
331 biblionumber => $item_bibnum,
333 )->store();
334 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
335 'itemAlreadyOnHold',
336 "Patron cannot place a second item level hold for a given item" );
337 $hold->delete();
339 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
340 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
341 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
343 # Regression test for bug 9532
344 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
345 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
346 AddReserve(
347 $branch_1,
348 $borrowernumbers[0],
349 $bibnum,
354 CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
355 "cannot request item if policy that matches on item-level item type forbids it"
357 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
359 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
360 "can request item if policy that matches on item type allows it"
363 t::lib::Mocks::mock_preference('item-level_itypes', 0);
364 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
366 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
367 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
371 # Test branch item rules
373 $dbh->do('DELETE FROM issuingrules');
374 $dbh->do(
375 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
376 VALUES (?, ?, ?, ?)},
378 '*', '*', '*', 25
380 $dbh->do('DELETE FROM branch_item_rules');
381 $dbh->do('DELETE FROM default_branch_circ_rules');
382 $dbh->do('DELETE FROM default_branch_item_rules');
383 $dbh->do('DELETE FROM default_circ_rules');
384 $dbh->do(q{
385 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
386 VALUES (?, ?, ?, ?)
387 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
388 $dbh->do(q{
389 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
390 VALUES (?, ?, ?, ?)
391 }, {}, $branch_1, 'CAN', 1, 'homebranch');
392 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
393 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
394 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
395 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
396 "CanItemBeReserved should returns 'notReservable'");
398 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
399 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
400 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
401 'cannotReserveFromOtherBranches',
402 "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
404 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
405 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
406 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
407 "CanItemBeReserved should returns 'OK'");
409 # Bug 12632
410 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
411 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
413 $dbh->do('DELETE FROM reserves');
414 $dbh->do('DELETE FROM issues');
415 $dbh->do('DELETE FROM items');
416 $dbh->do('DELETE FROM biblio');
418 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
419 ( $item_bibnum, $item_bibitemnum, $itemnumber )
420 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
422 $dbh->do(
423 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
424 VALUES (?, ?, ?, ?, ?)},
426 '*', '*', 'ONLY1', 1, 99
428 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
429 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
431 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
433 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
434 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
437 # Helper method to set up a Biblio.
438 sub create_helper_biblio {
439 my $itemtype = shift;
440 my $bib = MARC::Record->new();
441 my $title = 'Silence in the library';
442 $bib->append_fields(
443 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
444 MARC::Field->new('245', ' ', ' ', a => $title),
445 MARC::Field->new('942', ' ', ' ', c => $itemtype),
447 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');