Bug 24264: (follow-up) fix bad test
[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::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         my $sth_delete_rules = $dbh->prepare(q|DELETE FROM default_circ_rules|);
118         my $sth_insert_rule = $dbh->prepare(q|INSERT INTO default_circ_rules(singleton, holdallowed, hold_fulfillment_policy, returnbranch) VALUES ('singleton', ?, 'any', 'homebranch');|);
119         my $sth_insert_branch_rule = $dbh->prepare(q|INSERT INTO default_branch_circ_rules(branchcode, holdallowed, hold_fulfillment_policy, returnbranch) VALUES (?, ?, 'any', 'homebranch');|);
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                 $sth_delete_rules->execute;
133                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
134
135                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
136                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
137                 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" );
138                 $is = IsAvailableForItemLevelRequest( $item1, $patron2);
139                 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" );
140                 $sth_insert_branch_rule->execute( $library_B, $hold_allowed_from_any_libraries );
141                 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
142                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
143                 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
145                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
146                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
147                 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" );
148                 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
149                 $sth_insert_branch_rule->execute( $library_A, $hold_allowed_from_any_libraries );
150                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
151                 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" );
152             }
153
154             {
155                 $sth_delete_rules->execute;
156                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
157
158                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
159                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
160                 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" );
161
162                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
163                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
164                 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" );
165             }
166         };
167
168         subtest 'Item is available at the same library' => sub {
169             plan tests => 4;
170
171             $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
172             #Scenario is:
173             #One shelf holds is 'If all unavailable'/2
174             #Item 1 homebranch library A is available
175             #Item 2 homebranch library A is checked out
176             #Borrower1 is from library A
177             #CircControl has no effect - same rule for all branches as set at line 96
178             #ReservesControlBranch is not checked in these subs we are testing?
179
180             {
181                 $sth_delete_rules->execute;
182                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
183
184                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
185                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
186                 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" );
187
188                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
189                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
190                 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             }
192
193             {
194                 $sth_delete_rules->execute;
195                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
196
197                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
198                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
199                 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" );
200
201                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
202                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
203                 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" );
204             }
205         };
206     };
207 }
208
209 my $itemtype2 = $builder->build({
210     source => 'Itemtype',
211     value  => { notforloan => 0 }
212 })->{itemtype};
213 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
214
215 my $hold = $builder->build({
216     source => 'Reserve',
217     value =>{
218         itemnumber => $item3->itemnumber,
219         found => 'T'
220     }
221 });
222
223 $rule = Koha::IssuingRule->new(
224     {
225         categorycode => '*',
226         itemtype     => $itemtype2,
227         branchcode   => '*',
228         issuelength  => 7,
229         lengthunit   => 8,
230         reservesallowed => 99,
231         onshelfholds => 0,
232     }
233 );
234 $rule->store();
235
236 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
237 is( $is, 1, "Item can be held, items in transit are not available" );
238
239 # Cleanup
240 $schema->storage->txn_rollback;