Bug 20837: Unit tests
[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 $borrower1 = $builder->build({
40     source => 'Borrower',
41     value => {
42         branchcode => $library1->{branchcode},
43         dateexpiry => '3000-01-01',
44     }
45 });
46
47 my $borrower2 = $builder->build({
48     source => 'Borrower',
49     value => {
50         branchcode => $library1->{branchcode},
51         dateexpiry => '3000-01-01',
52     }
53 });
54
55 my $borrower3 = $builder->build({
56     source => 'Borrower',
57     value => {
58         branchcode => $library2->{branchcode},
59         dateexpiry => '3000-01-01',
60     }
61 });
62
63 my $borrowernumber1 = $borrower1->{borrowernumber};
64 my $borrowernumber2 = $borrower2->{borrowernumber};
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 })->unblessed;
76 my $item2  = $builder->build_sample_item({
77     biblionumber=>$biblionumber,
78     itype=>$itemtype,
79     homebranch => $library_A,
80     holdingbranch => $library_A
81 })->unblessed;
82
83 # Test hold_fulfillment_policy
84
85
86
87 my $rule = Koha::IssuingRule->new(
88     {
89         categorycode => '*',
90         itemtype     => $itemtype,
91         branchcode   => '*',
92         issuelength  => 7,
93         lengthunit   => 8,
94         reservesallowed => 99,
95         onshelfholds => 2,
96     }
97 );
98 $rule->store();
99
100 my $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
101 is( $is, 0, "Item cannot be held, 2 items available" );
102
103 my $issue1 = AddIssue( $borrower2, $item1->{barcode} );
104
105 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
106 is( $is, 0, "Item cannot be held, 1 item available" );
107
108 AddIssue( $borrower2, $item2->{barcode} );
109
110 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
111 is( $is, 1, "Item can be held, no items available" );
112
113 AddReturn( $item1->{barcode} );
114
115 { # Remove the issue for the first patron, and modify the branch for item1
116     subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
117         plan tests => 2;
118
119         my $hold_allowed_from_home_library = 1;
120         my $hold_allowed_from_any_libraries = 2;
121         my $sth_delete_rules = $dbh->prepare(q|DELETE FROM default_circ_rules|);
122         my $sth_insert_rule = $dbh->prepare(q|INSERT INTO default_circ_rules(singleton, holdallowed, hold_fulfillment_policy, returnbranch) VALUES ('singleton', ?, 'any', 'homebranch');|);
123         my $sth_insert_branch_rule = $dbh->prepare(q|INSERT INTO default_branch_circ_rules(branchcode, holdallowed, hold_fulfillment_policy, returnbranch) VALUES (?, ?, 'any', 'homebranch');|);
124
125         subtest 'Item is available at a different library' => sub {
126             plan tests => 7;
127
128             $item1 = Koha::Items->find( $item1->{itemnumber} );
129             $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
130             $item1 = $item1->unblessed;
131             #Scenario is:
132             #One shelf holds is 'If all unavailable'/2
133             #Item 1 homebranch library B is available
134             #Item 2 homebranch library A is checked out
135             #Borrower1 is from library A
136
137             {
138                 $sth_delete_rules->execute;
139                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
140
141                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
142                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
143                 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" );
144                 $is = IsAvailableForItemLevelRequest( $item1, $borrower2);
145                 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" );
146                 $sth_insert_branch_rule->execute( $library_B, $hold_allowed_from_any_libraries );
147                 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
148                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
149                 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" );
150
151                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
152                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
153                 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" );
154                 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
155                 $sth_insert_branch_rule->execute( $library_A, $hold_allowed_from_any_libraries );
156                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
157                 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" );
158             }
159
160             {
161                 $sth_delete_rules->execute;
162                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
163
164                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
165                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
166                 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" );
167
168                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
169                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
170                 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" );
171             }
172         };
173
174         subtest 'Item is available at the same library' => sub {
175             plan tests => 4;
176
177             $item1 = Koha::Items->find( $item1->{itemnumber} );
178             $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
179             $item1 = $item1->unblessed;
180             #Scenario is:
181             #One shelf holds is 'If all unavailable'/2
182             #Item 1 homebranch library A is available
183             #Item 2 homebranch library A is checked out
184             #Borrower1 is from library A
185             #CircControl has no effect - same rule for all branches as set at line 96
186             #ReservesControlBranch is not checked in these subs we are testing?
187
188             {
189                 $sth_delete_rules->execute;
190                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
191
192                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
193                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
194                 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" );
195
196                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
197                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
198                 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" );
199             }
200
201             {
202                 $sth_delete_rules->execute;
203                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
204
205                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
206                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
207                 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" );
208
209                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
210                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
211                 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" );
212             }
213         };
214     };
215 }
216
217 my $itemtype2 = $builder->build({
218     source => 'Itemtype',
219     value  => { notforloan => 0 }
220 })->{itemtype};
221 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
222
223 my $hold = $builder->build({
224     source => 'Reserve',
225     value =>{
226         itemnumber => $item3->itemnumber,
227         found => 'T'
228     }
229 });
230
231 $rule = Koha::IssuingRule->new(
232     {
233         categorycode => '*',
234         itemtype     => $itemtype2,
235         branchcode   => '*',
236         issuelength  => 7,
237         lengthunit   => 8,
238         reservesallowed => 99,
239         onshelfholds => 0,
240     }
241 );
242 $rule->store();
243
244 $is = IsAvailableForItemLevelRequest( $item3->unblessed, $borrower1);
245 is( $is, 1, "Item can be held, items in transit are not available" );
246
247 # Cleanup
248 $schema->storage->txn_rollback;