9 use Koha
::CirculationRules
;
11 use Test
::More tests
=> 11;
13 use t
::lib
::TestBuilder
;
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({
29 my $library2 = $builder->build({
32 my $itemtype = $builder->build({
34 value
=> { notforloan
=> 0 }
37 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library1->{branchcode
} });
40 my $patron1 = $builder->build_object({
41 class => 'Koha::Patrons',
43 branchcode
=> $library1->{branchcode
},
44 dateexpiry
=> '3000-01-01',
47 my $borrower1 = $patron1->unblessed;
49 my $patron2 = $builder->build_object({
50 class => 'Koha::Patrons',
52 branchcode
=> $library1->{branchcode
},
53 dateexpiry
=> '3000-01-01',
57 my $patron3 = $builder->build_object({
58 class => 'Koha::Patrons',
60 branchcode
=> $library2->{branchcode
},
61 dateexpiry
=> '3000-01-01',
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,
73 homebranch
=> $library_A,
74 holdingbranch
=> $library_A
76 my $item2 = $builder->build_sample_item({
77 biblionumber
=>$biblionumber,
79 homebranch
=> $library_A,
80 holdingbranch
=> $library_A
83 # Test hold_fulfillment_policy
84 $dbh->do("DELETE FROM circulation_rules");
85 Koha
::CirculationRules
->set_rules(
87 categorycode
=> undef,
88 itemtype
=> $itemtype,
93 reservesallowed
=> 99,
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 {
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 {
135 $item1->set({homebranch
=> $library_B, holdingbranch
=> $library_B })->store;
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 {
204 $item1->set({homebranch
=> $library_A, holdingbranch
=> $library_A })->store;
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 }
260 my $item3 = $builder->build_sample_item({ itype
=> $itemtype2 });
262 my $hold = $builder->build({
265 itemnumber
=> $item3->itemnumber,
270 Koha
::CirculationRules
->set_rules(
272 categorycode
=> undef,
273 itemtype
=> $itemtype2,
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 {
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
298 my $biblio2 = $builder->build_sample_biblio( { itemtype
=> $itemtype } );
299 my $biblionumber1 = $biblio2->biblionumber;
300 my $item4 = $builder->build_sample_item(
301 { biblionumber
=> $biblionumber1,
303 homebranch
=> $library_A,
304 holdingbranch
=> $library_A
307 my $item5 = $builder->build_sample_item(
308 { biblionumber
=> $biblionumber1,
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,
324 reservesallowed
=> 99,
325 holds_per_record
=> 99,
330 Koha
::CirculationRules
->set_rules(
331 { categorycode
=> undef,
332 itemtype
=> $itemtype2,
337 reservesallowed
=> 0,
338 holds_per_record
=> 0,
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" );
368 $schema->storage->txn_rollback;
370 sub set_holdallowed_rule
{
371 my ( $holdallowed, $branchcode ) = @_;
372 Koha
::CirculationRules
->set_rules(
374 branchcode
=> $branchcode || undef,
377 holdallowed
=> $holdallowed,
378 hold_fulfillment_policy
=> 'any',
379 returnbranch
=> 'homebranch',