6 use t::lib::TestBuilder;
10 use Test::More tests => 54;
20 use Koha::DateUtils qw( dt_from_string output_pref );
26 use lib $FindBin::Bin;
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
32 my $builder = t::lib::TestBuilder->new();
33 my $dbh = C4::Context->dbh;
35 # Create two random branches
36 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
37 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
39 my $category = $builder->build({ source => 'Category' });
41 my $borrowers_count = 5;
43 $dbh->do('DELETE FROM itemtypes');
44 $dbh->do('DELETE FROM reserves');
45 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
46 $insert_sth->execute('CAN');
47 $insert_sth->execute('CANNOT');
48 $insert_sth->execute('DUMMY');
49 $insert_sth->execute('ONLY1');
51 # Setup Test------------------------
52 # Create a biblio instance for testing
53 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
55 # Create item instance for testing.
56 my ($item_bibnum, $item_bibitemnum, $itemnumber)
57 = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
59 # Create some borrowers
61 foreach (1..$borrowers_count) {
62 my $borrowernumber = AddMember(
63 firstname => 'my firstname',
64 surname => 'my surname ' . $_,
65 categorycode => $category->{categorycode},
66 branchcode => $branch_1,
68 push @borrowernumbers, $borrowernumber;
71 my $biblionumber = $bibnum;
73 # Create five item level holds
74 foreach my $borrowernumber ( @borrowernumbers ) {
80 my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
85 my $checkitem = $itemnumber,
90 my $biblio = Koha::Biblios->find( $biblionumber );
91 my $holds = $biblio->holds;
92 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
93 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
94 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
95 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
96 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
97 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
99 my $item = Koha::Items->find( $itemnumber );
100 $holds = $item->current_holds;
101 my $first_hold = $holds->next;
102 my $reservedate = $first_hold->reservedate;
103 my $borrowernumber = $first_hold->borrowernumber;
104 my $branch_1code = $first_hold->branchcode;
105 my $reserve_id = $first_hold->reserve_id;
106 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
107 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
108 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
109 ok($reserve_id, "Test holds_placed_today()");
111 my $hold = Koha::Holds->find( $reserve_id );
112 ok( $hold, "Koha::Holds found the hold" );
113 my $hold_biblio = $hold->biblio();
114 ok( $hold_biblio, "Got biblio using biblio() method" );
115 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
116 my $hold_item = $hold->item();
117 ok( $hold_item, "Got item using item() method" );
118 ok( $hold_item == $hold->item(), "item method returns stashed item" );
119 my $hold_branch = $hold->branch();
120 ok( $hold_branch, "Got branch using branch() method" );
121 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
122 my $hold_found = $hold->found();
123 $hold->set({ found => 'W'})->store();
124 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
125 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
127 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
128 $holds = $patron->holds;
129 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
132 $holds = $item->current_holds;
133 $first_hold = $holds->next;
134 $borrowernumber = $first_hold->borrowernumber;
135 $branch_1code = $first_hold->branchcode;
136 $reserve_id = $first_hold->reserve_id;
139 reserve_id => $reserve_id,
141 branchcode => $branch_1,
142 itemnumber => $itemnumber,
143 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
146 $hold = Koha::Holds->find( $reserve_id );
147 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
148 ok( $hold->suspend, "Test ModReserve, suspend hold" );
149 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
151 ModReserve({ # call without reserve_id
153 biblionumber => $item_bibnum,
154 itemnumber => $itemnumber,
155 borrowernumber => $borrowernumber,
157 $hold = Koha::Holds->find( $reserve_id );
158 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
160 ToggleSuspend( $reserve_id );
161 $hold = Koha::Holds->find( $reserve_id );
162 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
164 ToggleSuspend( $reserve_id, '2012-01-01' );
165 $hold = Koha::Holds->find( $reserve_id );
166 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
168 AutoUnsuspendReserves();
169 $hold = Koha::Holds->find( $reserve_id );
170 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
173 borrowernumber => $borrowernumber,
174 biblionumber => $biblionumber,
176 suspend_until => '2012-01-01',
178 $hold = Koha::Holds->find( $reserve_id );
179 is( $hold->suspend, 1, "Test SuspendAll()" );
180 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
183 borrowernumber => $borrowernumber,
184 biblionumber => $biblionumber,
187 $hold = Koha::Holds->find( $reserve_id );
188 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
189 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
191 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
205 $patron = Koha::Patrons->find( $borrowernumber );
206 $holds = $patron->holds;
207 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
208 ModReserveMinusPriority( $itemnumber, $reserveid );
209 $holds = $patron->holds;
210 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
212 $holds = $biblio->holds;
213 $hold = $holds->next;
214 AlterPriority( 'top', $hold->reserve_id );
215 $hold = Koha::Holds->find( $reserveid );
216 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
218 AlterPriority( 'down', $hold->reserve_id );
219 $hold = Koha::Holds->find( $reserveid );
220 is( $hold->priority, '2', "Test AlterPriority(), move down" );
222 AlterPriority( 'up', $hold->reserve_id );
223 $hold = Koha::Holds->find( $reserveid );
224 is( $hold->priority, '1', "Test AlterPriority(), move up" );
226 AlterPriority( 'bottom', $hold->reserve_id );
227 $hold = Koha::Holds->find( $reserveid );
228 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
230 # Regression test for bug 2394
232 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
233 # a patron is not permittedo to request an item whose homebranch (i.e.,
234 # owner of the item) is different from the patron's own library.
235 # However, if canreservefromotherbranches is turned ON, the patron can
236 # create such hold requests.
238 # Note that canreservefromotherbranches has no effect if
239 # IndependentBranches is OFF.
241 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
242 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
243 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
244 $dbh->do('DELETE FROM issuingrules');
246 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
247 VALUES (?, ?, ?, ?, ?)},
249 '*', '*', '*', 25, 99
252 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
253 VALUES (?, ?, ?, ?, ?)},
255 '*', '*', 'CANNOT', 0, 99
258 # make sure some basic sysprefs are set
259 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
260 t::lib::Mocks::mock_preference('item-level_itypes', 1);
262 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
263 t::lib::Mocks::mock_preference('IndependentBranches', 0);
265 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
266 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
269 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
270 t::lib::Mocks::mock_preference('IndependentBranches', 1);
271 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
273 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
274 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
277 # ... unless canreservefromotherbranches is ON
278 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
280 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
281 '... unless canreservefromotherbranches is ON (bug 2394)'
285 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
286 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
287 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
288 my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
289 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
290 my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
291 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
292 my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
293 my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
294 my $hold3 = Koha::Holds->find( $reserveid3 );
295 is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
296 ModReserve({ reserve_id => $reserveid1, rank => 'del' });
297 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
298 is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
301 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
302 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
303 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
304 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
306 $hold = Koha::Hold->new(
308 borrowernumber => $borrowernumbers[0],
309 itemnumber => $itemnumber,
310 biblionumber => $item_bibnum,
313 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
315 "Patron cannot place a second item level hold for a given item" );
318 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
319 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
320 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
322 # Regression test for bug 9532
323 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
324 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
333 CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
334 "cannot request item if policy that matches on item-level item type forbids it"
336 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
338 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
339 "can request item if policy that matches on item type allows it"
342 t::lib::Mocks::mock_preference('item-level_itypes', 0);
343 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
345 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
346 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
350 # Test branch item rules
352 $dbh->do('DELETE FROM issuingrules');
354 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
355 VALUES (?, ?, ?, ?)},
359 $dbh->do('DELETE FROM branch_item_rules');
360 $dbh->do('DELETE FROM default_branch_circ_rules');
361 $dbh->do('DELETE FROM default_branch_item_rules');
362 $dbh->do('DELETE FROM default_circ_rules');
364 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
366 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
368 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
370 }, {}, $branch_1, 'CAN', 1, 'homebranch');
371 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
372 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
373 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
374 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
375 "CanItemBeReserved should return 'notReservable'");
377 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
378 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
379 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
380 'cannotReserveFromOtherBranches',
381 "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
383 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
384 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
385 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
386 "CanItemBeReserved should return 'OK'");
389 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
390 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
392 $dbh->do('DELETE FROM reserves');
393 $dbh->do('DELETE FROM issues');
394 $dbh->do('DELETE FROM items');
395 $dbh->do('DELETE FROM biblio');
397 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
398 ( $item_bibnum, $item_bibitemnum, $itemnumber )
399 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
402 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
403 VALUES (?, ?, ?, ?, ?)},
405 '*', '*', 'ONLY1', 1, 99
407 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
408 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
410 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
412 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
413 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
416 # Helper method to set up a Biblio.
417 sub create_helper_biblio {
418 my $itemtype = shift;
419 my $bib = MARC::Record->new();
420 my $title = 'Silence in the library';
422 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
423 MARC::Field->new('245', ' ', ' ', a => $title),
424 MARC::Field->new('942', ' ', ' ', c => $itemtype),
426 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');