Bug 17327: (QA followup) Remove Carp::Always which is not used
[koha.git] / 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 => 38;
23 use t::lib::TestBuilder;
24
25 use C4::Reserves qw( GetMaxPatronHoldsForRecord AddReserve CanBookBeReserved );
26 use Koha::Database;
27 use Koha::Holds;
28
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
31
32 my $builder = t::lib::TestBuilder->new();
33 my $library = $builder->build(
34     {
35         source => 'Branch',
36     }
37 );
38
39 my $category = $builder->build(
40     {
41         source => 'Category',
42     }
43 );
44 my $patron = $builder->build(
45     {
46         source => 'Borrower',
47         value  => {
48             categorycode => $category->{categorycode},
49             branchcode   => $library->{branchcode},
50         },
51     }
52 );
53
54 my $itemtype1 = $builder->build(
55     {
56         source => 'Itemtype'
57     }
58 );
59
60 my $itemtype2 = $builder->build(
61     {
62         source => 'Itemtype'
63     }
64 );
65
66 my $biblio = $builder->build(
67     {
68         source => 'Biblio',
69         value  => {
70             title => 'Title 1',
71         },
72     }
73 );
74 my $item1 = $builder->build(
75     {
76         source => 'Item',
77         value  => {
78             biblionumber  => $biblio->{biblionumber},
79             itype         => $itemtype1->{itemtype},
80             homebranch    => $library->{branchcode},
81             holdingbranch => $library->{branchcode},
82         },
83     }
84 );
85 my $item2 = $builder->build(
86     {
87         source => 'Item',
88         value  => {
89             biblionumber  => $biblio->{biblionumber},
90             itype         => $itemtype2->{itemtype},
91             homebranch    => $library->{branchcode},
92             holdingbranch => $library->{branchcode},
93         },
94     }
95 );
96 my $item3 = $builder->build(
97     {
98         source => 'Item',
99         value  => {
100             biblionumber  => $biblio->{biblionumber},
101             itype         => $itemtype2->{itemtype},
102             homebranch    => $library->{branchcode},
103             holdingbranch => $library->{branchcode},
104         },
105     }
106 );
107
108 my $rules_rs = Koha::Database->new()->schema()->resultset('Issuingrule');
109 $rules_rs->delete();
110
111 # Test GetMaxPatronHoldsForRecord and GetHoldRule
112 my $rule1 = $rules_rs->new(
113     {
114         categorycode     => '*',
115         itemtype         => '*',
116         branchcode       => '*',
117         reservesallowed  => 1,
118         holds_per_record => 1,
119     }
120 )->insert();
121
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}
128 );
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' );
134
135 my $rule2 = $rules_rs->new(
136     {
137         categorycode     => $category->{categorycode},
138         itemtype         => '*',
139         branchcode       => '*',
140         reservesallowed  => 2,
141         holds_per_record => 2,
142     }
143 )->insert();
144
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}
151 );
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' );
157
158 my $rule3 = $rules_rs->new(
159     {
160         categorycode     => $category->{categorycode},
161         itemtype         => $itemtype1->{itemtype},
162         branchcode       => '*',
163         reservesallowed  => 3,
164         holds_per_record => 3,
165     }
166 )->insert();
167
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}
174 );
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' );
180
181 my $rule4 = $rules_rs->new(
182     {
183         categorycode     => $category->{categorycode},
184         itemtype         => $itemtype2->{itemtype},
185         branchcode       => '*',
186         reservesallowed  => 4,
187         holds_per_record => 4,
188     }
189 )->insert();
190
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}
197 );
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' );
203
204 my $rule5 = $rules_rs->new(
205     {
206         categorycode     => $category->{categorycode},
207         itemtype         => $itemtype2->{itemtype},
208         branchcode       => $library->{branchcode},
209         reservesallowed  => 5,
210         holds_per_record => 5,
211     }
212 )->insert();
213
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}
220 );
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' );
226
227 $rule1->delete();
228 $rule2->delete();
229 $rule3->delete();
230 $rule4->delete();
231 $rule5->delete();
232
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" );
235
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},
242     priority => 1,
243 })->store();
244
245 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
246 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
247
248 $hold->itemnumber( $item1->{itemnumber} );
249 $hold->store();
250
251 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
252 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
253
254 $hold->delete();
255
256 # Test multi-hold via AddReserve
257 $rule = $rules_rs->new(
258     {
259         categorycode     => '*',
260         itemtype         => '*',
261         branchcode       => '*',
262         reservesallowed  => 2,
263         holds_per_record => 2,
264     }
265 )->insert();
266
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' );
271
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' );
276
277 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
278 is( $can, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
279
280 $schema->storage->txn_rollback;
281
282 1;