Bug 14778: Install fixtures for t/Biblio.t
[koha.git] / t / db_dependent / HoldsQueue.t
blob97018a7fdb2d623181c391d4ab6173fc41fda4e7
1 #!/usr/bin/perl
3 # Test C4::HoldsQueue::CreateQueue() for both transport cost matrix
4 # and StaticHoldsQueueWeight array (no RandomizeHoldsQueueWeight, no point)
5 # Wraps tests in transaction that's rolled back, so no data is destroyed
6 # MySQL WARNING: This makes sense only if your tables are InnoDB, otherwise
7 # transactions are not supported and mess is left behind
9 use Modern::Perl;
11 use C4::Context;
13 use Data::Dumper;
15 use Test::More tests => 22;
18 use C4::Branch;
19 use C4::ItemType;
20 use C4::Members;
22 BEGIN {
23 use FindBin;
24 use lib $FindBin::Bin;
25 use_ok('C4::Reserves');
26 use_ok('C4::HoldsQueue');
29 # Start transaction
30 my $dbh = C4::Context->dbh;
31 $dbh->{AutoCommit} = 0;
32 $dbh->{RaiseError} = 1;
34 my $TITLE = "Test Holds Queue XXX";
36 my %data = (
37 cardnumber => 'CARDNUMBER42',
38 firstname => 'my firstname',
39 surname => 'my surname',
40 categorycode => 'S',
41 branchcode => 'CPL',
44 my $borrowernumber = AddMember(%data);
45 my $borrower = GetMember( borrowernumber => $borrowernumber );
46 # Set special (for this test) branches
47 my $borrower_branchcode = $borrower->{branchcode};
48 my $branches = C4::Branch::GetBranches;
49 my @other_branches = grep { $_ ne $borrower_branchcode } keys %$branches;
50 my $least_cost_branch_code = pop @other_branches
51 or BAIL_OUT("No point testing only one branch...");
52 my @item_types = C4::ItemType->all;
53 my $itemtype = grep { $_->{notforloan} == 1 } @item_types
54 or BAIL_OUT("No adequate itemtype");
56 #Set up the stage
57 # Sysprefs and cost matrix
58 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
59 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
60 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
62 $dbh->do("DELETE FROM transport_cost");
63 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
64 # Favour $least_cost_branch_code
65 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
66 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
67 my @b = @other_branches;
68 while ( my $b1 = shift @b ) {
69 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
70 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
71 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
76 # Loanable items - all possible combinations of homebranch and holdingbranch
77 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
78 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
79 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
80 or BAIL_OUT("Cannot find newly created biblio record");
81 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
82 VALUES ($biblionumber, '', '$itemtype')");
83 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
84 or BAIL_OUT("Cannot find newly created biblioitems record");
86 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
87 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
88 my $first_barcode = int(rand(1000000000000)); # XXX
89 my $barcode = $first_barcode;
90 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
91 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
92 $items_insert_sth->execute($barcode++, $_, $_);
93 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
96 # Remove existing reserves, makes debugging easier
97 $dbh->do("DELETE FROM reserves");
98 my $bibitems = undef;
99 my $priority = 1;
100 # Make a reserve
101 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
102 # $resdate, $expdate, $notes, $title, $checkitem, $found
103 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
105 # Tests
106 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
107 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
108 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
109 JOIN items USING (itemnumber)
110 WHERE borrowernumber = $borrowernumber");
112 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
113 C4::Context->set_preference('AutomaticItemReturn', 0);
114 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
115 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
117 $dbh->do("DELETE FROM tmp_holdsqueue");
118 $dbh->do("DELETE FROM hold_fill_targets");
119 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
120 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
121 # test_queue will flush
122 C4::Context->set_preference('AutomaticItemReturn', 1);
123 # Not sure how to make this test more difficult - holding branch does not matter
125 $dbh->do("DELETE FROM tmp_holdsqueue");
126 $dbh->do("DELETE FROM hold_fill_targets");
127 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
128 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
129 C4::Context->set_preference('AutomaticItemReturn', 0);
130 # We have a book available held in borrower branch
131 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
132 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
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 holdingbranch = '$borrower_branchcode')");
137 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
138 # No book available in borrower branch, pick according to the rules
139 # Frst branch from StaticHoldsQueueWeight
140 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
141 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
142 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
143 my $queue_item = $queue->[0];
144 ok( $queue_item
145 && $queue_item->{pickbranch} eq $borrower_branchcode
146 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
147 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
148 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
151 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
152 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
155 # XXX All this tests are for borrower branch pick-up.
156 # Maybe needs expanding to homebranch or holdingbranch pick-up.
158 # Cleanup
159 $dbh->rollback;
161 ### Test holds queue builder does not violate holds policy ###
163 # Clear out existing rules relating to holdallowed
164 $dbh->do("DELETE FROM default_branch_circ_rules");
165 $dbh->do("DELETE FROM default_branch_item_rules");
166 $dbh->do("DELETE FROM default_circ_rules");
168 C4::Context->set_preference('UseTransportCostMatrix', 0);
170 my @branchcodes = keys %$branches;
171 ( $itemtype ) = @{ $dbh->selectrow_arrayref("SELECT itemtype FROM itemtypes LIMIT 1") };
173 my $borrowernumber1 = AddMember(
175 cardnumber => 'CARDNUMBER1',
176 firstname => 'Firstname',
177 surname => 'Surname',
178 categorycode => 'S',
179 branchcode => $branchcodes[0],
182 my $borrowernumber2 = AddMember(
184 cardnumber => 'CARDNUMBER2',
185 firstname => 'Firstname',
186 surname => 'Surname',
187 categorycode => 'S',
188 branchcode => $branchcodes[1],
191 my $borrowernumber3 = AddMember(
193 cardnumber => 'CARDNUMBER3',
194 firstname => 'Firstname',
195 surname => 'Surname',
196 categorycode => 'S',
197 branchcode => $branchcodes[2],
200 my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
201 my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
202 my $borrower3 = GetMember( borrowernumber => $borrowernumber3 );
204 $dbh->do(qq{
205 INSERT INTO biblio (
206 frameworkcode,
207 author,
208 title,
209 datecreated
210 ) VALUES (
211 'SER',
212 'Koha test',
213 '$TITLE',
214 '2011-02-01'
217 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
218 or BAIL_OUT("Cannot find newly created biblio record");
220 $dbh->do(qq{
221 INSERT INTO biblioitems (
222 biblionumber,
223 marcxml,
224 itemtype
225 ) VALUES (
226 $biblionumber,
227 '',
228 '$itemtype'
231 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
232 or BAIL_OUT("Cannot find newly created biblioitems record");
234 $items_insert_sth = $dbh->prepare(qq{
235 INSERT INTO items (
236 biblionumber,
237 biblioitemnumber,
238 barcode,
239 homebranch,
240 holdingbranch,
241 notforloan,
242 damaged,
243 itemlost,
244 withdrawn,
245 onloan,
246 itype
247 ) VALUES (
248 $biblionumber,
249 $biblioitemnumber,
257 NULL,
258 '$itemtype'
261 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
262 $barcode = int( rand(1000000000000) );
263 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
264 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
265 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
267 $dbh->do("DELETE FROM reserves");
268 my $sth = $dbh->prepare(q{
269 INSERT INTO reserves (
270 borrowernumber,
271 biblionumber,
272 branchcode,
273 priority,
274 reservedate
275 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
277 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
278 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
279 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
280 #warn "RESERVES" . Data::Dumper::Dumper( $dbh->selectall_arrayref("SELECT * FROM reserves", { Slice => {} }) );
281 #warn "ITEMS: " . Data::Dumper::Dumper( $dbh->selectall_arrayref("SELECT * FROM items WHERE biblionumber = $biblionumber", { Slice => {} }) );
283 my $holds_queue;
285 $dbh->do("DELETE FROM default_circ_rules");
286 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
287 C4::HoldsQueue::CreateQueue();
288 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
289 ok( @$holds_queue == 2, "Holds queue filling correct number for default holds policy 'from home library'" );
290 ok( $holds_queue->[0]->{cardnumber} eq 'CARDNUMBER1', "Holds queue filling 1st correct hold for default holds policy 'from home library'");
291 ok( $holds_queue->[1]->{cardnumber} eq 'CARDNUMBER2', "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
293 $dbh->do("DELETE FROM default_circ_rules");
294 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
295 C4::HoldsQueue::CreateQueue();
296 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
297 ok( @$holds_queue == 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
298 #warn "HOLDS QUEUE: " . Data::Dumper::Dumper( $holds_queue );
300 # Bug 14297
301 $borrowernumber = AddMember(%data);
302 $borrower = GetMember( borrowernumber => $borrowernumber );
303 $borrower_branchcode = $borrower->{branchcode};
304 $itemtype = $item_types[0]->{itemtype};
305 my $library_A = 'CPL';
306 my $library_B = 'FFL';
307 my $library_C = 'MPL'; # Same as our borrower's home library
308 $dbh->do("DELETE FROM reserves");
309 $dbh->do("DELETE FROM issues");
310 $dbh->do("DELETE FROM items");
311 $dbh->do("DELETE FROM biblio");
312 $dbh->do("DELETE FROM biblioitems");
313 $dbh->do("DELETE FROM transport_cost");
314 $dbh->do("DELETE FROM tmp_holdsqueue");
315 $dbh->do("DELETE FROM hold_fill_targets");
316 $dbh->do("DELETE FROM default_branch_circ_rules");
317 $dbh->do("DELETE FROM default_branch_item_rules");
318 $dbh->do("DELETE FROM default_circ_rules");
319 $dbh->do("DELETE FROM branch_item_rules");
321 $dbh->do("
322 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
325 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
326 or BAIL_OUT("Cannot find newly created biblio record");
328 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
330 $biblioitemnumber =
331 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
332 or BAIL_OUT("Cannot find newly created biblioitems record");
334 $dbh->do("
335 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
336 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
339 $dbh->do("
340 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
341 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
344 $dbh->do("
345 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
346 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
349 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
350 undef, join( ',', $library_B, $library_A, $library_C ) );
351 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
353 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
354 C4::HoldsQueue::CreateQueue();
355 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
356 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from least cost branch" );
357 # End Bug 14297
359 # Cleanup
360 $dbh->rollback;
362 ### END Test holds queue builder does not violate holds policy ###
364 sub test_queue {
365 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
367 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
369 $use_cost_matrix_sth->execute($use_cost_matrix);
370 C4::Context->clear_syspref_cache();
371 C4::HoldsQueue::CreateQueue();
373 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
374 my $r = $results->[0];
376 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
377 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
378 if $hold_branch;
380 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
381 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
382 unless $ok;
385 sub dump_records {
386 my ($tablename) = @_;
387 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);