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 => 16;
23 use t::lib::TestBuilder;
26 use C4::Reserves qw( GetMaxPatronHoldsForRecord CanBookBeReserved AddReserve );
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new();
34 my $library = $builder->build(
40 my $category = $builder->build(
45 my $patron = $builder->build(
49 categorycode => $category->{categorycode},
50 branchcode => $library->{branchcode},
55 my $itemtype1 = $builder->build(
61 my $itemtype2 = $builder->build(
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
93 Koha::CirculationRules->set_rules(
95 categorycode => undef,
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' );
110 Koha::CirculationRules->set_rules(
112 categorycode => $category->{categorycode},
116 reservesallowed => 2,
117 holds_per_record => 2,
122 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
123 is( $max, 2, 'GetMaxPatronHoldsForRecord returns max of 2' );
125 Koha::CirculationRules->set_rules(
127 categorycode => $category->{categorycode},
128 itemtype => $itemtype1->{itemtype},
131 reservesallowed => 3,
132 holds_per_record => 3,
137 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
138 is( $max, 3, 'GetMaxPatronHoldsForRecord returns max of 3' );
140 Koha::CirculationRules->set_rules(
142 categorycode => $category->{categorycode},
143 itemtype => $itemtype2->{itemtype},
146 reservesallowed => 4,
147 holds_per_record => 4,
152 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
153 is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' );
155 Koha::CirculationRules->set_rules(
157 categorycode => $category->{categorycode},
158 itemtype => $itemtype2->{itemtype},
159 branchcode => $library->{branchcode},
161 reservesallowed => 5,
162 holds_per_record => 5,
167 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
168 is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 5' );
170 Koha::CirculationRules->set_rules(
172 categorycode => undef,
174 branchcode => $library->{branchcode},
176 reservesallowed => 9,
177 holds_per_record => 9,
182 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
183 is( $max, 9, 'GetMaxPatronHoldsForRecord returns max of 9 because Library specific all itemtypes all categories rule comes before All libraries specific type and specific category' );
185 Koha::CirculationRules->delete();
187 my $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
188 is( $holds->forced_hold_level, undef, "No holds does not force an item or record level hold" );
190 # Test Koha::Holds::forced_hold_level
191 my $hold = Koha::Hold->new({
192 borrowernumber => $patron->{borrowernumber},
193 reservedate => '1981-06-10',
194 biblionumber => $biblio->biblionumber,
195 branchcode => $library->{branchcode},
199 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
200 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
202 $hold->itemnumber( $item1->itemnumber );
205 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
206 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
208 my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
209 $hold->itemnumber( undef );
210 $hold->item_group_id( $item_group->id );
213 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
214 is( $holds->forced_hold_level, 'item_group', "Item group level hold forces item group level holds" );
218 # Test multi-hold via AddReserve
219 Koha::CirculationRules->set_rules(
221 categorycode => undef,
225 reservesallowed => 3,
226 holds_per_record => 2,
231 my $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
232 is( $can->{status}, 'OK', 'Hold can be placed with 0 holds' );
233 my $hold_id = AddReserve(
235 branchcode => $library->{branchcode},
236 borrowernumber => $patron->{borrowernumber},
237 biblionumber => $biblio->biblionumber,
241 ok( $hold_id, 'First hold was placed' );
243 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
244 is( $can->{status}, 'OK', 'Hold can be placed with 1 hold' );
245 $hold_id = AddReserve(
247 branchcode => $library->{branchcode},
248 borrowernumber => $patron->{borrowernumber},
249 biblionumber => $biblio->biblionumber,
253 ok( $hold_id, 'Second hold was placed' );
255 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
256 is( $can->{status}, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
258 Koha::Holds->find($hold_id)->found("W")->store;
259 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
260 is( $can->{status}, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
262 $schema->storage->txn_rollback;