Bug 9302: Use patron-title.inc
[koha.git] / t / db_dependent / HoldsQueue.t
blobe2f749a6237d3d17e552fd302cfd7fe33b60d395
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;
20 use Koha::Holds;
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
25 BEGIN {
26 use FindBin;
27 use lib $FindBin::Bin;
28 use_ok('C4::Reserves');
29 use_ok('C4::HoldsQueue');
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
34 my $dbh = C4::Context->dbh;
36 my $builder = t::lib::TestBuilder->new;
38 my $library1 = $builder->build({
39 source => 'Branch',
40 });
41 my $library2 = $builder->build({
42 source => 'Branch',
43 });
44 my $library3 = $builder->build({
45 source => 'Branch',
46 });
48 my $TITLE = "Test Holds Queue XXX";
50 my $borrower = $builder->build({
51 source => 'Borrower',
52 value => {
53 branchcode => $library1->{branchcode},
55 });
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 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
65 #Set up the stage
66 # Sysprefs and cost matrix
67 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
68 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
69 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
70 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
71 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
73 $dbh->do("DELETE FROM transport_cost");
74 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
75 # Favour $least_cost_branch_code
76 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
77 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
78 my @b = @other_branches;
79 while ( my $b1 = shift @b ) {
80 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
81 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
82 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
87 # Loanable items - all possible combinations of homebranch and holdingbranch
88 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
89 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
90 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
91 or BAIL_OUT("Cannot find newly created biblio record");
92 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype)
93 VALUES ($biblionumber, '$itemtype')");
94 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
95 or BAIL_OUT("Cannot find newly created biblioitems record");
97 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
98 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
99 my $first_barcode = int(rand(1000000000000)); # XXX
100 my $barcode = $first_barcode;
101 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
102 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
103 $items_insert_sth->execute($barcode++, $_, $_);
104 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
107 # Remove existing reserves, makes debugging easier
108 $dbh->do("DELETE FROM reserves");
109 my $bibitems = undef;
110 my $priority = 1;
111 # Make a reserve
112 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
113 # $resdate, $expdate, $notes, $title, $checkitem, $found
114 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
116 # Tests
117 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
118 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
119 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
120 JOIN items USING (itemnumber)
121 WHERE borrowernumber = $borrowernumber");
123 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
124 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
125 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
126 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
128 $dbh->do("DELETE FROM tmp_holdsqueue");
129 $dbh->do("DELETE FROM hold_fill_targets");
130 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
131 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
132 # test_queue will flush
133 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
134 # Not sure how to make this test more difficult - holding branch does not matter
136 $dbh->do("DELETE FROM tmp_holdsqueue");
137 $dbh->do("DELETE FROM hold_fill_targets");
138 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
139 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
140 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
141 # We have a book available held in borrower branch
142 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
143 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
145 $dbh->do("DELETE FROM tmp_holdsqueue");
146 $dbh->do("DELETE FROM hold_fill_targets");
147 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
148 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
149 # No book available in borrower branch, pick according to the rules
150 # Frst branch from StaticHoldsQueueWeight
151 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
152 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
153 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
154 my $queue_item = $queue->[0];
155 ok( $queue_item
156 && $queue_item->{pickbranch} eq $borrower_branchcode
157 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
158 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
159 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
162 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
163 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
166 # XXX All this tests are for borrower branch pick-up.
167 # Maybe needs expanding to homebranch or holdingbranch pick-up.
169 $schema->txn_rollback;
170 $schema->txn_begin;
172 ### Test holds queue builder does not violate holds policy ###
174 # Clear out existing rules relating to holdallowed
175 $dbh->do("DELETE FROM default_branch_circ_rules");
176 $dbh->do("DELETE FROM default_branch_item_rules");
177 $dbh->do("DELETE FROM default_circ_rules");
179 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
181 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
183 $library1 = $builder->build({
184 source => 'Branch',
186 $library2 = $builder->build({
187 source => 'Branch',
189 $library3 = $builder->build({
190 source => 'Branch',
192 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
194 my $borrower1 = $builder->build({
195 source => 'Borrower',
196 value => {
197 branchcode => $branchcodes[0],
200 my $borrower2 = $builder->build({
201 source => 'Borrower',
202 value => {
203 branchcode => $branchcodes[1],
206 my $borrower3 = $builder->build({
207 source => 'Borrower',
208 value => {
209 branchcode => $branchcodes[2],
213 $dbh->do(qq{
214 INSERT INTO biblio (
215 frameworkcode,
216 author,
217 title,
218 datecreated
219 ) VALUES (
220 'SER',
221 'Koha test',
222 '$TITLE',
223 '2011-02-01'
226 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
227 or BAIL_OUT("Cannot find newly created biblio record");
229 $dbh->do(qq{
230 INSERT INTO biblioitems (
231 biblionumber,
232 itemtype
233 ) VALUES (
234 $biblionumber,
235 '$itemtype'
238 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
239 or BAIL_OUT("Cannot find newly created biblioitems record");
241 $items_insert_sth = $dbh->prepare(qq{
242 INSERT INTO items (
243 biblionumber,
244 biblioitemnumber,
245 barcode,
246 homebranch,
247 holdingbranch,
248 notforloan,
249 damaged,
250 itemlost,
251 withdrawn,
252 onloan,
253 itype
254 ) VALUES (
255 $biblionumber,
256 $biblioitemnumber,
264 NULL,
265 '$itemtype'
268 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
269 $barcode = int( rand(1000000000000) );
270 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
271 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
272 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
274 $dbh->do("DELETE FROM reserves");
275 my $sth = $dbh->prepare(q{
276 INSERT INTO reserves (
277 borrowernumber,
278 biblionumber,
279 branchcode,
280 priority,
281 reservedate
282 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
284 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
285 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
286 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
288 my $holds_queue;
290 $dbh->do("DELETE FROM default_circ_rules");
291 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
292 C4::HoldsQueue::CreateQueue();
293 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
294 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
295 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
296 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
298 # Test skipping hold picks for closed libraries.
299 # At this point in the test, we have 2 rows in the holds queue
300 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
301 # and make today a holiday for MPL. When we run it again we should only
302 # have 1 row in the holds queue
303 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
304 my $today = dt_from_string();
305 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
306 day => $today->day(),
307 month => $today->month(),
308 year => $today->year(),
309 title => "$today",
310 description => "$today",
312 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
313 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
314 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
315 C4::HoldsQueue::CreateQueue();
316 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
317 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
318 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
320 $dbh->do("DELETE FROM default_circ_rules");
321 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
322 C4::HoldsQueue::CreateQueue();
323 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
324 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
326 # Test skipping hold picks for closed libraries without transport cost matrix
327 # At this point in the test, we have 3 rows in the holds queue
328 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
329 # and use our previously created holiday for MPL
330 # When we run it again we should only have 2 rows in the holds queue
331 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
332 C4::HoldsQueue::CreateQueue();
333 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
334 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
335 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
337 ## Test LocalHoldsPriority
338 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
340 $dbh->do("DELETE FROM default_circ_rules");
341 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
342 $dbh->do("DELETE FROM issues");
344 # Test homebranch = patron branch
345 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
346 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
347 C4::Context->clear_syspref_cache();
348 $dbh->do("DELETE FROM reserves");
349 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
350 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
351 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
353 $dbh->do("DELETE FROM items");
354 # barcode, homebranch, holdingbranch, itemtype
355 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
357 C4::HoldsQueue::CreateQueue();
358 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
359 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
361 # Test holdingbranch = patron branch
362 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
363 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
364 C4::Context->clear_syspref_cache();
365 $dbh->do("DELETE FROM reserves");
366 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
367 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
368 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
370 $dbh->do("DELETE FROM items");
371 # barcode, homebranch, holdingbranch, itemtype
372 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
374 C4::HoldsQueue::CreateQueue();
375 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
376 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
378 # Test holdingbranch = pickup branch
379 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
380 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
381 C4::Context->clear_syspref_cache();
382 $dbh->do("DELETE FROM reserves");
383 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
384 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
385 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
387 $dbh->do("DELETE FROM items");
388 # barcode, homebranch, holdingbranch, itemtype
389 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
391 C4::HoldsQueue::CreateQueue();
392 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
393 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
395 # Test homebranch = pickup branch
396 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
397 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
398 C4::Context->clear_syspref_cache();
399 $dbh->do("DELETE FROM reserves");
400 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
401 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
402 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
404 $dbh->do("DELETE FROM items");
405 # barcode, homebranch, holdingbranch, itemtype
406 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
408 C4::HoldsQueue::CreateQueue();
409 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
410 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
412 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
413 ## End testing of LocalHoldsPriority
416 # Bug 14297
417 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
418 $borrowernumber = $borrower3->{borrowernumber};
419 my $library_A = $library1->{branchcode};
420 my $library_B = $library2->{branchcode};
421 my $library_C = $borrower3->{branchcode};
422 $dbh->do("DELETE FROM reserves");
423 $dbh->do("DELETE FROM issues");
424 $dbh->do("DELETE FROM items");
425 $dbh->do("DELETE FROM biblio");
426 $dbh->do("DELETE FROM biblioitems");
427 $dbh->do("DELETE FROM transport_cost");
428 $dbh->do("DELETE FROM tmp_holdsqueue");
429 $dbh->do("DELETE FROM hold_fill_targets");
430 $dbh->do("DELETE FROM default_branch_circ_rules");
431 $dbh->do("DELETE FROM default_branch_item_rules");
432 $dbh->do("DELETE FROM default_circ_rules");
433 $dbh->do("DELETE FROM branch_item_rules");
435 $dbh->do("
436 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
439 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
440 or BAIL_OUT("Cannot find newly created biblio record");
442 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
444 $biblioitemnumber =
445 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
446 or BAIL_OUT("Cannot find newly created biblioitems record");
448 $dbh->do("
449 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
450 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
453 $dbh->do("
454 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
455 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
458 $dbh->do("
459 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
460 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
463 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
464 undef, join( ',', $library_B, $library_A, $library_C ) );
465 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
467 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
468 C4::HoldsQueue::CreateQueue();
469 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
470 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
471 # End Bug 14297
473 # Bug 15062
474 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
475 $borrowernumber = $borrower2->{borrowernumber};
476 $library_A = $library1->{branchcode};
477 $library_B = $library2->{branchcode};
478 $dbh->do("DELETE FROM reserves");
479 $dbh->do("DELETE FROM issues");
480 $dbh->do("DELETE FROM items");
481 $dbh->do("DELETE FROM biblio");
482 $dbh->do("DELETE FROM biblioitems");
483 $dbh->do("DELETE FROM transport_cost");
484 $dbh->do("DELETE FROM tmp_holdsqueue");
485 $dbh->do("DELETE FROM hold_fill_targets");
486 $dbh->do("DELETE FROM default_branch_circ_rules");
487 $dbh->do("DELETE FROM default_branch_item_rules");
488 $dbh->do("DELETE FROM default_circ_rules");
489 $dbh->do("DELETE FROM branch_item_rules");
491 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
493 my $tc_rs = $schema->resultset('TransportCost');
494 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
495 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
497 $dbh->do("
498 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
501 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
502 or BAIL_OUT("Cannot find newly created biblio record");
504 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
506 $biblioitemnumber =
507 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
508 or BAIL_OUT("Cannot find newly created biblioitems record");
510 $dbh->do("
511 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
512 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
515 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
516 C4::HoldsQueue::CreateQueue();
517 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
518 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
519 # End Bug 15062
521 # Test hold_fulfillment_policy
522 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
523 $borrowernumber = $borrower3->{borrowernumber};
524 $library_A = $library1->{branchcode};
525 $library_B = $library2->{branchcode};
526 $library_C = $library3->{branchcode};
527 $dbh->do("DELETE FROM reserves");
528 $dbh->do("DELETE FROM issues");
529 $dbh->do("DELETE FROM items");
530 $dbh->do("DELETE FROM biblio");
531 $dbh->do("DELETE FROM biblioitems");
532 $dbh->do("DELETE FROM transport_cost");
533 $dbh->do("DELETE FROM tmp_holdsqueue");
534 $dbh->do("DELETE FROM hold_fill_targets");
535 $dbh->do("DELETE FROM default_branch_circ_rules");
536 $dbh->do("DELETE FROM default_branch_item_rules");
537 $dbh->do("DELETE FROM default_circ_rules");
538 $dbh->do("DELETE FROM branch_item_rules");
540 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
542 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
543 or BAIL_OUT("Cannot find newly created biblio record");
545 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
547 $biblioitemnumber =
548 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
549 or BAIL_OUT("Cannot find newly created biblioitems record");
551 $dbh->do("
552 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
553 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
556 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
557 $dbh->do("DELETE FROM default_circ_rules");
558 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
560 # Home branch matches pickup branch
561 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
562 C4::HoldsQueue::CreateQueue();
563 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
564 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
565 Koha::Holds->find( $reserve_id )->cancel;
567 # Holding branch matches pickup branch
568 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
569 C4::HoldsQueue::CreateQueue();
570 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
571 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
572 Koha::Holds->find( $reserve_id )->cancel;
574 # Neither branch matches pickup branch
575 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
576 C4::HoldsQueue::CreateQueue();
577 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
578 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
579 Koha::Holds->find( $reserve_id )->cancel;
581 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
582 $dbh->do("DELETE FROM default_circ_rules");
583 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
585 # Home branch matches pickup branch
586 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
587 C4::HoldsQueue::CreateQueue();
588 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
589 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
590 Koha::Holds->find( $reserve_id )->cancel;
592 # Holding branch matches pickup branch
593 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
594 C4::HoldsQueue::CreateQueue();
595 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
596 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
597 Koha::Holds->find( $reserve_id )->cancel;
599 # Neither branch matches pickup branch
600 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
601 C4::HoldsQueue::CreateQueue();
602 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
603 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
604 Koha::Holds->find( $reserve_id )->cancel;
606 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
607 $dbh->do("DELETE FROM default_circ_rules");
608 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
610 # Home branch matches pickup branch
611 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
612 C4::HoldsQueue::CreateQueue();
613 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
614 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
615 Koha::Holds->find( $reserve_id )->cancel;
617 # Holding branch matches pickup branch
618 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
619 C4::HoldsQueue::CreateQueue();
620 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
621 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
622 Koha::Holds->find( $reserve_id )->cancel;
624 # Neither branch matches pickup branch
625 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
626 C4::HoldsQueue::CreateQueue();
627 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
628 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
629 Koha::Holds->find( $reserve_id )->cancel;
631 # End testing hold_fulfillment_policy
633 # Test hold itemtype limit
634 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
635 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
636 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
637 $borrowernumber = $borrower3->{borrowernumber};
638 my $branchcode = $library1->{branchcode};
639 $dbh->do("DELETE FROM reserves");
640 $dbh->do("DELETE FROM issues");
641 $dbh->do("DELETE FROM items");
642 $dbh->do("DELETE FROM biblio");
643 $dbh->do("DELETE FROM biblioitems");
644 $dbh->do("DELETE FROM transport_cost");
645 $dbh->do("DELETE FROM tmp_holdsqueue");
646 $dbh->do("DELETE FROM hold_fill_targets");
647 $dbh->do("DELETE FROM default_branch_circ_rules");
648 $dbh->do("DELETE FROM default_branch_item_rules");
649 $dbh->do("DELETE FROM default_circ_rules");
650 $dbh->do("DELETE FROM branch_item_rules");
652 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
654 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
655 or BAIL_OUT("Cannot find newly created biblio record");
657 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
659 $biblioitemnumber =
660 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
661 or BAIL_OUT("Cannot find newly created biblioitems record");
663 $dbh->do("
664 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
665 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
668 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
669 $dbh->do("DELETE FROM default_circ_rules");
670 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
672 # Home branch matches pickup branch
673 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
674 C4::HoldsQueue::CreateQueue();
675 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
676 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
677 Koha::Holds->find( $reserve_id )->cancel;
679 # Holding branch matches pickup branch
680 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
681 C4::HoldsQueue::CreateQueue();
682 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
683 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
684 Koha::Holds->find( $reserve_id )->cancel;
686 # Neither branch matches pickup branch
687 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
688 C4::HoldsQueue::CreateQueue();
689 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
690 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
691 Koha::Holds->find( $reserve_id )->cancel;
693 # End testing hold itemtype limit
696 # Test Local Holds Priority - Bug 18001
697 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
698 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
699 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
701 $dbh->do("DELETE FROM tmp_holdsqueue");
702 $dbh->do("DELETE FROM hold_fill_targets");
703 $dbh->do("DELETE FROM reserves");
704 $dbh->do("DELETE FROM default_branch_circ_rules");
705 $dbh->do("DELETE FROM default_branch_item_rules");
706 $dbh->do("DELETE FROM default_circ_rules");
707 $dbh->do("DELETE FROM branch_item_rules");
709 my $item = Koha::Items->find( { biblionumber => $biblionumber } );
710 $item->holdingbranch( $item->homebranch );
711 $item->store();
713 my $item2 = Koha::Item->new( $item->unblessed );
714 $item2->itemnumber( undef );
715 $item2->store();
717 my $item3 = Koha::Item->new( $item->unblessed );
718 $item3->itemnumber( undef );
719 $item3->store();
721 $reserve_id = AddReserve( $item->homebranch, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
723 C4::HoldsQueue::CreateQueue();
725 my $queue_rs = $schema->resultset('TmpHoldsqueue');
726 is( $queue_rs->count(), 1, "Hold queue contains one hold from chosen from three possible items" );
728 subtest 'Trivial test for UpdateTransportCostMatrix' => sub {
729 plan tests => 1;
730 my $recs = [
731 { frombranch => $library1->{branchcode}, tobranch => $library2->{branchcode}, cost => 1, disable_transfer => 0 },
732 { frombranch => $library2->{branchcode}, tobranch => $library3->{branchcode}, cost => 0, disable_transfer => 1 },
734 C4::HoldsQueue::UpdateTransportCostMatrix( $recs );
735 is( $schema->resultset('TransportCost')->count, 2, 'UpdateTransportCostMatrix added two records' );
738 # Cleanup
739 $schema->storage->txn_rollback;
741 ### END Test holds queue builder does not violate holds policy ###
743 sub test_queue {
744 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
746 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
748 $use_cost_matrix_sth->execute($use_cost_matrix);
749 C4::Context->clear_syspref_cache();
750 C4::HoldsQueue::CreateQueue();
752 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
753 my $r = $results->[0];
755 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
756 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
757 if $hold_branch;
759 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
760 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
761 unless $ok;
764 sub dump_records {
765 my ($tablename) = @_;
766 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);