Bug 18624: (followup) opac-authorities-home.tt uses 'any'
[koha.git] / t / db_dependent / HoldsQueue.t
blob288432e3adf1108b862bd7b9cbd7fc6cc30318af
1 #!/usr/bin/perl
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
9 use Modern::Perl;
11 use Test::More tests => 44;
12 use Data::Dumper;
14 use C4::Calendar;
15 use C4::Context;
16 use C4::Members;
17 use Koha::Database;
18 use Koha::DateUtils;
19 use Koha::Items;
21 use t::lib::TestBuilder;
22 use t::lib::Mocks;
24 BEGIN {
25 use FindBin;
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({
38 source => 'Branch',
39 });
40 my $library2 = $builder->build({
41 source => 'Branch',
42 });
43 my $library3 = $builder->build({
44 source => 'Branch',
45 });
47 my $TITLE = "Test Holds Queue XXX";
49 my $borrower = $builder->build({
50 source => 'Borrower',
51 value => {
52 branchcode => $library1->{branchcode},
54 });
56 my $borrowernumber = $borrower->{borrowernumber};
57 # Set special (for this test) branches
58 my $borrower_branchcode = $borrower->{branchcode};
59 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
60 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
61 my $least_cost_branch_code = pop @other_branches;
62 my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
64 #Set up the stage
65 # Sysprefs and cost matrix
66 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
67 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
68 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
69 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
70 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
72 $dbh->do("DELETE FROM transport_cost");
73 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
74 # Favour $least_cost_branch_code
75 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
76 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
77 my @b = @other_branches;
78 while ( my $b1 = shift @b ) {
79 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
80 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
81 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
86 # Loanable items - all possible combinations of homebranch and holdingbranch
87 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
88 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
89 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
90 or BAIL_OUT("Cannot find newly created biblio record");
91 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype)
92 VALUES ($biblionumber, '$itemtype')");
93 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
94 or BAIL_OUT("Cannot find newly created biblioitems record");
96 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
97 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
98 my $first_barcode = int(rand(1000000000000)); # XXX
99 my $barcode = $first_barcode;
100 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
101 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
102 $items_insert_sth->execute($barcode++, $_, $_);
103 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
106 # Remove existing reserves, makes debugging easier
107 $dbh->do("DELETE FROM reserves");
108 my $bibitems = undef;
109 my $priority = 1;
110 # Make a reserve
111 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
112 # $resdate, $expdate, $notes, $title, $checkitem, $found
113 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
115 # Tests
116 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
117 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
118 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
119 JOIN items USING (itemnumber)
120 WHERE borrowernumber = $borrowernumber");
122 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
123 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
124 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
125 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
127 $dbh->do("DELETE FROM tmp_holdsqueue");
128 $dbh->do("DELETE FROM hold_fill_targets");
129 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
130 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
131 # test_queue will flush
132 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
133 # Not sure how to make this test more difficult - holding branch does not matter
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 homebranch = '$borrower_branchcode')");
138 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
139 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
140 # We have a book available held in borrower branch
141 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
142 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
144 $dbh->do("DELETE FROM tmp_holdsqueue");
145 $dbh->do("DELETE FROM hold_fill_targets");
146 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
147 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
148 # No book available in borrower branch, pick according to the rules
149 # Frst branch from StaticHoldsQueueWeight
150 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
151 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
152 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
153 my $queue_item = $queue->[0];
154 ok( $queue_item
155 && $queue_item->{pickbranch} eq $borrower_branchcode
156 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
157 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
158 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
161 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
162 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
165 # XXX All this tests are for borrower branch pick-up.
166 # Maybe needs expanding to homebranch or holdingbranch pick-up.
168 $schema->txn_rollback;
169 $schema->txn_begin;
171 ### Test holds queue builder does not violate holds policy ###
173 # Clear out existing rules relating to holdallowed
174 $dbh->do("DELETE FROM default_branch_circ_rules");
175 $dbh->do("DELETE FROM default_branch_item_rules");
176 $dbh->do("DELETE FROM default_circ_rules");
178 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
180 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
182 $library1 = $builder->build({
183 source => 'Branch',
185 $library2 = $builder->build({
186 source => 'Branch',
188 $library3 = $builder->build({
189 source => 'Branch',
191 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
193 my $borrower1 = $builder->build({
194 source => 'Borrower',
195 value => {
196 branchcode => $branchcodes[0],
199 my $borrower2 = $builder->build({
200 source => 'Borrower',
201 value => {
202 branchcode => $branchcodes[1],
205 my $borrower3 = $builder->build({
206 source => 'Borrower',
207 value => {
208 branchcode => $branchcodes[2],
212 $dbh->do(qq{
213 INSERT INTO biblio (
214 frameworkcode,
215 author,
216 title,
217 datecreated
218 ) VALUES (
219 'SER',
220 'Koha test',
221 '$TITLE',
222 '2011-02-01'
225 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
226 or BAIL_OUT("Cannot find newly created biblio record");
228 $dbh->do(qq{
229 INSERT INTO biblioitems (
230 biblionumber,
231 itemtype
232 ) VALUES (
233 $biblionumber,
234 '$itemtype'
237 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
238 or BAIL_OUT("Cannot find newly created biblioitems record");
240 $items_insert_sth = $dbh->prepare(qq{
241 INSERT INTO items (
242 biblionumber,
243 biblioitemnumber,
244 barcode,
245 homebranch,
246 holdingbranch,
247 notforloan,
248 damaged,
249 itemlost,
250 withdrawn,
251 onloan,
252 itype
253 ) VALUES (
254 $biblionumber,
255 $biblioitemnumber,
263 NULL,
264 '$itemtype'
267 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
268 $barcode = int( rand(1000000000000) );
269 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
270 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
271 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
273 $dbh->do("DELETE FROM reserves");
274 my $sth = $dbh->prepare(q{
275 INSERT INTO reserves (
276 borrowernumber,
277 biblionumber,
278 branchcode,
279 priority,
280 reservedate
281 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
283 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
284 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
285 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
287 my $holds_queue;
289 $dbh->do("DELETE FROM default_circ_rules");
290 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
291 C4::HoldsQueue::CreateQueue();
292 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
293 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
294 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
295 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
297 # Test skipping hold picks for closed libraries.
298 # At this point in the test, we have 2 rows in the holds queue
299 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
300 # and make today a holiday for MPL. When we run it again we should only
301 # have 1 row in the holds queue
302 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
303 my $today = dt_from_string();
304 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
305 day => $today->day(),
306 month => $today->month(),
307 year => $today->year(),
308 title => "$today",
309 description => "$today",
311 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
312 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
313 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
314 C4::HoldsQueue::CreateQueue();
315 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
316 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
317 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
319 $dbh->do("DELETE FROM default_circ_rules");
320 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
321 C4::HoldsQueue::CreateQueue();
322 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
323 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
325 # Test skipping hold picks for closed libraries without transport cost matrix
326 # At this point in the test, we have 3 rows in the holds queue
327 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
328 # and use our previously created holiday for MPL
329 # When we run it again we should only have 2 rows in the holds queue
330 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
331 C4::HoldsQueue::CreateQueue();
332 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
333 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
334 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
336 ## Test LocalHoldsPriority
337 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
339 $dbh->do("DELETE FROM default_circ_rules");
340 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
341 $dbh->do("DELETE FROM issues");
343 # Test homebranch = patron branch
344 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
345 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
346 C4::Context->clear_syspref_cache();
347 $dbh->do("DELETE FROM reserves");
348 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
349 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
350 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
352 $dbh->do("DELETE FROM items");
353 # barcode, homebranch, holdingbranch, itemtype
354 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
356 C4::HoldsQueue::CreateQueue();
357 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
358 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
360 # Test holdingbranch = patron branch
361 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
362 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
363 C4::Context->clear_syspref_cache();
364 $dbh->do("DELETE FROM reserves");
365 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
366 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
367 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
369 $dbh->do("DELETE FROM items");
370 # barcode, homebranch, holdingbranch, itemtype
371 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
373 C4::HoldsQueue::CreateQueue();
374 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
375 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
377 # Test holdingbranch = pickup branch
378 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
379 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
380 C4::Context->clear_syspref_cache();
381 $dbh->do("DELETE FROM reserves");
382 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
383 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
384 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
386 $dbh->do("DELETE FROM items");
387 # barcode, homebranch, holdingbranch, itemtype
388 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
390 C4::HoldsQueue::CreateQueue();
391 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
392 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
394 # Test homebranch = pickup branch
395 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
396 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
397 C4::Context->clear_syspref_cache();
398 $dbh->do("DELETE FROM reserves");
399 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
400 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
401 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
403 $dbh->do("DELETE FROM items");
404 # barcode, homebranch, holdingbranch, itemtype
405 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
407 C4::HoldsQueue::CreateQueue();
408 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
409 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
411 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
412 ## End testing of LocalHoldsPriority
415 # Bug 14297
416 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
417 $borrowernumber = $borrower3->{borrowernumber};
418 my $library_A = $library1->{branchcode};
419 my $library_B = $library2->{branchcode};
420 my $library_C = $borrower3->{branchcode};
421 $dbh->do("DELETE FROM reserves");
422 $dbh->do("DELETE FROM issues");
423 $dbh->do("DELETE FROM items");
424 $dbh->do("DELETE FROM biblio");
425 $dbh->do("DELETE FROM biblioitems");
426 $dbh->do("DELETE FROM transport_cost");
427 $dbh->do("DELETE FROM tmp_holdsqueue");
428 $dbh->do("DELETE FROM hold_fill_targets");
429 $dbh->do("DELETE FROM default_branch_circ_rules");
430 $dbh->do("DELETE FROM default_branch_item_rules");
431 $dbh->do("DELETE FROM default_circ_rules");
432 $dbh->do("DELETE FROM branch_item_rules");
434 $dbh->do("
435 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
438 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
439 or BAIL_OUT("Cannot find newly created biblio record");
441 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
443 $biblioitemnumber =
444 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
445 or BAIL_OUT("Cannot find newly created biblioitems record");
447 $dbh->do("
448 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
449 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
452 $dbh->do("
453 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
454 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
457 $dbh->do("
458 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
459 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
462 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
463 undef, join( ',', $library_B, $library_A, $library_C ) );
464 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
466 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
467 C4::HoldsQueue::CreateQueue();
468 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
469 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
470 # End Bug 14297
472 # Bug 15062
473 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
474 $borrowernumber = $borrower2->{borrowernumber};
475 $library_A = $library1->{branchcode};
476 $library_B = $library2->{branchcode};
477 $dbh->do("DELETE FROM reserves");
478 $dbh->do("DELETE FROM issues");
479 $dbh->do("DELETE FROM items");
480 $dbh->do("DELETE FROM biblio");
481 $dbh->do("DELETE FROM biblioitems");
482 $dbh->do("DELETE FROM transport_cost");
483 $dbh->do("DELETE FROM tmp_holdsqueue");
484 $dbh->do("DELETE FROM hold_fill_targets");
485 $dbh->do("DELETE FROM default_branch_circ_rules");
486 $dbh->do("DELETE FROM default_branch_item_rules");
487 $dbh->do("DELETE FROM default_circ_rules");
488 $dbh->do("DELETE FROM branch_item_rules");
490 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
492 my $tc_rs = $schema->resultset('TransportCost');
493 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
494 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
496 $dbh->do("
497 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
500 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
501 or BAIL_OUT("Cannot find newly created biblio record");
503 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
505 $biblioitemnumber =
506 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
507 or BAIL_OUT("Cannot find newly created biblioitems record");
509 $dbh->do("
510 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
511 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
514 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
515 C4::HoldsQueue::CreateQueue();
516 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
517 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
518 # End Bug 15062
520 # Test hold_fulfillment_policy
521 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
522 $borrowernumber = $borrower3->{borrowernumber};
523 $library_A = $library1->{branchcode};
524 $library_B = $library2->{branchcode};
525 $library_C = $library3->{branchcode};
526 $dbh->do("DELETE FROM reserves");
527 $dbh->do("DELETE FROM issues");
528 $dbh->do("DELETE FROM items");
529 $dbh->do("DELETE FROM biblio");
530 $dbh->do("DELETE FROM biblioitems");
531 $dbh->do("DELETE FROM transport_cost");
532 $dbh->do("DELETE FROM tmp_holdsqueue");
533 $dbh->do("DELETE FROM hold_fill_targets");
534 $dbh->do("DELETE FROM default_branch_circ_rules");
535 $dbh->do("DELETE FROM default_branch_item_rules");
536 $dbh->do("DELETE FROM default_circ_rules");
537 $dbh->do("DELETE FROM branch_item_rules");
539 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
541 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
542 or BAIL_OUT("Cannot find newly created biblio record");
544 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
546 $biblioitemnumber =
547 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
548 or BAIL_OUT("Cannot find newly created biblioitems record");
550 $dbh->do("
551 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
552 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
555 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
556 $dbh->do("DELETE FROM default_circ_rules");
557 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
559 # Home branch matches pickup branch
560 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
561 C4::HoldsQueue::CreateQueue();
562 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
563 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
564 CancelReserve( { reserve_id => $reserve_id } );
566 # Holding branch matches pickup branch
567 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
568 C4::HoldsQueue::CreateQueue();
569 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
570 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
571 CancelReserve( { reserve_id => $reserve_id } );
573 # Neither branch matches pickup branch
574 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
575 C4::HoldsQueue::CreateQueue();
576 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
577 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
578 CancelReserve( { reserve_id => $reserve_id } );
580 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
581 $dbh->do("DELETE FROM default_circ_rules");
582 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
584 # Home branch matches pickup branch
585 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
586 C4::HoldsQueue::CreateQueue();
587 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
588 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
589 CancelReserve( { reserve_id => $reserve_id } );
591 # Holding branch matches pickup branch
592 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
593 C4::HoldsQueue::CreateQueue();
594 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
595 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
596 CancelReserve( { reserve_id => $reserve_id } );
598 # Neither branch matches pickup branch
599 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
600 C4::HoldsQueue::CreateQueue();
601 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
602 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
603 CancelReserve( { reserve_id => $reserve_id } );
605 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
606 $dbh->do("DELETE FROM default_circ_rules");
607 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
609 # Home branch matches pickup branch
610 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
611 C4::HoldsQueue::CreateQueue();
612 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
613 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
614 CancelReserve( { reserve_id => $reserve_id } );
616 # Holding branch matches pickup branch
617 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
618 C4::HoldsQueue::CreateQueue();
619 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
620 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
621 CancelReserve( { reserve_id => $reserve_id } );
623 # Neither branch matches pickup branch
624 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
625 C4::HoldsQueue::CreateQueue();
626 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
627 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
628 CancelReserve( { reserve_id => $reserve_id } );
630 # End testing hold_fulfillment_policy
632 # Test hold itemtype limit
633 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
634 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
635 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
636 $borrowernumber = $borrower3->{borrowernumber};
637 my $branchcode = $library1->{branchcode};
638 $dbh->do("DELETE FROM reserves");
639 $dbh->do("DELETE FROM issues");
640 $dbh->do("DELETE FROM items");
641 $dbh->do("DELETE FROM biblio");
642 $dbh->do("DELETE FROM biblioitems");
643 $dbh->do("DELETE FROM transport_cost");
644 $dbh->do("DELETE FROM tmp_holdsqueue");
645 $dbh->do("DELETE FROM hold_fill_targets");
646 $dbh->do("DELETE FROM default_branch_circ_rules");
647 $dbh->do("DELETE FROM default_branch_item_rules");
648 $dbh->do("DELETE FROM default_circ_rules");
649 $dbh->do("DELETE FROM branch_item_rules");
651 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
653 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
654 or BAIL_OUT("Cannot find newly created biblio record");
656 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
658 $biblioitemnumber =
659 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
660 or BAIL_OUT("Cannot find newly created biblioitems record");
662 $dbh->do("
663 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
664 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
667 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
668 $dbh->do("DELETE FROM default_circ_rules");
669 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
671 # Home branch matches pickup branch
672 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
673 C4::HoldsQueue::CreateQueue();
674 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
675 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
676 CancelReserve( { reserve_id => $reserve_id } );
678 # Holding branch matches pickup branch
679 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
680 C4::HoldsQueue::CreateQueue();
681 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
682 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
683 CancelReserve( { reserve_id => $reserve_id } );
685 # Neither branch matches pickup branch
686 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
687 C4::HoldsQueue::CreateQueue();
688 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
689 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
690 CancelReserve( { reserve_id => $reserve_id } );
692 # End testing hold itemtype limit
695 # Test Local Holds Priority - Bug 18001
696 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
697 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
698 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
700 $dbh->do("DELETE FROM tmp_holdsqueue");
701 $dbh->do("DELETE FROM hold_fill_targets");
702 $dbh->do("DELETE FROM reserves");
703 $dbh->do("DELETE FROM default_branch_circ_rules");
704 $dbh->do("DELETE FROM default_branch_item_rules");
705 $dbh->do("DELETE FROM default_circ_rules");
706 $dbh->do("DELETE FROM branch_item_rules");
708 my $item = Koha::Items->find( { biblionumber => $biblionumber } );
709 $item->holdingbranch( $item->homebranch );
710 $item->store();
712 my $item2 = Koha::Item->new( $item->unblessed );
713 $item2->itemnumber( undef );
714 $item2->store();
716 my $item3 = Koha::Item->new( $item->unblessed );
717 $item3->itemnumber( undef );
718 $item3->store();
720 $reserve_id = AddReserve( $item->homebranch, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
722 C4::HoldsQueue::CreateQueue();
724 my $queue_rs = $schema->resultset('TmpHoldsqueue');
725 is( $queue_rs->count(), 1, "Hold queue contains one hold from chosen from three possible items" );
727 subtest 'Trivial test for UpdateTransportCostMatrix' => sub {
728 plan tests => 1;
729 my $recs = [
730 { frombranch => $library1->{branchcode}, tobranch => $library2->{branchcode}, cost => 1, disable_transfer => 0 },
731 { frombranch => $library2->{branchcode}, tobranch => $library3->{branchcode}, cost => 0, disable_transfer => 1 },
733 C4::HoldsQueue::UpdateTransportCostMatrix( $recs );
734 is( $schema->resultset('TransportCost')->count, 2, 'UpdateTransportCostMatrix added two records' );
737 # Cleanup
738 $schema->storage->txn_rollback;
740 ### END Test holds queue builder does not violate holds policy ###
742 sub test_queue {
743 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
745 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
747 $use_cost_matrix_sth->execute($use_cost_matrix);
748 C4::Context->clear_syspref_cache();
749 C4::HoldsQueue::CreateQueue();
751 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
752 my $r = $results->[0];
754 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
755 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
756 if $hold_branch;
758 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
759 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
760 unless $ok;
763 sub dump_records {
764 my ($tablename) = @_;
765 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);