755721d824d0620a56b5274a2cda061cd793aefe
[koha-equinox.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use C4::Circulation;
7 use C4::Items;
8 use Koha::IssuingRule;
9 use Koha::Items;
10 use Test::More tests => 6;
11
12 use t::lib::TestBuilder;
13 use t::lib::Mocks;
14
15 BEGIN {
16     use_ok('C4::Reserves');
17 }
18
19 my $schema = Koha::Database->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
22
23 my $builder = t::lib::TestBuilder->new;
24
25 my $library1 = $builder->build({
26     source => 'Branch',
27 });
28 my $library2 = $builder->build({
29     source => 'Branch',
30 });
31 my $itemtype = $builder->build({
32     source => 'Itemtype',
33     value  => { notforloan => 0 }
34 })->{itemtype};
35
36 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
37
38
39 my $patron1 = $builder->build_object({
40     class => 'Koha::Patrons',
41     value => {
42         branchcode => $library1->{branchcode},
43         dateexpiry => '3000-01-01',
44     }
45 });
46 my $borrower1 = $patron1->unblessed;
47
48 my $patron2 = $builder->build_object({
49     class => 'Koha::Patrons',
50     value => {
51         branchcode => $library1->{branchcode},
52         dateexpiry => '3000-01-01',
53     }
54 });
55
56 my $patron3 = $builder->build_object({
57     class => 'Koha::Patrons',
58     value => {
59         branchcode => $library2->{branchcode},
60         dateexpiry => '3000-01-01',
61     }
62 });
63
64 my $library_A = $library1->{branchcode};
65 my $library_B = $library2->{branchcode};
66
67 my $biblio = $builder->build_sample_biblio({itemtype=>$itemtype});
68 my $biblionumber = $biblio->biblionumber;
69 my $item1  = $builder->build_sample_item({
70     biblionumber=>$biblionumber,
71     itype=>$itemtype,
72     homebranch => $library_A,
73     holdingbranch => $library_A
74 });
75 my $item2  = $builder->build_sample_item({
76     biblionumber=>$biblionumber,
77     itype=>$itemtype,
78     homebranch => $library_A,
79     holdingbranch => $library_A
80 });
81
82 # Test hold_fulfillment_policy
83 my $rule = Koha::IssuingRule->new(
84     {
85         categorycode => '*',
86         itemtype     => $itemtype,
87         branchcode   => '*',
88         issuelength  => 7,
89         lengthunit   => 8,
90         reservesallowed => 99,
91         onshelfholds => 2,
92     }
93 );
94 $rule->store();
95
96 my $is = IsAvailableForItemLevelRequest( $item1, $patron1);
97 is( $is, 0, "Item cannot be held, 2 items available" );
98
99 my $issue1 = AddIssue( $patron2->unblessed, $item1->barcode );
100
101 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
102 is( $is, 0, "Item cannot be held, 1 item available" );
103
104 AddIssue( $patron2->unblessed, $item2->barcode );
105
106 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
107 is( $is, 1, "Item can be held, no items available" );
108
109 AddReturn( $item1->barcode );
110
111 { # Remove the issue for the first patron, and modify the branch for item1
112     subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
113         plan tests => 2;
114
115         my $hold_allowed_from_home_library = 1;
116         my $hold_allowed_from_any_libraries = 2;
117
118         subtest 'Item is available at a different library' => sub {
119             plan tests => 7;
120
121             $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
122             #Scenario is:
123             #One shelf holds is 'If all unavailable'/2
124             #Item 1 homebranch library B is available
125             #Item 2 homebranch library A is checked out
126             #Borrower1 is from library A
127
128             {
129                 set_holdallowed_rule( $hold_allowed_from_home_library );
130
131                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
132                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
133                 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" );
134                 $is = IsAvailableForItemLevelRequest( $item1, $patron2);
135                 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" );
136                 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_B );
137                 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
138                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
139                 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" );
140
141                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
142                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
143                 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" );
144                 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
145                 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_A );
146                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
147                 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" );
148             }
149
150             {
151                 set_holdallowed_rule( $hold_allowed_from_any_libraries );
152
153                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
154                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
155                 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" );
156
157                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
158                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
159                 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" );
160             }
161         };
162
163         subtest 'Item is available at the same library' => sub {
164             plan tests => 4;
165
166             $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
167             #Scenario is:
168             #One shelf holds is 'If all unavailable'/2
169             #Item 1 homebranch library A is available
170             #Item 2 homebranch library A is checked out
171             #Borrower1 is from library A
172             #CircControl has no effect - same rule for all branches as set at line 96
173             #ReservesControlBranch is not checked in these subs we are testing?
174
175             {
176                 set_holdallowed_rule( $hold_allowed_from_home_library );
177
178                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
179                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
180                 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" );
181
182                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
183                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
184                 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" );
185             }
186
187             {
188                 set_holdallowed_rule( $hold_allowed_from_any_libraries );
189
190                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
191                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
192                 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" );
193
194                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
195                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
196                 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" );
197             }
198         };
199     };
200 }
201
202 my $itemtype2 = $builder->build({
203     source => 'Itemtype',
204     value  => { notforloan => 0 }
205 })->{itemtype};
206 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
207
208 my $hold = $builder->build({
209     source => 'Reserve',
210     value =>{
211         itemnumber => $item3->itemnumber,
212         found => 'T'
213     }
214 });
215
216 $rule = Koha::IssuingRule->new(
217     {
218         categorycode => '*',
219         itemtype     => $itemtype2,
220         branchcode   => '*',
221         issuelength  => 7,
222         lengthunit   => 8,
223         reservesallowed => 99,
224         onshelfholds => 0,
225     }
226 );
227 $rule->store();
228
229 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
230 is( $is, 1, "Item can be held, items in transit are not available" );
231
232 # Cleanup
233 $schema->storage->txn_rollback;
234
235 sub set_holdallowed_rule {
236     my ( $holdallowed, $branchcode ) = @_;
237     Koha::CirculationRules->set_rules(
238         {
239             branchcode   => $branchcode || undef,
240             categorycode => undef,
241             itemtype     => undef,
242             rules        => {
243                 holdallowed              => $holdallowed,
244                 hold_fulfillment_policy  => 'any',
245                 returnbranch             => 'homebranch',
246             }
247         }
248     );
249 }