6 use t::lib::TestBuilder;
10 use Test::More tests => 57;
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" );
125 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
126 $holds = $patron->holds;
127 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
130 ok( GetReserveCount( $borrowernumbers[0] ), "Test GetReserveCount()" );
133 CancelReserve({ 'reserve_id' => $reserve_id });
134 $holds = $biblio->holds;
135 is( $holds->count, $borrowers_count - 1, "Test CancelReserve()" );
137 $holds = $item->current_holds;
138 $first_hold = $holds->next;
139 $borrowernumber = $first_hold->borrowernumber;
140 $branch_1code = $first_hold->branchcode;
141 $reserve_id = $first_hold->reserve_id;
144 reserve_id => $reserve_id,
146 branchcode => $branch_1,
147 itemnumber => $itemnumber,
148 suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
151 my $reserve = GetReserve( $reserve_id );
152 ok( $reserve->{'priority'} eq '4', "Test GetReserve(), priority changed correctly" );
153 ok( $reserve->{'suspend'}, "Test GetReserve(), suspend hold" );
154 is( $reserve->{'suspend_until'}, '2013-01-01 00:00:00', "Test GetReserve(), suspend until date" );
156 ToggleSuspend( $reserve_id );
157 $reserve = GetReserve( $reserve_id );
158 ok( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
160 ToggleSuspend( $reserve_id, '2012-01-01' );
161 $reserve = GetReserve( $reserve_id );
162 is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
164 AutoUnsuspendReserves();
165 $reserve = GetReserve( $reserve_id );
166 ok( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
169 borrowernumber => $borrowernumber,
170 biblionumber => $biblionumber,
172 suspend_until => '2012-01-01',
174 $reserve = GetReserve( $reserve_id );
175 is( $reserve->{'suspend'}, 1, "Test SuspendAll()" );
176 is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
179 borrowernumber => $borrowernumber,
180 biblionumber => $biblionumber,
183 $reserve = GetReserve( $reserve_id );
184 is( $reserve->{'suspend'}, 0, "Test resuming with SuspendAll()" );
185 is( $reserve->{'suspend_until'}, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
187 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
201 $patron = Koha::Patrons->find( $borrowernumber );
202 $holds = $patron->holds;
203 my $reserveid = C4::Reserves::GetReserveId(
205 biblionumber => $biblionumber,
206 borrowernumber => $borrowernumber
209 is( $reserveid, $holds->next->reserve_id, "Test GetReserveId" );
210 ModReserveMinusPriority( $itemnumber, $reserve->{'reserve_id'} );
211 $holds = $patron->holds;
212 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
214 $holds = $biblio->holds;
215 $hold = $holds->next;
216 AlterPriority( 'top', $hold->reserve_id );
217 $reserve = GetReserve( $reserve->{'reserve_id'} );
218 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" );
220 AlterPriority( 'down', $reserve->{'reserve_id'} );
221 $reserve = GetReserve( $reserve->{'reserve_id'} );
222 is( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
224 AlterPriority( 'up', $reserve->{'reserve_id'} );
225 $reserve = GetReserve( $reserve->{'reserve_id'} );
226 is( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
228 AlterPriority( 'bottom', $reserve->{'reserve_id'} );
229 $reserve = GetReserve( $reserve->{'reserve_id'} );
230 is( $reserve->{'priority'}, '5', "Test AlterPriority(), move to bottom" );
232 # Regression test for bug 2394
234 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
235 # a patron is not permittedo to request an item whose homebranch (i.e.,
236 # owner of the item) is different from the patron's own library.
237 # However, if canreservefromotherbranches is turned ON, the patron can
238 # create such hold requests.
240 # Note that canreservefromotherbranches has no effect if
241 # IndependentBranches is OFF.
243 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
244 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
245 = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
246 $dbh->do('DELETE FROM issuingrules');
248 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
249 VALUES (?, ?, ?, ?, ?)},
251 '*', '*', '*', 25, 99
254 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
255 VALUES (?, ?, ?, ?, ?)},
257 '*', '*', 'CANNOT', 0, 99
260 # make sure some basic sysprefs are set
261 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
262 t::lib::Mocks::mock_preference('item-level_itypes', 1);
264 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
265 t::lib::Mocks::mock_preference('IndependentBranches', 0);
267 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
268 '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
271 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
272 t::lib::Mocks::mock_preference('IndependentBranches', 1);
273 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
275 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
276 '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
279 # ... unless canreservefromotherbranches is ON
280 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
282 CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
283 '... unless canreservefromotherbranches is ON (bug 2394)'
286 # Regression test for bug 11336
287 ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
288 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
297 my $reserveid1 = C4::Reserves::GetReserveId(
299 biblionumber => $bibnum,
300 borrowernumber => $borrowernumbers[0]
304 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
312 my $reserveid2 = C4::Reserves::GetReserveId(
314 biblionumber => $bibnum,
315 borrowernumber => $borrowernumbers[1]
319 CancelReserve({ reserve_id => $reserveid1 });
321 my $reserve2 = GetReserve( $reserveid2 );
322 is( $reserve2->{priority}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
324 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
332 my $reserveid3 = C4::Reserves::GetReserveId(
334 biblionumber => $bibnum,
335 borrowernumber => $borrowernumbers[0]
339 my $reserve3 = GetReserve( $reserveid3 );
340 is( $reserve3->{priority}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
342 ModReserve({ reserve_id => $reserveid2, rank => 'del' });
343 $reserve3 = GetReserve( $reserveid3 );
344 is( $reserve3->{priority}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
346 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
347 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
348 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
349 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
351 $hold = Koha::Hold->new(
353 borrowernumber => $borrowernumbers[0],
354 itemnumber => $itemnumber,
355 biblionumber => $item_bibnum,
358 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
360 "Patron cannot place a second item level hold for a given item" );
363 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
364 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
365 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
367 # Regression test for bug 9532
368 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
369 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
378 CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
379 "cannot request item if policy that matches on item-level item type forbids it"
381 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
383 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
384 "can request item if policy that matches on item type allows it"
387 t::lib::Mocks::mock_preference('item-level_itypes', 0);
388 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
390 CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
391 "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
395 # Test branch item rules
397 $dbh->do('DELETE FROM issuingrules');
399 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
400 VALUES (?, ?, ?, ?)},
404 $dbh->do('DELETE FROM branch_item_rules');
405 $dbh->do('DELETE FROM default_branch_circ_rules');
406 $dbh->do('DELETE FROM default_branch_item_rules');
407 $dbh->do('DELETE FROM default_circ_rules');
409 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
411 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
413 INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
415 }, {}, $branch_1, 'CAN', 1, 'homebranch');
416 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
417 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
418 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
419 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
420 "CanItemBeReserved should returns 'notReservable'");
422 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
423 { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
424 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
425 'cannotReserveFromOtherBranches',
426 "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
428 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
429 { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
430 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
431 "CanItemBeReserved should returns 'OK'");
434 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
435 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
437 $dbh->do('DELETE FROM reserves');
438 $dbh->do('DELETE FROM issues');
439 $dbh->do('DELETE FROM items');
440 $dbh->do('DELETE FROM biblio');
442 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
443 ( $item_bibnum, $item_bibitemnum, $itemnumber )
444 = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
447 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
448 VALUES (?, ?, ?, ?, ?)},
450 '*', '*', 'ONLY1', 1, 99
452 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
453 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
455 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
457 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
458 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
461 # Helper method to set up a Biblio.
462 sub create_helper_biblio {
463 my $itemtype = shift;
464 my $bib = MARC::Record->new();
465 my $title = 'Silence in the library';
467 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
468 MARC::Field->new('245', ' ', ' ', a => $title),
469 MARC::Field->new('942', ' ', ' ', c => $itemtype),
471 return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');