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