bug 5825: (follow-up) add regression 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 C4::Context;
12
13 use Data::Dumper;
14
15 use Test::More tests => 19;
16
17 use C4::Branch;
18 use C4::ItemType;
19 use C4::Members;
20
21 BEGIN {
22     use FindBin;
23     use lib $FindBin::Bin;
24     use_ok('C4::Reserves');
25     use_ok('C4::HoldsQueue');
26 }
27
28 # Start transaction
29 my $dbh = C4::Context->dbh;
30 $dbh->{AutoCommit} = 0;
31 $dbh->{RaiseError} = 1;
32
33 my $TITLE = "Test Holds Queue XXX";
34
35 my %data = (
36     cardnumber => 'CARDNUMBER42',
37     firstname =>  'my firstname',
38     surname => 'my surname',
39     categorycode => 'S',
40     branchcode => 'CPL',
41 );
42
43 my $borrowernumber = AddMember(%data);
44 my $borrower = GetMember( borrowernumber => $borrowernumber );
45 # Set special (for this test) branches
46 my $borrower_branchcode = $borrower->{branchcode};
47 my $branches = C4::Branch::GetBranches;
48 my @other_branches = grep { $_ ne $borrower_branchcode } keys %$branches;
49 my $least_cost_branch_code = pop @other_branches
50   or BAIL_OUT("No point testing only one branch...");
51 my @item_types = C4::ItemType->all;
52 my $itemtype = grep { $_->{notforloan} == 1 } @item_types
53   or BAIL_OUT("No adequate itemtype");
54
55 #Set up the stage
56 # Sysprefs and cost matrix
57 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
58          join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
59 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
60
61 $dbh->do("DELETE FROM transport_cost");
62 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
63 # Favour $least_cost_branch_code
64 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
65 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
66 my @b = @other_branches;
67 while ( my $b1 = shift @b ) {
68     foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
69         $transport_cost_insert_sth->execute($b1, $b2, 0.5);
70         $transport_cost_insert_sth->execute($b2, $b1, 0.5);
71     }
72 }
73
74
75 # Loanable items - all possible combinations of homebranch and holdingbranch
76 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
77           VALUES             ('SER', 'Koha test', '$TITLE', '2011-02-01')");
78 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
79   or BAIL_OUT("Cannot find newly created biblio record");
80 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
81           VALUES                  ($biblionumber, '', '$itemtype')");
82 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
83   or BAIL_OUT("Cannot find newly created biblioitems record");
84
85 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, wthdrawn, onloan, itype)
86                                       VALUES            ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
87 my $first_barcode = int(rand(1000000000000)); # XXX
88 my $barcode = $first_barcode;
89 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
90     $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
91     $items_insert_sth->execute($barcode++, $_, $_);
92     $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
93 }
94
95 # Remove existing reserves, makes debugging easier
96 $dbh->do("DELETE FROM reserves");
97 my $constraint = undef;
98 my $bibitems = undef;
99 my $priority = 1;
100 # Make a reserve
101 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $constraint, $bibitems,  $priority );
102 #                           $resdate, $expdate, $notes, $title, $checkitem, $found
103 $dbh->do("UPDATE reserves SET reservedate = reservedate - 1");
104
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");
111
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);
116
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
124 test_queue ('take from holdingbranch AutomaticItemReturn on', 0, $borrower_branchcode, undef);
125 test_queue ('take from holdingbranch AutomaticItemReturn on', 1, $borrower_branchcode, $least_cost_branch_code);
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')");
130 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
131 C4::Context->set_preference('AutomaticItemReturn', 0);
132 # We have a book available held in borrower branch
133 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
134 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
135
136 $dbh->do("DELETE FROM tmp_holdsqueue");
137 $dbh->do("DELETE FROM hold_fill_targets");
138 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
139 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
140 # No book available in borrower branch, pick according to the rules
141 # Frst branch from StaticHoldsQueueWeight
142 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
143 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
144 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
145 my $queue_item = $queue->[0];
146 ok( $queue_item
147  && $queue_item->{pickbranch} eq $borrower_branchcode
148  && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
149   or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
150 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
151
152 # XXX All this tests are for borrower branch pick-up.
153 # Maybe needs expanding to homebranch or holdingbranch pick-up.
154
155 # Cleanup
156 $dbh->rollback;
157
158 sub test_queue {
159     my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
160
161     $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
162
163     $use_cost_matrix_sth->execute($use_cost_matrix);
164     C4::Context->clear_syspref_cache();
165     C4::HoldsQueue::CreateQueue();
166
167     my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
168     my $r = $results->[0];
169
170     my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
171     $ok &&=  is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
172       if $hold_branch;
173
174     diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
175         . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
176       unless $ok;
177 }
178
179 sub dump_records {
180     my ($tablename) = @_;
181     return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
182 }