Bug 24602: Tests for get_onshlefholds_policy
[koha.git] / t / db_dependent / Koha / CirculationRules.t
1 #!/usr/bin/perl
2
3 # Copyright 2018 Koha Development team
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 => 2;
23 use Test::Exception;
24
25 use Koha::CirculationRules;
26 use Koha::Database;
27
28 use t::lib::TestBuilder;
29
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 subtest 'set_rule + get_effective_rule' => sub {
34     plan tests => 14;
35
36     $schema->storage->txn_begin;
37
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;
44
45     my $rule;
46     Koha::CirculationRules->delete;
47
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";
51
52     $rule = Koha::CirculationRules->get_effective_rule(
53         {
54             branchcode   => $branchcode,
55             categorycode => $categorycode,
56             itemtype     => $itemtype,
57             rule_name    => $rule_name,
58         }
59     );
60     is( $rule, undef, 'Undef should be returned if no rule exist' );
61
62     Koha::CirculationRules->set_rule(
63         {
64             branchcode   => '*',
65             categorycode => '*',
66             itemtype     => '*',
67             rule_name    => $rule_name,
68             rule_value   => $default_rule_value,
69         }
70     );
71
72     $rule = Koha::CirculationRules->get_effective_rule(
73         {
74             branchcode   => undef,
75             categorycode => undef,
76             itemtype     => undef,
77             rule_name    => $rule_name,
78         }
79     );
80     is( $rule->rule_value, $default_rule_value, 'undef means default' );
81     $rule = Koha::CirculationRules->get_effective_rule(
82         {
83             branchcode   => '*',
84             categorycode => '*',
85             itemtype     => '*',
86             rule_name    => $rule_name,
87         }
88     );
89
90     is( $rule->rule_value, $default_rule_value, '* means default' );
91
92     Koha::CirculationRules->set_rule(
93         {
94             branchcode   => '*',
95             categorycode => '*',
96             itemtype     => $itemtype,
97             rule_name    => $rule_name,
98             rule_value   => 2,
99         }
100     );
101
102     $rule = Koha::CirculationRules->get_effective_rule(
103         {
104             branchcode   => $branchcode,
105             categorycode => $categorycode,
106             itemtype     => $itemtype,
107             rule_name    => $rule_name,
108         }
109     );
110     is( $rule->rule_value, 2,
111         'More specific rule is returned when itemtype is given' );
112
113     $rule = Koha::CirculationRules->get_effective_rule(
114         {
115             branchcode   => $branchcode_2,
116             categorycode => '*',
117             itemtype     => '*',
118             rule_name    => $rule_name,
119         }
120     );
121     is( $rule->rule_value, 1,
122         'Default rule is returned if there is no rule for this branchcode' );
123
124     Koha::CirculationRules->set_rule(
125         {
126             branchcode   => '*',
127             categorycode => $categorycode,
128             itemtype     => '*',
129             rule_name    => $rule_name,
130             rule_value   => 3,
131         }
132     );
133
134     $rule = Koha::CirculationRules->get_effective_rule(
135         {
136
137             branchcode   => $branchcode,
138             categorycode => $categorycode,
139             itemtype     => $itemtype,
140             rule_name    => $rule_name,
141         }
142     );
143     is( $rule->rule_value, 3,
144         'More specific rule is returned when categorycode exists' );
145
146     Koha::CirculationRules->set_rule(
147         {
148             branchcode   => '*',
149             categorycode => $categorycode,
150             itemtype     => $itemtype,
151             rule_name    => $rule_name,
152             rule_value   => 4,
153         }
154     );
155     $rule = Koha::CirculationRules->get_effective_rule(
156         {
157             branchcode   => $branchcode,
158             categorycode => $categorycode,
159             itemtype     => $itemtype,
160             rule_name    => $rule_name,
161         }
162     );
163     is( $rule->rule_value, 4,
164         'More specific rule is returned when categorycode and itemtype exist' );
165
166     Koha::CirculationRules->set_rule(
167         {
168             branchcode   => $branchcode,
169             categorycode => '*',
170             itemtype     => '*',
171             rule_name    => $rule_name,
172             rule_value   => 5,
173         }
174     );
175     $rule = Koha::CirculationRules->get_effective_rule(
176         {
177             branchcode   => $branchcode,
178             categorycode => $categorycode,
179             itemtype     => $itemtype,
180             rule_name    => $rule_name,
181         }
182     );
183     is( $rule->rule_value, 5,
184         'More specific rule is returned when branchcode exists' );
185
186     Koha::CirculationRules->set_rule(
187         {
188             branchcode   => $branchcode,
189             categorycode => '*',
190             itemtype     => $itemtype,
191             rule_name    => $rule_name,
192             rule_value   => 6,
193         }
194     );
195     $rule = Koha::CirculationRules->get_effective_rule(
196         {
197             branchcode   => $branchcode,
198             categorycode => $categorycode,
199             itemtype     => $itemtype,
200             rule_name    => $rule_name,
201         }
202     );
203     is( $rule->rule_value, 6,
204         'More specific rule is returned when branchcode and itemtype exists' );
205
206     Koha::CirculationRules->set_rule(
207         {
208             branchcode   => $branchcode,
209             categorycode => $categorycode,
210             itemtype     => '*',
211             rule_name    => $rule_name,
212             rule_value   => 7,
213         }
214     );
215     $rule = Koha::CirculationRules->get_effective_rule(
216         {
217             branchcode   => $branchcode,
218             categorycode => $categorycode,
219             itemtype     => $itemtype,
220             rule_name    => $rule_name,
221         }
222     );
223     is( $rule->rule_value, 7,
224         'More specific rule is returned when branchcode and categorycode exist'
225     );
226
227     Koha::CirculationRules->set_rule(
228         {
229             branchcode   => $branchcode,
230             categorycode => $categorycode,
231             itemtype     => $itemtype,
232             rule_name    => $rule_name,
233             rule_value   => 8,
234         }
235     );
236     $rule = Koha::CirculationRules->get_effective_rule(
237         {
238             branchcode   => $branchcode,
239             categorycode => $categorycode,
240             itemtype     => $itemtype,
241             rule_name    => $rule_name,
242         }
243     );
244     is( $rule->rule_value, 8,
245         'More specific rule is returned when branchcode, categorycode and itemtype exist'
246     );
247
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");
252
253     $schema->storage->txn_rollback;
254 };
255
256 subtest 'get_onshelfholds_policy() tests' => sub {
257
258     plan tests => 2;
259
260     $schema->storage->txn_begin;
261
262     my $item = $builder->build_sample_item();
263
264     my $circ_rules = Koha::CirculationRules->new;
265     # Cleanup
266     $circ_rules->search({ rule_name => 'onshelfholds' })->delete;
267
268     $circ_rules->set_rule(
269         {
270             branchcode   => '*',
271             categorycode => '*',
272             itemtype     => '*',
273             rule_name    => 'onshelfholds',
274             rule_value   => 1,
275         }
276     );
277
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' );
282
283     $schema->storage->txn_rollback;
284 };