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