66e28367591e9d403b29be1245cd30536eb1bf41
[koha.git] / t / db_dependent / Koha / IssuingRules.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha-Suomi Oy
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 => 3;
23
24 use Benchmark;
25
26 use Koha::IssuingRules;
27
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder      = t::lib::TestBuilder->new;
35
36 subtest 'get_effective_issuing_rule' => sub {
37     plan tests => 3;
38
39     my $patron       = $builder->build({ source => 'Borrower' });
40     my $item     = $builder->build({ source => 'Item' });
41
42     my $categorycode = $patron->{'categorycode'};
43     my $itemtype     = $item->{'itype'};
44     my $branchcode   = $item->{'homebranch'};
45
46     subtest 'Call with undefined values' => sub {
47         plan tests => 4;
48
49         my $rule;
50         Koha::IssuingRules->delete;
51
52         is(Koha::IssuingRules->search->count, 0, 'There are no issuing rules.');
53         $rule = Koha::IssuingRules->get_effective_issuing_rule({
54             branchcode   => undef,
55             categorycode => undef,
56             itemtype     => undef,
57         });
58         is($rule, undef, 'When I attempt to get effective issuing rule by'
59            .' providing undefined values, then undef is returned.');
60         ok(Koha::IssuingRule->new({
61             branchcode => '*',
62             categorycode => '*',
63             itemtype => '*',
64         })->store, 'Given I added an issuing rule branchcode => *,'
65            .' categorycode => *, itemtype => *,');
66         $rule = Koha::IssuingRules->get_effective_issuing_rule({
67             branchcode   => undef,
68             categorycode => undef,
69             itemtype     => undef,
70         });
71         ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective'
72            .' issuing rule by providing undefined values, then the above one is'
73            .' returned.');
74     };
75
76     subtest 'Get effective issuing rule in correct order' => sub {
77         plan tests => 18;
78
79         my $rule;
80         Koha::IssuingRules->delete;
81         is(Koha::IssuingRules->search->count, 0, 'There are no issuing rules.');
82         $rule = Koha::IssuingRules->get_effective_issuing_rule({
83             branchcode   => $branchcode,
84             categorycode => $categorycode,
85             itemtype     => $itemtype,
86         });
87         is($rule, undef, 'When I attempt to get effective issuing rule, then undef'
88                         .' is returned.');
89
90         ok(Koha::IssuingRule->new({
91             branchcode => '*',
92             categorycode => '*',
93             itemtype => '*',
94         })->store, 'Given I added an issuing rule branchcode => *, categorycode => *, itemtype => *,');
95         $rule = Koha::IssuingRules->get_effective_issuing_rule({
96             branchcode   => $branchcode,
97             categorycode => $categorycode,
98             itemtype     => $itemtype,
99         });
100         ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective issuing rule,'
101            .' then the above one is returned.');
102
103         ok(Koha::IssuingRule->new({
104             branchcode => '*',
105             categorycode => '*',
106             itemtype => $itemtype,
107         })->store, "Given I added an issuing rule branchcode => *, categorycode => *, itemtype => $itemtype,");
108         $rule = Koha::IssuingRules->get_effective_issuing_rule({
109             branchcode   => $branchcode,
110             categorycode => $categorycode,
111             itemtype     => $itemtype,
112         });
113         ok(_row_match($rule, '*', '*', $itemtype), 'When I attempt to get effective issuing rule,'
114            .' then the above one is returned.');
115
116         ok(Koha::IssuingRule->new({
117             branchcode => '*',
118             categorycode => $categorycode,
119             itemtype => '*',
120         })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => *,");
121         $rule = Koha::IssuingRules->get_effective_issuing_rule({
122             branchcode   => $branchcode,
123             categorycode => $categorycode,
124             itemtype     => $itemtype,
125         });
126         ok(_row_match($rule, '*', $categorycode, '*'), 'When I attempt to get effective issuing rule,'
127            .' then the above one is returned.');
128
129         ok(Koha::IssuingRule->new({
130             branchcode => '*',
131             categorycode => $categorycode,
132             itemtype => $itemtype,
133         })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => $itemtype,");
134         $rule = Koha::IssuingRules->get_effective_issuing_rule({
135             branchcode   => $branchcode,
136             categorycode => $categorycode,
137             itemtype     => $itemtype,
138         });
139         ok(_row_match($rule, '*', $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
140            .' then the above one is returned.');
141
142         ok(Koha::IssuingRule->new({
143             branchcode => $branchcode,
144             categorycode => '*',
145             itemtype => '*',
146         })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => '*',");
147         $rule = Koha::IssuingRules->get_effective_issuing_rule({
148             branchcode   => $branchcode,
149             categorycode => $categorycode,
150             itemtype     => $itemtype,
151         });
152         ok(_row_match($rule, $branchcode, '*', '*'), 'When I attempt to get effective issuing rule,'
153            .' then the above one is returned.');
154
155         ok(Koha::IssuingRule->new({
156             branchcode => $branchcode,
157             categorycode => '*',
158             itemtype => $itemtype,
159         })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => $itemtype,");
160         $rule = Koha::IssuingRules->get_effective_issuing_rule({
161             branchcode   => $branchcode,
162             categorycode => $categorycode,
163             itemtype     => $itemtype,
164         });
165         ok(_row_match($rule, $branchcode, '*', $itemtype), 'When I attempt to get effective issuing rule,'
166            .' then the above one is returned.');
167
168         ok(Koha::IssuingRule->new({
169             branchcode => $branchcode,
170             categorycode => $categorycode,
171             itemtype => '*',
172         })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => '*',");
173         $rule = Koha::IssuingRules->get_effective_issuing_rule({
174             branchcode   => $branchcode,
175             categorycode => $categorycode,
176             itemtype     => $itemtype,
177         });
178         ok(_row_match($rule, $branchcode, $categorycode, '*'), 'When I attempt to get effective issuing rule,'
179            .' then the above one is returned.');
180
181         ok(Koha::IssuingRule->new({
182             branchcode => $branchcode,
183             categorycode => $categorycode,
184             itemtype => $itemtype,
185         })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => $itemtype,");
186         $rule = Koha::IssuingRules->get_effective_issuing_rule({
187             branchcode   => $branchcode,
188             categorycode => $categorycode,
189             itemtype     => $itemtype,
190         });
191         ok(_row_match($rule, $branchcode, $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
192            .' then the above one is returned.');
193     };
194
195     subtest 'Performance' => sub {
196         plan tests => 4;
197
198         my $worst_case = timethis(500,
199                     sub { Koha::IssuingRules->get_effective_issuing_rule({
200                             branchcode   => 'nonexistent',
201                             categorycode => 'nonexistent',
202                             itemtype     => 'nonexistent',
203                         });
204                     }
205                 );
206         my $mid_case = timethis(500,
207                     sub { Koha::IssuingRules->get_effective_issuing_rule({
208                             branchcode   => $branchcode,
209                             categorycode => 'nonexistent',
210                             itemtype     => 'nonexistent',
211                         });
212                     }
213                 );
214         my $sec_best_case = timethis(500,
215                     sub { Koha::IssuingRules->get_effective_issuing_rule({
216                             branchcode   => $branchcode,
217                             categorycode => $categorycode,
218                             itemtype     => 'nonexistent',
219                         });
220                     }
221                 );
222         my $best_case = timethis(500,
223                     sub { Koha::IssuingRules->get_effective_issuing_rule({
224                             branchcode   => $branchcode,
225                             categorycode => $categorycode,
226                             itemtype     => $itemtype,
227                         });
228                     }
229                 );
230         ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
231            .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
232            .' times per second.');
233         ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
234            .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
235            .' times per second.');
236         ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
237            .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
238            .' times per second.');
239         ok($best_case, 'In best case, get_effective_issuing_rule finds matching'
240            .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
241            .' times per second.');
242     };
243 };
244
245 subtest 'get_opacitemholds_policy' => sub {
246     plan tests => 4;
247     my $itype = $builder->build_object({ class => 'Koha::ItemTypes' });
248     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
249     my $library = $builder->build_object({ class => 'Koha::Libraries' });
250     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
251     my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
252     my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems', value => { itemtype => $itemtype->itemtype, biblionumber => $biblio->biblionumber } } );
253     my $item = $builder->build_object(
254         {   class  => 'Koha::Items',
255             value  => {
256                 homebranch    => $library->branchcode,
257                 holdingbranch => $library->branchcode,
258                 notforloan    => 0,
259                 itemlost      => 0,
260                 withdrawn     => 0,
261                 biblionumber  => $biblio->biblionumber,
262                 biblioitemnumber => $biblioitem->biblioitemnumber,
263                 itype         => $itype->itemtype,
264             }
265         }
266     );
267
268     Koha::IssuingRules->delete;
269     Koha::IssuingRule->new({categorycode => '*', itemtype => '*',                 branchcode => '*', opacitemholds => "N"})->store;
270     Koha::IssuingRule->new({categorycode => '*', itemtype => $itype->itemtype,    branchcode => '*', opacitemholds => "Y"})->store;
271     Koha::IssuingRule->new({categorycode => '*', itemtype => $itemtype->itemtype, branchcode => '*', opacitemholds => "N"})->store;
272     t::lib::Mocks::mock_preference('item-level_itypes', 1);
273     my $opacitemholds = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
274     is ( $opacitemholds, 'Y', 'Patrons can place a hold on this itype');
275     t::lib::Mocks::mock_preference('item-level_itypes', 0);
276     $opacitemholds = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
277     is ( $opacitemholds, 'N', 'Patrons cannot place a hold on this itemtype');
278
279     Koha::IssuingRules->delete;
280     Koha::IssuingRule->new({categorycode => '*', itemtype => '*',                 branchcode => '*', opacitemholds => "N"})->store;
281     Koha::IssuingRule->new({categorycode => '*', itemtype => $itype->itemtype,    branchcode => '*', opacitemholds => "N"})->store;
282     Koha::IssuingRule->new({categorycode => '*', itemtype => $itemtype->itemtype, branchcode => '*', opacitemholds => "Y"})->store;
283     t::lib::Mocks::mock_preference('item-level_itypes', 1);
284     $opacitemholds = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
285     is ( $opacitemholds, 'N', 'Patrons cannot place a hold on this itype');
286     t::lib::Mocks::mock_preference('item-level_itypes', 0);
287     $opacitemholds = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
288     is ( $opacitemholds, 'Y', 'Patrons can place a hold on this itemtype');
289
290     $patron->delete;
291 };
292
293 subtest 'get_onshelfholds_policy' => sub {
294     plan tests => 3;
295
296     t::lib::Mocks::mock_preference('item-level_itypes', 1);
297     Koha::IssuingRules->delete;
298
299     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
300     my $item = $builder->build_object({ class => 'Koha::Items' });
301
302     is( Koha::IssuingRules->get_onshelfholds_policy({ item => $item, patron => $patron }), undef, 'Should return undef when no rules can be found' );
303     Koha::IssuingRule->new({ categorycode => $patron->categorycode, itemtype => $item->itype, branchcode => '*', onshelfholds => "0" })->store;
304     is( Koha::IssuingRules->get_onshelfholds_policy({ item => $item, patron => $patron }), 0, 'Should be zero' );
305     Koha::IssuingRule->new({ categorycode => $patron->categorycode, itemtype => $item->itype, branchcode => $item->holdingbranch, onshelfholds => "2" })->store;
306     is( Koha::IssuingRules->get_onshelfholds_policy({ item => $item, patron => $patron }), 2, 'Should be two now' );
307 };
308
309 sub _row_match {
310     my ($rule, $branchcode, $categorycode, $itemtype) = @_;
311
312     return $rule->branchcode eq $branchcode && $rule->categorycode eq $categorycode
313             && $rule->itemtype eq $itemtype;
314 }
315
316 $schema->storage->txn_rollback;
317