]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Reserves/MultiplePerRecord.t
Bug 17599: Make the tests 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 $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 t::lib::Mocks::mock_preference('item-level_itypes', 1); # Assuming the item type is defined at item level
124
125 my $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
126 is( $max, 1, 'GetMaxPatronHoldsForRecord returns max of 1' );
127 my $rule = C4::Reserves::GetHoldRule(
128     $category->{categorycode},
129     $itemtype1->{itemtype},
130     $library->{branchcode}
131 );
132 is( $rule->{categorycode},     '*', 'Got rule with universal categorycode' );
133 is( $rule->{itemtype},         '*', 'Got rule with universal itemtype' );
134 is( $rule->{branchcode},       '*', 'Got rule with universal branchcode' );
135 is( $rule->{reservesallowed},  1,   'Got reservesallowed of 1' );
136 is( $rule->{holds_per_record}, 1,   'Got holds_per_record of 1' );
137
138 my $rule2 = $rules_rs->new(
139     {
140         categorycode     => $category->{categorycode},
141         itemtype         => '*',
142         branchcode       => '*',
143         reservesallowed  => 2,
144         holds_per_record => 2,
145     }
146 )->insert();
147
148 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
149 is( $max, 2, 'GetMaxPatronHoldsForRecord returns max of 2' );
150 $rule = C4::Reserves::GetHoldRule(
151     $category->{categorycode},
152     $itemtype1->{itemtype},
153     $library->{branchcode}
154 );
155 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
156 is( $rule->{itemtype},         '*',                       'Got rule with universal itemtype' );
157 is( $rule->{branchcode},       '*',                       'Got rule with universal branchcode' );
158 is( $rule->{reservesallowed},  2,                         'Got reservesallowed of 2' );
159 is( $rule->{holds_per_record}, 2,                         'Got holds_per_record of 2' );
160
161 my $rule3 = $rules_rs->new(
162     {
163         categorycode     => $category->{categorycode},
164         itemtype         => $itemtype1->{itemtype},
165         branchcode       => '*',
166         reservesallowed  => 3,
167         holds_per_record => 3,
168     }
169 )->insert();
170
171 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
172 is( $max, 3, 'GetMaxPatronHoldsForRecord returns max of 3' );
173 $rule = C4::Reserves::GetHoldRule(
174     $category->{categorycode},
175     $itemtype1->{itemtype},
176     $library->{branchcode}
177 );
178 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
179 is( $rule->{itemtype},         $itemtype1->{itemtype},    'Got rule with universal itemtype' );
180 is( $rule->{branchcode},       '*',                       'Got rule with universal branchcode' );
181 is( $rule->{reservesallowed},  3,                         'Got reservesallowed of 3' );
182 is( $rule->{holds_per_record}, 3,                         'Got holds_per_record of 3' );
183
184 my $rule4 = $rules_rs->new(
185     {
186         categorycode     => $category->{categorycode},
187         itemtype         => $itemtype2->{itemtype},
188         branchcode       => '*',
189         reservesallowed  => 4,
190         holds_per_record => 4,
191     }
192 )->insert();
193
194 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
195 is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' );
196 $rule = C4::Reserves::GetHoldRule(
197     $category->{categorycode},
198     $itemtype2->{itemtype},
199     $library->{branchcode}
200 );
201 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
202 is( $rule->{itemtype},         $itemtype2->{itemtype},    'Got rule with universal itemtype' );
203 is( $rule->{branchcode},       '*',                       'Got rule with universal branchcode' );
204 is( $rule->{reservesallowed},  4,                         'Got reservesallowed of 4' );
205 is( $rule->{holds_per_record}, 4,                         'Got holds_per_record of 4' );
206
207 my $rule5 = $rules_rs->new(
208     {
209         categorycode     => $category->{categorycode},
210         itemtype         => $itemtype2->{itemtype},
211         branchcode       => $library->{branchcode},
212         reservesallowed  => 5,
213         holds_per_record => 5,
214     }
215 )->insert();
216
217 $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->{biblionumber} );
218 is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 1' );
219 $rule = C4::Reserves::GetHoldRule(
220     $category->{categorycode},
221     $itemtype2->{itemtype},
222     $library->{branchcode}
223 );
224 is( $rule->{categorycode},     $category->{categorycode}, 'Got rule with specific categorycode' );
225 is( $rule->{itemtype},         $itemtype2->{itemtype},    'Got rule with universal itemtype' );
226 is( $rule->{branchcode},       $library->{branchcode},    'Got rule with specific branchcode' );
227 is( $rule->{reservesallowed},  5,                         'Got reservesallowed of 5' );
228 is( $rule->{holds_per_record}, 5,                         'Got holds_per_record of 5' );
229
230 $rule1->delete();
231 $rule2->delete();
232 $rule3->delete();
233 $rule4->delete();
234 $rule5->delete();
235
236 my $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
237 is( $holds->forced_hold_level, undef, "No holds does not force an item or record level hold" );
238
239 # Test Koha::Holds::forced_hold_level
240 my $hold = Koha::Hold->new({
241     borrowernumber => $patron->{borrowernumber},
242     reservedate => '1981-06-10',
243     biblionumber => $biblio->{biblionumber},
244     branchcode => $library->{branchcode},
245     priority => 1,
246 })->store();
247
248 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
249 is( $holds->forced_hold_level, 'record', "Record level hold forces record level holds" );
250
251 $hold->itemnumber( $item1->{itemnumber} );
252 $hold->store();
253
254 $holds = Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } );
255 is( $holds->forced_hold_level, 'item', "Item level hold forces item level holds" );
256
257 $hold->delete();
258
259 # Test multi-hold via AddReserve
260 $rule = $rules_rs->new(
261     {
262         categorycode     => '*',
263         itemtype         => '*',
264         branchcode       => '*',
265         reservesallowed  => 2,
266         holds_per_record => 2,
267     }
268 )->insert();
269
270 my $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
271 is( $can, 'OK', 'Hold can be placed with 0 holds' );
272 my $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
273 ok( $hold_id, 'First hold was placed' );
274
275 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
276 is( $can, 'OK', 'Hold can be placed with 1 hold' );
277 $hold_id = AddReserve( $library->{branchcode}, $patron->{borrowernumber}, $biblio->{biblionumber}, '', 1 );
278 ok( $hold_id, 'Second hold was placed' );
279
280 $can = CanBookBeReserved($patron->{borrowernumber}, $biblio->{biblionumber});
281 is( $can, 'tooManyHoldsForThisRecord', 'Third hold exceeds limit of holds per record' );
282
283 $schema->storage->txn_rollback;
284
285 1;