Bug 24786: (QA follow-up) Remove trailing space in classes
[koha.git] / t / db_dependent / Reserves / MultiplePerRecord.t
blob8d5cd68396c16721852ff923d697a1235ff4327a
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 => 40;
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_sample_biblio;
68 my $item1 = $builder->build_sample_item(
70 biblionumber => $biblio->biblionumber,
71 itype => $itemtype1->{itemtype},
72 library => $library->{branchcode},
75 my $item2 = $builder->build_sample_item(
77 biblionumber => $biblio->biblionumber,
78 itype => $itemtype2->{itemtype},
79 library => $library->{branchcode},
82 my $item3 = $builder->build_sample_item(
84 biblionumber => $biblio->biblionumber,
85 itype => $itemtype2->{itemtype},
86 library => $library->{branchcode},
90 Koha::CirculationRules->delete();
92 # Test GetMaxPatronHoldsForRecord and GetHoldRule
93 Koha::CirculationRules->set_rules(
95 categorycode => undef,
96 itemtype => undef,
97 branchcode => undef,
98 rules => {
99 reservesallowed => 1,
100 holds_per_record => 1,
105 t::lib::Mocks::mock_preference('item-level_itypes', 1); # Assuming the item type is defined at item level
107 my $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
108 is( $max, 1, 'GetMaxPatronHoldsForRecord returns max of 1' );
109 my $rule = C4::Reserves::GetHoldRule(
110 $category->{categorycode},
111 $itemtype1->{itemtype},
112 $library->{branchcode}
114 is( $rule->{categorycode}, undef, 'Got rule with universal categorycode' );
115 is( $rule->{itemtype}, undef, 'Got rule with universal itemtype' );
116 is( $rule->{branchcode}, undef, 'Got rule with universal branchcode' );
117 is( $rule->{reservesallowed}, 1, 'Got reservesallowed of 1' );
118 is( $rule->{holds_per_record}, 1, 'Got holds_per_record of 1' );
120 Koha::CirculationRules->set_rules(
122 categorycode => $category->{categorycode},
123 itemtype => undef,
124 branchcode => undef,
125 rules => {
126 reservesallowed => 2,
127 holds_per_record => 2,
132 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
133 is( $max, 2, 'GetMaxPatronHoldsForRecord returns max of 2' );
134 $rule = C4::Reserves::GetHoldRule(
135 $category->{categorycode},
136 $itemtype1->{itemtype},
137 $library->{branchcode}
139 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
140 is( $rule->{itemtype}, undef, 'Got rule with universal itemtype' );
141 is( $rule->{branchcode}, undef, 'Got rule with universal branchcode' );
142 is( $rule->{reservesallowed}, 2, 'Got reservesallowed of 2' );
143 is( $rule->{holds_per_record}, 2, 'Got holds_per_record of 2' );
145 Koha::CirculationRules->set_rules(
147 categorycode => $category->{categorycode},
148 itemtype => $itemtype1->{itemtype},
149 branchcode => undef,
150 rules => {
151 reservesallowed => 3,
152 holds_per_record => 3,
157 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
158 is( $max, 3, 'GetMaxPatronHoldsForRecord returns max of 3' );
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}, $itemtype1->{itemtype}, 'Got rule with universal itemtype' );
166 is( $rule->{branchcode}, undef, 'Got rule with universal branchcode' );
167 is( $rule->{reservesallowed}, 3, 'Got reservesallowed of 3' );
168 is( $rule->{holds_per_record}, 3, 'Got holds_per_record of 3' );
170 Koha::CirculationRules->set_rules(
172 categorycode => $category->{categorycode},
173 itemtype => $itemtype2->{itemtype},
174 branchcode => undef,
175 rules => {
176 reservesallowed => 4,
177 holds_per_record => 4,
182 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
183 is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' );
184 $rule = C4::Reserves::GetHoldRule(
185 $category->{categorycode},
186 $itemtype2->{itemtype},
187 $library->{branchcode}
189 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
190 is( $rule->{itemtype}, $itemtype2->{itemtype}, 'Got rule with universal itemtype' );
191 is( $rule->{branchcode}, undef, 'Got rule with universal branchcode' );
192 is( $rule->{reservesallowed}, 4, 'Got reservesallowed of 4' );
193 is( $rule->{holds_per_record}, 4, 'Got holds_per_record of 4' );
195 Koha::CirculationRules->set_rules(
197 categorycode => $category->{categorycode},
198 itemtype => $itemtype2->{itemtype},
199 branchcode => $library->{branchcode},
200 rules => {
201 reservesallowed => 5,
202 holds_per_record => 5,
207 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
208 is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 1' );
209 $rule = C4::Reserves::GetHoldRule(
210 $category->{categorycode},
211 $itemtype2->{itemtype},
212 $library->{branchcode}
214 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
215 is( $rule->{itemtype}, $itemtype2->{itemtype}, 'Got rule with universal itemtype' );
216 is( $rule->{branchcode}, $library->{branchcode}, 'Got rule with specific branchcode' );
217 is( $rule->{reservesallowed}, 5, 'Got reservesallowed of 5' );
218 is( $rule->{holds_per_record}, 5, 'Got holds_per_record of 5' );
220 Koha::CirculationRules->delete();
222 my $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
223 is( $holds->forced_hold_level, undef, "No holds does not force an item or record level hold" );
225 # Test Koha::Holds::forced_hold_level
226 my $hold = Koha::Hold->new({
227 borrowernumber => $patron->{borrowernumber},
228 reservedate => '1981-06-10',
229 biblionumber => $biblio->biblionumber,
230 branchcode => $library->{branchcode},
231 priority => 1,
232 })->store();
234 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
235 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
237 $hold->itemnumber( $item1->itemnumber );
238 $hold->store();
240 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
241 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
243 $hold->delete();
245 # Test multi-hold via AddReserve
246 Koha::CirculationRules->set_rules(
248 categorycode => undef,
249 itemtype => undef,
250 branchcode => undef,
251 rules => {
252 reservesallowed => 3,
253 holds_per_record => 2,
258 my $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
259 is( $can->{status}, 'OK', 'Hold can be placed with 0 holds' );
260 my $hold_id = AddReserve(
262 branchcode => $library->{branchcode},
263 borrowernumber => $patron->{borrowernumber},
264 biblionumber => $biblio->biblionumber,
265 priority => 1
268 ok( $hold_id, 'First hold was placed' );
270 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
271 is( $can->{status}, 'OK', 'Hold can be placed with 1 hold' );
272 $hold_id = AddReserve(
274 branchcode => $library->{branchcode},
275 borrowernumber => $patron->{borrowernumber},
276 biblionumber => $biblio->biblionumber,
277 priority => 1
280 ok( $hold_id, 'Second hold was placed' );
282 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
283 is( $can->{status}, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
285 Koha::Holds->find($hold_id)->found("W")->store;
286 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
287 is( $can->{status}, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
289 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber, undef, { ignore_found_holds => 1 });
290 is( $can->{status}, 'OK', 'Third hold is allowed when ignoring waiting holds' );
292 $schema->storage->txn_rollback;