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>.
22 use Test::More tests => 38;
23 use t::lib::TestBuilder;
25 use C4::Reserves qw( GetMaxPatronHoldsForRecord AddReserve CanBookBeReserved );
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
32 my $builder = t::lib::TestBuilder->new();
33 my $library = $builder->build(
39 my $category = $builder->build(
44 my $patron = $builder->build(
48 categorycode => $category->{categorycode},
49 branchcode => $library->{branchcode},
54 my $itemtype1 = $builder->build(
60 my $itemtype2 = $builder->build(
66 my $biblio = $builder->build(
74 my $item1 = $builder->build(
78 biblionumber => $biblio->{biblionumber},
79 itype => $itemtype1->{itemtype},
80 homebranch => $library->{branchcode},
81 holdingbranch => $library->{branchcode},
85 my $item2 = $builder->build(
89 biblionumber => $biblio->{biblionumber},
90 itype => $itemtype2->{itemtype},
91 homebranch => $library->{branchcode},
92 holdingbranch => $library->{branchcode},
96 my $item3 = $builder->build(
100 biblionumber => $biblio->{biblionumber},
101 itype => $itemtype2->{itemtype},
102 homebranch => $library->{branchcode},
103 holdingbranch => $library->{branchcode},
108 my $rules_rs = Koha::Database->new()->schema()->resultset('Issuingrule');
111 # Test GetMaxPatronHoldsForRecord and GetHoldRule
112 my $rule1 = $rules_rs->new(
117 reservesallowed => 1,
118 holds_per_record => 1,
122 my $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
123 is( $max, 1, 'GetMaxPatronHoldsForRecord returns max of 1' );
124 my $rule = C4::Reserves::GetHoldRule(
125 $category->{categorycode},
126 $itemtype1->{itemtype},
127 $library->{branchcode}
129 is( $rule->{categorycode}, '*', 'Got rule with universal categorycode' );
130 is( $rule->{itemtype}, '*', 'Got rule with universal itemtype' );
131 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
132 is( $rule->{reservesallowed}, 1, 'Got reservesallowed of 1' );
133 is( $rule->{holds_per_record}, 1, 'Got holds_per_record of 1' );
135 my $rule2 = $rules_rs->new(
137 categorycode => $category->{categorycode},
140 reservesallowed => 2,
141 holds_per_record => 2,
145 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
146 is( $max, 2, 'GetMaxPatronHoldsForRecord returns max of 2' );
147 $rule = C4::Reserves::GetHoldRule(
148 $category->{categorycode},
149 $itemtype1->{itemtype},
150 $library->{branchcode}
152 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
153 is( $rule->{itemtype}, '*', 'Got rule with universal itemtype' );
154 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
155 is( $rule->{reservesallowed}, 2, 'Got reservesallowed of 2' );
156 is( $rule->{holds_per_record}, 2, 'Got holds_per_record of 2' );
158 my $rule3 = $rules_rs->new(
160 categorycode => $category->{categorycode},
161 itemtype => $itemtype1->{itemtype},
163 reservesallowed => 3,
164 holds_per_record => 3,
168 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
169 is( $max, 3, 'GetMaxPatronHoldsForRecord returns max of 3' );
170 $rule = C4::Reserves::GetHoldRule(
171 $category->{categorycode},
172 $itemtype1->{itemtype},
173 $library->{branchcode}
175 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
176 is( $rule->{itemtype}, $itemtype1->{itemtype}, 'Got rule with universal itemtype' );
177 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
178 is( $rule->{reservesallowed}, 3, 'Got reservesallowed of 3' );
179 is( $rule->{holds_per_record}, 3, 'Got holds_per_record of 3' );
181 my $rule4 = $rules_rs->new(
183 categorycode => $category->{categorycode},
184 itemtype => $itemtype2->{itemtype},
186 reservesallowed => 4,
187 holds_per_record => 4,
191 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
192 is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' );
193 $rule = C4::Reserves::GetHoldRule(
194 $category->{categorycode},
195 $itemtype2->{itemtype},
196 $library->{branchcode}
198 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
199 is( $rule->{itemtype}, $itemtype2->{itemtype}, 'Got rule with universal itemtype' );
200 is( $rule->{branchcode}, '*', 'Got rule with universal branchcode' );
201 is( $rule->{reservesallowed}, 4, 'Got reservesallowed of 4' );
202 is( $rule->{holds_per_record}, 4, 'Got holds_per_record of 4' );
204 my $rule5 = $rules_rs->new(
206 categorycode => $category->{categorycode},
207 itemtype => $itemtype2->{itemtype},
208 branchcode => $library->{branchcode},
209 reservesallowed => 5,
210 holds_per_record => 5,
214 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
215 is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 1' );
216 $rule = C4::Reserves::GetHoldRule(
217 $category->{categorycode},
218 $itemtype2->{itemtype},
219 $library->{branchcode}
221 is( $rule->{categorycode}, $category->{categorycode}, 'Got rule with specific categorycode' );
222 is( $rule->{itemtype}, $itemtype2->{itemtype}, 'Got rule with universal itemtype' );
223 is( $rule->{branchcode}, $library->{branchcode}, 'Got rule with specific branchcode' );
224 is( $rule->{reservesallowed}, 5, 'Got reservesallowed of 5' );
225 is( $rule->{holds_per_record}, 5, 'Got holds_per_record of 5' );
233 my $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
234 is( $holds->forced_hold_level, undef, "No holds does not force an item or record level hold" );
236 # Test Koha::Holds::forced_hold_level
237 my $hold = Koha::Hold->new({
238 borrowernumber => $patron->{borrowernumber},
239 reservedate => '1981-06-10',
240 biblionumber => $biblio->{biblionumber},
241 branchcode => $library->{branchcode},
245 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
246 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
248 $hold->itemnumber( $item1->{itemnumber} );
251 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
252 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
256 # Test multi-hold via AddReserve
257 $rule = $rules_rs->new(
262 reservesallowed => 2,
263 holds_per_record => 2,
267 my $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
268 is( $can, 'OK', 'Hold can be placed with 0 holds' );
269 my $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
270 ok( $hold_id, 'First hold was placed' );
272 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
273 is( $can, 'OK', 'Hold can be placed with 1 hold' );
274 $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
275 ok( $hold_id, 'Second hold was placed' );
277 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
278 is( $can, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
280 $schema->storage->txn_rollback;