Bug 24159: Add new tests for get_useDaysMode_effective_value
[koha.git] / t / db_dependent / Koha / CirculationRules.t
blobd2868d26c800e40b39c2117482babcac6a06b484
1 #!/usr/bin/perl
3 # Copyright 2020 Koha Development team
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>.
20 use Modern::Perl;
22 use Test::More tests => 3;
23 use Test::Exception;
25 use Koha::CirculationRules;
26 use Koha::Database;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 subtest 'set_rule + get_effective_rule' => sub {
35 plan tests => 14;
37 $schema->storage->txn_begin;
39 my $categorycode = $builder->build_object( { class => 'Koha::Patron::Categories' } )->categorycode;
40 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype;
41 my $branchcode = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
42 my $branchcode_2 = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
43 my $rule_name = 'maxissueqty';
44 my $default_rule_value = 1;
46 my $rule;
47 Koha::CirculationRules->delete;
49 throws_ok { Koha::CirculationRules->get_effective_rule }
50 'Koha::Exceptions::MissingParameter',
51 "Exception should be raised if get_effective_rule is called without rule_name parameter";
53 $rule = Koha::CirculationRules->get_effective_rule(
55 branchcode => $branchcode,
56 categorycode => $categorycode,
57 itemtype => $itemtype,
58 rule_name => $rule_name,
61 is( $rule, undef, 'Undef should be returned if no rule exist' );
63 Koha::CirculationRules->set_rule(
65 branchcode => '*',
66 categorycode => '*',
67 itemtype => '*',
68 rule_name => $rule_name,
69 rule_value => $default_rule_value,
73 $rule = Koha::CirculationRules->get_effective_rule(
75 branchcode => undef,
76 categorycode => undef,
77 itemtype => undef,
78 rule_name => $rule_name,
81 is( $rule->rule_value, $default_rule_value, 'undef means default' );
82 $rule = Koha::CirculationRules->get_effective_rule(
84 branchcode => '*',
85 categorycode => '*',
86 itemtype => '*',
87 rule_name => $rule_name,
91 is( $rule->rule_value, $default_rule_value, '* means default' );
93 Koha::CirculationRules->set_rule(
95 branchcode => '*',
96 categorycode => '*',
97 itemtype => $itemtype,
98 rule_name => $rule_name,
99 rule_value => 2,
103 $rule = Koha::CirculationRules->get_effective_rule(
105 branchcode => $branchcode,
106 categorycode => $categorycode,
107 itemtype => $itemtype,
108 rule_name => $rule_name,
111 is( $rule->rule_value, 2,
112 'More specific rule is returned when itemtype is given' );
114 $rule = Koha::CirculationRules->get_effective_rule(
116 branchcode => $branchcode_2,
117 categorycode => '*',
118 itemtype => '*',
119 rule_name => $rule_name,
122 is( $rule->rule_value, 1,
123 'Default rule is returned if there is no rule for this branchcode' );
125 Koha::CirculationRules->set_rule(
127 branchcode => '*',
128 categorycode => $categorycode,
129 itemtype => '*',
130 rule_name => $rule_name,
131 rule_value => 3,
135 $rule = Koha::CirculationRules->get_effective_rule(
138 branchcode => $branchcode,
139 categorycode => $categorycode,
140 itemtype => $itemtype,
141 rule_name => $rule_name,
144 is( $rule->rule_value, 3,
145 'More specific rule is returned when categorycode exists' );
147 Koha::CirculationRules->set_rule(
149 branchcode => '*',
150 categorycode => $categorycode,
151 itemtype => $itemtype,
152 rule_name => $rule_name,
153 rule_value => 4,
156 $rule = Koha::CirculationRules->get_effective_rule(
158 branchcode => $branchcode,
159 categorycode => $categorycode,
160 itemtype => $itemtype,
161 rule_name => $rule_name,
164 is( $rule->rule_value, 4,
165 'More specific rule is returned when categorycode and itemtype exist' );
167 Koha::CirculationRules->set_rule(
169 branchcode => $branchcode,
170 categorycode => '*',
171 itemtype => '*',
172 rule_name => $rule_name,
173 rule_value => 5,
176 $rule = Koha::CirculationRules->get_effective_rule(
178 branchcode => $branchcode,
179 categorycode => $categorycode,
180 itemtype => $itemtype,
181 rule_name => $rule_name,
184 is( $rule->rule_value, 5,
185 'More specific rule is returned when branchcode exists' );
187 Koha::CirculationRules->set_rule(
189 branchcode => $branchcode,
190 categorycode => '*',
191 itemtype => $itemtype,
192 rule_name => $rule_name,
193 rule_value => 6,
196 $rule = Koha::CirculationRules->get_effective_rule(
198 branchcode => $branchcode,
199 categorycode => $categorycode,
200 itemtype => $itemtype,
201 rule_name => $rule_name,
204 is( $rule->rule_value, 6,
205 'More specific rule is returned when branchcode and itemtype exists' );
207 Koha::CirculationRules->set_rule(
209 branchcode => $branchcode,
210 categorycode => $categorycode,
211 itemtype => '*',
212 rule_name => $rule_name,
213 rule_value => 7,
216 $rule = Koha::CirculationRules->get_effective_rule(
218 branchcode => $branchcode,
219 categorycode => $categorycode,
220 itemtype => $itemtype,
221 rule_name => $rule_name,
224 is( $rule->rule_value, 7,
225 'More specific rule is returned when branchcode and categorycode exist'
228 Koha::CirculationRules->set_rule(
230 branchcode => $branchcode,
231 categorycode => $categorycode,
232 itemtype => $itemtype,
233 rule_name => $rule_name,
234 rule_value => 8,
237 $rule = Koha::CirculationRules->get_effective_rule(
239 branchcode => $branchcode,
240 categorycode => $categorycode,
241 itemtype => $itemtype,
242 rule_name => $rule_name,
245 is( $rule->rule_value, 8,
246 'More specific rule is returned when branchcode, categorycode and itemtype exist'
249 my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
250 is( $our_branch_rules->count, 4, "We added 8 rules");
251 $our_branch_rules->delete;
252 is( $our_branch_rules->count, 0, "We deleted 8 rules");
254 $schema->storage->txn_rollback;
257 subtest 'get_onshelfholds_policy() tests' => sub {
259 plan tests => 2;
261 $schema->storage->txn_begin;
263 my $item = $builder->build_sample_item();
265 my $circ_rules = Koha::CirculationRules->new;
266 # Cleanup
267 $circ_rules->search({ rule_name => 'onshelfholds' })->delete;
269 $circ_rules->set_rule(
271 branchcode => '*',
272 categorycode => '*',
273 itemtype => '*',
274 rule_name => 'onshelfholds',
275 rule_value => 1,
279 is( $circ_rules->get_onshelfholds_policy({ item => $item }), 1, 'If rule_value is set on a matching rule, return it' );
280 # Delete the rule (i.e. get_effective_rule returns undef)
281 $circ_rules->delete;
282 is( $circ_rules->get_onshelfholds_policy({ item => $item }), 0, 'If no matching rule, fallback to 0' );
284 $schema->storage->txn_rollback;
287 subtest 'get_useDaysMode_effective_value' => sub {
288 plan tests => 4;
290 $schema->storage->txn_begin;
292 my $item_1 = $builder->build_sample_item();
293 my $item_2 = $builder->build_sample_item();
295 my $circ_rules =
296 Koha::CirculationRules->search( { rule_name => 'useDaysMode' } )->delete;
298 # Default value 'Datedue' at pref level
299 t::lib::Mocks::mock_preference( 'useDaysMode', 'Datedue' );
302 Koha::CirculationRules->get_useDaysMode_effective_value(
304 categorycode => undef,
305 itemtype => $item_1->effective_itemtype,
306 branchcode => undef
309 'Datedue',
310 'useDaysMode default to pref value if the rule does not exist'
313 Koha::CirculationRules->set_rule(
315 branchcode => '*',
316 categorycode => '*',
317 itemtype => '*',
318 rule_name => 'useDaysMode',
319 rule_value => 'Calendar',
322 Koha::CirculationRules->set_rule(
324 branchcode => '*',
325 categorycode => '*',
326 itemtype => $item_1->effective_itemtype,
327 rule_name => 'useDaysMode',
328 rule_value => 'Days',
333 Koha::CirculationRules->get_useDaysMode_effective_value(
335 categorycode => undef,
336 itemtype => $item_1->effective_itemtype,
337 branchcode => undef
340 'Days',
341 "useDaysMode for item_1 is the specific rule"
344 Koha::CirculationRules->get_useDaysMode_effective_value(
346 categorycode => undef,
347 itemtype => $item_2->effective_itemtype,
348 branchcode => undef
351 'Calendar',
352 "useDaysMode for item_2 is the one defined for the default circ rule"
355 Koha::CirculationRules->set_rule(
357 branchcode => '*',
358 categorycode => '*',
359 itemtype => $item_2->effective_itemtype,
360 rule_name => 'useDaysMode',
361 rule_value => '',
366 Koha::CirculationRules->get_useDaysMode_effective_value(
368 categorycode => undef,
369 itemtype => $item_2->effective_itemtype,
370 branchcode => undef
373 'Datedue',
374 'useDaysMode default to pref value if the rule exists but set to""'
377 $schema->storage->txn_rollback;