Bug 21817: Centralize the mock of userenv from 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
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
32
33 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
34
35 my $bib_title = "Test Title";
36
37 my $borrower1 = $builder->build({
38     source => 'Borrower',
39     value => {
40         branchcode => $library1->{branchcode},
41         dateexpiry => '3000-01-01',
42     }
43 });
44
45 my $borrower2 = $builder->build({
46     source => 'Borrower',
47     value => {
48         branchcode => $library1->{branchcode},
49         dateexpiry => '3000-01-01',
50     }
51 });
52
53 # Test hold_fulfillment_policy
54 my ( $itemtype ) = @{ $dbh->selectrow_arrayref("SELECT itemtype FROM itemtypes LIMIT 1") };
55 my $borrowernumber1 = $borrower1->{borrowernumber};
56 my $borrowernumber2 = $borrower2->{borrowernumber};
57 my $library_A = $library1->{branchcode};
58 my $library_B = $library2->{branchcode};
59
60 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
61
62 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
63   or BAIL_OUT("Cannot find newly created biblio record");
64
65 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
66
67 my $biblioitemnumber =
68   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
69   or BAIL_OUT("Cannot find newly created biblioitems record");
70
71 $dbh->do("
72     INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
73     VALUES ('AllowHoldIf1', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
74 ");
75
76 my $itemnumber1 =
77   $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
78   or BAIL_OUT("Cannot find newly created item");
79
80 my $item1 = GetItem( $itemnumber1 );
81
82 $dbh->do("
83     INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
84     VALUES ('AllowHoldIf2', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
85 ");
86
87 my $itemnumber2 =
88   $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber ORDER BY itemnumber DESC")
89   or BAIL_OUT("Cannot find newly created item");
90
91 my $item2 = GetItem( $itemnumber2 );
92
93 $dbh->do("DELETE FROM issuingrules");
94 my $rule = Koha::IssuingRule->new(
95     {
96         categorycode => '*',
97         itemtype     => '*',
98         branchcode   => '*',
99         maxissueqty  => 99,
100         issuelength  => 7,
101         lengthunit   => 8,
102         reservesallowed => 99,
103         onshelfholds => 2,
104     }
105 );
106 $rule->store();
107
108 my $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
109 is( $is, 0, "Item cannot be held, 2 items available" );
110
111 my $issue1 = AddIssue( $borrower2, $item1->{barcode} );
112
113 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
114 is( $is, 0, "Item cannot be held, 1 item available" );
115
116 AddIssue( $borrower2, $item2->{barcode} );
117
118 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
119 is( $is, 1, "Item can be held, no items available" );
120
121 AddReturn( $item1->{barcode} );
122
123 { # Remove the issue for the first patron, and modify the branch for item1
124     subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
125         plan tests => 2;
126
127         my $hold_allowed_from_home_library = 1;
128         my $hold_allowed_from_any_libraries = 2;
129         my $sth_delete_rules = $dbh->prepare(q|DELETE FROM default_circ_rules|);
130         my $sth_insert_rule = $dbh->prepare(q|INSERT INTO default_circ_rules(singleton, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch) VALUES ('singleton', NULL, NULL, ?, 'any', 'homebranch');|);
131
132         subtest 'Item is available at a different library' => sub {
133             plan tests => 4;
134
135             Koha::Items->find( $item1->{itemnumber} )->set({homebranch => $library_B, holdingbranch => $library_B })->store;
136             $item1 = GetItem( $itemnumber1 );
137             #Scenario is:
138             #One shelf holds is 'If all unavailable'/2
139             #Item 1 homebranch library B is available
140             #Item 2 homebranch library A is checked out
141             #Borrower1 is from library A
142             #CircControl has no effect - same rule for all branches as set at line 96
143             #FIXME: ReservesControlBranch is not checked in these subs we are testing
144
145             {
146                 $sth_delete_rules->execute;
147                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
148
149                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
150                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
151                 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" );
152
153                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
154                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
155                 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" );
156             }
157
158             {
159                 $sth_delete_rules->execute;
160                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
161
162                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
163                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
164                 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" );
165
166                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
167                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
168                 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" );
169             }
170         };
171
172         subtest 'Item is available at the same library' => sub {
173             plan tests => 4;
174
175             Koha::Items->find( $item1->{itemnumber} )->set({homebranch => $library_A, holdingbranch => $library_A })->store;
176             $item1 = GetItem( $itemnumber1 );
177             #Scenario is:
178             #One shelf holds is 'If all unavailable'/2
179             #Item 1 homebranch library A is available
180             #Item 2 homebranch library A is checked out
181             #Borrower1 is from library A
182             #CircControl has no effect - same rule for all branches as set at line 96
183             #ReservesControlBranch is not checked in these subs we are testing?
184
185             {
186                 $sth_delete_rules->execute;
187                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
188
189                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
190                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
191                 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" );
192
193                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
194                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
195                 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" );
196             }
197
198             {
199                 $sth_delete_rules->execute;
200                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
201
202                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
203                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
204                 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" );
205
206                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
207                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
208                 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" );
209             }
210         };
211     };
212 }
213
214 my $biblio = $builder->build({
215     source => 'Biblio',
216 });
217
218 my $item3 = $builder->build({
219     source => 'Item',
220     value => {
221         biblionumber => $biblio->{biblionumber},
222         itemlost     => 0,
223         notforloan   => 0,
224         withdrawn    => 0,
225         damaged      => 0,
226         onloan       => undef,
227     }
228 });
229
230 my $hold = $builder->build({
231     source => 'Reserve',
232     value =>{
233         itemnumber => $item3->{itemnumber},
234         found => 'T'
235     }
236 });
237
238 $dbh->do("DELETE FROM issuingrules");
239 $rule = Koha::IssuingRule->new(
240     {
241         categorycode => '*',
242         itemtype     => '*',
243         branchcode   => '*',
244         maxissueqty  => 99,
245         issuelength  => 7,
246         lengthunit   => 8,
247         reservesallowed => 99,
248         onshelfholds => 0,
249     }
250 );
251 $rule->store();
252
253 $is = IsAvailableForItemLevelRequest( $item3, $borrower1);
254 is( $is, 1, "Item can be held, items in transit are not available" );
255
256 # Cleanup
257 $schema->storage->txn_rollback;