Bug 24683: Fix for take smart rules into account in "if all unavailable"
[koha.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
blobc10d2c5b62a0f8a56c74817f7093eea8dfa28763
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use C4::Context;
6 use C4::Circulation;
7 use C4::Items;
8 use Koha::Items;
9 use Koha::CirculationRules;
11 use Test::More tests => 11;
13 use t::lib::TestBuilder;
14 use t::lib::Mocks;
16 BEGIN {
17 use_ok('C4::Reserves');
20 my $schema = Koha::Database->schema;
21 $schema->storage->txn_begin;
22 my $dbh = C4::Context->dbh;
24 my $builder = t::lib::TestBuilder->new;
26 my $library1 = $builder->build({
27 source => 'Branch',
28 });
29 my $library2 = $builder->build({
30 source => 'Branch',
31 });
32 my $itemtype = $builder->build({
33 source => 'Itemtype',
34 value => { notforloan => 0 }
35 })->{itemtype};
37 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
40 my $patron1 = $builder->build_object({
41 class => 'Koha::Patrons',
42 value => {
43 branchcode => $library1->{branchcode},
44 dateexpiry => '3000-01-01',
46 });
47 my $borrower1 = $patron1->unblessed;
49 my $patron2 = $builder->build_object({
50 class => 'Koha::Patrons',
51 value => {
52 branchcode => $library1->{branchcode},
53 dateexpiry => '3000-01-01',
55 });
57 my $patron3 = $builder->build_object({
58 class => 'Koha::Patrons',
59 value => {
60 branchcode => $library2->{branchcode},
61 dateexpiry => '3000-01-01',
63 });
65 my $library_A = $library1->{branchcode};
66 my $library_B = $library2->{branchcode};
68 my $biblio = $builder->build_sample_biblio({itemtype=>$itemtype});
69 my $biblionumber = $biblio->biblionumber;
70 my $item1 = $builder->build_sample_item({
71 biblionumber=>$biblionumber,
72 itype=>$itemtype,
73 homebranch => $library_A,
74 holdingbranch => $library_A
75 });
76 my $item2 = $builder->build_sample_item({
77 biblionumber=>$biblionumber,
78 itype=>$itemtype,
79 homebranch => $library_A,
80 holdingbranch => $library_A
81 });
83 # Test hold_fulfillment_policy
84 $dbh->do("DELETE FROM circulation_rules");
85 Koha::CirculationRules->set_rules(
87 categorycode => undef,
88 itemtype => $itemtype,
89 branchcode => undef,
90 rules => {
91 issuelength => 7,
92 lengthunit => 8,
93 reservesallowed => 99,
94 onshelfholds => 2,
99 my $is;
101 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 });
102 is( $is, 1, "Items availability: both of 2 items are available" );
104 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
105 is( $is, 0, "Item cannot be held, 2 items available" );
107 my $issue1 = AddIssue( $patron2->unblessed, $item1->barcode );
109 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 });
110 is( $is, 1, "Items availability: one item is available" );
112 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
113 is( $is, 0, "Item cannot be held, 1 item available" );
115 AddIssue( $patron2->unblessed, $item2->barcode );
117 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 });
118 is( $is, 0, "Items availability: none of items are available" );
120 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
121 is( $is, 1, "Item can be held, no items available" );
123 AddReturn( $item1->barcode );
125 { # Remove the issue for the first patron, and modify the branch for item1
126 subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
127 plan tests => 2;
129 my $hold_allowed_from_home_library = 1;
130 my $hold_allowed_from_any_libraries = 2;
132 subtest 'Item is available at a different library' => sub {
133 plan tests => 13;
135 $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
136 #Scenario is:
137 #One shelf holds is 'If all unavailable'/2
138 #Item 1 homebranch library B is available
139 #Item 2 homebranch library A is checked out
140 #Borrower1 is from library A
143 set_holdallowed_rule( $hold_allowed_from_home_library );
145 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
147 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
148 is( $is, 0, "Items availability: hold allowed from home + ReservesControlBranch=ItemHomeLibrary + one item is available at different library" );
150 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
151 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at different library, not holdable = none available => the hold is allowed at item level" );
152 $is = IsAvailableForItemLevelRequest( $item1, $patron2);
153 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at home library, holdable = one available => the hold is not allowed at item level" );
154 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_B );
155 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
157 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
158 is( $is, 1, "Items availability: hold allowed from any library for library B + ReservesControlBranch=ItemHomeLibrary + one item is available at different library" );
160 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
161 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at different library, holdable = one available => the hold is not allowed at item level" );
163 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
165 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
166 is( $is, 0, "Items availability: hold allowed from any library for library B + ReservesControlBranch=PatronLibrary + one item is available at different library" );
168 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
169 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at different library, not holdable = none available => the hold is allowed at item level" );
170 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
171 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_A );
173 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
174 is( $is, 1, "Items availability: hold allowed from any library for library A + ReservesControlBranch=PatronLibrary + one item is available at different library" );
176 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
177 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at different library, holdable = one available => the hold is not allowed at item level" );
181 set_holdallowed_rule( $hold_allowed_from_any_libraries );
183 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
185 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
186 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at different library" );
188 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
189 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level" );
191 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
193 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
194 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at different library" );
196 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
197 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=PatronLibrary, One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level" );
201 subtest 'Item is available at the same library' => sub {
202 plan tests => 8;
204 $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
205 #Scenario is:
206 #One shelf holds is 'If all unavailable'/2
207 #Item 1 homebranch library A is available
208 #Item 2 homebranch library A is checked out
209 #Borrower1 is from library A
210 #CircControl has no effect - same rule for all branches as set at line 96
211 #ReservesControlBranch is not checked in these subs we are testing?
214 set_holdallowed_rule( $hold_allowed_from_home_library );
216 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
218 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
219 is( $is, 1, "Items availability: hold allowed from home library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library" );
221 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
222 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
224 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
226 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
227 is( $is, 1, "Items availability: hold allowed from home library + ReservesControlBranch=PatronLibrary + one item is available at home library" );
229 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
230 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
234 set_holdallowed_rule( $hold_allowed_from_any_libraries );
236 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
238 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
239 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library" );
241 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
242 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
244 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
246 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
247 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at home library" );
249 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
250 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=PatronLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
256 my $itemtype2 = $builder->build({
257 source => 'Itemtype',
258 value => { notforloan => 0 }
259 })->{itemtype};
260 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
262 my $hold = $builder->build({
263 source => 'Reserve',
264 value =>{
265 itemnumber => $item3->itemnumber,
266 found => 'T'
270 Koha::CirculationRules->set_rules(
272 categorycode => undef,
273 itemtype => $itemtype2,
274 branchcode => undef,
275 rules => {
276 maxissueqty => 99,
277 onshelfholds => 0,
282 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
283 is( $is, 1, "Items availability: 1 item is available, 1 item held in T" );
285 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
286 is( $is, 1, "Item can be held, items in transit are not available" );
288 subtest 'Check holds availability with different item types' => sub {
289 plan tests => 6;
291 # Check for holds availability when different item types have different
292 # smart rules assigned both with "if all unavailable" set,
293 # and $itemtype rule allows holds, $itemtype2 rule disallows holds.
294 # So, $item should be available for hold when checked out even if $item2
295 # is not checked out, because anyway $item2 unavailable for holds by rule
296 # (Bug 24683):
298 my $biblio2 = $builder->build_sample_biblio( { itemtype => $itemtype } );
299 my $biblionumber1 = $biblio2->biblionumber;
300 my $item4 = $builder->build_sample_item(
301 { biblionumber => $biblionumber1,
302 itype => $itemtype,
303 homebranch => $library_A,
304 holdingbranch => $library_A
307 my $item5 = $builder->build_sample_item(
308 { biblionumber => $biblionumber1,
309 itype => $itemtype2,
310 homebranch => $library_A,
311 holdingbranch => $library_A
315 # Test hold_fulfillment_policy
316 $dbh->do("DELETE FROM circulation_rules");
317 Koha::CirculationRules->set_rules(
318 { categorycode => undef,
319 itemtype => $itemtype,
320 branchcode => undef,
321 rules => {
322 issuelength => 7,
323 lengthunit => 8,
324 reservesallowed => 99,
325 holds_per_record => 99,
326 onshelfholds => 2,
330 Koha::CirculationRules->set_rules(
331 { categorycode => undef,
332 itemtype => $itemtype2,
333 branchcode => undef,
334 rules => {
335 issuelength => 7,
336 lengthunit => 8,
337 reservesallowed => 0,
338 holds_per_record => 0,
339 onshelfholds => 2,
344 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber1, patron => $patron1 } );
345 is( $is, 1, "Items availability: 2 items, one allowed by smart rule but not checked out, another one not allowed to be held by smart rule" );
347 $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
348 is( $is, 0, "Item4 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
350 $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
351 is( $is, 0, "Item5 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
353 AddIssue( $patron2->unblessed, $item4->barcode );
355 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber1, patron => $patron1 } );
356 is( $is, 0, "Items availability: 2 items, one allowed by smart rule and checked out, another one not allowed to be held by smart rule" );
358 $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
359 is( $is, 1, "Item4 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
361 $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
362 # Note: read IsAvailableForItemLevelRequest sub description about CanItemBeReserved/CanBookBeReserved:
363 is( $is, 1, "Item5 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
367 # Cleanup
368 $schema->storage->txn_rollback;
370 sub set_holdallowed_rule {
371 my ( $holdallowed, $branchcode ) = @_;
372 Koha::CirculationRules->set_rules(
374 branchcode => $branchcode || undef,
375 itemtype => undef,
376 rules => {
377 holdallowed => $holdallowed,
378 hold_fulfillment_policy => 'any',
379 returnbranch => 'homebranch',