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