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 => 17;
24 use lib $FindBin::Bin;
25 use_ok('C4::Reserves');
26 use_ok('C4::HoldsQueue');
30 my $dbh = C4::Context->dbh;
31 $dbh->{AutoCommit} = 0;
32 $dbh->{RaiseError} = 1;
34 my $TITLE = "Test Holds Queue XXX";
37 cardnumber => 'CARDNUMBER42',
38 firstname => 'my firstname',
39 surname => 'my surname',
44 my $borrowernumber = AddMember(%data);
45 my $borrower = GetMember( borrowernumber => $borrowernumber );
46 # Set special (for this test) branches
47 my $borrower_branchcode = $borrower->{branchcode};
48 my $branches = C4::Branch::GetBranches;
49 my @other_branches = grep { $_ ne $borrower_branchcode } keys %$branches;
50 my $least_cost_branch_code = pop @other_branches
51 or BAIL_OUT("No point testing only one branch...");
52 my @item_types = C4::ItemType->all;
53 my $itemtype = grep { $_->{notforloan} == 1 } @item_types
54 or BAIL_OUT("No adequate itemtype");
57 # Sysprefs and cost matrix
58 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
59 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
60 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
62 $dbh->do("DELETE FROM transport_cost");
63 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
64 # Favour $least_cost_branch_code
65 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
66 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
67 my @b = @other_branches;
68 while ( my $b1 = shift @b ) {
69 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
70 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
71 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
76 # Loanable items - all possible combinations of homebranch and holdingbranch
77 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
78 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
79 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
80 or BAIL_OUT("Cannot find newly created biblio record");
81 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
82 VALUES ($biblionumber, '', '$itemtype')");
83 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
84 or BAIL_OUT("Cannot find newly created biblioitems record");
86 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
87 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
88 my $first_barcode = int(rand(1000000000000)); # XXX
89 my $barcode = $first_barcode;
90 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
91 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
92 $items_insert_sth->execute($barcode++, $_, $_);
93 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
96 # Remove existing reserves, makes debugging easier
97 $dbh->do("DELETE FROM reserves");
98 my $constraint = undef;
102 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $constraint, $bibitems, $priority );
103 # $resdate, $expdate, $notes, $title, $checkitem, $found
104 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
107 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
108 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
109 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
110 JOIN items USING (itemnumber)
111 WHERE borrowernumber = $borrowernumber");
113 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
114 C4::Context->set_preference('AutomaticItemReturn', 0);
115 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
116 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
118 $dbh->do("DELETE FROM tmp_holdsqueue");
119 $dbh->do("DELETE FROM hold_fill_targets");
120 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
121 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
122 # test_queue will flush
123 C4::Context->set_preference('AutomaticItemReturn', 1);
124 # Not sure how to make this test more difficult - holding branch does not matter
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 homebranch = '$borrower_branchcode')");
129 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
130 C4::Context->set_preference('AutomaticItemReturn', 0);
131 # We have a book available held in borrower branch
132 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
133 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
135 $dbh->do("DELETE FROM tmp_holdsqueue");
136 $dbh->do("DELETE FROM hold_fill_targets");
137 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
138 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
139 # No book available in borrower branch, pick according to the rules
140 # Frst branch from StaticHoldsQueueWeight
141 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
142 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
143 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
144 my $queue_item = $queue->[0];
146 && $queue_item->{pickbranch} eq $borrower_branchcode
147 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
148 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
149 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
152 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
153 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
156 # XXX All this tests are for borrower branch pick-up.
157 # Maybe needs expanding to homebranch or holdingbranch pick-up.
163 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
165 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
167 $use_cost_matrix_sth->execute($use_cost_matrix);
168 C4::Context->clear_syspref_cache();
169 C4::HoldsQueue::CreateQueue();
171 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
172 my $r = $results->[0];
174 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
175 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
178 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
179 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
184 my ($tablename) = @_;
185 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);