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
11 use Test
::More tests
=> 42;
20 use t
::lib
::TestBuilder
;
25 use lib
$FindBin::Bin
;
26 use_ok
('C4::Reserves');
27 use_ok
('C4::HoldsQueue');
30 my $schema = Koha
::Database
->schema;
31 $schema->storage->txn_begin;
32 my $dbh = C4
::Context
->dbh;
34 my $builder = t
::lib
::TestBuilder
->new;
36 my $library1 = $builder->build({
39 my $library2 = $builder->build({
42 my $library3 = $builder->build({
46 my $TITLE = "Test Holds Queue XXX";
48 my $borrower = $builder->build({
51 branchcode
=> $library1->{branchcode
},
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
};
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'");
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);
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");
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);
105 # Remove existing reserves, makes debugging easier
106 $dbh->do("DELETE FROM reserves");
107 my $bibitems = undef;
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 )");
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");
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);
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
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);
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];
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)' );
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'
164 # XXX All this tests are for borrower branch pick-up.
165 # Maybe needs expanding to homebranch or holdingbranch pick-up.
167 $schema->txn_rollback;
170 ### Test holds queue builder does not violate holds policy ###
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");
177 t
::lib
::Mocks
::mock_preference
('UseTransportCostMatrix', 0);
179 $itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
181 $library1 = $builder->build({
184 $library2 = $builder->build({
187 $library3 = $builder->build({
190 @branchcodes = ( $library1->{branchcode
}, $library2->{branchcode
}, $library3->{branchcode
} );
192 my $borrower1 = $builder->build({
193 source
=> 'Borrower',
195 branchcode
=> $branchcodes[0],
198 my $borrower2 = $builder->build({
199 source
=> 'Borrower',
201 branchcode
=> $branchcodes[1],
204 my $borrower3 = $builder->build({
205 source
=> 'Borrower',
207 branchcode
=> $branchcodes[2],
224 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
225 or BAIL_OUT
("Cannot find newly created biblio record");
228 INSERT INTO biblioitems
(
238 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
239 or BAIL_OUT
("Cannot find newly created biblioitems record");
241 $items_insert_sth = $dbh->prepare(qq{
268 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
269 $barcode = int( rand(1000000000000) );
270 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
271 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
272 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
274 $dbh->do("DELETE FROM reserves");
275 my $sth = $dbh->prepare(q{
276 INSERT INTO reserves (
282 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
284 $sth->execute( $borrower1->{borrowernumber
}, $biblionumber, $branchcodes[0], 1 );
285 $sth->execute( $borrower2->{borrowernumber
}, $biblionumber, $branchcodes[0], 2 );
286 $sth->execute( $borrower3->{borrowernumber
}, $biblionumber, $branchcodes[0], 3 );
290 $dbh->do("DELETE FROM default_circ_rules");
291 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
292 C4
::HoldsQueue
::CreateQueue
();
293 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
294 is
( @
$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
295 is
( $holds_queue->[0]->{cardnumber
}, $borrower1->{cardnumber
}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
296 is
( $holds_queue->[1]->{cardnumber
}, $borrower2->{cardnumber
}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
298 # Test skipping hold picks for closed libraries.
299 # At this point in the test, we have 2 rows in the holds queue
300 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
301 # and make today a holiday for MPL. When we run it again we should only
302 # have 1 row in the holds queue
303 t
::lib
::Mocks
::mock_preference
('HoldsQueueSkipClosed', 1);
304 my $today = dt_from_string
();
305 C4
::Calendar
->new( branchcode
=> $branchcodes[0] )->insert_single_holiday(
306 day
=> $today->day(),
307 month
=> $today->month(),
308 year
=> $today->year(),
310 description
=> "$today",
312 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
313 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
314 is
( Koha
::Calendar
->new( branchcode
=> $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
315 C4
::HoldsQueue
::CreateQueue
();
316 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
317 is
( scalar( @
$holds_queue ), 1, "Holds not filled with items from closed libraries" );
318 t
::lib
::Mocks
::mock_preference
('HoldsQueueSkipClosed', 0);
320 $dbh->do("DELETE FROM default_circ_rules");
321 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
322 C4
::HoldsQueue
::CreateQueue
();
323 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
324 is
( @
$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
326 # Test skipping hold picks for closed libraries without transport cost matrix
327 # At this point in the test, we have 3 rows in the holds queue
328 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
329 # and use our previously created holiday for MPL
330 # When we run it again we should only have 2 rows in the holds queue
331 t
::lib
::Mocks
::mock_preference
( 'HoldsQueueSkipClosed', 1 );
332 C4
::HoldsQueue
::CreateQueue
();
333 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
334 is
( scalar( @
$holds_queue ), 2, "Holds not filled with items from closed libraries" );
335 t
::lib
::Mocks
::mock_preference
( 'HoldsQueueSkipClosed', 0 );
337 ## Test LocalHoldsPriority
338 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriority', 1);
340 $dbh->do("DELETE FROM default_circ_rules");
341 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
342 $dbh->do("DELETE FROM issues");
344 # Test homebranch = patron branch
345 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityPatronControl', 'HomeLibrary');
346 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityItemControl', 'homebranch');
347 C4
::Context
->clear_syspref_cache();
348 $dbh->do("DELETE FROM reserves");
349 $sth->execute( $borrower1->{borrowernumber
}, $biblionumber, $branchcodes[0], 1 );
350 $sth->execute( $borrower2->{borrowernumber
}, $biblionumber, $branchcodes[0], 2 );
351 $sth->execute( $borrower3->{borrowernumber
}, $biblionumber, $branchcodes[0], 3 );
353 $dbh->do("DELETE FROM items");
354 # barcode, homebranch, holdingbranch, itemtype
355 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
357 C4
::HoldsQueue
::CreateQueue
();
358 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
359 is
( $holds_queue->[0]->{cardnumber
}, $borrower3->{cardnumber
}, "Holds queue giving priority to patron who's home library matches item's home library");
361 # Test holdingbranch = patron branch
362 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityPatronControl', 'HomeLibrary');
363 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityItemControl', 'holdingbranch');
364 C4
::Context
->clear_syspref_cache();
365 $dbh->do("DELETE FROM reserves");
366 $sth->execute( $borrower1->{borrowernumber
}, $biblionumber, $branchcodes[0], 1 );
367 $sth->execute( $borrower2->{borrowernumber
}, $biblionumber, $branchcodes[0], 2 );
368 $sth->execute( $borrower3->{borrowernumber
}, $biblionumber, $branchcodes[0], 3 );
370 $dbh->do("DELETE FROM items");
371 # barcode, homebranch, holdingbranch, itemtype
372 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
374 C4
::HoldsQueue
::CreateQueue
();
375 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
376 is
( $holds_queue->[0]->{cardnumber
}, $borrower3->{cardnumber
}, "Holds queue giving priority to patron who's home library matches item's holding library");
378 # Test holdingbranch = pickup branch
379 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityPatronControl', 'PickupLibrary');
380 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityItemControl', 'holdingbranch');
381 C4
::Context
->clear_syspref_cache();
382 $dbh->do("DELETE FROM reserves");
383 $sth->execute( $borrower1->{borrowernumber
}, $biblionumber, $branchcodes[0], 1 );
384 $sth->execute( $borrower2->{borrowernumber
}, $biblionumber, $branchcodes[0], 2 );
385 $sth->execute( $borrower3->{borrowernumber
}, $biblionumber, $branchcodes[2], 3 );
387 $dbh->do("DELETE FROM items");
388 # barcode, homebranch, holdingbranch, itemtype
389 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
391 C4
::HoldsQueue
::CreateQueue
();
392 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
393 is
( $holds_queue->[0]->{cardnumber
}, $borrower3->{cardnumber
}, "Holds queue giving priority to patron who's home library matches item's holding library");
395 # Test homebranch = pickup branch
396 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityPatronControl', 'PickupLibrary');
397 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriorityItemControl', 'homebranch');
398 C4
::Context
->clear_syspref_cache();
399 $dbh->do("DELETE FROM reserves");
400 $sth->execute( $borrower1->{borrowernumber
}, $biblionumber, $branchcodes[0], 1 );
401 $sth->execute( $borrower2->{borrowernumber
}, $biblionumber, $branchcodes[0], 2 );
402 $sth->execute( $borrower3->{borrowernumber
}, $biblionumber, $branchcodes[2], 3 );
404 $dbh->do("DELETE FROM items");
405 # barcode, homebranch, holdingbranch, itemtype
406 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
408 C4
::HoldsQueue
::CreateQueue
();
409 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
410 is
( $holds_queue->[0]->{cardnumber
}, $borrower3->{cardnumber
}, "Holds queue giving priority to patron who's home library matches item's holding library");
412 t
::lib
::Mocks
::mock_preference
('LocalHoldsPriority', 0);
413 ## End testing of LocalHoldsPriority
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");
436 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
439 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
440 or BAIL_OUT
("Cannot find newly created biblio record");
442 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
445 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
446 or BAIL_OUT
("Cannot find newly created biblioitems record");
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')
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')
459 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
460 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
463 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
464 undef, join( ',', $library_B, $library_A, $library_C ) );
465 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
467 my $reserve_id = AddReserve
( $library_C, $borrowernumber, $biblionumber, '', 1 );
468 C4
::HoldsQueue
::CreateQueue
();
469 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
470 is
( @
$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
474 $itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
475 $borrowernumber = $borrower2->{borrowernumber
};
476 $library_A = $library1->{branchcode
};
477 $library_B = $library2->{branchcode
};
478 $dbh->do("DELETE FROM reserves");
479 $dbh->do("DELETE FROM issues");
480 $dbh->do("DELETE FROM items");
481 $dbh->do("DELETE FROM biblio");
482 $dbh->do("DELETE FROM biblioitems");
483 $dbh->do("DELETE FROM transport_cost");
484 $dbh->do("DELETE FROM tmp_holdsqueue");
485 $dbh->do("DELETE FROM hold_fill_targets");
486 $dbh->do("DELETE FROM default_branch_circ_rules");
487 $dbh->do("DELETE FROM default_branch_item_rules");
488 $dbh->do("DELETE FROM default_circ_rules");
489 $dbh->do("DELETE FROM branch_item_rules");
491 t
::lib
::Mocks
::mock_preference
("UseTransportCostMatrix",1);
493 my $tc_rs = $schema->resultset('TransportCost');
494 $tc_rs->create({ frombranch
=> $library_A, tobranch
=> $library_B, cost
=> 0, disable_transfer
=> 1 });
495 $tc_rs->create({ frombranch
=> $library_B, tobranch
=> $library_A, cost
=> 0, disable_transfer
=> 1 });
498 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
501 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
502 or BAIL_OUT
("Cannot find newly created biblio record");
504 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
507 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
508 or BAIL_OUT
("Cannot find newly created biblioitems record");
511 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
512 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
515 $reserve_id = AddReserve
( $library_B, $borrowernumber, $biblionumber, '', 1 );
516 C4
::HoldsQueue
::CreateQueue
();
517 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
518 is
( @
$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
521 # Test hold_fulfillment_policy
522 t
::lib
::Mocks
::mock_preference
( "UseTransportCostMatrix", 0 );
523 $borrowernumber = $borrower3->{borrowernumber
};
524 $library_A = $library1->{branchcode
};
525 $library_B = $library2->{branchcode
};
526 $library_C = $library3->{branchcode
};
527 $dbh->do("DELETE FROM reserves");
528 $dbh->do("DELETE FROM issues");
529 $dbh->do("DELETE FROM items");
530 $dbh->do("DELETE FROM biblio");
531 $dbh->do("DELETE FROM biblioitems");
532 $dbh->do("DELETE FROM transport_cost");
533 $dbh->do("DELETE FROM tmp_holdsqueue");
534 $dbh->do("DELETE FROM hold_fill_targets");
535 $dbh->do("DELETE FROM default_branch_circ_rules");
536 $dbh->do("DELETE FROM default_branch_item_rules");
537 $dbh->do("DELETE FROM default_circ_rules");
538 $dbh->do("DELETE FROM branch_item_rules");
540 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
542 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
543 or BAIL_OUT
("Cannot find newly created biblio record");
545 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
548 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
549 or BAIL_OUT
("Cannot find newly created biblioitems record");
552 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
553 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
556 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
557 $dbh->do("DELETE FROM default_circ_rules");
558 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
560 # Home branch matches pickup branch
561 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1 );
562 C4
::HoldsQueue
::CreateQueue
();
563 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
564 is
( @
$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
565 CancelReserve
( { reserve_id
=> $reserve_id } );
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 } );
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 } );
581 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
582 $dbh->do("DELETE FROM default_circ_rules");
583 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
585 # Home branch matches pickup branch
586 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1 );
587 C4
::HoldsQueue
::CreateQueue
();
588 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
589 is
( @
$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
590 CancelReserve
( { reserve_id
=> $reserve_id } );
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 } );
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 } );
606 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
607 $dbh->do("DELETE FROM default_circ_rules");
608 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
610 # Home branch matches pickup branch
611 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1 );
612 C4
::HoldsQueue
::CreateQueue
();
613 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
614 is
( @
$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
615 CancelReserve
( { reserve_id
=> $reserve_id } );
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 } );
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 } );
631 # End testing hold_fulfillment_policy
633 # Test hold itemtype limit
634 t
::lib
::Mocks
::mock_preference
( "UseTransportCostMatrix", 0 );
635 my $wrong_itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
636 my $right_itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
637 $borrowernumber = $borrower3->{borrowernumber
};
638 my $branchcode = $library1->{branchcode
};
639 $dbh->do("DELETE FROM reserves");
640 $dbh->do("DELETE FROM issues");
641 $dbh->do("DELETE FROM items");
642 $dbh->do("DELETE FROM biblio");
643 $dbh->do("DELETE FROM biblioitems");
644 $dbh->do("DELETE FROM transport_cost");
645 $dbh->do("DELETE FROM tmp_holdsqueue");
646 $dbh->do("DELETE FROM hold_fill_targets");
647 $dbh->do("DELETE FROM default_branch_circ_rules");
648 $dbh->do("DELETE FROM default_branch_item_rules");
649 $dbh->do("DELETE FROM default_circ_rules");
650 $dbh->do("DELETE FROM branch_item_rules");
652 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
654 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
655 or BAIL_OUT
("Cannot find newly created biblio record");
657 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
660 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
661 or BAIL_OUT
("Cannot find newly created biblioitems record");
664 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
665 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
668 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
669 $dbh->do("DELETE FROM default_circ_rules");
670 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
672 # Home branch matches pickup branch
673 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
674 C4
::HoldsQueue
::CreateQueue
();
675 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
676 is
( @
$holds_queue, 0, "Item with incorrect itemtype not targeted" );
677 CancelReserve
( { reserve_id
=> $reserve_id } );
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 } );
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 } );
693 # End testing hold itemtype limit
696 $schema->storage->txn_rollback;
698 ### END Test holds queue builder does not violate holds policy ###
701 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
703 $test_name = "$test_name (".($use_cost_matrix ?
"" : "don't ")."use cost matrix)";
705 $use_cost_matrix_sth->execute($use_cost_matrix);
706 C4
::Context
->clear_syspref_cache();
707 C4
::HoldsQueue
::CreateQueue
();
709 my $results = $dbh->selectall_arrayref($test_sth, { Slice
=> {} }); # should be only one
710 my $r = $results->[0];
712 my $ok = is
( $r->{pickbranch
}, $pick_branch, "$test_name pick up branch");
713 $ok &&= is
( $r->{holdingbranch
}, $hold_branch, "$test_name holding branch")
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)) )
722 my ($tablename) = @_;
723 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice
=> {} }, $borrowernumber);