Bug 18624: Regression test for 'any' vs 'all'
[koha.git] / t / db_dependent / Reserves / MultiplePerRecord.t
blob293ab6ab8686d7cd36637ea5a69acdc4e8b93c95
1 #!/usr/bin/perl
3 # Copyright 2016 ByWater Solutions
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 38;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
26 use C4::Reserves qw( GetMaxPatronHoldsForRecord AddReserve CanBookBeReserved );
27 use Koha::Database;
28 use Koha::Holds;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new();
34 my $library = $builder->build(
36 source => 'Branch',
40 my $category = $builder->build(
42 source => 'Category',
45 my $patron = $builder->build(
47 source => 'Borrower',
48 value => {
49 categorycode => $category->{categorycode},
50 branchcode => $library->{branchcode},
55 my $itemtype1 = $builder->build(
57 source => 'Itemtype'
61 my $itemtype2 = $builder->build(
63 source => 'Itemtype'
67 my $biblio = $builder->build(
69 source => 'Biblio',
70 value => {
71 title => 'Title 1',
75 my $biblioitem = $builder->build(
77 source => 'Biblioitem',
78 value => { biblionumber => $biblio->{biblionumber} }
81 my $item1 = $builder->build(
83 source => 'Item',
84 value => {
85 biblionumber => $biblio->{biblionumber},
86 itype => $itemtype1->{itemtype},
87 homebranch => $library->{branchcode},
88 holdingbranch => $library->{branchcode},
89 damaged => 0,
93 my $item2 = $builder->build(
95 source => 'Item',
96 value => {
97 biblionumber => $biblio->{biblionumber},
98 itype => $itemtype2->{itemtype},
99 homebranch => $library->{branchcode},
100 holdingbranch => $library->{branchcode},
101 damaged => 0,
105 my $item3 = $builder->build(
107 source => 'Item',
108 value => {
109 biblionumber => $biblio->{biblionumber},
110 itype => $itemtype2->{itemtype},
111 homebranch => $library->{branchcode},
112 holdingbranch => $library->{branchcode},
113 damaged => 0,
118 my $rules_rs = Koha::Database->new()->schema()->resultset('Issuingrule');
119 $rules_rs->delete();
121 # Test GetMaxPatronHoldsForRecord and GetHoldRule
122 my $rule1 = $rules_rs->new(
124 categorycode => '*',
125 itemtype => '*',
126 branchcode => '*',
127 reservesallowed => 1,
128 holds_per_record => 1,
130 )->insert();
132 t::lib::Mocks::mock_preference('item-level_itypes', 1); # Assuming the item type is defined at item level
134 my $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
135 is( $max, 1, 'GetMaxPatronHoldsForRecord returns max of 1' );
136 my $rule = C4::Reserves::GetHoldRule(
137 $category->{categorycode},
138 $itemtype1->{itemtype},
139 $library->{branchcode}
141 is( $rule->{categorycode}, '*', 'Got rule with universal categorycode' );
142 is( $rule->{itemtype}, '*', 'Got rule with universal itemtype' );
143 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
144 is( $rule->{reservesallowed}, 1, 'Got reservesallowed of 1' );
145 is( $rule->{holds_per_record}, 1, 'Got holds_per_record of 1' );
147 my $rule2 = $rules_rs->new(
149 categorycode => $category->{categorycode},
150 itemtype => '*',
151 branchcode => '*',
152 reservesallowed => 2,
153 holds_per_record => 2,
155 )->insert();
157 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
158 is( $max, 2, 'GetMaxPatronHoldsForRecord returns max of 2' );
159 $rule = C4::Reserves::GetHoldRule(
160 $category->{categorycode},
161 $itemtype1->{itemtype},
162 $library->{branchcode}
164 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
165 is( $rule->{itemtype}, '*', 'Got rule with universal itemtype' );
166 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
167 is( $rule->{reservesallowed}, 2, 'Got reservesallowed of 2' );
168 is( $rule->{holds_per_record}, 2, 'Got holds_per_record of 2' );
170 my $rule3 = $rules_rs->new(
172 categorycode => $category->{categorycode},
173 itemtype => $itemtype1->{itemtype},
174 branchcode => '*',
175 reservesallowed => 3,
176 holds_per_record => 3,
178 )->insert();
180 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
181 is( $max, 3, 'GetMaxPatronHoldsForRecord returns max of 3' );
182 $rule = C4::Reserves::GetHoldRule(
183 $category->{categorycode},
184 $itemtype1->{itemtype},
185 $library->{branchcode}
187 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
188 is( $rule->{itemtype}, $itemtype1->{itemtype}, 'Got rule with universal itemtype' );
189 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
190 is( $rule->{reservesallowed}, 3, 'Got reservesallowed of 3' );
191 is( $rule->{holds_per_record}, 3, 'Got holds_per_record of 3' );
193 my $rule4 = $rules_rs->new(
195 categorycode => $category->{categorycode},
196 itemtype => $itemtype2->{itemtype},
197 branchcode => '*',
198 reservesallowed => 4,
199 holds_per_record => 4,
201 )->insert();
203 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
204 is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' );
205 $rule = C4::Reserves::GetHoldRule(
206 $category->{categorycode},
207 $itemtype2->{itemtype},
208 $library->{branchcode}
210 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
211 is( $rule->{itemtype}, $itemtype2->{itemtype}, 'Got rule with universal itemtype' );
212 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
213 is( $rule->{reservesallowed}, 4, 'Got reservesallowed of 4' );
214 is( $rule->{holds_per_record}, 4, 'Got holds_per_record of 4' );
216 my $rule5 = $rules_rs->new(
218 categorycode => $category->{categorycode},
219 itemtype => $itemtype2->{itemtype},
220 branchcode => $library->{branchcode},
221 reservesallowed => 5,
222 holds_per_record => 5,
224 )->insert();
226 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
227 is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 1' );
228 $rule = C4::Reserves::GetHoldRule(
229 $category->{categorycode},
230 $itemtype2->{itemtype},
231 $library->{branchcode}
233 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
234 is( $rule->{itemtype}, $itemtype2->{itemtype}, 'Got rule with universal itemtype' );
235 is( $rule->{branchcode}, $library->{branchcode}, 'Got rule with specific branchcode' );
236 is( $rule->{reservesallowed}, 5, 'Got reservesallowed of 5' );
237 is( $rule->{holds_per_record}, 5, 'Got holds_per_record of 5' );
239 $rule1->delete();
240 $rule2->delete();
241 $rule3->delete();
242 $rule4->delete();
243 $rule5->delete();
245 my $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
246 is( $holds->forced_hold_level, undef, "No holds does not force an item or record level hold" );
248 # Test Koha::Holds::forced_hold_level
249 my $hold = Koha::Hold->new({
250 borrowernumber => $patron->{borrowernumber},
251 reservedate => '1981-06-10',
252 biblionumber => $biblio->{biblionumber},
253 branchcode => $library->{branchcode},
254 priority => 1,
255 })->store();
257 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
258 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
260 $hold->itemnumber( $item1->{itemnumber} );
261 $hold->store();
263 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
264 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
266 $hold->delete();
268 # Test multi-hold via AddReserve
269 $rule = $rules_rs->new(
271 categorycode => '*',
272 itemtype => '*',
273 branchcode => '*',
274 reservesallowed => 2,
275 holds_per_record => 2,
277 )->insert();
279 my $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
280 is( $can, 'OK', 'Hold can be placed with 0 holds' );
281 my $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
282 ok( $hold_id, 'First hold was placed' );
284 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
285 is( $can, 'OK', 'Hold can be placed with 1 hold' );
286 $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
287 ok( $hold_id, 'Second hold was placed' );
289 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
290 is( $can, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
292 $schema->storage->txn_rollback;