9 use Test
::More tests
=> 38;
16 use Koha
::DateUtils
qw( dt_from_string output_pref );
20 use lib
$FindBin::Bin
;
21 use_ok
('C4::Reserves');
24 my $dbh = C4
::Context
->dbh;
27 $dbh->{AutoCommit
} = 0;
28 $dbh->{RaiseError
} = 1;
30 my $borrowers_count = 5;
32 $dbh->do('DELETE FROM itemtypes');
33 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
34 $insert_sth->execute('CAN');
35 $insert_sth->execute('CANNOT');
37 # Setup Test------------------------
38 # Create a biblio instance for testing
39 my ($bibnum, $title, $bibitemnum) = create_helper_biblio
();
41 # Create item instance for testing.
42 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> 'CPL', holdingbranch
=> 'CPL' } , $bibnum);
44 # Create some borrowers
46 foreach (1..$borrowers_count) {
47 my $borrowernumber = AddMember
(
48 firstname
=> 'my firstname',
49 surname
=> 'my surname ' . $_,
53 push @borrowernumbers, $borrowernumber;
56 my $biblionumber = $bibnum;
58 my @branches = GetBranchesLoop
();
59 my $branch = $branches[0][0]{value
};
61 # Create five item level holds
62 foreach my $borrowernumber ( @borrowernumbers ) {
74 my $checkitem = $itemnumber,
80 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber });
81 is
( scalar(@
$reserves), $borrowers_count, "Test GetReserves()" );
84 my ( $reservedate, $borrowernumber, $branchcode, $reserve_id ) = GetReservesFromItemnumber
($itemnumber);
85 is
( $reservedate, output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 }), "GetReservesFromItemnumber should return a valid reserve date");
86 is
( $borrowernumber, $borrowernumbers[0], "GetReservesFromItemnumber should return a valid borrowernumber");
87 is
( $branchcode, 'CPL', "GetReservesFromItemnumber should return a valid branchcode");
88 ok
($reserve_id, "Test GetReservesFromItemnumber()");
91 my ( $reserve ) = GetReservesFromBorrowernumber
($borrowernumbers[0]);
92 ok
( $reserve->{'borrowernumber'} eq $borrowernumbers[0], "Test GetReservesFromBorrowernumber()");
95 ok
( GetReserveCount
( $borrowernumbers[0] ), "Test GetReserveCount()" );
98 CancelReserve
({ 'reserve_id' => $reserve_id });
99 $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber });
100 is
( scalar(@
$reserves), $borrowers_count - 1, "Test CancelReserve()" );
103 ( $reservedate, $borrowernumber, $branchcode, $reserve_id ) = GetReservesFromItemnumber
($itemnumber);
105 reserve_id
=> $reserve_id,
107 branchcode
=> $branch,
108 itemnumber
=> $itemnumber,
109 suspend_until
=> C4
::Dates
->new("2013-01-01","iso")->output(),
111 $reserve = GetReserve
( $reserve_id );
112 ok
( $reserve->{'priority'} eq '4', "Test GetReserve(), priority changed correctly" );
113 ok
( $reserve->{'suspend'}, "Test GetReserve(), suspend hold" );
114 ok
( $reserve->{'suspend_until'} eq '2013-01-01 00:00:00', "Test GetReserve(), suspend until date" );
116 ToggleSuspend
( $reserve_id );
117 $reserve = GetReserve
( $reserve_id );
118 ok
( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
120 ToggleSuspend
( $reserve_id, '2012-01-01' );
121 $reserve = GetReserve
( $reserve_id );
122 ok
( $reserve->{'suspend_until'} eq '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
124 AutoUnsuspendReserves
();
125 $reserve = GetReserve
( $reserve_id );
126 ok
( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
128 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
133 my $constraint = 'a',
143 ( $reserve ) = GetReservesFromBorrowernumber
($borrowernumber);
144 my $reserveid = C4
::Reserves
::GetReserveId
(
146 biblionumber
=> $biblionumber,
147 borrowernumber
=> $borrowernumber
150 is
( $reserveid, $reserve->{reserve_id
}, "Test GetReserveId" );
151 ModReserveMinusPriority
( $itemnumber, $reserve->{'reserve_id'} );
152 ( $reserve ) = GetReservesFromBorrowernumber
($borrowernumber);
153 ok
( $reserve->{'itemnumber'} eq $itemnumber, "Test ModReserveMinusPriority()" );
156 my $reserve2 = GetReserveInfo
( $reserve->{'reserve_id'} );
157 ok
( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" );
160 $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
161 $reserve = $reserves->[1];
162 AlterPriority
( 'top', $reserve->{'reserve_id'} );
163 $reserve = GetReserve
( $reserve->{'reserve_id'} );
164 ok
( $reserve->{'priority'} eq '1', "Test AlterPriority(), move to top" );
166 AlterPriority
( 'down', $reserve->{'reserve_id'} );
167 $reserve = GetReserve
( $reserve->{'reserve_id'} );
168 ok
( $reserve->{'priority'} eq '2', "Test AlterPriority(), move down" );
170 AlterPriority
( 'up', $reserve->{'reserve_id'} );
171 $reserve = GetReserve
( $reserve->{'reserve_id'} );
172 ok
( $reserve->{'priority'} eq '1', "Test AlterPriority(), move up" );
174 AlterPriority
( 'bottom', $reserve->{'reserve_id'} );
175 $reserve = GetReserve
( $reserve->{'reserve_id'} );
176 ok
( $reserve->{'priority'} eq '5', "Test AlterPriority(), move to bottom" );
178 # Regression test for bug 2394
180 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
181 # a patron is not permittedo to request an item whose homebranch (i.e.,
182 # owner of the item) is different from the patron's own library.
183 # However, if canreservefromotherbranches is turned ON, the patron can
184 # create such hold requests.
186 # Note that canreservefromotherbranches has no effect if
187 # IndependentBranches is OFF.
189 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio
();
190 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
191 = AddItem
({ homebranch
=> 'MPL', holdingbranch
=> 'MPL' } , $foreign_bibnum);
192 $dbh->do('DELETE FROM issuingrules');
194 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
195 VALUES (?, ?, ?, ?)},
200 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
201 VALUES (?, ?, ?, ?)},
203 '*', '*', 'CANNOT', 0
206 # make sure some basic sysprefs are set
207 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'homebranch');
208 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
210 # if IndependentBranches is OFF, a CPL patron can reserve an MPL item
211 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
213 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber),
214 'CPL patron allowed to reserve MPL item with IndependentBranches OFF (bug 2394)'
217 # if IndependentBranches is OFF, a CPL patron cannot reserve an MPL item
218 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 1);
219 t
::lib
::Mocks
::mock_preference
('canreservefromotherbranches', 0);
221 ! CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber),
222 'CPL patron NOT allowed to reserve MPL item with IndependentBranches ON ... (bug 2394)'
225 # ... unless canreservefromotherbranches is ON
226 t
::lib
::Mocks
::mock_preference
('canreservefromotherbranches', 1);
228 CanItemBeReserved
($borrowernumbers[0], $foreign_itemnumber),
229 '... unless canreservefromotherbranches is ON (bug 2394)'
232 # Regression test for bug 11336
233 ($bibnum, $title, $bibitemnum) = create_helper_biblio
();
234 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> 'CPL', holdingbranch
=> 'CPL' } , $bibnum);
244 my $reserveid1 = C4
::Reserves
::GetReserveId
(
246 biblionumber
=> $bibnum,
247 borrowernumber
=> $borrowernumbers[0]
251 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> 'CPL', holdingbranch
=> 'CPL' } , $bibnum);
260 my $reserveid2 = C4
::Reserves
::GetReserveId
(
262 biblionumber
=> $bibnum,
263 borrowernumber
=> $borrowernumbers[1]
267 CancelReserve
({ reserve_id
=> $reserveid1 });
269 $reserve2 = GetReserve
( $reserveid2 );
270 is
( $reserve2->{priority
}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
272 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> 'CPL', holdingbranch
=> 'CPL' } , $bibnum);
281 my $reserveid3 = C4
::Reserves
::GetReserveId
(
283 biblionumber
=> $bibnum,
284 borrowernumber
=> $borrowernumbers[0]
288 my $reserve3 = GetReserve
( $reserveid3 );
289 is
( $reserve3->{priority
}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
291 ModReserve
({ reserve_id
=> $reserveid2, rank
=> 'del' });
292 $reserve3 = GetReserve
( $reserveid3 );
293 is
( $reserve3->{priority
}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
295 ModItem
({ damaged
=> 1 }, $item_bibnum, $itemnumber);
296 C4
::Context
->set_preference( 'AllowHoldsOnDamagedItems', 1 );
297 ok
( CanItemBeReserved
( $borrowernumbers[0], $itemnumber), "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
298 ok
( defined( ( CheckReserves
($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
299 C4
::Context
->set_preference( 'AllowHoldsOnDamagedItems', 0 );
300 ok
( !CanItemBeReserved
( $borrowernumbers[0], $itemnumber), "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
301 ok
( !defined( ( CheckReserves
($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
303 # Regression test for bug 9532
304 ($bibnum, $title, $bibitemnum) = create_helper_biblio
('CANNOT');
305 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem
({ homebranch
=> 'CPL', holdingbranch
=> 'CPL', itype
=> 'CANNOT' } , $bibnum);
315 !CanItemBeReserved
( $borrowernumbers[0], $itemnumber),
316 "cannot request item if policy that matches on item-level item type forbids it"
318 ModItem
({ itype
=> 'CAN' }, $item_bibnum, $itemnumber);
320 CanItemBeReserved
( $borrowernumbers[0], $itemnumber),
321 "can request item if policy that matches on item type allows it"
324 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0);
325 ModItem
({ itype
=> undef }, $item_bibnum, $itemnumber);
327 !CanItemBeReserved
( $borrowernumbers[0], $itemnumber),
328 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
331 # Test CancelExpiredReserves
332 C4
::Context
->set_preference('ExpireReservesMaxPickUpDelay', 1);
333 C4
::Context
->set_preference('ReservesMaxPickUpDelay', 1);
335 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
338 $reserves = $dbh->selectall_arrayref('SELECT * FROM reserves', { Slice
=> {} });
339 $reserve = $reserves->[0];
340 my $calendar = C4
::Calendar
->new(branchcode
=> $reserve->{branchcode
});
341 $calendar->insert_single_holiday(
346 description
=> 'Test',
348 $reserve_id = $reserve->{reserve_id
};
349 $dbh->do("UPDATE reserves SET waitingdate = DATE_SUB( NOW(), INTERVAL 5 DAY ), found = 'W', priority = 0 WHERE reserve_id = ?", undef, $reserve_id );
350 C4
::Context
->set_preference('ExpireReservesOnHolidays', 0);
351 CancelExpiredReserves
();
352 my $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
353 is
( $count, 1, "Waiting reserve beyond max pickup delay *not* canceled on holiday" );
354 C4
::Context
->set_preference('ExpireReservesOnHolidays', 1);
355 CancelExpiredReserves
();
356 $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
357 is
( $count, 0, "Waiting reserve beyond max pickup delay canceled on holiday" );
359 # Test expirationdate
360 $reserve = $reserves->[1];
361 $reserve_id = $reserve->{reserve_id
};
362 $dbh->do("UPDATE reserves SET expirationdate = DATE_SUB( NOW(), INTERVAL 1 DAY ) WHERE reserve_id = ?", undef, $reserve_id );
363 CancelExpiredReserves
();
364 $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
365 is
( $count, 0, "Reserve with manual expiration date canceled correctly" );
367 # Helper method to set up a Biblio.
368 sub create_helper_biblio
{
369 my $itemtype = shift;
370 my $bib = MARC
::Record
->new();
371 my $title = 'Silence in the library';
373 MARC
::Field
->new('100', ' ', ' ', a
=> 'Moffat, Steven'),
374 MARC
::Field
->new('245', ' ', ' ', a
=> $title),
375 MARC
::Field
->new('942', ' ', ' ', c
=> $itemtype),
377 return ($bibnum, $title, $bibitemnum) = AddBiblio
($bib, '');