Bug 15524: (QA follow-up) Change Can[Book|Item]BeReserved to return hashref, pass...
[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 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(
68     {
69         source => 'Biblio',
70         value  => {
71             title => 'Title 1',
72         },
73     }
74 );
75 my $biblioitem = $builder->build(
76     {
77         source => 'Biblioitem',
78         value  => { biblionumber => $biblio->{biblionumber} }
79     }
80 );
81 my $item1 = $builder->build(
82     {
83         source => 'Item',
84         value  => {
85             biblionumber  => $biblio->{biblionumber},
86             itype         => $itemtype1->{itemtype},
87             homebranch    => $library->{branchcode},
88             holdingbranch => $library->{branchcode},
89             damaged       => 0,
90         },
91     }
92 );
93 my $item2 = $builder->build(
94     {
95         source => 'Item',
96         value  => {
97             biblionumber  => $biblio->{biblionumber},
98             itype         => $itemtype2->{itemtype},
99             homebranch    => $library->{branchcode},
100             holdingbranch => $library->{branchcode},
101             damaged       => 0,
102         },
103     }
104 );
105 my $item3 = $builder->build(
106     {
107         source => 'Item',
108         value  => {
109             biblionumber  => $biblio->{biblionumber},
110             itype         => $itemtype2->{itemtype},
111             homebranch    => $library->{branchcode},
112             holdingbranch => $library->{branchcode},
113             damaged       => 0,
114         },
115     }
116 );
117
118 my $rules_rs = Koha::Database->new()->schema()->resultset('Issuingrule');
119 $rules_rs->delete();
120
121 # Test GetMaxPatronHoldsForRecord and GetHoldRule
122 my $rule1 = $rules_rs->new(
123     {
124         categorycode     => '*',
125         itemtype         => '*',
126         branchcode       => '*',
127         reservesallowed  => 1,
128         holds_per_record => 1,
129     }
130 )->insert();
131
132 t::lib::Mocks::mock_preference('item-level_itypes', 1); # Assuming the item type is defined at item level
133
134 my $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
135 is( $max, 1, 'GetMaxPatronHoldsForRecord returns max of 1' );
136 my $rule = C4::Reserves::GetHoldRule(
137     $category->{categorycode},
138     $itemtype1->{itemtype},
139     $library->{branchcode}
140 );
141 is( $rule->{categorycode},     '*', 'Got rule with universal categorycode' );
142 is( $rule->{itemtype},         '*', 'Got rule with universal itemtype' );
143 is( $rule->{branchcode},       '*', 'Got rule with universal branchcode' );
144 is( $rule->{reservesallowed},  1,   'Got reservesallowed of 1' );
145 is( $rule->{holds_per_record}, 1,   'Got holds_per_record of 1' );
146
147 my $rule2 = $rules_rs->new(
148     {
149         categorycode     => $category->{categorycode},
150         itemtype         => '*',
151         branchcode       => '*',
152         reservesallowed  => 2,
153         holds_per_record => 2,
154     }
155 )->insert();
156
157 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
158 is( $max, 2, 'GetMaxPatronHoldsForRecord returns max of 2' );
159 $rule = C4::Reserves::GetHoldRule(
160     $category->{categorycode},
161     $itemtype1->{itemtype},
162     $library->{branchcode}
163 );
164 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
165 is( $rule->{itemtype},         '*',                       'Got rule with universal itemtype' );
166 is( $rule->{branchcode},       '*',                       'Got rule with universal branchcode' );
167 is( $rule->{reservesallowed},  2,                         'Got reservesallowed of 2' );
168 is( $rule->{holds_per_record}, 2,                         'Got holds_per_record of 2' );
169
170 my $rule3 = $rules_rs->new(
171     {
172         categorycode     => $category->{categorycode},
173         itemtype         => $itemtype1->{itemtype},
174         branchcode       => '*',
175         reservesallowed  => 3,
176         holds_per_record => 3,
177     }
178 )->insert();
179
180 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
181 is( $max, 3, 'GetMaxPatronHoldsForRecord returns max of 3' );
182 $rule = C4::Reserves::GetHoldRule(
183     $category->{categorycode},
184     $itemtype1->{itemtype},
185     $library->{branchcode}
186 );
187 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
188 is( $rule->{itemtype},         $itemtype1->{itemtype},    'Got rule with universal itemtype' );
189 is( $rule->{branchcode},       '*',                       'Got rule with universal branchcode' );
190 is( $rule->{reservesallowed},  3,                         'Got reservesallowed of 3' );
191 is( $rule->{holds_per_record}, 3,                         'Got holds_per_record of 3' );
192
193 my $rule4 = $rules_rs->new(
194     {
195         categorycode     => $category->{categorycode},
196         itemtype         => $itemtype2->{itemtype},
197         branchcode       => '*',
198         reservesallowed  => 4,
199         holds_per_record => 4,
200     }
201 )->insert();
202
203 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
204 is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' );
205 $rule = C4::Reserves::GetHoldRule(
206     $category->{categorycode},
207     $itemtype2->{itemtype},
208     $library->{branchcode}
209 );
210 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
211 is( $rule->{itemtype},         $itemtype2->{itemtype},    'Got rule with universal itemtype' );
212 is( $rule->{branchcode},       '*',                       'Got rule with universal branchcode' );
213 is( $rule->{reservesallowed},  4,                         'Got reservesallowed of 4' );
214 is( $rule->{holds_per_record}, 4,                         'Got holds_per_record of 4' );
215
216 my $rule5 = $rules_rs->new(
217     {
218         categorycode     => $category->{categorycode},
219         itemtype         => $itemtype2->{itemtype},
220         branchcode       => $library->{branchcode},
221         reservesallowed  => 5,
222         holds_per_record => 5,
223     }
224 )->insert();
225
226 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
227 is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 1' );
228 $rule = C4::Reserves::GetHoldRule(
229     $category->{categorycode},
230     $itemtype2->{itemtype},
231     $library->{branchcode}
232 );
233 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
234 is( $rule->{itemtype},         $itemtype2->{itemtype},    'Got rule with universal itemtype' );
235 is( $rule->{branchcode},       $library->{branchcode},    'Got rule with specific branchcode' );
236 is( $rule->{reservesallowed},  5,                         'Got reservesallowed of 5' );
237 is( $rule->{holds_per_record}, 5,                         'Got holds_per_record of 5' );
238
239 $rule1->delete();
240 $rule2->delete();
241 $rule3->delete();
242 $rule4->delete();
243 $rule5->delete();
244
245 my $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
246 is( $holds->forced_hold_level, undef, "No holds does not force an item or record level hold" );
247
248 # Test Koha::Holds::forced_hold_level
249 my $hold = Koha::Hold->new({
250     borrowernumber => $patron->{borrowernumber},
251     reservedate => '1981-06-10',
252     biblionumber => $biblio->{biblionumber},
253     branchcode => $library->{branchcode},
254     priority => 1,
255 })->store();
256
257 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
258 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
259
260 $hold->itemnumber( $item1->{itemnumber} );
261 $hold->store();
262
263 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
264 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
265
266 $hold->delete();
267
268 # Test multi-hold via AddReserve
269 $rule = $rules_rs->new(
270     {
271         categorycode     => '*',
272         itemtype         => '*',
273         branchcode       => '*',
274         reservesallowed  => 2,
275         holds_per_record => 2,
276     }
277 )->insert();
278
279 my $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
280 is( $can->{status}, 'OK', 'Hold can be placed with 0 holds' );
281 my $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
282 ok( $hold_id, 'First hold was placed' );
283
284 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
285 is( $can->{status}, 'OK', 'Hold can be placed with 1 hold' );
286 $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
287 ok( $hold_id, 'Second hold was placed' );
288
289 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
290 is( $can->{status}, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
291
292 $schema->storage->txn_rollback;
293