# MySQL WARNING: This makes sense only if your tables are InnoDB, otherwise
# transactions are not supported and mess is left behind
-use strict;
-use warnings;
+use Modern::Perl;
+
use C4::Context;
use Data::Dumper;
-use Test::More tests => 18;
+use Test::More tests => 17;
+
+
+use C4::Branch;
+use C4::ItemType;
+use C4::Members;
BEGIN {
use FindBin;
use_ok('C4::HoldsQueue');
}
-my $TITLE = "Test Holds Queue XXX";
-# Pick a plausible borrower. Easier than creating one.
-my $BORROWER_QRY = <<EOQ;
-select *
-from borrowers
-where borrowernumber = (select max(borrowernumber) from issues)
-EOQ
+# Start transaction
my $dbh = C4::Context->dbh;
-my $borrower = $dbh->selectrow_hashref($BORROWER_QRY);
-my $borrowernumber = $borrower->{borrowernumber};
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
+my $TITLE = "Test Holds Queue XXX";
+
+my %data = (
+ cardnumber => 'CARDNUMBER42',
+ firstname => 'my firstname',
+ surname => 'my surname',
+ categorycode => 'S',
+ branchcode => 'CPL',
+);
+
+my $borrowernumber = AddMember(%data);
+my $borrower = GetMember( borrowernumber => $borrowernumber );
# Set special (for this test) branches
my $borrower_branchcode = $borrower->{branchcode};
-my @other_branches = grep { $_ ne $borrower_branchcode } @{ $dbh->selectcol_arrayref("SELECT branchcode FROM branches") };
+my $branches = C4::Branch::GetBranches;
+my @other_branches = grep { $_ ne $borrower_branchcode } keys %$branches;
my $least_cost_branch_code = pop @other_branches
or BAIL_OUT("No point testing only one branch...");
-my $itemtype = $dbh->selectrow_array("SELECT min(itemtype) FROM itemtypes WHERE notforloan = 0")
+my @item_types = C4::ItemType->all;
+my $itemtype = grep { $_->{notforloan} == 1 } @item_types
or BAIL_OUT("No adequate itemtype");
-# Start transaction
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
-
#Set up the stage
# Sysprefs and cost matrix
$dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
# Make a reserve
AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $constraint, $bibitems, $priority );
# $resdate, $expdate, $notes, $title, $checkitem, $found
-$dbh->do("UPDATE reserves SET reservedate = reservedate - 1");
+$dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
# Tests
my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
WHERE borrowernumber = $borrowernumber");
# We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
+C4::Context->set_preference('AutomaticItemReturn', 0);
test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
$dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
$dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
# test_queue will flush
-$dbh->do("UPDATE systempreferences SET value = 1 WHERE variable = 'AutomaticItemReturn'");
+C4::Context->set_preference('AutomaticItemReturn', 1);
# Not sure how to make this test more difficult - holding branch does not matter
-test_queue ('take from holdingbranch AutomaticItemReturn on', 0, $borrower_branchcode, undef);
-test_queue ('take from holdingbranch AutomaticItemReturn on', 1, $borrower_branchcode, $least_cost_branch_code);
$dbh->do("DELETE FROM tmp_holdsqueue");
$dbh->do("DELETE FROM hold_fill_targets");
$dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
$dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
-$dbh->do("UPDATE systempreferences SET value = 0 WHERE variable = 'AutomaticItemReturn'");
+C4::Context->set_preference('AutomaticItemReturn', 0);
# We have a book available held in borrower branch
test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
&& $queue_item->{pickbranch} eq $borrower_branchcode
&& $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
+ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
+
+ok(
+ C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
+ 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
+);
# XXX All this tests are for borrower branch pick-up.
# Maybe needs expanding to homebranch or holdingbranch pick-up.
# Cleanup
$dbh->rollback;
-exit;
-
sub test_queue {
my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;