use C4::Context;
-use Test::More tests => 61;
+use Test::More tests => 55;
use MARC::Record;
use C4::Biblio;
use C4::Items;
-use C4::Members;
use C4::Calendar;
use Koha::Database;
use Koha::DateUtils qw( dt_from_string output_pref );
use Koha::Biblios;
use Koha::Holds;
+use Koha::Patrons;
BEGIN {
use FindBin;
# Create some borrowers
my @borrowernumbers;
foreach (1..$borrowers_count) {
- my $borrowernumber = AddMember(
+ my $borrowernumber = Koha::Patron->new({
firstname => 'my firstname',
surname => 'my surname ' . $_,
categorycode => $category->{categorycode},
branchcode => $branch_1,
- );
+ })->store->borrowernumber;
push @borrowernumbers, $borrowernumber;
}
is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
-my ( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber($itemnumber);
-is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "GetReservesFromItemnumber should return a valid reserve date");
-is( $borrowernumber, $borrowernumbers[0], "GetReservesFromItemnumber should return a valid borrowernumber");
-is( $branch_1code, $branch_1, "GetReservesFromItemnumber should return a valid branchcode");
-ok($reserve_id, "Test GetReservesFromItemnumber()");
+my $item = Koha::Items->find( $itemnumber );
+$holds = $item->current_holds;
+my $first_hold = $holds->next;
+my $reservedate = $first_hold->reservedate;
+my $borrowernumber = $first_hold->borrowernumber;
+my $branch_1code = $first_hold->branchcode;
+my $reserve_id = $first_hold->reserve_id;
+is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
+is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
+is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
+ok($reserve_id, "Test holds_placed_today()");
my $hold = Koha::Holds->find( $reserve_id );
ok( $hold, "Koha::Holds found the hold" );
my $hold_found = $hold->found();
$hold->set({ found => 'W'})->store();
is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
+is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
-my ( $reserve ) = GetReservesFromBorrowernumber($borrowernumbers[0]);
-ok( $reserve->{'borrowernumber'} eq $borrowernumbers[0], "Test GetReservesFromBorrowernumber()");
-
+my $patron = Koha::Patrons->find( $borrowernumbers[0] );
+$holds = $patron->holds;
+is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
-ok( GetReserveCount( $borrowernumbers[0] ), "Test GetReserveCount()" );
+$holds = $item->current_holds;
+$first_hold = $holds->next;
+$borrowernumber = $first_hold->borrowernumber;
+$branch_1code = $first_hold->branchcode;
+$reserve_id = $first_hold->reserve_id;
-CancelReserve({ 'reserve_id' => $reserve_id });
-$holds = $biblio->holds;
-is( $holds->count, $borrowers_count - 1, "Test CancelReserve()" );
-
-
-( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber($itemnumber);
ModReserve({
reserve_id => $reserve_id,
rank => '4',
suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
});
-$reserve = GetReserve( $reserve_id );
-ok( $reserve->{'priority'} eq '4', "Test GetReserve(), priority changed correctly" );
-ok( $reserve->{'suspend'}, "Test GetReserve(), suspend hold" );
-is( $reserve->{'suspend_until'}, '2013-01-01 00:00:00', "Test GetReserve(), suspend until date" );
+$hold = Koha::Holds->find( $reserve_id );
+ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
+ok( $hold->suspend, "Test ModReserve, suspend hold" );
+is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
+
+ModReserve({ # call without reserve_id
+ rank => '3',
+ biblionumber => $item_bibnum,
+ itemnumber => $itemnumber,
+ borrowernumber => $borrowernumber,
+});
+$hold = Koha::Holds->find( $reserve_id );
+ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
ToggleSuspend( $reserve_id );
-$reserve = GetReserve( $reserve_id );
-ok( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
+$hold = Koha::Holds->find( $reserve_id );
+ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
ToggleSuspend( $reserve_id, '2012-01-01' );
-$reserve = GetReserve( $reserve_id );
-is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
+$hold = Koha::Holds->find( $reserve_id );
+is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
AutoUnsuspendReserves();
-$reserve = GetReserve( $reserve_id );
-ok( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
+$hold = Koha::Holds->find( $reserve_id );
+ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
SuspendAll(
borrowernumber => $borrowernumber,
suspend => 1,
suspend_until => '2012-01-01',
);
-$reserve = GetReserve( $reserve_id );
-is( $reserve->{'suspend'}, 1, "Test SuspendAll()" );
-is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
+$hold = Koha::Holds->find( $reserve_id );
+is( $hold->suspend, 1, "Test SuspendAll()" );
+is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
SuspendAll(
borrowernumber => $borrowernumber,
biblionumber => $biblionumber,
suspend => 0,
);
-$reserve = GetReserve( $reserve_id );
-is( $reserve->{'suspend'}, 0, "Test resuming with SuspendAll()" );
-is( $reserve->{'suspend_until'}, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
+$hold = Koha::Holds->find( $reserve_id );
+is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
+is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
# Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
AddReserve(
my $checkitem,
my $found,
);
-( $reserve ) = GetReservesFromBorrowernumber($borrowernumber);
-my $reserveid = C4::Reserves::GetReserveId(
- {
- biblionumber => $biblionumber,
- borrowernumber => $borrowernumber
- }
-);
-is( $reserveid, $reserve->{reserve_id}, "Test GetReserveId" );
-ModReserveMinusPriority( $itemnumber, $reserve->{'reserve_id'} );
-( $reserve ) = GetReservesFromBorrowernumber($borrowernumber);
-ok( $reserve->{'itemnumber'} eq $itemnumber, "Test ModReserveMinusPriority()" );
-
-
-my $reserve2 = GetReserveInfo( $reserve->{'reserve_id'} );
-ok( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" );
-
+$patron = Koha::Patrons->find( $borrowernumber );
+$holds = $patron->holds;
+my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
+ModReserveMinusPriority( $itemnumber, $reserveid );
+$holds = $patron->holds;
+is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
$holds = $biblio->holds;
-my $hold = $holds->next;
+$hold = $holds->next;
AlterPriority( 'top', $hold->reserve_id );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '1', "Test AlterPriority(), move to top" );
-AlterPriority( 'down', $reserve->{'reserve_id'} );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
+AlterPriority( 'down', $hold->reserve_id );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '2', "Test AlterPriority(), move down" );
-AlterPriority( 'up', $reserve->{'reserve_id'} );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
+AlterPriority( 'up', $hold->reserve_id );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '1', "Test AlterPriority(), move up" );
-AlterPriority( 'bottom', $reserve->{'reserve_id'} );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '5', "Test AlterPriority(), move to bottom" );
+AlterPriority( 'bottom', $hold->reserve_id );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
# Regression test for bug 2394
#
'... unless canreservefromotherbranches is ON (bug 2394)'
);
-# Regression test for bug 11336
-($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-AddReserve(
- $branch_1,
- $borrowernumbers[0],
- $bibnum,
- '',
- 1,
-);
-
-my $reserveid1 = C4::Reserves::GetReserveId(
- {
- biblionumber => $bibnum,
- borrowernumber => $borrowernumbers[0]
- }
-);
-
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-AddReserve(
- $branch_1,
- $borrowernumbers[1],
- $bibnum,
- '',
- 2,
-);
-my $reserveid2 = C4::Reserves::GetReserveId(
- {
- biblionumber => $bibnum,
- borrowernumber => $borrowernumbers[1]
- }
-);
-
-CancelReserve({ reserve_id => $reserveid1 });
-
-$reserve2 = GetReserve( $reserveid2 );
-is( $reserve2->{priority}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
-
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-AddReserve(
- $branch_1,
- $borrowernumbers[0],
- $bibnum,
- '',
- 2,
-);
-my $reserveid3 = C4::Reserves::GetReserveId(
- {
- biblionumber => $bibnum,
- borrowernumber => $borrowernumbers[0]
- }
-);
-
-my $reserve3 = GetReserve( $reserveid3 );
-is( $reserve3->{priority}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
-
-ModReserve({ reserve_id => $reserveid2, rank => 'del' });
-$reserve3 = GetReserve( $reserveid3 );
-is( $reserve3->{priority}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
+{
+ # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
+ ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
+ ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+ my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
+ ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+ my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
+ ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+ my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
+ my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
+ my $hold3 = Koha::Holds->find( $reserveid3 );
+ is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
+ ModReserve({ reserve_id => $reserveid1, rank => 'del' });
+ ModReserve({ reserve_id => $reserveid2, rank => 'del' });
+ is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
+}
ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
{ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
- "CanItemBeReserved should returns 'notReservable'");
+ "CanItemBeReserved should return 'notReservable'");
($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
{ homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
'cannotReserveFromOtherBranches',
- "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
+ "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
{ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
- "CanItemBeReserved should returns 'OK'");
-
-
-# Test CancelExpiredReserves
-t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
-t::lib::Mocks::mock_preference('ReservesMaxPickUpDelay', 1);
-
-my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
-$year += 1900;
-$mon += 1;
-my $reserves = $dbh->selectall_arrayref('SELECT * FROM reserves', { Slice => {} });
-$reserve = $reserves->[0];
-my $calendar = C4::Calendar->new(branchcode => $reserve->{branchcode});
-$calendar->insert_single_holiday(
- day => $mday,
- month => $mon,
- year => $year,
- title => 'Test',
- description => 'Test',
-);
-$reserve_id = $reserve->{reserve_id};
-$dbh->do("UPDATE reserves SET waitingdate = DATE_SUB( NOW(), INTERVAL 5 DAY ), found = 'W', priority = 0 WHERE reserve_id = ?", undef, $reserve_id );
-t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0);
-CancelExpiredReserves();
-my $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
-is( $count, 1, "Waiting reserve beyond max pickup delay *not* canceled on holiday" );
-t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
-CancelExpiredReserves();
-$count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
-is( $count, 0, "Waiting reserve beyond max pickup delay canceled on holiday" );
-
-# Test expirationdate
-$reserve = $reserves->[1];
-$reserve_id = $reserve->{reserve_id};
-$dbh->do("UPDATE reserves SET expirationdate = DATE_SUB( NOW(), INTERVAL 1 DAY ) WHERE reserve_id = ?", undef, $reserve_id );
-CancelExpiredReserves();
-$count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id );
-is( $count, 0, "Reserve with manual expiration date canceled correctly" );
+ "CanItemBeReserved should return 'OK'");
# Bug 12632
t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );