Bug 18936: Convert issuingrules fields to circulation_rules
[koha.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
blob72012ca84e4e477871e8fd9b42765c393d146366
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 => 6;
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 => '*',
88 itemtype => $itemtype,
89 branchcode => '*',
90 rules => {
91 issuelength => 7,
92 lengthunit => 8,
93 reservesallowed => 99,
94 onshelfholds => 2,
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 {
116 plan tests => 2;
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 {
122 plan tests => 7;
124 $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
125 #Scenario is:
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 {
167 plan tests => 4;
169 $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
170 #Scenario is:
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 }
208 })->{itemtype};
209 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
211 my $hold = $builder->build({
212 source => 'Reserve',
213 value =>{
214 itemnumber => $item3->itemnumber,
215 found => 'T'
219 Koha::IssuingRule->new(
221 categorycode => '*',
222 itemtype => $itemtype2,
223 branchcode => '*',
224 issuelength => 7,
225 lengthunit => 8,
226 reservesallowed => 99,
227 onshelfholds => 0,
229 )->store();
231 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
232 is( $is, 1, "Item can be held, items in transit are not available" );
234 # Cleanup
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,
243 itemtype => undef,
244 rules => {
245 holdallowed => $holdallowed,
246 hold_fulfillment_policy => 'any',
247 returnbranch => 'homebranch',