Bug 16453: Make Elasticsearch tests be skipped if configuration entry missing
[koha.git] / t / db_dependent / HoldsQueue.t
1 #!/usr/bin/perl
2
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
8
9 use Modern::Perl;
10
11 use Test::More tests => 38;
12 use Data::Dumper;
13
14 use C4::Branch;
15 use C4::Calendar;
16 use C4::Context;
17 use C4::Members;
18 use Koha::Database;
19 use Koha::DateUtils;
20 use Koha::ItemType;
21
22 use t::lib::TestBuilder;
23
24 use Koha::ItemTypes;
25
26 BEGIN {
27     use FindBin;
28     use lib $FindBin::Bin;
29     use_ok('C4::Reserves');
30     use_ok('C4::HoldsQueue');
31 }
32
33 my $schema = Koha::Database->schema;
34 $schema->storage->txn_begin;
35 my $dbh = C4::Context->dbh;
36
37 my $builder = t::lib::TestBuilder->new;
38
39 my $library1 = $builder->build({
40     source => 'Branch',
41 });
42 my $library2 = $builder->build({
43     source => 'Branch',
44 });
45 my $library3 = $builder->build({
46     source => 'Branch',
47 });
48
49 my $TITLE = "Test Holds Queue XXX";
50
51 my $borrower = $builder->build({
52     source => 'Borrower',
53     value => {
54         branchcode => $library1->{branchcode},
55     }
56 });
57
58 my $borrowernumber = $borrower->{borrowernumber};
59 # Set special (for this test) branches
60 my $borrower_branchcode = $borrower->{branchcode};
61 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
62 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
63 my $least_cost_branch_code = pop @other_branches;
64 my $itemtype = Koha::ItemTypes->search({ notforloan => 1 })->next;
65 $itemtype or BAIL_OUT("No adequate itemtype"); #FIXME Should be $itemtype = $itemtype->itemtype
66
67 #Set up the stage
68 # Sysprefs and cost matrix
69 C4::Context->set_preference('HoldsQueueSkipClosed', 0);
70 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
71          join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
72 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
73
74 $dbh->do("DELETE FROM transport_cost");
75 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
76 # Favour $least_cost_branch_code
77 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
78 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
79 my @b = @other_branches;
80 while ( my $b1 = shift @b ) {
81     foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
82         $transport_cost_insert_sth->execute($b1, $b2, 0.5);
83         $transport_cost_insert_sth->execute($b2, $b1, 0.5);
84     }
85 }
86
87
88 # Loanable items - all possible combinations of homebranch and holdingbranch
89 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
90           VALUES             ('SER', 'Koha test', '$TITLE', '2011-02-01')");
91 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
92   or BAIL_OUT("Cannot find newly created biblio record");
93 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
94           VALUES                  ($biblionumber, '', '$itemtype')");
95 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
96   or BAIL_OUT("Cannot find newly created biblioitems record");
97
98 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
99                                       VALUES            ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
100 my $first_barcode = int(rand(1000000000000)); # XXX
101 my $barcode = $first_barcode;
102 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
103     $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
104     $items_insert_sth->execute($barcode++, $_, $_);
105     $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
106 }
107
108 # Remove existing reserves, makes debugging easier
109 $dbh->do("DELETE FROM reserves");
110 my $bibitems = undef;
111 my $priority = 1;
112 # Make a reserve
113 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems,  $priority );
114 #                           $resdate, $expdate, $notes, $title, $checkitem, $found
115 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
116
117 # Tests
118 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
119 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
120                               JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
121                               JOIN items USING (itemnumber)
122                               WHERE borrowernumber = $borrowernumber");
123
124 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
125 C4::Context->set_preference('AutomaticItemReturn', 0);
126 test_queue ('take from homebranch',  0, $borrower_branchcode, $borrower_branchcode);
127 test_queue ('take from homebranch',  1, $borrower_branchcode, $borrower_branchcode);
128
129 $dbh->do("DELETE FROM tmp_holdsqueue");
130 $dbh->do("DELETE FROM hold_fill_targets");
131 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
132 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
133 # test_queue will flush
134 C4::Context->set_preference('AutomaticItemReturn', 1);
135 # Not sure how to make this test more difficult - holding branch does not matter
136
137 $dbh->do("DELETE FROM tmp_holdsqueue");
138 $dbh->do("DELETE FROM hold_fill_targets");
139 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
140 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
141 C4::Context->set_preference('AutomaticItemReturn', 0);
142 # We have a book available held in borrower branch
143 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
144 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
145
146 $dbh->do("DELETE FROM tmp_holdsqueue");
147 $dbh->do("DELETE FROM hold_fill_targets");
148 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
149 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
150 # No book available in borrower branch, pick according to the rules
151 # Frst branch from StaticHoldsQueueWeight
152 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
153 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
154 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
155 my $queue_item = $queue->[0];
156 ok( $queue_item
157  && $queue_item->{pickbranch} eq $borrower_branchcode
158  && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
159   or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
160 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
161
162 ok(
163     C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
164     'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
165 );
166
167 # XXX All this tests are for borrower branch pick-up.
168 # Maybe needs expanding to homebranch or holdingbranch pick-up.
169
170 $schema->txn_rollback;
171 $schema->txn_begin;
172
173 ### Test holds queue builder does not violate holds policy ###
174
175 # Clear out existing rules relating to holdallowed
176 $dbh->do("DELETE FROM default_branch_circ_rules");
177 $dbh->do("DELETE FROM default_branch_item_rules");
178 $dbh->do("DELETE FROM default_circ_rules");
179
180 C4::Context->set_preference('UseTransportCostMatrix', 0);
181
182 $itemtype = Koha::ItemTypes->search->next->itemtype;
183
184 $library1 = $builder->build({
185     source => 'Branch',
186 });
187 $library2 = $builder->build({
188     source => 'Branch',
189 });
190 $library3 = $builder->build({
191     source => 'Branch',
192 });
193 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
194
195 my $borrower1 = $builder->build({
196     source => 'Borrower',
197     value => {
198         branchcode => $branchcodes[0],
199     },
200 });
201 my $borrower2 = $builder->build({
202     source => 'Borrower',
203     value => {
204         branchcode => $branchcodes[1],
205     },
206 });
207 my $borrower3 = $builder->build({
208     source => 'Borrower',
209     value => {
210         branchcode => $branchcodes[2],
211     },
212 });
213
214 $dbh->do(qq{
215     INSERT INTO biblio (
216         frameworkcode, 
217         author, 
218         title, 
219         datecreated
220     ) VALUES (
221         'SER', 
222         'Koha test', 
223         '$TITLE', 
224         '2011-02-01'
225     )
226 });
227 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
228   or BAIL_OUT("Cannot find newly created biblio record");
229
230 $dbh->do(qq{
231     INSERT INTO biblioitems (
232         biblionumber, 
233         marcxml, 
234         itemtype
235     ) VALUES (
236         $biblionumber, 
237         '', 
238         '$itemtype'
239     )
240 });
241 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
242   or BAIL_OUT("Cannot find newly created biblioitems record");
243
244 $items_insert_sth = $dbh->prepare(qq{
245     INSERT INTO items (
246         biblionumber, 
247         biblioitemnumber,
248         barcode,
249         homebranch,
250         holdingbranch,
251         notforloan,
252         damaged,
253         itemlost,
254         withdrawn,
255         onloan,
256         itype
257     ) VALUES (
258         $biblionumber,
259         $biblioitemnumber,
260         ?,
261         ?,
262         ?,
263         0,
264         0,
265         0,
266         0,
267         NULL,
268         '$itemtype'
269     )
270 });
271 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
272 $barcode = int( rand(1000000000000) );
273 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
274 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
275 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
276
277 $dbh->do("DELETE FROM reserves");
278 my $sth = $dbh->prepare(q{
279     INSERT INTO reserves ( 
280         borrowernumber,
281         biblionumber,
282         branchcode,
283         priority,
284         reservedate
285     ) VALUES ( ?,?,?,?, CURRENT_DATE() )
286 });
287 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
288 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
289 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
290
291 my $holds_queue;
292
293 $dbh->do("DELETE FROM default_circ_rules");
294 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
295 C4::HoldsQueue::CreateQueue();
296 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
297 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
298 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
299 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
300
301 # Test skipping hold picks for closed libraries.
302 # At this point in the test, we have 2 rows in the holds queue
303 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
304 # and make today a holiday for MPL. When we run it again we should only
305 # have 1 row in the holds queue
306 C4::Context->set_preference('HoldsQueueSkipClosed', 1);
307 my $today = dt_from_string();
308 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
309     day         => $today->day(),
310     month       => $today->month(),
311     year        => $today->year(),
312     title       => "$today",
313     description => "$today",
314 );
315 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
316 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
317 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
318 C4::HoldsQueue::CreateQueue();
319 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
320 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
321 C4::Context->set_preference('HoldsQueueSkipClosed', 0);
322
323 $dbh->do("DELETE FROM default_circ_rules");
324 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
325 C4::HoldsQueue::CreateQueue();
326 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
327 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
328
329 # Test skipping hold picks for closed libraries without transport cost matrix
330 # At this point in the test, we have 3 rows in the holds queue
331 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
332 # and use our previously created holiday for MPL
333 # When we run it again we should only have 2 rows in the holds queue
334 C4::Context->set_preference( 'HoldsQueueSkipClosed', 1 );
335 C4::HoldsQueue::CreateQueue();
336 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
337 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
338 C4::Context->set_preference( 'HoldsQueueSkipClosed', 0 );
339
340 # Bug 14297
341 $itemtype = Koha::ItemTypes->search->next->itemtype;
342 $borrowernumber = $borrower3->{borrowernumber};
343 my $library_A = $library1->{branchcode};
344 my $library_B = $library2->{branchcode};
345 my $library_C = $borrower3->{branchcode};
346 $dbh->do("DELETE FROM reserves");
347 $dbh->do("DELETE FROM issues");
348 $dbh->do("DELETE FROM items");
349 $dbh->do("DELETE FROM biblio");
350 $dbh->do("DELETE FROM biblioitems");
351 $dbh->do("DELETE FROM transport_cost");
352 $dbh->do("DELETE FROM tmp_holdsqueue");
353 $dbh->do("DELETE FROM hold_fill_targets");
354 $dbh->do("DELETE FROM default_branch_circ_rules");
355 $dbh->do("DELETE FROM default_branch_item_rules");
356 $dbh->do("DELETE FROM default_circ_rules");
357 $dbh->do("DELETE FROM branch_item_rules");
358
359 $dbh->do("
360     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
361 ");
362
363 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
364   or BAIL_OUT("Cannot find newly created biblio record");
365
366 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
367
368 $biblioitemnumber =
369   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
370   or BAIL_OUT("Cannot find newly created biblioitems record");
371
372 $dbh->do("
373     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
374     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
375 ");
376
377 $dbh->do("
378     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
379     VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
380 ");
381
382 $dbh->do("
383     INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
384     ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
385 ");
386
387 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
388     undef, join( ',', $library_B, $library_A, $library_C ) );
389 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
390
391 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
392 C4::HoldsQueue::CreateQueue();
393 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
394 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
395 # End Bug 14297
396
397 # Bug 15062
398 $itemtype = Koha::ItemTypes->search->next->itemtype;
399 $borrowernumber = $borrower2->{borrowernumber};
400 $library_A = $library1->{branchcode};
401 $library_B = $library2->{branchcode};
402 $dbh->do("DELETE FROM reserves");
403 $dbh->do("DELETE FROM issues");
404 $dbh->do("DELETE FROM items");
405 $dbh->do("DELETE FROM biblio");
406 $dbh->do("DELETE FROM biblioitems");
407 $dbh->do("DELETE FROM transport_cost");
408 $dbh->do("DELETE FROM tmp_holdsqueue");
409 $dbh->do("DELETE FROM hold_fill_targets");
410 $dbh->do("DELETE FROM default_branch_circ_rules");
411 $dbh->do("DELETE FROM default_branch_item_rules");
412 $dbh->do("DELETE FROM default_circ_rules");
413 $dbh->do("DELETE FROM branch_item_rules");
414
415 C4::Context->set_preference("UseTransportCostMatrix",1);
416
417 my $tc_rs = $schema->resultset('TransportCost');
418 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
419 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
420
421 $dbh->do("
422     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
423 ");
424
425 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
426   or BAIL_OUT("Cannot find newly created biblio record");
427
428 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
429
430 $biblioitemnumber =
431   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
432   or BAIL_OUT("Cannot find newly created biblioitems record");
433
434 $dbh->do("
435     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
436     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
437 ");
438
439 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
440 C4::HoldsQueue::CreateQueue();
441 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
442 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
443 # End Bug 15062
444
445 # Test hold_fulfillment_policy
446 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
447 $borrowernumber = $borrower3->{borrowernumber};
448 $library_A = $library1->{branchcode};
449 $library_B = $library2->{branchcode};
450 $library_C = $library3->{branchcode};
451 $dbh->do("DELETE FROM reserves");
452 $dbh->do("DELETE FROM issues");
453 $dbh->do("DELETE FROM items");
454 $dbh->do("DELETE FROM biblio");
455 $dbh->do("DELETE FROM biblioitems");
456 $dbh->do("DELETE FROM transport_cost");
457 $dbh->do("DELETE FROM tmp_holdsqueue");
458 $dbh->do("DELETE FROM hold_fill_targets");
459 $dbh->do("DELETE FROM default_branch_circ_rules");
460 $dbh->do("DELETE FROM default_branch_item_rules");
461 $dbh->do("DELETE FROM default_circ_rules");
462 $dbh->do("DELETE FROM branch_item_rules");
463
464 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
465
466 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
467   or BAIL_OUT("Cannot find newly created biblio record");
468
469 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
470
471 $biblioitemnumber =
472   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
473   or BAIL_OUT("Cannot find newly created biblioitems record");
474
475 $dbh->do("
476     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
477     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
478 ");
479
480 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
481 $dbh->do("DELETE FROM default_circ_rules");
482 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
483
484 # Home branch matches pickup branch
485 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
486 C4::HoldsQueue::CreateQueue();
487 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
488 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
489 CancelReserve( { reserve_id => $reserve_id } );
490
491 # Holding branch matches pickup branch
492 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
493 C4::HoldsQueue::CreateQueue();
494 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
495 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
496 CancelReserve( { reserve_id => $reserve_id } );
497
498 # Neither branch matches pickup branch
499 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
500 C4::HoldsQueue::CreateQueue();
501 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
502 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
503 CancelReserve( { reserve_id => $reserve_id } );
504
505 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
506 $dbh->do("DELETE FROM default_circ_rules");
507 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
508
509 # Home branch matches pickup branch
510 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
511 C4::HoldsQueue::CreateQueue();
512 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
513 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
514 CancelReserve( { reserve_id => $reserve_id } );
515
516 # Holding branch matches pickup branch
517 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
518 C4::HoldsQueue::CreateQueue();
519 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
520 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
521 CancelReserve( { reserve_id => $reserve_id } );
522
523 # Neither branch matches pickup branch
524 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
525 C4::HoldsQueue::CreateQueue();
526 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
527 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
528 CancelReserve( { reserve_id => $reserve_id } );
529
530 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
531 $dbh->do("DELETE FROM default_circ_rules");
532 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
533
534 # Home branch matches pickup branch
535 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
536 C4::HoldsQueue::CreateQueue();
537 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
538 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
539 CancelReserve( { reserve_id => $reserve_id } );
540
541 # Holding branch matches pickup branch
542 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
543 C4::HoldsQueue::CreateQueue();
544 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
545 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
546 CancelReserve( { reserve_id => $reserve_id } );
547
548 # Neither branch matches pickup branch
549 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
550 C4::HoldsQueue::CreateQueue();
551 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
552 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
553 CancelReserve( { reserve_id => $reserve_id } );
554
555 # End testing hold_fulfillment_policy
556
557 # Test hold itemtype limit
558 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
559 my @itemtypes = Koha::ItemTypes->search();
560 my $wrong_itemtype = $itemtypes[0]->itemtype;
561 my $right_itemtype = $itemtypes[1]->itemtype;
562 $borrowernumber = $borrower3->{borrowernumber};
563 my $branchcode = $library1->{branchcode};
564 $dbh->do("DELETE FROM reserves");
565 $dbh->do("DELETE FROM issues");
566 $dbh->do("DELETE FROM items");
567 $dbh->do("DELETE FROM biblio");
568 $dbh->do("DELETE FROM biblioitems");
569 $dbh->do("DELETE FROM transport_cost");
570 $dbh->do("DELETE FROM tmp_holdsqueue");
571 $dbh->do("DELETE FROM hold_fill_targets");
572 $dbh->do("DELETE FROM default_branch_circ_rules");
573 $dbh->do("DELETE FROM default_branch_item_rules");
574 $dbh->do("DELETE FROM default_circ_rules");
575 $dbh->do("DELETE FROM branch_item_rules");
576
577 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
578
579 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
580   or BAIL_OUT("Cannot find newly created biblio record");
581
582 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
583
584 $biblioitemnumber =
585   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
586   or BAIL_OUT("Cannot find newly created biblioitems record");
587
588 $dbh->do("
589     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
590     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
591 ");
592
593 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
594 $dbh->do("DELETE FROM default_circ_rules");
595 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
596
597 # Home branch matches pickup branch
598 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
599 C4::HoldsQueue::CreateQueue();
600 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
601 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
602 CancelReserve( { reserve_id => $reserve_id } );
603
604 # Holding branch matches pickup branch
605 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
606 C4::HoldsQueue::CreateQueue();
607 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
608 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
609 CancelReserve( { reserve_id => $reserve_id } );
610
611 # Neither branch matches pickup branch
612 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
613 C4::HoldsQueue::CreateQueue();
614 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
615 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
616 CancelReserve( { reserve_id => $reserve_id } );
617
618 # End testing hold itemtype limit
619
620 # Cleanup
621 $schema->storage->txn_rollback;
622
623 ### END Test holds queue builder does not violate holds policy ###
624
625 sub test_queue {
626     my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
627
628     $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
629
630     $use_cost_matrix_sth->execute($use_cost_matrix);
631     C4::Context->clear_syspref_cache();
632     C4::HoldsQueue::CreateQueue();
633
634     my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
635     my $r = $results->[0];
636
637     my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
638     $ok &&=  is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
639       if $hold_branch;
640
641     diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
642         . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
643       unless $ok;
644 }
645
646 sub dump_records {
647     my ($tablename) = @_;
648     return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
649 }