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