3 # Test C4::HoldsQueue::CreateQueue() for both transport cost matrix
4 # and StaticHoldsQueueWeight array (no RandomizeHoldsQueueWeight, no point)
5 # Wraps tests in transaction that's rolled back, so no data is destroyed
6 # MySQL WARNING: This makes sense only if your tables are InnoDB, otherwise
7 # transactions are not supported and mess is left behind
15 use Test::More tests => 18;
19 use lib $FindBin::Bin;
20 use_ok('C4::Reserves');
21 use_ok('C4::HoldsQueue');
24 my $TITLE = "Test Holds Queue XXX";
25 # Pick a plausible borrower. Easier than creating one.
26 my $BORROWER_QRY = <<EOQ;
29 where borrowernumber = (select max(borrowernumber) from issues)
31 my $dbh = C4::Context->dbh;
32 my $borrower = $dbh->selectrow_hashref($BORROWER_QRY);
33 my $borrowernumber = $borrower->{borrowernumber};
34 # Set special (for this test) branches
35 my $borrower_branchcode = $borrower->{branchcode};
36 my @other_branches = grep { $_ ne $borrower_branchcode } @{ $dbh->selectcol_arrayref("SELECT branchcode FROM branches") };
37 my $least_cost_branch_code = pop @other_branches
38 or BAIL_OUT("No point testing only one branch...");
39 my $itemtype = $dbh->selectrow_array("SELECT min(itemtype) FROM itemtypes WHERE notforloan = 0")
40 or BAIL_OUT("No adequate itemtype");
43 $dbh->{AutoCommit} = 0;
44 $dbh->{RaiseError} = 1;
47 # Sysprefs and cost matrix
48 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
49 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
50 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
52 $dbh->do("DELETE FROM transport_cost");
53 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
54 # Favour $least_cost_branch_code
55 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
56 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
57 my @b = @other_branches;
58 while ( my $b1 = shift @b ) {
59 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
60 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
61 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
66 # Loanable items - all possible combinations of homebranch and holdingbranch
67 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
68 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
69 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
70 or BAIL_OUT("Cannot find newly created biblio record");
71 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
72 VALUES ($biblionumber, '', '$itemtype')");
73 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
74 or BAIL_OUT("Cannot find newly created biblioitems record");
76 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, wthdrawn, onloan, itype)
77 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
78 my $first_barcode = int(rand(1000000000000)); # XXX
79 my $barcode = $first_barcode;
80 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
81 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
82 $items_insert_sth->execute($barcode++, $_, $_);
83 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
86 # Remove existing reserves, makes debugging easier
87 $dbh->do("DELETE FROM reserves");
88 my $constraint = undef;
92 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $constraint, $bibitems, $priority );
93 # $resdate, $expdate, $notes, $title, $checkitem, $found
94 $dbh->do("UPDATE reserves SET reservedate = reservedate - 1");
97 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
98 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
99 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
100 JOIN items USING (itemnumber)
101 WHERE borrowernumber = $borrowernumber");
103 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
104 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
105 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
107 $dbh->do("DELETE FROM tmp_holdsqueue");
108 $dbh->do("DELETE FROM hold_fill_targets");
109 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
110 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
111 # test_queue will flush
112 $dbh->do("UPDATE systempreferences SET value = 1 WHERE variable = 'AutomaticItemReturn'");
113 # Not sure how to make this test more difficult - holding branch does not matter
114 test_queue ('take from holdingbranch AutomaticItemReturn on', 0, $borrower_branchcode, undef);
115 test_queue ('take from holdingbranch AutomaticItemReturn on', 1, $borrower_branchcode, $least_cost_branch_code);
117 $dbh->do("DELETE FROM tmp_holdsqueue");
118 $dbh->do("DELETE FROM hold_fill_targets");
119 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
120 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
121 $dbh->do("UPDATE systempreferences SET value = 0 WHERE variable = 'AutomaticItemReturn'");
122 # We have a book available held in borrower branch
123 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
124 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
126 $dbh->do("DELETE FROM tmp_holdsqueue");
127 $dbh->do("DELETE FROM hold_fill_targets");
128 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
129 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
130 # No book available in borrower branch, pick according to the rules
131 # Frst branch from StaticHoldsQueueWeight
132 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
133 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
134 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
135 my $queue_item = $queue->[0];
137 && $queue_item->{pickbranch} eq $borrower_branchcode
138 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
139 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
141 # XXX All this tests are for borrower branch pick-up.
142 # Maybe needs expanding to homebranch or holdingbranch pick-up.
150 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
152 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
154 $use_cost_matrix_sth->execute($use_cost_matrix);
155 C4::Context->clear_syspref_cache();
156 C4::HoldsQueue::CreateQueue();
158 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
159 my $r = $results->[0];
161 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
162 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
165 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
166 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
171 my ($tablename) = @_;
172 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);