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
11 use Test::More tests => 42;
20 use t::lib::TestBuilder;
25 use lib $FindBin::Bin;
26 use_ok('C4::Reserves');
27 use_ok('C4::HoldsQueue');
30 my $schema = Koha::Database->schema;
31 $schema->storage->txn_begin;
32 my $dbh = C4::Context->dbh;
34 my $builder = t::lib::TestBuilder->new;
36 my $library1 = $builder->build({
39 my $library2 = $builder->build({
42 my $library3 = $builder->build({
46 my $TITLE = "Test Holds Queue XXX";
48 my $borrower = $builder->build({
51 branchcode => $library1->{branchcode},
55 my $borrowernumber = $borrower->{borrowernumber};
56 # Set special (for this test) branches
57 my $borrower_branchcode = $borrower->{branchcode};
58 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
59 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
60 my $least_cost_branch_code = pop @other_branches;
61 my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
64 # Sysprefs and cost matrix
65 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
66 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
67 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
68 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
69 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
71 $dbh->do("DELETE FROM transport_cost");
72 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
73 # Favour $least_cost_branch_code
74 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
75 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
76 my @b = @other_branches;
77 while ( my $b1 = shift @b ) {
78 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
79 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
80 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
85 # Loanable items - all possible combinations of homebranch and holdingbranch
86 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
87 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
88 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
89 or BAIL_OUT("Cannot find newly created biblio record");
90 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype)
91 VALUES ($biblionumber, '$itemtype')");
92 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
93 or BAIL_OUT("Cannot find newly created biblioitems record");
95 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
96 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
97 my $first_barcode = int(rand(1000000000000)); # XXX
98 my $barcode = $first_barcode;
99 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
100 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
101 $items_insert_sth->execute($barcode++, $_, $_);
102 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
105 # Remove existing reserves, makes debugging easier
106 $dbh->do("DELETE FROM reserves");
107 my $bibitems = undef;
110 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
111 # $resdate, $expdate, $notes, $title, $checkitem, $found
112 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
115 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
116 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
117 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
118 JOIN items USING (itemnumber)
119 WHERE borrowernumber = $borrowernumber");
121 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
122 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
123 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
124 test_queue ('take from homebranch', 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 homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
129 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
130 # test_queue will flush
131 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
132 # Not sure how to make this test more difficult - holding branch does not matter
134 $dbh->do("DELETE FROM tmp_holdsqueue");
135 $dbh->do("DELETE FROM hold_fill_targets");
136 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
137 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
138 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
139 # We have a book available held in borrower branch
140 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
141 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
143 $dbh->do("DELETE FROM tmp_holdsqueue");
144 $dbh->do("DELETE FROM hold_fill_targets");
145 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
146 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
147 # No book available in borrower branch, pick according to the rules
148 # Frst branch from StaticHoldsQueueWeight
149 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
150 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
151 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
152 my $queue_item = $queue->[0];
154 && $queue_item->{pickbranch} eq $borrower_branchcode
155 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
156 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
157 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
160 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
161 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
164 # XXX All this tests are for borrower branch pick-up.
165 # Maybe needs expanding to homebranch or holdingbranch pick-up.
167 $schema->txn_rollback;
170 ### Test holds queue builder does not violate holds policy ###
172 # Clear out existing rules relating to holdallowed
173 $dbh->do("DELETE FROM default_branch_circ_rules");
174 $dbh->do("DELETE FROM default_branch_item_rules");
175 $dbh->do("DELETE FROM default_circ_rules");
177 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
179 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
181 $library1 = $builder->build({
184 $library2 = $builder->build({
187 $library3 = $builder->build({
190 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
192 my $borrower1 = $builder->build({
193 source => 'Borrower',
195 branchcode => $branchcodes[0],
198 my $borrower2 = $builder->build({
199 source => 'Borrower',
201 branchcode => $branchcodes[1],
204 my $borrower3 = $builder->build({
205 source => 'Borrower',
207 branchcode => $branchcodes[2],
224 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
225 or BAIL_OUT("Cannot find newly created biblio record");
228 INSERT INTO biblioitems (
236 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
237 or BAIL_OUT("Cannot find newly created biblioitems record");
239 $items_insert_sth = $dbh->prepare(qq{
266 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
267 $barcode = int( rand(1000000000000) );
268 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
269 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
270 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
272 $dbh->do("DELETE FROM reserves");
273 my $sth = $dbh->prepare(q{
274 INSERT INTO reserves (
280 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
282 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
283 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
284 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
288 $dbh->do("DELETE FROM default_circ_rules");
289 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
290 C4::HoldsQueue::CreateQueue();
291 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
292 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
293 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
294 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
296 # Test skipping hold picks for closed libraries.
297 # At this point in the test, we have 2 rows in the holds queue
298 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
299 # and make today a holiday for MPL. When we run it again we should only
300 # have 1 row in the holds queue
301 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
302 my $today = dt_from_string();
303 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
304 day => $today->day(),
305 month => $today->month(),
306 year => $today->year(),
308 description => "$today",
310 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
311 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
312 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
313 C4::HoldsQueue::CreateQueue();
314 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
315 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
316 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
318 $dbh->do("DELETE FROM default_circ_rules");
319 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
320 C4::HoldsQueue::CreateQueue();
321 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
322 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
324 # Test skipping hold picks for closed libraries without transport cost matrix
325 # At this point in the test, we have 3 rows in the holds queue
326 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
327 # and use our previously created holiday for MPL
328 # When we run it again we should only have 2 rows in the holds queue
329 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
330 C4::HoldsQueue::CreateQueue();
331 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
332 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
333 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
335 ## Test LocalHoldsPriority
336 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
338 $dbh->do("DELETE FROM default_circ_rules");
339 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
340 $dbh->do("DELETE FROM issues");
342 # Test homebranch = patron branch
343 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
344 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
345 C4::Context->clear_syspref_cache();
346 $dbh->do("DELETE FROM reserves");
347 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
348 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
349 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
351 $dbh->do("DELETE FROM items");
352 # barcode, homebranch, holdingbranch, itemtype
353 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
355 C4::HoldsQueue::CreateQueue();
356 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
357 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
359 # Test holdingbranch = patron branch
360 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
361 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
362 C4::Context->clear_syspref_cache();
363 $dbh->do("DELETE FROM reserves");
364 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
365 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
366 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
368 $dbh->do("DELETE FROM items");
369 # barcode, homebranch, holdingbranch, itemtype
370 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
372 C4::HoldsQueue::CreateQueue();
373 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
374 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
376 # Test holdingbranch = pickup branch
377 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
378 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
379 C4::Context->clear_syspref_cache();
380 $dbh->do("DELETE FROM reserves");
381 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
382 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
383 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
385 $dbh->do("DELETE FROM items");
386 # barcode, homebranch, holdingbranch, itemtype
387 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
389 C4::HoldsQueue::CreateQueue();
390 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
391 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
393 # Test homebranch = pickup branch
394 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
395 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
396 C4::Context->clear_syspref_cache();
397 $dbh->do("DELETE FROM reserves");
398 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
399 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
400 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
402 $dbh->do("DELETE FROM items");
403 # barcode, homebranch, holdingbranch, itemtype
404 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
406 C4::HoldsQueue::CreateQueue();
407 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
408 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
410 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
411 ## End testing of LocalHoldsPriority
415 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
416 $borrowernumber = $borrower3->{borrowernumber};
417 my $library_A = $library1->{branchcode};
418 my $library_B = $library2->{branchcode};
419 my $library_C = $borrower3->{branchcode};
420 $dbh->do("DELETE FROM reserves");
421 $dbh->do("DELETE FROM issues");
422 $dbh->do("DELETE FROM items");
423 $dbh->do("DELETE FROM biblio");
424 $dbh->do("DELETE FROM biblioitems");
425 $dbh->do("DELETE FROM transport_cost");
426 $dbh->do("DELETE FROM tmp_holdsqueue");
427 $dbh->do("DELETE FROM hold_fill_targets");
428 $dbh->do("DELETE FROM default_branch_circ_rules");
429 $dbh->do("DELETE FROM default_branch_item_rules");
430 $dbh->do("DELETE FROM default_circ_rules");
431 $dbh->do("DELETE FROM branch_item_rules");
434 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
437 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
438 or BAIL_OUT("Cannot find newly created biblio record");
440 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
443 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
444 or BAIL_OUT("Cannot find newly created biblioitems record");
447 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
448 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
452 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
453 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
457 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
458 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
461 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
462 undef, join( ',', $library_B, $library_A, $library_C ) );
463 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
465 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
466 C4::HoldsQueue::CreateQueue();
467 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
468 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
472 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
473 $borrowernumber = $borrower2->{borrowernumber};
474 $library_A = $library1->{branchcode};
475 $library_B = $library2->{branchcode};
476 $dbh->do("DELETE FROM reserves");
477 $dbh->do("DELETE FROM issues");
478 $dbh->do("DELETE FROM items");
479 $dbh->do("DELETE FROM biblio");
480 $dbh->do("DELETE FROM biblioitems");
481 $dbh->do("DELETE FROM transport_cost");
482 $dbh->do("DELETE FROM tmp_holdsqueue");
483 $dbh->do("DELETE FROM hold_fill_targets");
484 $dbh->do("DELETE FROM default_branch_circ_rules");
485 $dbh->do("DELETE FROM default_branch_item_rules");
486 $dbh->do("DELETE FROM default_circ_rules");
487 $dbh->do("DELETE FROM branch_item_rules");
489 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
491 my $tc_rs = $schema->resultset('TransportCost');
492 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
493 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
496 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
499 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
500 or BAIL_OUT("Cannot find newly created biblio record");
502 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
505 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
506 or BAIL_OUT("Cannot find newly created biblioitems record");
509 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
510 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
513 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
514 C4::HoldsQueue::CreateQueue();
515 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
516 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
519 # Test hold_fulfillment_policy
520 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
521 $borrowernumber = $borrower3->{borrowernumber};
522 $library_A = $library1->{branchcode};
523 $library_B = $library2->{branchcode};
524 $library_C = $library3->{branchcode};
525 $dbh->do("DELETE FROM reserves");
526 $dbh->do("DELETE FROM issues");
527 $dbh->do("DELETE FROM items");
528 $dbh->do("DELETE FROM biblio");
529 $dbh->do("DELETE FROM biblioitems");
530 $dbh->do("DELETE FROM transport_cost");
531 $dbh->do("DELETE FROM tmp_holdsqueue");
532 $dbh->do("DELETE FROM hold_fill_targets");
533 $dbh->do("DELETE FROM default_branch_circ_rules");
534 $dbh->do("DELETE FROM default_branch_item_rules");
535 $dbh->do("DELETE FROM default_circ_rules");
536 $dbh->do("DELETE FROM branch_item_rules");
538 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
540 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
541 or BAIL_OUT("Cannot find newly created biblio record");
543 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
546 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
547 or BAIL_OUT("Cannot find newly created biblioitems record");
550 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
551 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
554 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
555 $dbh->do("DELETE FROM default_circ_rules");
556 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
558 # Home branch matches pickup branch
559 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
560 C4::HoldsQueue::CreateQueue();
561 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
562 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
563 CancelReserve( { reserve_id => $reserve_id } );
565 # Holding branch matches pickup branch
566 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
567 C4::HoldsQueue::CreateQueue();
568 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
569 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
570 CancelReserve( { reserve_id => $reserve_id } );
572 # Neither branch matches pickup branch
573 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
574 C4::HoldsQueue::CreateQueue();
575 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
576 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
577 CancelReserve( { reserve_id => $reserve_id } );
579 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
580 $dbh->do("DELETE FROM default_circ_rules");
581 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
583 # Home branch matches pickup branch
584 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
585 C4::HoldsQueue::CreateQueue();
586 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
587 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
588 CancelReserve( { reserve_id => $reserve_id } );
590 # Holding branch matches pickup branch
591 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
592 C4::HoldsQueue::CreateQueue();
593 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
594 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
595 CancelReserve( { reserve_id => $reserve_id } );
597 # Neither branch matches pickup branch
598 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
599 C4::HoldsQueue::CreateQueue();
600 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
601 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
602 CancelReserve( { reserve_id => $reserve_id } );
604 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
605 $dbh->do("DELETE FROM default_circ_rules");
606 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
608 # Home branch matches pickup branch
609 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
610 C4::HoldsQueue::CreateQueue();
611 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
612 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
613 CancelReserve( { reserve_id => $reserve_id } );
615 # Holding branch matches pickup branch
616 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
617 C4::HoldsQueue::CreateQueue();
618 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
619 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
620 CancelReserve( { reserve_id => $reserve_id } );
622 # Neither branch matches pickup branch
623 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
624 C4::HoldsQueue::CreateQueue();
625 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
626 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
627 CancelReserve( { reserve_id => $reserve_id } );
629 # End testing hold_fulfillment_policy
631 # Test hold itemtype limit
632 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
633 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
634 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
635 $borrowernumber = $borrower3->{borrowernumber};
636 my $branchcode = $library1->{branchcode};
637 $dbh->do("DELETE FROM reserves");
638 $dbh->do("DELETE FROM issues");
639 $dbh->do("DELETE FROM items");
640 $dbh->do("DELETE FROM biblio");
641 $dbh->do("DELETE FROM biblioitems");
642 $dbh->do("DELETE FROM transport_cost");
643 $dbh->do("DELETE FROM tmp_holdsqueue");
644 $dbh->do("DELETE FROM hold_fill_targets");
645 $dbh->do("DELETE FROM default_branch_circ_rules");
646 $dbh->do("DELETE FROM default_branch_item_rules");
647 $dbh->do("DELETE FROM default_circ_rules");
648 $dbh->do("DELETE FROM branch_item_rules");
650 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
652 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
653 or BAIL_OUT("Cannot find newly created biblio record");
655 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
658 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
659 or BAIL_OUT("Cannot find newly created biblioitems record");
662 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
663 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
666 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
667 $dbh->do("DELETE FROM default_circ_rules");
668 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
670 # Home branch matches pickup branch
671 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
672 C4::HoldsQueue::CreateQueue();
673 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
674 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
675 CancelReserve( { reserve_id => $reserve_id } );
677 # Holding branch matches pickup branch
678 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
679 C4::HoldsQueue::CreateQueue();
680 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
681 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
682 CancelReserve( { reserve_id => $reserve_id } );
684 # Neither branch matches pickup branch
685 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
686 C4::HoldsQueue::CreateQueue();
687 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
688 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
689 CancelReserve( { reserve_id => $reserve_id } );
691 # End testing hold itemtype limit
694 $schema->storage->txn_rollback;
696 ### END Test holds queue builder does not violate holds policy ###
699 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
701 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
703 $use_cost_matrix_sth->execute($use_cost_matrix);
704 C4::Context->clear_syspref_cache();
705 C4::HoldsQueue::CreateQueue();
707 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
708 my $r = $results->[0];
710 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
711 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
714 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
715 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
720 my ($tablename) = @_;
721 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);