9 use Koha
::CirculationRules
;
11 use Test
::More tests
=> 6;
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(
88 itemtype
=> $itemtype,
93 reservesallowed
=> 99,
99 my $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
100 is
( $is, 0, "Item cannot be held, 2 items available" );
102 my $issue1 = AddIssue
( $patron2->unblessed, $item1->barcode );
104 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
105 is
( $is, 0, "Item cannot be held, 1 item available" );
107 AddIssue
( $patron2->unblessed, $item2->barcode );
109 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
110 is
( $is, 1, "Item can be held, no items available" );
112 AddReturn
( $item1->barcode );
114 { # Remove the issue for the first patron, and modify the branch for item1
115 subtest
'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
118 my $hold_allowed_from_home_library = 1;
119 my $hold_allowed_from_any_libraries = 2;
121 subtest
'Item is available at a different library' => sub {
124 $item1->set({homebranch
=> $library_B, holdingbranch
=> $library_B })->store;
126 #One shelf holds is 'If all unavailable'/2
127 #Item 1 homebranch library B is available
128 #Item 2 homebranch library A is checked out
129 #Borrower1 is from library A
132 set_holdallowed_rule
( $hold_allowed_from_home_library );
134 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'ItemHomeLibrary');
135 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
136 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" );
137 $is = IsAvailableForItemLevelRequest
( $item1, $patron2);
138 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" );
139 set_holdallowed_rule
( $hold_allowed_from_any_libraries, $library_B );
140 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
141 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
142 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" );
144 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'PatronLibrary');
145 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
146 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" );
147 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
148 set_holdallowed_rule
( $hold_allowed_from_any_libraries, $library_A );
149 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
150 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" );
154 set_holdallowed_rule
( $hold_allowed_from_any_libraries );
156 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'ItemHomeLibrary');
157 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
158 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" );
160 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'PatronLibrary');
161 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
162 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" );
166 subtest
'Item is available at the same library' => sub {
169 $item1->set({homebranch
=> $library_A, holdingbranch
=> $library_A })->store;
171 #One shelf holds is 'If all unavailable'/2
172 #Item 1 homebranch library A is available
173 #Item 2 homebranch library A is checked out
174 #Borrower1 is from library A
175 #CircControl has no effect - same rule for all branches as set at line 96
176 #ReservesControlBranch is not checked in these subs we are testing?
179 set_holdallowed_rule
( $hold_allowed_from_home_library );
181 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'ItemHomeLibrary');
182 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
183 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" );
185 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'PatronLibrary');
186 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
187 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" );
191 set_holdallowed_rule
( $hold_allowed_from_any_libraries );
193 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'ItemHomeLibrary');
194 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
195 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" );
197 t
::lib
::Mocks
::mock_preference
('ReservesControlBranch', 'PatronLibrary');
198 $is = IsAvailableForItemLevelRequest
( $item1, $patron1);
199 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" );
205 my $itemtype2 = $builder->build({
206 source
=> 'Itemtype',
207 value
=> { notforloan
=> 0 }
209 my $item3 = $builder->build_sample_item({ itype
=> $itemtype2 });
211 my $hold = $builder->build({
214 itemnumber
=> $item3->itemnumber,
219 Koha
::IssuingRule
->new(
222 itemtype
=> $itemtype2,
226 reservesallowed
=> 99,
231 $is = IsAvailableForItemLevelRequest
( $item3, $patron1);
232 is
( $is, 1, "Item can be held, items in transit are not available" );
235 $schema->storage->txn_rollback;
237 sub set_holdallowed_rule
{
238 my ( $holdallowed, $branchcode ) = @_;
239 Koha
::CirculationRules
->set_rules(
241 branchcode
=> $branchcode || undef,
242 categorycode
=> undef,
245 holdallowed
=> $holdallowed,
246 hold_fulfillment_policy
=> 'any',
247 returnbranch
=> 'homebranch',