6 use t
::lib
::TestBuilder
;
11 use Test
::More tests
=> 60;
18 use Koha
::DateUtils
qw( dt_from_string output_pref );
23 use lib
$FindBin::Bin
;
24 use_ok
('C4::Reserves');
27 my $schema = Koha
::Database
->new->schema;
28 $schema->storage->txn_begin;
30 my $builder = t
::lib
::TestBuilder
->new();
31 my $dbh = C4
::Context
->dbh;
33 # Create two random branches
34 my $branch_1 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
35 my $branch_2 = $builder->build({ source
=> 'Branch' })->{ branchcode
};
37 my $category = $builder->build({ source
=> 'Category' });
39 my $borrowers_count = 5;
41 $dbh->do('DELETE FROM itemtypes');
42 $dbh->do('DELETE FROM reserves');
43 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
44 $insert_sth->execute('CAN');
45 $insert_sth->execute('CANNOT');
46 $insert_sth->execute('DUMMY');
47 $insert_sth->execute('ONLY1');
49 # Setup Test------------------------
50 # Create a biblio instance for testing
51 my ($bibnum, $title, $bibitemnum) = create_helper_biblio
('DUMMY');
53 # Create item instance for testing.
54 my ($item_bibnum, $item_bibitemnum, $itemnumber)
55 = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1 } , $bibnum);
57 # Create some borrowers
59 foreach (1..$borrowers_count) {
60 my $borrowernumber = AddMember
(
61 firstname
=> 'my firstname',
62 surname
=> 'my surname ' . $_,
63 categorycode
=> $category->{categorycode
},
64 branchcode
=> $branch_1,
66 push @borrowernumbers, $borrowernumber;
69 my $biblionumber = $bibnum;
71 # Create five item level holds
72 foreach my $borrowernumber ( @borrowernumbers ) {
78 my $priority = C4
::Reserves
::CalculatePriority
( $biblionumber ),
83 my $checkitem = $itemnumber,
88 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber });
89 is
( scalar(@
$reserves), $borrowers_count, "Test GetReserves()" );
91 is
( $reserves->[0]->{priority
}, 1, "Reserve 1 has a priority of 1" );
92 is
( $reserves->[1]->{priority
}, 2, "Reserve 2 has a priority of 2" );
93 is
( $reserves->[2]->{priority
}, 3, "Reserve 3 has a priority of 3" );
94 is
( $reserves->[3]->{priority
}, 4, "Reserve 4 has a priority of 4" );
95 is
( $reserves->[4]->{priority
}, 5, "Reserve 5 has a priority of 5" );
97 my ( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber
($itemnumber);
98 is
( $reservedate, output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 }), "GetReservesFromItemnumber should return a valid reserve date");
99 is
( $borrowernumber, $borrowernumbers[0], "GetReservesFromItemnumber should return a valid borrowernumber");
100 is
( $branch_1code, $branch_1, "GetReservesFromItemnumber should return a valid branchcode");
101 ok
($reserve_id, "Test GetReservesFromItemnumber()");
103 my $hold = Koha
::Holds
->find( $reserve_id );
104 ok
( $hold, "Koha::Holds found the hold" );
105 my $hold_biblio = $hold->biblio();
106 ok
( $hold_biblio, "Got biblio using biblio() method" );
107 ok
( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
108 my $hold_item = $hold->item();
109 ok
( $hold_item, "Got item using item() method" );
110 ok
( $hold_item == $hold->item(), "item method returns stashed item" );
111 my $hold_branch = $hold->branch();
112 ok
( $hold_branch, "Got branch using branch() method" );
113 ok
( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
114 my $hold_found = $hold->found();
115 $hold->set({ found
=> 'W'})->store();
116 is
( Koha
::Holds
->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
118 my ( $reserve ) = GetReservesFromBorrowernumber
($borrowernumbers[0]);
119 ok
( $reserve->{'borrowernumber'} eq $borrowernumbers[0], "Test GetReservesFromBorrowernumber()");
122 ok
( GetReserveCount
( $borrowernumbers[0] ), "Test GetReserveCount()" );
125 CancelReserve
({ 'reserve_id' => $reserve_id });
126 $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber });
127 is
( scalar(@
$reserves), $borrowers_count - 1, "Test CancelReserve()" );
130 ( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber
($itemnumber);
132 reserve_id
=> $reserve_id,
134 branchcode
=> $branch_1,
135 itemnumber
=> $itemnumber,
136 suspend_until
=> output_pref
( { dt
=> dt_from_string
( "2013-01-01", "iso" ), dateonly
=> 1 } ),
139 $reserve = GetReserve
( $reserve_id );
140 ok
( $reserve->{'priority'} eq '4', "Test GetReserve(), priority changed correctly" );
141 ok
( $reserve->{'suspend'}, "Test GetReserve(), suspend hold" );
142 is
( $reserve->{'suspend_until'}, '2013-01-01 00:00:00', "Test GetReserve(), suspend until date" );
144 ToggleSuspend
( $reserve_id );
145 $reserve = GetReserve
( $reserve_id );
146 ok
( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
148 ToggleSuspend
( $reserve_id, '2012-01-01' );
149 $reserve = GetReserve
( $reserve_id );
150 is
( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
152 AutoUnsuspendReserves
();
153 $reserve = GetReserve
( $reserve_id );
154 ok
( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
157 borrowernumber
=> $borrowernumber,
158 biblionumber
=> $biblionumber,
160 suspend_until
=> '2012-01-01',
162 $reserve = GetReserve
( $reserve_id );
163 is
( $reserve->{'suspend'}, 1, "Test SuspendAll()" );
164 is
( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
167 borrowernumber
=> $borrowernumber,
168 biblionumber
=> $biblionumber,
171 $reserve = GetReserve
( $reserve_id );
172 is
( $reserve->{'suspend'}, 0, "Test resuming with SuspendAll()" );
173 is
( $reserve->{'suspend_until'}, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
175 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
189 ( $reserve ) = GetReservesFromBorrowernumber
($borrowernumber);
190 my $reserveid = C4
::Reserves
::GetReserveId
(
192 biblionumber
=> $biblionumber,
193 borrowernumber
=> $borrowernumber
196 is
( $reserveid, $reserve->{reserve_id
}, "Test GetReserveId" );
197 ModReserveMinusPriority
( $itemnumber, $reserve->{'reserve_id'} );
198 ( $reserve ) = GetReservesFromBorrowernumber
($borrowernumber);
199 ok
( $reserve->{'itemnumber'} eq $itemnumber, "Test ModReserveMinusPriority()" );
202 my $reserve2 = GetReserveInfo
( $reserve->{'reserve_id'} );
203 ok
( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" );
206 $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
207 $reserve = $reserves->[1];
208 AlterPriority
( 'top', $reserve->{'reserve_id'} );
209 $reserve = GetReserve
( $reserve->{'reserve_id'} );
210 is
( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" );
212 AlterPriority
( 'down', $reserve->{'reserve_id'} );
213 $reserve = GetReserve
( $reserve->{'reserve_id'} );
214 is
( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
216 AlterPriority
( 'up', $reserve->{'reserve_id'} );
217 $reserve = GetReserve
( $reserve->{'reserve_id'} );
218 is
( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
220 AlterPriority
( 'bottom', $reserve->{'reserve_id'} );
221 $reserve = GetReserve
( $reserve->{'reserve_id'} );
222 is
( $reserve->{'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');
240 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
241 VALUES (?, ?, ?, ?)},
246 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
247 VALUES (?, ?, ?, ?)},
249 '*', '*', 'CANNOT', 0
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);
289 my $reserveid1 = C4
::Reserves
::GetReserveId
(
291 biblionumber
=> $bibnum,
292 borrowernumber
=> $borrowernumbers[0]
296 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1 } , $bibnum);
304 my $reserveid2 = C4
::Reserves
::GetReserveId
(
306 biblionumber
=> $bibnum,
307 borrowernumber
=> $borrowernumbers[1]
311 CancelReserve
({ reserve_id
=> $reserveid1 });
313 $reserve2 = GetReserve
( $reserveid2 );
314 is
( $reserve2->{priority
}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
316 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1 } , $bibnum);
324 my $reserveid3 = C4
::Reserves
::GetReserveId
(
326 biblionumber
=> $bibnum,
327 borrowernumber
=> $borrowernumbers[0]
331 my $reserve3 = GetReserve
( $reserveid3 );
332 is
( $reserve3->{priority
}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
334 ModReserve
({ reserve_id
=> $reserveid2, rank
=> 'del' });
335 $reserve3 = GetReserve
( $reserveid3 );
336 is
( $reserve3->{priority
}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
338 ModItem
({ damaged
=> 1 }, $item_bibnum, $itemnumber);
339 t
::lib
::Mocks
::mock_preference
( 'AllowHoldsOnDamagedItems', 1 );
340 ok
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
341 ok
( defined( ( CheckReserves
($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
342 t
::lib
::Mocks
::mock_preference
( 'AllowHoldsOnDamagedItems', 0 );
343 ok
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
344 ok
( !defined( ( CheckReserves
($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
346 # Regression test for bug 9532
347 ($bibnum, $title, $bibitemnum) = create_helper_biblio
('CANNOT');
348 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> $branch_1, holdingbranch
=> $branch_1, itype
=> 'CANNOT' } , $bibnum);
357 CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
358 "cannot request item if policy that matches on item-level item type forbids it"
360 ModItem
({ itype
=> 'CAN' }, $item_bibnum, $itemnumber);
362 CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'OK',
363 "can request item if policy that matches on item type allows it"
366 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0);
367 ModItem
({ itype
=> undef }, $item_bibnum, $itemnumber);
369 CanItemBeReserved
( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
370 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
374 # Test branch item rules
376 $dbh->do('DELETE FROM issuingrules');
378 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
379 VALUES (?, ?, ?, ?)},
383 $dbh->do('DELETE FROM branch_item_rules');
384 $dbh->do('DELETE FROM default_branch_circ_rules');
385 $dbh->do('DELETE FROM default_branch_item_rules');
386 $dbh->do('DELETE FROM default_circ_rules');
388 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
390 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
392 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
394 }, {}, $branch_1, 'CAN', 1, 'homebranch');
395 ($bibnum, $title, $bibitemnum) = create_helper_biblio
('CANNOT');
396 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
(
397 { homebranch
=> $branch_1, holdingbranch
=> $branch_1, itype
=> 'CANNOT' } , $bibnum);
398 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber), 'notReservable',
399 "CanItemBeReserved should returns 'notReservable'");
401 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
(
402 { homebranch
=> $branch_2, holdingbranch
=> $branch_1, itype
=> 'CAN' } , $bibnum);
403 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber),
404 'cannotReserveFromOtherBranches',
405 "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
407 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
(
408 { homebranch
=> $branch_1, holdingbranch
=> $branch_1, itype
=> 'CAN' } , $bibnum);
409 is
(CanItemBeReserved
($borrowernumbers[0], $itemnumber), 'OK',
410 "CanItemBeReserved should returns 'OK'");
413 # Test CancelExpiredReserves
414 t
::lib
::Mocks
::mock_preference
('ExpireReservesMaxPickUpDelay', 1);
415 t
::lib
::Mocks
::mock_preference
('ReservesMaxPickUpDelay', 1);
417 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
420 $reserves = $dbh->selectall_arrayref('SELECT * FROM reserves', { Slice
=> {} });
421 $reserve = $reserves->[0];
422 my $calendar = C4
::Calendar
->new(branchcode
=> $reserve->{branchcode
});
423 $calendar->insert_single_holiday(
428 description
=> 'Test',
430 $reserve_id = $reserve->{reserve_id
};
431 $dbh->do("UPDATE reserves SET waitingdate = DATE_SUB( NOW(), INTERVAL 5 DAY ), found = 'W', priority = 0 WHERE reserve_id = ?", undef, $reserve_id );
432 t
::lib
::Mocks
::mock_preference
('ExpireReservesOnHolidays', 0);
433 CancelExpiredReserves
();
434 my $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
435 is
( $count, 1, "Waiting reserve beyond max pickup delay *not* canceled on holiday" );
436 t
::lib
::Mocks
::mock_preference
('ExpireReservesOnHolidays', 1);
437 CancelExpiredReserves
();
438 $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
439 is
( $count, 0, "Waiting reserve beyond max pickup delay canceled on holiday" );
441 # Test expirationdate
442 $reserve = $reserves->[1];
443 $reserve_id = $reserve->{reserve_id
};
444 $dbh->do("UPDATE reserves SET expirationdate = DATE_SUB( NOW(), INTERVAL 1 DAY ) WHERE reserve_id = ?", undef, $reserve_id );
445 CancelExpiredReserves
();
446 $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
447 is
( $count, 0, "Reserve with manual expiration date canceled correctly" );
450 t
::lib
::Mocks
::mock_preference
( 'item-level_itypes', 1 );
451 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
453 $dbh->do('DELETE FROM reserves');
454 $dbh->do('DELETE FROM issues');
455 $dbh->do('DELETE FROM items');
456 $dbh->do('DELETE FROM biblio');
458 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio
('ONLY1');
459 ( $item_bibnum, $item_bibitemnum, $itemnumber )
460 = AddItem
( { homebranch
=> $branch_1, holdingbranch
=> $branch_1 }, $bibnum );
463 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
464 VALUES (?, ?, ?, ?)},
468 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber ),
469 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
471 my $res_id = AddReserve
( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
473 is
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber ),
474 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
477 # Helper method to set up a Biblio.
478 sub create_helper_biblio
{
479 my $itemtype = shift;
480 my $bib = MARC
::Record
->new();
481 my $title = 'Silence in the library';
483 MARC
::Field
->new('100', ' ', ' ', a
=> 'Moffat, Steven'),
484 MARC
::Field
->new('245', ' ', ' ', a
=> $title),
485 MARC
::Field
->new('942', ' ', ' ', c
=> $itemtype),
487 return ($bibnum, $title, $bibitemnum) = AddBiblio
($bib, '');