Bug 23051: (QA follow-up) Missing curly and tabs and fix test
[koha.git] / t / db_dependent / Koha / CirculationRules.t
blob7e27b59feb5cfd5f3c0e4ad512c80709fe6e760f
1 #!/usr/bin/perl
3 # Copyright 2018 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 => 2;
23 use Test::Exception;
25 use Koha::CirculationRules;
26 use Koha::Database;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 subtest 'set_rule + get_effective_rule' => sub {
34 plan tests => 14;
36 $schema->storage->txn_begin;
38 my $categorycode = $builder->build_object( { class => 'Koha::Patron::Categories' } )->categorycode;
39 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype;
40 my $branchcode = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
41 my $branchcode_2 = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
42 my $rule_name = 'maxissueqty';
43 my $default_rule_value = 1;
45 my $rule;
46 Koha::CirculationRules->delete;
48 throws_ok { Koha::CirculationRules->get_effective_rule }
49 'Koha::Exceptions::MissingParameter',
50 "Exception should be raised if get_effective_rule is called without rule_name parameter";
52 $rule = Koha::CirculationRules->get_effective_rule(
54 branchcode => $branchcode,
55 categorycode => $categorycode,
56 itemtype => $itemtype,
57 rule_name => $rule_name,
60 is( $rule, undef, 'Undef should be returned if no rule exist' );
62 Koha::CirculationRules->set_rule(
64 branchcode => '*',
65 categorycode => '*',
66 itemtype => '*',
67 rule_name => $rule_name,
68 rule_value => $default_rule_value,
72 $rule = Koha::CirculationRules->get_effective_rule(
74 branchcode => undef,
75 categorycode => undef,
76 itemtype => undef,
77 rule_name => $rule_name,
80 is( $rule->rule_value, $default_rule_value, 'undef means default' );
81 $rule = Koha::CirculationRules->get_effective_rule(
83 branchcode => '*',
84 categorycode => '*',
85 itemtype => '*',
86 rule_name => $rule_name,
90 is( $rule->rule_value, $default_rule_value, '* means default' );
92 Koha::CirculationRules->set_rule(
94 branchcode => '*',
95 categorycode => '*',
96 itemtype => $itemtype,
97 rule_name => $rule_name,
98 rule_value => 2,
102 $rule = Koha::CirculationRules->get_effective_rule(
104 branchcode => $branchcode,
105 categorycode => $categorycode,
106 itemtype => $itemtype,
107 rule_name => $rule_name,
110 is( $rule->rule_value, 2,
111 'More specific rule is returned when itemtype is given' );
113 $rule = Koha::CirculationRules->get_effective_rule(
115 branchcode => $branchcode_2,
116 categorycode => '*',
117 itemtype => '*',
118 rule_name => $rule_name,
121 is( $rule->rule_value, 1,
122 'Default rule is returned if there is no rule for this branchcode' );
124 Koha::CirculationRules->set_rule(
126 branchcode => '*',
127 categorycode => $categorycode,
128 itemtype => '*',
129 rule_name => $rule_name,
130 rule_value => 3,
134 $rule = Koha::CirculationRules->get_effective_rule(
137 branchcode => $branchcode,
138 categorycode => $categorycode,
139 itemtype => $itemtype,
140 rule_name => $rule_name,
143 is( $rule->rule_value, 3,
144 'More specific rule is returned when categorycode exists' );
146 Koha::CirculationRules->set_rule(
148 branchcode => '*',
149 categorycode => $categorycode,
150 itemtype => $itemtype,
151 rule_name => $rule_name,
152 rule_value => 4,
155 $rule = Koha::CirculationRules->get_effective_rule(
157 branchcode => $branchcode,
158 categorycode => $categorycode,
159 itemtype => $itemtype,
160 rule_name => $rule_name,
163 is( $rule->rule_value, 4,
164 'More specific rule is returned when categorycode and itemtype exist' );
166 Koha::CirculationRules->set_rule(
168 branchcode => $branchcode,
169 categorycode => '*',
170 itemtype => '*',
171 rule_name => $rule_name,
172 rule_value => 5,
175 $rule = Koha::CirculationRules->get_effective_rule(
177 branchcode => $branchcode,
178 categorycode => $categorycode,
179 itemtype => $itemtype,
180 rule_name => $rule_name,
183 is( $rule->rule_value, 5,
184 'More specific rule is returned when branchcode exists' );
186 Koha::CirculationRules->set_rule(
188 branchcode => $branchcode,
189 categorycode => '*',
190 itemtype => $itemtype,
191 rule_name => $rule_name,
192 rule_value => 6,
195 $rule = Koha::CirculationRules->get_effective_rule(
197 branchcode => $branchcode,
198 categorycode => $categorycode,
199 itemtype => $itemtype,
200 rule_name => $rule_name,
203 is( $rule->rule_value, 6,
204 'More specific rule is returned when branchcode and itemtype exists' );
206 Koha::CirculationRules->set_rule(
208 branchcode => $branchcode,
209 categorycode => $categorycode,
210 itemtype => '*',
211 rule_name => $rule_name,
212 rule_value => 7,
215 $rule = Koha::CirculationRules->get_effective_rule(
217 branchcode => $branchcode,
218 categorycode => $categorycode,
219 itemtype => $itemtype,
220 rule_name => $rule_name,
223 is( $rule->rule_value, 7,
224 'More specific rule is returned when branchcode and categorycode exist'
227 Koha::CirculationRules->set_rule(
229 branchcode => $branchcode,
230 categorycode => $categorycode,
231 itemtype => $itemtype,
232 rule_name => $rule_name,
233 rule_value => 8,
236 $rule = Koha::CirculationRules->get_effective_rule(
238 branchcode => $branchcode,
239 categorycode => $categorycode,
240 itemtype => $itemtype,
241 rule_name => $rule_name,
244 is( $rule->rule_value, 8,
245 'More specific rule is returned when branchcode, categorycode and itemtype exist'
248 my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
249 is( $our_branch_rules->count, 4, "We added 8 rules");
250 $our_branch_rules->delete;
251 is( $our_branch_rules->count, 0, "We deleted 8 rules");
253 $schema->storage->txn_rollback;
256 subtest 'get_onshelfholds_policy() tests' => sub {
258 plan tests => 2;
260 $schema->storage->txn_begin;
262 my $item = $builder->build_sample_item();
264 my $circ_rules = Koha::CirculationRules->new;
265 # Cleanup
266 $circ_rules->search({ rule_name => 'onshelfholds' })->delete;
268 $circ_rules->set_rule(
270 branchcode => '*',
271 categorycode => '*',
272 itemtype => '*',
273 rule_name => 'onshelfholds',
274 rule_value => 1,
278 is( $circ_rules->get_onshelfholds_policy({ item => $item }), 1, 'If rule_value is set on a matching rule, return it' );
279 # Delete the rule (i.e. get_effective_rule returns undef)
280 $circ_rules->delete;
281 is( $circ_rules->get_onshelfholds_policy({ item => $item }), 0, 'If no matching rule, fallback to 0' );
283 $schema->storage->txn_rollback;