Bug 18936: (QA follow-up) Fix inconsistencies in IssuingRules.t
[koha.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::Items;
9 use Koha::CirculationRules;
10
11 use Test::More tests => 6;
12
13 use t::lib::TestBuilder;
14 use t::lib::Mocks;
15
16 BEGIN {
17     use_ok('C4::Reserves');
18 }
19
20 my $schema = Koha::Database->schema;
21 $schema->storage->txn_begin;
22 my $dbh = C4::Context->dbh;
23
24 my $builder = t::lib::TestBuilder->new;
25
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};
36
37 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
38
39
40 my $patron1 = $builder->build_object({
41     class => 'Koha::Patrons',
42     value => {
43         branchcode => $library1->{branchcode},
44         dateexpiry => '3000-01-01',
45     }
46 });
47 my $borrower1 = $patron1->unblessed;
48
49 my $patron2 = $builder->build_object({
50     class => 'Koha::Patrons',
51     value => {
52         branchcode => $library1->{branchcode},
53         dateexpiry => '3000-01-01',
54     }
55 });
56
57 my $patron3 = $builder->build_object({
58     class => 'Koha::Patrons',
59     value => {
60         branchcode => $library2->{branchcode},
61         dateexpiry => '3000-01-01',
62     }
63 });
64
65 my $library_A = $library1->{branchcode};
66 my $library_B = $library2->{branchcode};
67
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 });
82
83 # Test hold_fulfillment_policy
84 $dbh->do("DELETE FROM circulation_rules");
85 Koha::CirculationRules->set_rules(
86     {
87         categorycode => undef,
88         itemtype     => $itemtype,
89         branchcode   => undef,
90         rules        => {
91             issuelength     => 7,
92             lengthunit      => 8,
93             reservesallowed => 99,
94             onshelfholds    => 2,
95         }
96     }
97 );
98
99 my $is = IsAvailableForItemLevelRequest( $item1, $patron1);
100 is( $is, 0, "Item cannot be held, 2 items available" );
101
102 my $issue1 = AddIssue( $patron2->unblessed, $item1->barcode );
103
104 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
105 is( $is, 0, "Item cannot be held, 1 item available" );
106
107 AddIssue( $patron2->unblessed, $item2->barcode );
108
109 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
110 is( $is, 1, "Item can be held, no items available" );
111
112 AddReturn( $item1->barcode );
113
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;
117
118         my $hold_allowed_from_home_library = 1;
119         my $hold_allowed_from_any_libraries = 2;
120
121         subtest 'Item is available at a different library' => sub {
122             plan tests => 7;
123
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
130
131             {
132                 set_holdallowed_rule( $hold_allowed_from_home_library );
133
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" );
143
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" );
151             }
152
153             {
154                 set_holdallowed_rule( $hold_allowed_from_any_libraries );
155
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" );
159
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" );
163             }
164         };
165
166         subtest 'Item is available at the same library' => sub {
167             plan tests => 4;
168
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?
177
178             {
179                 set_holdallowed_rule( $hold_allowed_from_home_library );
180
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" );
184
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" );
188             }
189
190             {
191                 set_holdallowed_rule( $hold_allowed_from_any_libraries );
192
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" );
196
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" );
200             }
201         };
202     };
203 }
204
205 my $itemtype2 = $builder->build({
206     source => 'Itemtype',
207     value  => { notforloan => 0 }
208 })->{itemtype};
209 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
210
211 my $hold = $builder->build({
212     source => 'Reserve',
213     value =>{
214         itemnumber => $item3->itemnumber,
215         found => 'T'
216     }
217 });
218
219 Koha::CirculationRules->set_rules(
220     {
221         categorycode => undef,
222         itemtype     => $itemtype2,
223         branchcode   => undef,
224         rules        => {
225             maxissueqty     => 99,
226             onshelfholds    => 0,
227         }
228     }
229 );
230
231 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
232 is( $is, 1, "Item can be held, items in transit are not available" );
233
234 # Cleanup
235 $schema->storage->txn_rollback;
236
237 sub set_holdallowed_rule {
238     my ( $holdallowed, $branchcode ) = @_;
239     Koha::CirculationRules->set_rules(
240         {
241             branchcode   => $branchcode || undef,
242             itemtype     => undef,
243             rules        => {
244                 holdallowed              => $holdallowed,
245                 hold_fulfillment_policy  => 'any',
246                 returnbranch             => 'homebranch',
247             }
248         }
249     );
250 }