daf527d62235d1e45232586c94a4e9b4dcd08d51
[srvgit] / t / db_dependent / Holds / HoldFulfillmentPolicy.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use Koha::CirculationRules;
7
8 use Test::More tests => 11;
9
10 use t::lib::TestBuilder;
11 use t::lib::Mocks;
12 use Koha::Holds;
13
14 BEGIN {
15     use_ok('C4::Reserves');
16 }
17
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $dbh = C4::Context->dbh;
21
22 my $builder = t::lib::TestBuilder->new;
23
24 my $library1 = $builder->build({
25     source => 'Branch',
26 });
27 my $library2 = $builder->build({
28     source => 'Branch',
29 });
30 my $library3 = $builder->build({
31     source => 'Branch',
32 });
33 my $itemtype = $builder->build_sample_item->itype;
34
35 my $bib_title = "Test Title";
36
37 my $borrower = $builder->build({
38     source => 'Borrower',
39     value => {
40         branchcode => $library1->{branchcode},
41     }
42 });
43
44 # Test hold_fulfillment_policy
45 my $borrowernumber = $borrower->{borrowernumber};
46 my $library_A = $library1->{branchcode};
47 my $library_B = $library2->{branchcode};
48 my $library_C = $library3->{branchcode};
49 $dbh->do("DELETE FROM transport_cost");
50 $dbh->do("DELETE FROM tmp_holdsqueue");
51 $dbh->do("DELETE FROM hold_fill_targets");
52 $dbh->do("DELETE FROM circulation_rules");
53
54 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
55
56 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
57   or BAIL_OUT("Cannot find newly created biblio record");
58
59 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
60
61 my $biblioitemnumber =
62   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
63   or BAIL_OUT("Cannot find newly created biblioitems record");
64
65 $dbh->do("
66     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
67     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
68 ");
69
70 my $itemnumber =
71   $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
72   or BAIL_OUT("Cannot find newly created item");
73
74 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
75 $dbh->do("DELETE FROM circulation_rules");
76 Koha::CirculationRules->set_rules(
77     {
78         branchcode   => undef,
79         itemtype     => undef,
80         rules        => {
81             holdallowed             => 'from_any_library',
82             hold_fulfillment_policy => 'homebranch',
83         }
84     }
85 );
86
87 # Home branch matches pickup branch
88 my $reserve_id = AddReserve(
89     {
90         branchcode     => $library_A,
91         borrowernumber => $borrowernumber,
92         biblionumber   => $biblionumber,
93         priority       => 1,
94     }
95 );
96 my ( $status ) = CheckReserves($itemnumber);
97 is( $status, 'Reserved', "Hold where pickup branch matches home branch targeted" );
98 Koha::Holds->find( $reserve_id )->cancel;
99
100 # Holding branch matches pickup branch
101 $reserve_id = AddReserve(
102     {
103         branchcode     => $library_B,
104         borrowernumber => $borrowernumber,
105         biblionumber   => $biblionumber,
106         priority       => 1,
107     }
108 );
109 ( $status ) = CheckReserves($itemnumber);
110 is($status, q{}, "Hold where pickup ne home, pickup eq home not targeted" );
111 Koha::Holds->find( $reserve_id )->cancel;
112
113 # Neither branch matches pickup branch
114 $reserve_id = AddReserve(
115     {
116         branchcode     => $library_C,
117         borrowernumber => $borrowernumber,
118         biblionumber   => $biblionumber,
119         priority       => 1,
120     }
121 );
122 ( $status ) = CheckReserves($itemnumber);
123 is( $status, q{}, "Hold where pickup ne home, pickup ne holding not targeted" );
124 Koha::Holds->find( $reserve_id )->cancel;
125
126 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
127 $dbh->do("DELETE FROM circulation_rules");
128 Koha::CirculationRules->set_rules(
129     {
130         branchcode   => undef,
131         itemtype     => undef,
132         rules        => {
133             holdallowed             => 'from_any_library',
134             hold_fulfillment_policy => 'holdingbranch',
135         }
136     }
137 );
138
139 # Home branch matches pickup branch
140 $reserve_id = AddReserve(
141     {
142         branchcode     => $library_A,
143         borrowernumber => $borrowernumber,
144         biblionumber   => $biblionumber,
145         priority       => 1,
146     }
147 );
148 ( $status ) = CheckReserves($itemnumber);
149 is( $status, q{}, "Hold where pickup eq home, pickup ne holding not targeted" );
150 Koha::Holds->find( $reserve_id )->cancel;
151
152 # Holding branch matches pickup branch
153 $reserve_id = AddReserve(
154     {
155         branchcode     => $library_B,
156         borrowernumber => $borrowernumber,
157         biblionumber   => $biblionumber,
158         priority       => 1,
159     }
160 );
161 ( $status ) = CheckReserves($itemnumber);
162 is( $status, 'Reserved', "Hold where pickup ne home, pickup eq holding targeted" );
163 Koha::Holds->find( $reserve_id )->cancel;
164
165 # Neither branch matches pickup branch
166 $reserve_id = AddReserve(
167     {
168         branchcode     => $library_C,
169         borrowernumber => $borrowernumber,
170         biblionumber   => $biblionumber,
171         priority       => 1,
172     }
173 );
174 ( $status ) = CheckReserves($itemnumber);
175 is( $status, q{}, "Hold where pickup ne home, pickup ne holding not targeted" );
176 Koha::Holds->find( $reserve_id )->cancel;
177
178 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
179 $dbh->do("DELETE FROM circulation_rules");
180 Koha::CirculationRules->set_rules(
181     {
182         branchcode   => undef,
183         itemtype     => undef,
184         rules        => {
185             holdallowed             => 'from_any_library',
186             hold_fulfillment_policy => 'any',
187         }
188     }
189 );
190
191 # Home branch matches pickup branch
192 $reserve_id = AddReserve(
193     {
194         branchcode     => $library_A,
195         borrowernumber => $borrowernumber,
196         biblionumber   => $biblionumber,
197         priority       => 1,
198     }
199 );
200 ( $status ) = CheckReserves($itemnumber);
201 is( $status, 'Reserved', "Hold where pickup eq home, pickup ne holding targeted" );
202 Koha::Holds->find( $reserve_id )->cancel;
203
204 # Holding branch matches pickup branch
205 $reserve_id = AddReserve(
206     {
207         branchcode     => $library_B,
208         borrowernumber => $borrowernumber,
209         biblionumber   => $biblionumber,
210         priority       => 1,
211     }
212 );
213 ( $status ) = CheckReserves($itemnumber);
214 is( $status, 'Reserved', "Hold where pickup ne home, pickup eq holding targeted" );
215 Koha::Holds->find( $reserve_id )->cancel;
216
217 # Neither branch matches pickup branch
218 $reserve_id = AddReserve(
219     {
220         branchcode     => $library_C,
221         borrowernumber => $borrowernumber,
222         biblionumber   => $biblionumber,
223         priority       => 1,
224     }
225 );
226 ( $status ) = CheckReserves($itemnumber);
227 is( $status, 'Reserved', "Hold where pickup ne home, pickup ne holding targeted" );
228 Koha::Holds->find( $reserve_id )->cancel;
229
230 # Test enforement of branch transfer limits
231 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits',  '1' );
232 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
233 Koha::Holds->search()->delete();
234 my ($item) = Koha::Biblios->find($biblionumber)->items->as_list;
235 my $limit = Koha::Item::Transfer::Limit->new(
236     {
237         toBranch   => $library_C,
238         fromBranch => $item->holdingbranch,
239         itemtype   => $item->effective_itemtype,
240     }
241 )->store();
242 $reserve_id = AddReserve(
243     {
244         branchcode     => $library_C,
245         borrowernumber => $borrowernumber,
246         biblionumber   => $biblionumber,
247         priority       => 1
248     }
249 );
250 ($status) = CheckReserves($itemnumber);
251 is( $status, '',  "No hold where branch transfer is not allowed" );
252 Koha::Holds->find($reserve_id)->cancel;