3 # Copyright 2016 Koha-Suomi Oy
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 3;
23 use Test::Deep qw( cmp_methods );
28 use Koha::CirculationRules;
30 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'get_effective_issuing_rule' => sub {
41 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
42 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
43 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
45 subtest 'Call with undefined values' => sub {
49 Koha::CirculationRules->delete;
51 is(Koha::CirculationRules->search->count, 0, 'There are no issuing rules.');
52 # undef, undef, undef => 1
53 $rule = Koha::CirculationRules->get_effective_rule({
55 categorycode => undef,
60 is($rule, undef, 'When I attempt to get effective issuing rule by'
61 .' providing undefined values, then undef is returned.');
63 # undef, undef, undef => 2
65 Koha::CirculationRule->new(
68 categorycode => undef,
74 'Given I added an issuing rule branchcode => undef,'
75 .' categorycode => undef, itemtype => undef,');
76 $rule = Koha::CirculationRules->get_effective_rule({
78 categorycode => undef,
86 categorycode => undef,
91 'When I attempt to get effective'
92 .' issuing rule by providing undefined values, then the above one is'
97 subtest 'Performance' => sub {
100 my $worst_case = timethis(500,
101 sub { Koha::CirculationRules->get_effective_rule({
102 branchcode => 'nonexistent',
103 categorycode => 'nonexistent',
104 itemtype => 'nonexistent',
105 rule_name => 'nonexistent',
109 my $mid_case = timethis(500,
110 sub { Koha::CirculationRules->get_effective_rule({
111 branchcode => $branchcode,
112 categorycode => 'nonexistent',
113 itemtype => 'nonexistent',
114 rule_name => 'nonexistent',
118 my $sec_best_case = timethis(500,
119 sub { Koha::CirculationRules->get_effective_rule({
120 branchcode => $branchcode,
121 categorycode => $categorycode,
122 itemtype => 'nonexistent',
123 rule_name => 'nonexistent',
127 my $best_case = timethis(500,
128 sub { Koha::CirculationRules->get_effective_rule({
129 branchcode => $branchcode,
130 categorycode => $categorycode,
131 itemtype => $itemtype,
132 rule_name => 'nonexistent',
136 ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
137 .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
138 .' times per second.');
139 ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
140 .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
141 .' times per second.');
142 ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
143 .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
144 .' times per second.');
145 ok($best_case, 'In best case, get_effective_issuing_rule finds matching'
146 .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
147 .' times per second.');
151 subtest 'set_rule' => sub {
154 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
155 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
156 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
158 subtest 'Correct call' => sub {
161 Koha::CirculationRules->delete;
164 Koha::CirculationRules->set_rule( {
165 branchcode => $branchcode,
166 rule_name => 'lostreturn',
169 }, 'setting lostreturn with branch' );
172 Koha::CirculationRules->set_rule( {
173 branchcode => $branchcode,
174 categorycode => $categorycode,
175 rule_name => 'patron_maxissueqty',
178 }, 'setting patron_maxissueqty with branch/category succeeds' );
181 Koha::CirculationRules->set_rule( {
182 branchcode => $branchcode,
183 itemtype => $itemtype,
184 rule_name => 'holdallowed',
187 }, 'setting holdallowed with branch/itemtype succeeds' );
190 Koha::CirculationRules->set_rule( {
191 branchcode => $branchcode,
192 categorycode => $categorycode,
193 itemtype => $itemtype,
197 }, 'setting fine with branch/category/itemtype succeeds' );
200 subtest 'Call with missing params' => sub {
203 Koha::CirculationRules->delete;
206 Koha::CirculationRules->set_rule( {
207 rule_name => 'lostreturn',
210 }, qr/branchcode/, 'setting lostreturn without branch fails' );
213 Koha::CirculationRules->set_rule( {
214 branchcode => $branchcode,
215 rule_name => 'patron_maxissueqty',
218 }, qr/categorycode/, 'setting patron_maxissueqty without categorycode fails' );
221 Koha::CirculationRules->set_rule( {
222 branchcode => $branchcode,
223 rule_name => 'holdallowed',
226 }, qr/itemtype/, 'setting holdallowed without itemtype fails' );
229 Koha::CirculationRules->set_rule( {
230 branchcode => $branchcode,
231 categorycode => $categorycode,
235 }, qr/itemtype/, 'setting fine without itemtype fails' );
238 subtest 'Call with extra params' => sub {
241 Koha::CirculationRules->delete;
244 Koha::CirculationRules->set_rule( {
245 branchcode => $branchcode,
246 categorycode => $categorycode,
247 rule_name => 'lostreturn',
250 }, qr/categorycode/, 'setting lostreturn with categorycode fails' );
253 Koha::CirculationRules->set_rule( {
254 branchcode => $branchcode,
255 categorycode => $categorycode,
256 itemtype => $itemtype,
257 rule_name => 'patron_maxissueqty',
260 }, qr/itemtype/, 'setting patron_maxissueqty with itemtype fails' );
263 Koha::CirculationRules->set_rule( {
264 branchcode => $branchcode,
265 rule_name => 'holdallowed',
266 categorycode => $categorycode,
267 itemtype => $itemtype,
270 }, qr/categorycode/, 'setting holdallowed with categorycode fails' );
274 subtest 'clone' => sub {
277 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
278 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
279 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
281 subtest 'Clone multiple rules' => sub {
284 Koha::CirculationRules->delete;
286 Koha::CirculationRule->new({
288 categorycode => $categorycode,
289 itemtype => $itemtype,
294 Koha::CirculationRule->new({
296 categorycode => $categorycode,
297 itemtype => $itemtype,
298 rule_name => 'lengthunit',
299 rule_value => 'days',
302 Koha::CirculationRules->search({ branchcode => undef })->clone($branchcode);
304 my $rule_fine = Koha::CirculationRules->get_effective_rule({
305 branchcode => $branchcode,
306 categorycode => $categorycode,
307 itemtype => $itemtype,
310 my $rule_lengthunit = Koha::CirculationRules->get_effective_rule({
311 branchcode => $branchcode,
312 categorycode => $categorycode,
313 itemtype => $itemtype,
314 rule_name => 'lengthunit',
320 branchcode => $branchcode,
321 categorycode => $categorycode,
322 itemtype => $itemtype,
326 'When I attempt to get cloned fine rule,'
327 .' then the above one is returned.'
332 branchcode => $branchcode,
333 categorycode => $categorycode,
334 itemtype => $itemtype,
335 rule_name => 'lengthunit',
336 rule_value => 'days',
338 'When I attempt to get cloned lengthunit rule,'
339 .' then the above one is returned.'
344 subtest 'Clone one rule' => sub {
347 Koha::CirculationRules->delete;
349 Koha::CirculationRule->new({
351 categorycode => $categorycode,
352 itemtype => $itemtype,
357 my $rule = Koha::CirculationRules->search({ branchcode => undef })->next;
358 $rule->clone($branchcode);
360 my $cloned_rule = Koha::CirculationRules->get_effective_rule({
361 branchcode => $branchcode,
362 categorycode => $categorycode,
363 itemtype => $itemtype,
370 branchcode => $branchcode,
371 categorycode => $categorycode,
372 itemtype => $itemtype,
376 'When I attempt to get cloned fine rule,'
377 .' then the above one is returned.'
384 my ( $rule, $expected, $message ) = @_;
386 ok( $rule, $message ) ?
387 cmp_methods( $rule, [ %$expected ], $message ) :
391 $schema->storage->txn_rollback;