6e8874070205b6d35f96ffb1a082169b60464e20
[srvgit] / t / db_dependent / Reserves / MultiplePerRecord.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 16;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use C4::Reserves qw( GetMaxPatronHoldsForRecord AddReserve CanBookBeReserved );
27 use Koha::Database;
28 use Koha::Holds;
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder = t::lib::TestBuilder->new();
34 my $library = $builder->build(
35     {
36         source => 'Branch',
37     }
38 );
39
40 my $category = $builder->build(
41     {
42         source => 'Category',
43     }
44 );
45 my $patron = $builder->build(
46     {
47         source => 'Borrower',
48         value  => {
49             categorycode => $category->{categorycode},
50             branchcode   => $library->{branchcode},
51         },
52     }
53 );
54
55 my $itemtype1 = $builder->build(
56     {
57         source => 'Itemtype'
58     }
59 );
60
61 my $itemtype2 = $builder->build(
62     {
63         source => 'Itemtype'
64     }
65 );
66
67 my $biblio = $builder->build_sample_biblio;
68 my $item1 = $builder->build_sample_item(
69     {
70         biblionumber => $biblio->biblionumber,
71         itype        => $itemtype1->{itemtype},
72         library      => $library->{branchcode},
73     },
74 );
75 my $item2 = $builder->build_sample_item(
76     {
77         biblionumber => $biblio->biblionumber,
78         itype        => $itemtype2->{itemtype},
79         library      => $library->{branchcode},
80     }
81 );
82 my $item3 = $builder->build_sample_item(
83     {
84         biblionumber => $biblio->biblionumber,
85         itype        => $itemtype2->{itemtype},
86         library      => $library->{branchcode},
87     }
88 );
89
90 Koha::CirculationRules->delete();
91
92 # Test GetMaxPatronHoldsForRecord
93 Koha::CirculationRules->set_rules(
94     {
95         categorycode => undef,
96         itemtype     => undef,
97         branchcode   => undef,
98         rules        => {
99             reservesallowed  => 1,
100             holds_per_record => 1,
101         }
102     }
103 );
104
105 t::lib::Mocks::mock_preference('item-level_itypes', 1); # Assuming the item type is defined at item level
106
107 my $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
108 is( $max, 1, 'GetMaxPatronHoldsForRecord returns max of 1' );
109
110 Koha::CirculationRules->set_rules(
111     {
112         categorycode => $category->{categorycode},
113         itemtype     => undef,
114         branchcode   => undef,
115         rules        => {
116             reservesallowed  => 2,
117             holds_per_record => 2,
118         }
119     }
120 );
121
122 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
123 is( $max, 2, 'GetMaxPatronHoldsForRecord returns max of 2' );
124
125 Koha::CirculationRules->set_rules(
126     {
127         categorycode => $category->{categorycode},
128         itemtype     => $itemtype1->{itemtype},
129         branchcode   => undef,
130         rules        => {
131             reservesallowed  => 3,
132             holds_per_record => 3,
133         }
134     }
135 );
136
137 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
138 is( $max, 3, 'GetMaxPatronHoldsForRecord returns max of 3' );
139
140 Koha::CirculationRules->set_rules(
141     {
142         categorycode => $category->{categorycode},
143         itemtype     => $itemtype2->{itemtype},
144         branchcode   => undef,
145         rules        => {
146             reservesallowed  => 4,
147             holds_per_record => 4,
148         }
149     }
150 );
151
152 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
153 is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' );
154
155 Koha::CirculationRules->set_rules(
156     {
157         categorycode => $category->{categorycode},
158         itemtype     => $itemtype2->{itemtype},
159         branchcode   => $library->{branchcode},
160         rules        => {
161             reservesallowed  => 5,
162             holds_per_record => 5,
163         }
164     }
165 );
166
167 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber );
168 is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 5' );
169
170 Koha::CirculationRules->set_rules(
171     {
172         categorycode => undef,
173         itemtype     => undef,
174         branchcode   => $library->{branchcode},
175         rules        => {
176             reservesallowed  => 9,
177             holds_per_record => 9,
178         }
179     }
180 );
181
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' );
184
185 Koha::CirculationRules->delete();
186
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" );
189
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},
196     priority => 1,
197 })->store();
198
199 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
200 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
201
202 $hold->itemnumber( $item1->itemnumber );
203 $hold->store();
204
205 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
206 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
207
208 $hold->delete();
209
210 # Test multi-hold via AddReserve
211 Koha::CirculationRules->set_rules(
212     {
213         categorycode => undef,
214         itemtype     => undef,
215         branchcode   => undef,
216         rules        => {
217             reservesallowed  => 3,
218             holds_per_record => 2,
219         }
220     }
221 );
222
223 my $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
224 is( $can->{status}, 'OK', 'Hold can be placed with 0 holds' );
225 my $hold_id = AddReserve(
226     {
227         branchcode     => $library->{branchcode},
228         borrowernumber => $patron->{borrowernumber},
229         biblionumber   => $biblio->biblionumber,
230         priority       => 1
231     }
232 );
233 ok( $hold_id, 'First hold was placed' );
234
235 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
236 is( $can->{status}, 'OK', 'Hold can be placed with 1 hold' );
237 $hold_id = AddReserve(
238     {
239         branchcode     => $library->{branchcode},
240         borrowernumber => $patron->{borrowernumber},
241         biblionumber   => $biblio->biblionumber,
242         priority       => 1
243     }
244 );
245 ok( $hold_id, 'Second hold was placed' );
246
247 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
248 is( $can->{status}, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
249
250 Koha::Holds->find($hold_id)->found("W")->store;
251 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber);
252 is( $can->{status}, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
253
254 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->biblionumber, undef, { ignore_found_holds => 1 });
255 is( $can->{status}, 'OK', 'Third hold is allowed when ignoring waiting holds' );
256
257 $schema->storage->txn_rollback;