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>.
22 use Test
::More tests
=> 3;
25 use Koha
::CirculationRules
;
28 use t
::lib
::TestBuilder
;
31 my $schema = Koha
::Database
->new->schema;
32 my $builder = t
::lib
::TestBuilder
->new;
34 subtest
'set_rule + get_effective_rule' => sub {
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;
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(
68 rule_name
=> $rule_name,
69 rule_value
=> $default_rule_value,
73 $rule = Koha
::CirculationRules
->get_effective_rule(
76 categorycode
=> undef,
78 rule_name
=> $rule_name,
81 is
( $rule->rule_value, $default_rule_value, 'undef means default' );
82 $rule = Koha
::CirculationRules
->get_effective_rule(
87 rule_name
=> $rule_name,
91 is
( $rule->rule_value, $default_rule_value, '* means default' );
93 Koha
::CirculationRules
->set_rule(
97 itemtype
=> $itemtype,
98 rule_name
=> $rule_name,
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,
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(
128 categorycode
=> $categorycode,
130 rule_name
=> $rule_name,
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(
150 categorycode
=> $categorycode,
151 itemtype
=> $itemtype,
152 rule_name
=> $rule_name,
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,
172 rule_name
=> $rule_name,
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,
191 itemtype
=> $itemtype,
192 rule_name
=> $rule_name,
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,
212 rule_name
=> $rule_name,
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,
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 {
261 $schema->storage->txn_begin;
263 my $item = $builder->build_sample_item();
265 my $circ_rules = Koha
::CirculationRules
->new;
267 $circ_rules->search({ rule_name
=> 'onshelfholds' })->delete;
269 $circ_rules->set_rule(
274 rule_name
=> 'onshelfholds',
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)
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 {
290 $schema->storage->txn_begin;
292 my $item_1 = $builder->build_sample_item();
293 my $item_2 = $builder->build_sample_item();
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,
310 'useDaysMode default to pref value if the rule does not exist'
313 Koha
::CirculationRules
->set_rule(
318 rule_name
=> 'useDaysMode',
319 rule_value
=> 'Calendar',
322 Koha
::CirculationRules
->set_rule(
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,
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,
352 "useDaysMode for item_2 is the one defined for the default circ rule"
355 Koha
::CirculationRules
->set_rule(
359 itemtype
=> $item_2->effective_itemtype,
360 rule_name
=> 'useDaysMode',
366 Koha
::CirculationRules
->get_useDaysMode_effective_value(
368 categorycode
=> undef,
369 itemtype
=> $item_2->effective_itemtype,
374 'useDaysMode default to pref value if the rule exists but set to""'
377 $schema->storage->txn_rollback;