255c757bf6a2f29da803c3cd132402f054e54746
[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         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, holdallowed, hold_fulfillment_policy, returnbranch) VALUES ('singleton', ?, 'any', 'homebranch');|);
131
132         subtest 'Item is available at a different library' => sub {
133             plan tests => 4;
134
135             $item1 = Koha::Items->find( $item1->{itemnumber} );
136             $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
137             $item1 = $item1->unblessed;
138             #Scenario is:
139             #One shelf holds is 'If all unavailable'/2
140             #Item 1 homebranch library B is available
141             #Item 2 homebranch library A is checked out
142             #Borrower1 is from library A
143             #CircControl has no effect - same rule for all branches as set at line 96
144             #FIXME: ReservesControlBranch is not checked in these subs we are testing
145
146             {
147                 $sth_delete_rules->execute;
148                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
149
150                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
151                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
152                 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" );
153
154                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
155                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
156                 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" );
157             }
158
159             {
160                 $sth_delete_rules->execute;
161                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
162
163                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
164                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
165                 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" );
166
167                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
168                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
169                 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" );
170             }
171         };
172
173         subtest 'Item is available at the same library' => sub {
174             plan tests => 4;
175
176             $item1 = Koha::Items->find( $item1->{itemnumber} );
177             $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
178             $item1 = $item1->unblessed;
179             #Scenario is:
180             #One shelf holds is 'If all unavailable'/2
181             #Item 1 homebranch library A is available
182             #Item 2 homebranch library A is checked out
183             #Borrower1 is from library A
184             #CircControl has no effect - same rule for all branches as set at line 96
185             #ReservesControlBranch is not checked in these subs we are testing?
186
187             {
188                 $sth_delete_rules->execute;
189                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
190
191                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
192                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
193                 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" );
194
195                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
196                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
197                 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" );
198             }
199
200             {
201                 $sth_delete_rules->execute;
202                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
203
204                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
205                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
206                 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" );
207
208                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
209                 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
210                 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" );
211             }
212         };
213     };
214 }
215
216 my $biblio = $builder->build({
217     source => 'Biblio',
218 });
219
220 my $item3 = $builder->build({
221     source => 'Item',
222     value => {
223         biblionumber => $biblio->{biblionumber},
224         itemlost     => 0,
225         notforloan   => 0,
226         withdrawn    => 0,
227         damaged      => 0,
228         onloan       => undef,
229     }
230 });
231
232 my $hold = $builder->build({
233     source => 'Reserve',
234     value =>{
235         itemnumber => $item3->{itemnumber},
236         found => 'T'
237     }
238 });
239
240 $dbh->do("DELETE FROM issuingrules");
241 $rule = Koha::IssuingRule->new(
242     {
243         categorycode => '*',
244         itemtype     => '*',
245         branchcode   => '*',
246         issuelength  => 7,
247         lengthunit   => 8,
248         reservesallowed => 99,
249         onshelfholds => 0,
250     }
251 );
252 $rule->store();
253
254 $is = IsAvailableForItemLevelRequest( $item3, $borrower1);
255 is( $is, 1, "Item can be held, items in transit are not available" );
256
257 # Cleanup
258 $schema->storage->txn_rollback;