|
Line 0
Link Here
|
| 0 |
- |
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 => 1; |
| 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 |
$schema->storage->txn_begin; |
| 32 |
|
| 33 |
my $builder = t::lib::TestBuilder->new; |
| 34 |
|
| 35 |
subtest 'set_rule + get_effective_rule' => sub { |
| 36 |
plan tests => 11; |
| 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 $rule_name = 'my_rule'; |
| 42 |
my $default_rule_value = 1; |
| 43 |
|
| 44 |
my $rule; |
| 45 |
Koha::CirculationRules->delete; |
| 46 |
|
| 47 |
throws_ok { Koha::CirculationRules->get_effective_rule } |
| 48 |
'Koha::Exceptions::MissingParameter', |
| 49 |
"Exception should be raised if get_effective_rule is called without rule_name parameter"; |
| 50 |
|
| 51 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 52 |
{ |
| 53 |
branchcode => $branchcode, |
| 54 |
categorycode => $categorycode, |
| 55 |
itemtype => $itemtype, |
| 56 |
rule_name => $rule_name, |
| 57 |
} |
| 58 |
); |
| 59 |
is( $rule, undef, 'Undef should be returned if no rule exist' ); |
| 60 |
|
| 61 |
Koha::CirculationRules->set_rule( |
| 62 |
{ |
| 63 |
branchcode => '*', |
| 64 |
categorycode => '*', |
| 65 |
itemtype => '*', |
| 66 |
rule_name => $rule_name, |
| 67 |
rule_value => $default_rule_value, |
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 72 |
{ |
| 73 |
branchcode => undef, |
| 74 |
categorycode => undef, |
| 75 |
itemtype => undef, |
| 76 |
rule_name => $rule_name, |
| 77 |
} |
| 78 |
); |
| 79 |
is( $rule->rule_value, $default_rule_value, 'undef means default' ); |
| 80 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 81 |
{ |
| 82 |
branchcode => '*', |
| 83 |
categorycode => '*', |
| 84 |
itemtype => '*', |
| 85 |
rule_name => $rule_name, |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
is( $rule->rule_value, $default_rule_value, '* means default' ); |
| 90 |
|
| 91 |
Koha::CirculationRules->set_rule( |
| 92 |
{ |
| 93 |
branchcode => '*', |
| 94 |
categorycode => '*', |
| 95 |
itemtype => $itemtype, |
| 96 |
rule_name => $rule_name, |
| 97 |
rule_value => 2, |
| 98 |
} |
| 99 |
); |
| 100 |
|
| 101 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 102 |
{ |
| 103 |
branchcode => $branchcode, |
| 104 |
categorycode => $categorycode, |
| 105 |
itemtype => $itemtype, |
| 106 |
rule_name => $rule_name, |
| 107 |
} |
| 108 |
); |
| 109 |
is( $rule->rule_value, 2, |
| 110 |
'More specific rule is returned when itemtype is given' ); |
| 111 |
|
| 112 |
Koha::CirculationRules->set_rule( |
| 113 |
{ |
| 114 |
branchcode => '*', |
| 115 |
categorycode => $categorycode, |
| 116 |
itemtype => '*', |
| 117 |
rule_name => $rule_name, |
| 118 |
rule_value => 3, |
| 119 |
} |
| 120 |
); |
| 121 |
|
| 122 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 123 |
{ |
| 124 |
|
| 125 |
branchcode => $branchcode, |
| 126 |
categorycode => $categorycode, |
| 127 |
itemtype => $itemtype, |
| 128 |
rule_name => $rule_name, |
| 129 |
} |
| 130 |
); |
| 131 |
is( $rule->rule_value, 3, |
| 132 |
'More specific rule is returned when categorycode exists' ); |
| 133 |
|
| 134 |
Koha::CirculationRules->set_rule( |
| 135 |
{ |
| 136 |
branchcode => '*', |
| 137 |
categorycode => $categorycode, |
| 138 |
itemtype => $itemtype, |
| 139 |
rule_name => $rule_name, |
| 140 |
rule_value => 4, |
| 141 |
} |
| 142 |
); |
| 143 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 144 |
{ |
| 145 |
branchcode => $branchcode, |
| 146 |
categorycode => $categorycode, |
| 147 |
itemtype => $itemtype, |
| 148 |
rule_name => $rule_name, |
| 149 |
} |
| 150 |
); |
| 151 |
is( $rule->rule_value, 4, |
| 152 |
'More specific rule is returned when categorycode and itemtype exist' ); |
| 153 |
|
| 154 |
Koha::CirculationRules->set_rule( |
| 155 |
{ |
| 156 |
branchcode => $branchcode, |
| 157 |
categorycode => '*', |
| 158 |
itemtype => '*', |
| 159 |
rule_name => $rule_name, |
| 160 |
rule_value => 5, |
| 161 |
} |
| 162 |
); |
| 163 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 164 |
{ |
| 165 |
branchcode => $branchcode, |
| 166 |
categorycode => $categorycode, |
| 167 |
itemtype => $itemtype, |
| 168 |
rule_name => $rule_name, |
| 169 |
} |
| 170 |
); |
| 171 |
is( $rule->rule_value, 5, |
| 172 |
'More specific rule is returned when branchcode exists' ); |
| 173 |
|
| 174 |
Koha::CirculationRules->set_rule( |
| 175 |
{ |
| 176 |
branchcode => $branchcode, |
| 177 |
categorycode => '*', |
| 178 |
itemtype => $itemtype, |
| 179 |
rule_name => $rule_name, |
| 180 |
rule_value => 6, |
| 181 |
} |
| 182 |
); |
| 183 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 184 |
{ |
| 185 |
branchcode => $branchcode, |
| 186 |
categorycode => $categorycode, |
| 187 |
itemtype => $itemtype, |
| 188 |
rule_name => $rule_name, |
| 189 |
} |
| 190 |
); |
| 191 |
is( $rule->rule_value, 6, |
| 192 |
'More specific rule is returned when branchcode and itemtype exists' ); |
| 193 |
|
| 194 |
Koha::CirculationRules->set_rule( |
| 195 |
{ |
| 196 |
branchcode => $branchcode, |
| 197 |
categorycode => $categorycode, |
| 198 |
itemtype => '*', |
| 199 |
rule_name => $rule_name, |
| 200 |
rule_value => 7, |
| 201 |
} |
| 202 |
); |
| 203 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 204 |
{ |
| 205 |
branchcode => $branchcode, |
| 206 |
categorycode => $categorycode, |
| 207 |
itemtype => $itemtype, |
| 208 |
rule_name => $rule_name, |
| 209 |
} |
| 210 |
); |
| 211 |
is( $rule->rule_value, 7, |
| 212 |
'More specific rule is returned when branchcode and categorycode exist' |
| 213 |
); |
| 214 |
|
| 215 |
Koha::CirculationRules->set_rule( |
| 216 |
{ |
| 217 |
branchcode => $branchcode, |
| 218 |
categorycode => $categorycode, |
| 219 |
itemtype => $itemtype, |
| 220 |
rule_name => $rule_name, |
| 221 |
rule_value => 8, |
| 222 |
} |
| 223 |
); |
| 224 |
$rule = Koha::CirculationRules->get_effective_rule( |
| 225 |
{ |
| 226 |
branchcode => $branchcode, |
| 227 |
categorycode => $categorycode, |
| 228 |
itemtype => $itemtype, |
| 229 |
rule_name => $rule_name, |
| 230 |
} |
| 231 |
); |
| 232 |
is( $rule->rule_value, 8, |
| 233 |
'More specific rule is returned when branchcode, categorycode and itemtype exist' |
| 234 |
); |
| 235 |
}; |
| 236 |
|
| 237 |
$schema->storage->txn_rollback; |