6 use t
::lib
::TestBuilder
;
10 use Test
::More tests
=> 55;
17 use Koha
::DateUtils
qw( dt_from_string output_pref );
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
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 ) {
79 my $priority = C4
::Reserves
::CalculatePriority
( $biblionumber ),
84 my $checkitem = $itemnumber,
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" );
124 is
( Koha
::Holds
->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
126 my $patron = Koha
::Patrons
->find( $borrowernumbers[0] );
127 $holds = $patron->holds;
128 is
( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
131 $holds = $item->current_holds;
132 $first_hold = $holds->next;
133 $borrowernumber = $first_hold->borrowernumber;
134 $branch_1code = $first_hold->branchcode;
135 $reserve_id = $first_hold->reserve_id;
138 reserve_id
=> $reserve_id,
140 branchcode
=> $branch_1,
141 itemnumber
=> $itemnumber,
142 suspend_until
=> output_pref
( { dt
=> dt_from_string
( "2013-01-01", "iso" ), dateonly
=> 1 } ),
145 $hold = Koha
::Holds
->find( $reserve_id );
146 ok
( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
147 ok
( $hold->suspend, "Test ModReserve, suspend hold" );
148 is
( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
150 ModReserve
({ # call without reserve_id
152 biblionumber
=> $item_bibnum,
153 itemnumber
=> $itemnumber,
154 borrowernumber
=> $borrowernumber,
156 $hold = Koha
::Holds
->find( $reserve_id );
157 ok
( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
159 ToggleSuspend
( $reserve_id );
160 $hold = Koha
::Holds
->find( $reserve_id );
161 ok
( ! $hold->suspend, "Test ToggleSuspend(), no date" );
163 ToggleSuspend
( $reserve_id, '2012-01-01' );
164 $hold = Koha
::Holds
->find( $reserve_id );
165 is
( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
167 AutoUnsuspendReserves
();
168 $hold = Koha
::Holds
->find( $reserve_id );
169 ok
( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
172 borrowernumber
=> $borrowernumber,
173 biblionumber
=> $biblionumber,
175 suspend_until
=> '2012-01-01',
177 $hold = Koha
::Holds
->find( $reserve_id );
178 is
( $hold->suspend, 1, "Test SuspendAll()" );
179 is
( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
182 borrowernumber
=> $borrowernumber,
183 biblionumber
=> $biblionumber,
186 $hold = Koha
::Holds
->find( $reserve_id );
187 is
( $hold->suspend, 0, "Test resuming with SuspendAll()" );
188 is
( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
190 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
204 $patron = Koha
::Patrons
->find( $borrowernumber );
205 $holds = $patron->holds;
206 my $reserveid = Koha
::Holds
->search({ biblionumber
=> $bibnum, borrowernumber
=> $borrowernumbers[0] })->next->reserve_id;
207 ModReserveMinusPriority
( $itemnumber, $reserveid );
208 $holds = $patron->holds;
209 is
( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
211 $holds = $biblio->holds;
212 $hold = $holds->next;
213 AlterPriority
( 'top', $hold->reserve_id );
214 $hold = Koha
::Holds
->find( $reserveid );
215 is
( $hold->priority, '1', "Test AlterPriority(), move to top" );
217 AlterPriority
( 'down', $hold->reserve_id );
218 $hold = Koha
::Holds
->find( $reserveid );
219 is
( $hold->priority, '2', "Test AlterPriority(), move down" );
221 AlterPriority
( 'up', $hold->reserve_id );
222 $hold = Koha
::Holds
->find( $reserveid );
223 is
( $hold->priority, '1', "Test AlterPriority(), move up" );
225 AlterPriority
( 'bottom', $hold->reserve_id );
226 $hold = Koha
::Holds
->find( $reserveid );
227 is
( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
229 # Regression test for bug 2394
231 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
232 # a patron is not permittedo to request an item whose homebranch (i.e.,
233 # owner of the item) is different from the patron's own library.
234 # However, if canreservefromotherbranches is turned ON, the patron can
235 # create such hold requests.
237 # Note that canreservefromotherbranches has no effect if
238 # IndependentBranches is OFF.
240 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio
('DUMMY');
241 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
242 = AddItem
({ homebranch
=> $branch_2, holdingbranch
=> $branch_2 } , $foreign_bibnum);
243 $dbh->do('DELETE FROM issuingrules');
245 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
246 VALUES (?, ?, ?, ?, ?)},
248 '*', '*', '*', 25, 99
251 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
252 VALUES (?, ?, ?, ?, ?)},
254 '*', '*', 'CANNOT', 0, 99
257 # make sure some basic sysprefs are set
258 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'ItemHomeLibrary');
259 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
261 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
262 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
264 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
265 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
268 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
269 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 1);
270 t
::lib
::Mocks
::mock_preference
('canreservefromotherbranches', 0);
272 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
273 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
276 # ... unless canreservefromotherbranches is ON
277 t
::lib
::Mocks
::mock_preference
('canreservefromotherbranches', 1);
279 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
280 '... unless canreservefromotherbranches is ON (bug 2394)'
284 # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
285 ($bibnum, $title, $bibitemnum) = create_helper_biblio
('DUMMY');
286 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1 } , $bibnum);
287 my $reserveid1 = AddReserve
($branch_1, $borrowernumbers[0], $bibnum, '', 1);
288 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1 } , $bibnum);
289 my $reserveid2 = AddReserve
($branch_1, $borrowernumbers[1], $bibnum, '', 2);
290 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1 } , $bibnum);
291 my $reserveid3 = AddReserve
($branch_1, $borrowernumbers[2], $bibnum, '', 3);
292 my $hhh = Koha
::Holds
->search({ biblionumber
=> $bibnum });
293 my $hold3 = Koha
::Holds
->find( $reserveid3 );
294 is
( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
295 ModReserve
({ reserve_id
=> $reserveid1, rank
=> 'del' });
296 ModReserve
({ reserve_id
=> $reserveid2, rank
=> 'del' });
297 is
( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
300 ModItem
({ damaged
=> 1 }, $item_bibnum, $itemnumber);
301 t
::lib
::Mocks
::mock_preference
( 'AllowHoldsOnDamagedItems', 1 );
302 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
303 ok
( defined( ( CheckReserves
($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
305 $hold = Koha
::Hold
->new(
307 borrowernumber
=> $borrowernumbers[0],
308 itemnumber
=> $itemnumber,
309 biblionumber
=> $item_bibnum,
312 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber ),
314 "Patron cannot place a second item level hold for a given item" );
317 t
::lib
::Mocks
::mock_preference
( 'AllowHoldsOnDamagedItems', 0 );
318 ok
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
319 ok
( !defined( ( CheckReserves
($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
321 # Regression test for bug 9532
322 ($bibnum, $title, $bibitemnum) = create_helper_biblio
('CANNOT');
323 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1, itype
=> 'CANNOT' } , $bibnum);
332 CanItemBeReserved
( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
333 "cannot request item if policy that matches on item-level item type forbids it"
335 ModItem
({ itype
=> 'CAN' }, $item_bibnum, $itemnumber);
337 CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'OK',
338 "can request item if policy that matches on item type allows it"
341 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0);
342 ModItem
({ itype
=> undef }, $item_bibnum, $itemnumber);
344 CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
345 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
349 # Test branch item rules
351 $dbh->do('DELETE FROM issuingrules');
353 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
354 VALUES (?, ?, ?, ?)},
358 $dbh->do('DELETE FROM branch_item_rules');
359 $dbh->do('DELETE FROM default_branch_circ_rules');
360 $dbh->do('DELETE FROM default_branch_item_rules');
361 $dbh->do('DELETE FROM default_circ_rules');
363 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
365 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
367 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
369 }, {}, $branch_1, 'CAN', 1, 'homebranch');
370 ($bibnum, $title, $bibitemnum) = create_helper_biblio
('CANNOT');
371 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
(
372 { homebranch
=> $branch_1, holdingbranch
=> $branch_1, itype
=> 'CANNOT' } , $bibnum);
373 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber), 'notReservable',
374 "CanItemBeReserved should return 'notReservable'");
376 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
(
377 { homebranch
=> $branch_2, holdingbranch
=> $branch_1, itype
=> 'CAN' } , $bibnum);
378 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber),
379 'cannotReserveFromOtherBranches',
380 "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
382 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
(
383 { homebranch
=> $branch_1, holdingbranch
=> $branch_1, itype
=> 'CAN' } , $bibnum);
384 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber), 'OK',
385 "CanItemBeReserved should return 'OK'");
388 t
::lib
::Mocks
::mock_preference
( 'item-level_itypes', 1 );
389 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
391 $dbh->do('DELETE FROM reserves');
392 $dbh->do('DELETE FROM issues');
393 $dbh->do('DELETE FROM items');
394 $dbh->do('DELETE FROM biblio');
396 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio
('ONLY1');
397 ( $item_bibnum, $item_bibitemnum, $itemnumber )
398 = AddItem
( { homebranch
=> $branch_1, holdingbranch
=> $branch_1 }, $bibnum );
401 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
402 VALUES (?, ?, ?, ?, ?)},
404 '*', '*', 'ONLY1', 1, 99
406 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber ),
407 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
409 my $res_id = AddReserve
( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
411 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber ),
412 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
415 # Helper method to set up a Biblio.
416 sub create_helper_biblio
{
417 my $itemtype = shift;
418 my $bib = MARC
::Record
->new();
419 my $title = 'Silence in the library';
421 MARC
::Field
->new('100', ' ', ' ', a
=> 'Moffat, Steven'),
422 MARC
::Field
->new('245', ' ', ' ', a
=> $title),
423 MARC
::Field
->new('942', ' ', ' ', c
=> $itemtype),
425 return ($bibnum, $title, $bibitemnum) = AddBiblio
($bib, '');