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 => 23;
22 use t::lib::TestBuilder;
26 use lib $FindBin::Bin;
27 use_ok('C4::Reserves');
28 use_ok('C4::HoldsQueue');
31 my $schema = Koha::Database->schema;
32 $schema->storage->txn_begin;
33 my $dbh = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
37 my $library1 = $builder->build({
40 my $library2 = $builder->build({
43 my $library3 = $builder->build({
47 my $TITLE = "Test Holds Queue XXX";
49 my $borrower = $builder->build({
53 branchcode => $library1->{branchcode},
57 my $borrowernumber = $borrower->{borrowernumber};
58 # Set special (for this test) branches
59 my $borrower_branchcode = $borrower->{branchcode};
60 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
61 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
62 my $least_cost_branch_code = pop @other_branches;
63 my @item_types = C4::ItemType->all;
64 my @for_loan = grep { $_->{notforloan} == 0 } @item_types
65 or BAIL_OUT("No adequate itemtype");
66 my $itemtype = $for_loan[0]->{itemtype};
69 # Sysprefs and cost matrix
70 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
71 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
72 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
74 $dbh->do("DELETE FROM transport_cost");
75 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
76 # Favour $least_cost_branch_code
77 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
78 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
79 my @b = @other_branches;
80 while ( my $b1 = shift @b ) {
81 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
82 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
83 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
88 # Loanable items - all possible combinations of homebranch and holdingbranch
89 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
90 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
91 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
92 or BAIL_OUT("Cannot find newly created biblio record");
93 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
94 VALUES ($biblionumber, '', '$itemtype')");
95 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
96 or BAIL_OUT("Cannot find newly created biblioitems record");
98 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
99 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
100 my $first_barcode = int(rand(1000000000000)); # XXX
101 my $barcode = $first_barcode;
102 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
103 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
104 $items_insert_sth->execute($barcode++, $_, $_);
105 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
108 # Remove existing reserves, makes debugging easier
109 $dbh->do("DELETE FROM reserves");
110 my $bibitems = undef;
113 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
114 # $resdate, $expdate, $notes, $title, $checkitem, $found
115 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
118 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
119 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
120 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
121 JOIN items USING (itemnumber)
122 WHERE borrowernumber = $borrowernumber");
124 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
125 C4::Context->set_preference('AutomaticItemReturn', 0);
126 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
127 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
129 $dbh->do("DELETE FROM tmp_holdsqueue");
130 $dbh->do("DELETE FROM hold_fill_targets");
131 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
132 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
133 # test_queue will flush
134 C4::Context->set_preference('AutomaticItemReturn', 1);
135 # Not sure how to make this test more difficult - holding branch does not matter
137 $dbh->do("DELETE FROM tmp_holdsqueue");
138 $dbh->do("DELETE FROM hold_fill_targets");
139 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
140 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
141 C4::Context->set_preference('AutomaticItemReturn', 0);
142 # We have a book available held in borrower branch
143 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
144 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
146 $dbh->do("DELETE FROM tmp_holdsqueue");
147 $dbh->do("DELETE FROM hold_fill_targets");
148 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
149 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
150 # No book available in borrower branch, pick according to the rules
151 # Frst branch from StaticHoldsQueueWeight
152 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
153 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
154 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
155 my $queue_item = $queue->[0];
157 && $queue_item->{pickbranch} eq $borrower_branchcode
158 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
159 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
160 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
163 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
164 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
167 # XXX All this tests are for borrower branch pick-up.
168 # Maybe needs expanding to homebranch or holdingbranch pick-up.
170 $schema->txn_rollback;
173 ### Test holds queue builder does not violate holds policy ###
175 # Clear out existing rules relating to holdallowed
176 $dbh->do("DELETE FROM default_branch_circ_rules");
177 $dbh->do("DELETE FROM default_branch_item_rules");
178 $dbh->do("DELETE FROM default_circ_rules");
180 C4::Context->set_preference('UseTransportCostMatrix', 0);
182 ( $itemtype ) = @{ $dbh->selectrow_arrayref("SELECT itemtype FROM itemtypes LIMIT 1") };
184 $library1 = $builder->build({
187 $library2 = $builder->build({
190 $library3 = $builder->build({
193 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
195 my $borrower1 = $builder->build({
196 source => 'Borrower',
199 branchcode => $branchcodes[0],
202 my $borrower2 = $builder->build({
203 source => 'Borrower',
206 branchcode => $branchcodes[1],
209 my $borrower3 = $builder->build({
210 source => 'Borrower',
213 branchcode => $branchcodes[2],
230 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
231 or BAIL_OUT("Cannot find newly created biblio record");
234 INSERT INTO biblioitems (
244 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
245 or BAIL_OUT("Cannot find newly created biblioitems record");
247 $items_insert_sth = $dbh->prepare(qq{
274 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
275 $barcode = int( rand(1000000000000) );
276 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
277 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
278 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
280 $dbh->do("DELETE FROM reserves");
281 my $sth = $dbh->prepare(q{
282 INSERT INTO reserves (
288 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
290 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
291 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
292 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
296 $dbh->do("DELETE FROM default_circ_rules");
297 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
298 C4::HoldsQueue::CreateQueue();
299 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
300 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
301 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
302 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
304 $dbh->do("DELETE FROM default_circ_rules");
305 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
306 C4::HoldsQueue::CreateQueue();
307 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
308 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
309 #warn "HOLDS QUEUE: " . Data::Dumper::Dumper( $holds_queue );
312 $itemtype = $item_types[0]->{itemtype};
313 $borrowernumber = $borrower3->{borrowernumber};
314 my $library_A = $library1->{branchcode};
315 my $library_B = $library2->{branchcode};
316 my $library_C = $borrower3->{branchcode};
317 $dbh->do("DELETE FROM reserves");
318 $dbh->do("DELETE FROM issues");
319 $dbh->do("DELETE FROM items");
320 $dbh->do("DELETE FROM biblio");
321 $dbh->do("DELETE FROM biblioitems");
322 $dbh->do("DELETE FROM transport_cost");
323 $dbh->do("DELETE FROM tmp_holdsqueue");
324 $dbh->do("DELETE FROM hold_fill_targets");
325 $dbh->do("DELETE FROM default_branch_circ_rules");
326 $dbh->do("DELETE FROM default_branch_item_rules");
327 $dbh->do("DELETE FROM default_circ_rules");
328 $dbh->do("DELETE FROM branch_item_rules");
331 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
334 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
335 or BAIL_OUT("Cannot find newly created biblio record");
337 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
340 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
341 or BAIL_OUT("Cannot find newly created biblioitems record");
344 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
345 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
349 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
350 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
354 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
355 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
358 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
359 undef, join( ',', $library_B, $library_A, $library_C ) );
360 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
362 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
363 C4::HoldsQueue::CreateQueue();
364 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
365 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
369 $itemtype = $item_types[0]->{itemtype};
370 $borrowernumber = $borrower2->{borrowernumber};
371 $library_A = $library1->{branchcode};
372 $library_B = $library2->{branchcode};
373 $dbh->do("DELETE FROM reserves");
374 $dbh->do("DELETE FROM issues");
375 $dbh->do("DELETE FROM items");
376 $dbh->do("DELETE FROM biblio");
377 $dbh->do("DELETE FROM biblioitems");
378 $dbh->do("DELETE FROM transport_cost");
379 $dbh->do("DELETE FROM tmp_holdsqueue");
380 $dbh->do("DELETE FROM hold_fill_targets");
381 $dbh->do("DELETE FROM default_branch_circ_rules");
382 $dbh->do("DELETE FROM default_branch_item_rules");
383 $dbh->do("DELETE FROM default_circ_rules");
384 $dbh->do("DELETE FROM branch_item_rules");
386 C4::Context->set_preference("UseTransportCostMatrix",1);
388 my $tc_rs = $schema->resultset('TransportCost');
389 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
390 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
393 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
396 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
397 or BAIL_OUT("Cannot find newly created biblio record");
399 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
402 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
403 or BAIL_OUT("Cannot find newly created biblioitems record");
406 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
407 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
410 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
411 C4::HoldsQueue::CreateQueue();
412 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
413 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
417 $schema->storage->txn_rollback;
419 ### END Test holds queue builder does not violate holds policy ###
422 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
424 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
426 $use_cost_matrix_sth->execute($use_cost_matrix);
427 C4::Context->clear_syspref_cache();
428 C4::HoldsQueue::CreateQueue();
430 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
431 my $r = $results->[0];
433 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
434 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
437 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
438 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
443 my ($tablename) = @_;
444 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);