Bug 18925: Move maxissueqty and maxonsiteissueqty to circulation_rules
[koha.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
blob647460d1f660d1dfa5621b4d14e409fab2a9d57c
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use C4::Context;
6 use C4::Circulation;
7 use C4::Items;
8 use Koha::IssuingRule;
9 use Koha::Items;
11 use Test::More tests => 6;
13 use t::lib::TestBuilder;
14 use t::lib::Mocks;
16 BEGIN {
17 use_ok('C4::Reserves');
20 my $schema = Koha::Database->schema;
21 $schema->storage->txn_begin;
22 my $dbh = C4::Context->dbh;
24 my $builder = t::lib::TestBuilder->new;
26 my $library1 = $builder->build({
27 source => 'Branch',
28 });
29 my $library2 = $builder->build({
30 source => 'Branch',
31 });
34 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
36 my $bib_title = "Test Title";
38 my $borrower1 = $builder->build({
39 source => 'Borrower',
40 value => {
41 branchcode => $library1->{branchcode},
42 dateexpiry => '3000-01-01',
44 });
46 my $borrower2 = $builder->build({
47 source => 'Borrower',
48 value => {
49 branchcode => $library1->{branchcode},
50 dateexpiry => '3000-01-01',
52 });
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};
61 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
63 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
64 or BAIL_OUT("Cannot find newly created biblio record");
66 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
68 my $biblioitemnumber =
69 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
70 or BAIL_OUT("Cannot find newly created biblioitems record");
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 ");
77 my $itemnumber1 =
78 $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
79 or BAIL_OUT("Cannot find newly created item");
81 my $item1 = Koha::Items->find( $itemnumber1 )->unblessed;
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 ");
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");
92 my $item2 = Koha::Items->find( $itemnumber2 )->unblessed;
94 $dbh->do("DELETE FROM issuingrules");
95 my $rule = Koha::IssuingRule->new(
97 categorycode => '*',
98 itemtype => '*',
99 branchcode => '*',
100 issuelength => 7,
101 lengthunit => 8,
102 reservesallowed => 99,
103 onshelfholds => 2,
106 $rule->store();
108 my $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
109 is( $is, 0, "Item cannot be held, 2 items available" );
111 my $issue1 = AddIssue( $borrower2, $item1->{barcode} );
113 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
114 is( $is, 0, "Item cannot be held, 1 item available" );
116 AddIssue( $borrower2, $item2->{barcode} );
118 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
119 is( $is, 1, "Item can be held, no items available" );
121 AddReturn( $item1->{barcode} );
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;
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');|);
132 subtest 'Item is available at a different library' => sub {
133 plan tests => 4;
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
147 $sth_delete_rules->execute;
148 $sth_insert_rule->execute( $hold_allowed_from_home_library );
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" );
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" );
160 $sth_delete_rules->execute;
161 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
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" );
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" );
173 subtest 'Item is available at the same library' => sub {
174 plan tests => 4;
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?
188 $sth_delete_rules->execute;
189 $sth_insert_rule->execute( $hold_allowed_from_home_library );
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" );
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" );
201 $sth_delete_rules->execute;
202 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
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" );
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" );
216 my $biblio = $builder->build({
217 source => 'Biblio',
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,
232 my $hold = $builder->build({
233 source => 'Reserve',
234 value =>{
235 itemnumber => $item3->{itemnumber},
236 found => 'T'
240 $dbh->do("DELETE FROM issuingrules");
241 $rule = Koha::IssuingRule->new(
243 categorycode => '*',
244 itemtype => '*',
245 branchcode => '*',
246 maxissueqty => 99,
247 issuelength => 7,
248 lengthunit => 8,
249 reservesallowed => 99,
250 onshelfholds => 0,
253 $rule->store();
255 $is = IsAvailableForItemLevelRequest( $item3, $borrower1);
256 is( $is, 1, "Item can be held, items in transit are not available" );
258 # Cleanup
259 $schema->storage->txn_rollback;