Bug 17654: Add tests to enforce swagger definition files integrity
[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 => 42;
12 use Data::Dumper;
13
14 use C4::Calendar;
15 use C4::Context;
16 use C4::Members;
17 use Koha::Database;
18 use Koha::DateUtils;
19
20 use t::lib::TestBuilder;
21 use t::lib::Mocks;
22
23 BEGIN {
24     use FindBin;
25     use lib $FindBin::Bin;
26     use_ok('C4::Reserves');
27     use_ok('C4::HoldsQueue');
28 }
29
30 my $schema = Koha::Database->schema;
31 $schema->storage->txn_begin;
32 my $dbh = C4::Context->dbh;
33
34 my $builder = t::lib::TestBuilder->new;
35
36 my $library1 = $builder->build({
37     source => 'Branch',
38 });
39 my $library2 = $builder->build({
40     source => 'Branch',
41 });
42 my $library3 = $builder->build({
43     source => 'Branch',
44 });
45
46 my $TITLE = "Test Holds Queue XXX";
47
48 my $borrower = $builder->build({
49     source => 'Borrower',
50     value => {
51         branchcode => $library1->{branchcode},
52     }
53 });
54
55 my $borrowernumber = $borrower->{borrowernumber};
56 # Set special (for this test) branches
57 my $borrower_branchcode = $borrower->{branchcode};
58 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
59 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
60 my $least_cost_branch_code = pop @other_branches;
61 my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
62
63 #Set up the stage
64 # Sysprefs and cost matrix
65 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
66 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
67 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
68          join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
69 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
70
71 $dbh->do("DELETE FROM transport_cost");
72 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
73 # Favour $least_cost_branch_code
74 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
75 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
76 my @b = @other_branches;
77 while ( my $b1 = shift @b ) {
78     foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
79         $transport_cost_insert_sth->execute($b1, $b2, 0.5);
80         $transport_cost_insert_sth->execute($b2, $b1, 0.5);
81     }
82 }
83
84
85 # Loanable items - all possible combinations of homebranch and holdingbranch
86 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
87           VALUES             ('SER', 'Koha test', '$TITLE', '2011-02-01')");
88 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
89   or BAIL_OUT("Cannot find newly created biblio record");
90 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
91           VALUES                  ($biblionumber, '', '$itemtype')");
92 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
93   or BAIL_OUT("Cannot find newly created biblioitems record");
94
95 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
96                                       VALUES            ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
97 my $first_barcode = int(rand(1000000000000)); # XXX
98 my $barcode = $first_barcode;
99 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
100     $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
101     $items_insert_sth->execute($barcode++, $_, $_);
102     $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
103 }
104
105 # Remove existing reserves, makes debugging easier
106 $dbh->do("DELETE FROM reserves");
107 my $bibitems = undef;
108 my $priority = 1;
109 # Make a reserve
110 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems,  $priority );
111 #                           $resdate, $expdate, $notes, $title, $checkitem, $found
112 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
113
114 # Tests
115 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
116 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
117                               JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
118                               JOIN items USING (itemnumber)
119                               WHERE borrowernumber = $borrowernumber");
120
121 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
122 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
123 test_queue ('take from homebranch',  0, $borrower_branchcode, $borrower_branchcode);
124 test_queue ('take from homebranch',  1, $borrower_branchcode, $borrower_branchcode);
125
126 $dbh->do("DELETE FROM tmp_holdsqueue");
127 $dbh->do("DELETE FROM hold_fill_targets");
128 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
129 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
130 # test_queue will flush
131 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
132 # Not sure how to make this test more difficult - holding branch does not matter
133
134 $dbh->do("DELETE FROM tmp_holdsqueue");
135 $dbh->do("DELETE FROM hold_fill_targets");
136 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
137 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
138 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
139 # We have a book available held in borrower branch
140 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
141 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
142
143 $dbh->do("DELETE FROM tmp_holdsqueue");
144 $dbh->do("DELETE FROM hold_fill_targets");
145 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
146 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
147 # No book available in borrower branch, pick according to the rules
148 # Frst branch from StaticHoldsQueueWeight
149 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
150 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
151 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
152 my $queue_item = $queue->[0];
153 ok( $queue_item
154  && $queue_item->{pickbranch} eq $borrower_branchcode
155  && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
156   or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
157 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
158
159 ok(
160     C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
161     'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
162 );
163
164 # XXX All this tests are for borrower branch pick-up.
165 # Maybe needs expanding to homebranch or holdingbranch pick-up.
166
167 $schema->txn_rollback;
168 $schema->txn_begin;
169
170 ### Test holds queue builder does not violate holds policy ###
171
172 # Clear out existing rules relating to holdallowed
173 $dbh->do("DELETE FROM default_branch_circ_rules");
174 $dbh->do("DELETE FROM default_branch_item_rules");
175 $dbh->do("DELETE FROM default_circ_rules");
176
177 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
178
179 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
180
181 $library1 = $builder->build({
182     source => 'Branch',
183 });
184 $library2 = $builder->build({
185     source => 'Branch',
186 });
187 $library3 = $builder->build({
188     source => 'Branch',
189 });
190 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
191
192 my $borrower1 = $builder->build({
193     source => 'Borrower',
194     value => {
195         branchcode => $branchcodes[0],
196     },
197 });
198 my $borrower2 = $builder->build({
199     source => 'Borrower',
200     value => {
201         branchcode => $branchcodes[1],
202     },
203 });
204 my $borrower3 = $builder->build({
205     source => 'Borrower',
206     value => {
207         branchcode => $branchcodes[2],
208     },
209 });
210
211 $dbh->do(qq{
212     INSERT INTO biblio (
213         frameworkcode, 
214         author, 
215         title, 
216         datecreated
217     ) VALUES (
218         'SER', 
219         'Koha test', 
220         '$TITLE', 
221         '2011-02-01'
222     )
223 });
224 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
225   or BAIL_OUT("Cannot find newly created biblio record");
226
227 $dbh->do(qq{
228     INSERT INTO biblioitems (
229         biblionumber, 
230         marcxml, 
231         itemtype
232     ) VALUES (
233         $biblionumber, 
234         '', 
235         '$itemtype'
236     )
237 });
238 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
239   or BAIL_OUT("Cannot find newly created biblioitems record");
240
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,
257         ?,
258         ?,
259         ?,
260         0,
261         0,
262         0,
263         0,
264         NULL,
265         '$itemtype'
266     )
267 });
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] );
273
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() )
283 });
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 );
287
288 my $holds_queue;
289
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'");
297
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",
311 );
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);
319
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'" );
325
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 );
336
337 ## Test LocalHoldsPriority
338 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
339
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");
343
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 );
352
353 $dbh->do("DELETE FROM items");
354 # barcode, homebranch, holdingbranch, itemtype
355 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
356
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");
360
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 );
369
370 $dbh->do("DELETE FROM items");
371 # barcode, homebranch, holdingbranch, itemtype
372 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
373
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");
377
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 );
386
387 $dbh->do("DELETE FROM items");
388 # barcode, homebranch, holdingbranch, itemtype
389 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
390
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");
394
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 );
403
404 $dbh->do("DELETE FROM items");
405 # barcode, homebranch, holdingbranch, itemtype
406 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
407
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");
411
412 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
413 ## End testing of LocalHoldsPriority
414
415
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");
434
435 $dbh->do("
436     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
437 ");
438
439 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
440   or BAIL_OUT("Cannot find newly created biblio record");
441
442 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
443
444 $biblioitemnumber =
445   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
446   or BAIL_OUT("Cannot find newly created biblioitems record");
447
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')
451 ");
452
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')
456 ");
457
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' );
461 ");
462
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'" );
466
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
472
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");
490
491 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
492
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 });
496
497 $dbh->do("
498     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
499 ");
500
501 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
502   or BAIL_OUT("Cannot find newly created biblio record");
503
504 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
505
506 $biblioitemnumber =
507   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
508   or BAIL_OUT("Cannot find newly created biblioitems record");
509
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')
513 ");
514
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
520
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");
539
540 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
541
542 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
543   or BAIL_OUT("Cannot find newly created biblio record");
544
545 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
546
547 $biblioitemnumber =
548   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
549   or BAIL_OUT("Cannot find newly created biblioitems record");
550
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')
554 ");
555
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' )");
559
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 CancelReserve( { reserve_id => $reserve_id } );
566
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 CancelReserve( { reserve_id => $reserve_id } );
573
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 CancelReserve( { reserve_id => $reserve_id } );
580
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' )");
584
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 CancelReserve( { reserve_id => $reserve_id } );
591
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 CancelReserve( { reserve_id => $reserve_id } );
598
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 CancelReserve( { reserve_id => $reserve_id } );
605
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' )");
609
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 CancelReserve( { reserve_id => $reserve_id } );
616
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 CancelReserve( { reserve_id => $reserve_id } );
623
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 CancelReserve( { reserve_id => $reserve_id } );
630
631 # End testing hold_fulfillment_policy
632
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");
651
652 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
653
654 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
655   or BAIL_OUT("Cannot find newly created biblio record");
656
657 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
658
659 $biblioitemnumber =
660   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
661   or BAIL_OUT("Cannot find newly created biblioitems record");
662
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')
666 ");
667
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' )");
671
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 CancelReserve( { reserve_id => $reserve_id } );
678
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 CancelReserve( { reserve_id => $reserve_id } );
685
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 CancelReserve( { reserve_id => $reserve_id } );
692
693 # End testing hold itemtype limit
694
695 # Cleanup
696 $schema->storage->txn_rollback;
697
698 ### END Test holds queue builder does not violate holds policy ###
699
700 sub test_queue {
701     my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
702
703     $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
704
705     $use_cost_matrix_sth->execute($use_cost_matrix);
706     C4::Context->clear_syspref_cache();
707     C4::HoldsQueue::CreateQueue();
708
709     my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
710     my $r = $results->[0];
711
712     my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
713     $ok &&=  is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
714       if $hold_branch;
715
716     diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
717         . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
718       unless $ok;
719 }
720
721 sub dump_records {
722     my ($tablename) = @_;
723     return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
724 }