|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2020 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::Configurations; |
| 26 |
use Koha::Database; |
| 27 |
|
| 28 |
use t::lib::TestBuilder; |
| 29 |
use t::lib::Mocks; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
| 34 |
subtest 'set_config + get_effective_config' => sub { |
| 35 |
|
| 36 |
plan tests => 17; |
| 37 |
|
| 38 |
$schema->storage->txn_begin; |
| 39 |
|
| 40 |
my $category_id = $builder->build_object( { class => 'Koha::Patron::Categories' } )->categorycode; |
| 41 |
my $item_type = $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype; |
| 42 |
my $library_id = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode; |
| 43 |
my $library_id_2 = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode; |
| 44 |
|
| 45 |
my $name = 'smtp_server'; |
| 46 |
my $default_value = 'localhost'; |
| 47 |
|
| 48 |
my $server_1 = 'smtp.demo1.edu'; |
| 49 |
my $server_2 = 'smtp.demo2.edu'; |
| 50 |
my $server_3 = 'smtp.demo3.edu'; |
| 51 |
my $server_4 = 'smtp.demo4.edu'; |
| 52 |
my $server_5 = 'smtp.demo5.edu'; |
| 53 |
my $server_6 = 'smtp.demo6.edu'; |
| 54 |
my $server_7 = 'smtp.demo7.edu'; |
| 55 |
|
| 56 |
my $config; |
| 57 |
|
| 58 |
Koha::Configurations->delete; |
| 59 |
|
| 60 |
throws_ok { Koha::Configurations->get_effective_config } |
| 61 |
'Koha::Exceptions::MissingParameter', |
| 62 |
"Exception should be raised if get_effective_config is called without name parameter"; |
| 63 |
|
| 64 |
$config = Koha::Configurations->get_effective_config( |
| 65 |
{ |
| 66 |
library_id => $library_id, |
| 67 |
category_id => $category_id, |
| 68 |
item_type => $item_type, |
| 69 |
name => $name, |
| 70 |
} |
| 71 |
); |
| 72 |
is( $config, undef, 'Undef should be returned if no rule exist' ); |
| 73 |
|
| 74 |
# Set a default value |
| 75 |
Koha::Configurations->set_config( |
| 76 |
{ |
| 77 |
library_id => undef, |
| 78 |
category_id => undef, |
| 79 |
item_type => undef, |
| 80 |
name => $name, |
| 81 |
value => $default_value, |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
$config = Koha::Configurations->get_effective_config( |
| 86 |
{ |
| 87 |
library_id => undef, |
| 88 |
category_id => undef, |
| 89 |
item_type => undef, |
| 90 |
name => $name, |
| 91 |
} |
| 92 |
); |
| 93 |
is( $config->value, $default_value, 'undef means default' ); |
| 94 |
|
| 95 |
Koha::Configurations->set_config( |
| 96 |
{ |
| 97 |
library_id => undef, |
| 98 |
category_id => undef, |
| 99 |
item_type => $item_type, |
| 100 |
name => $name, |
| 101 |
value => $server_1, |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
$config = Koha::Configurations->get_effective_config( |
| 106 |
{ |
| 107 |
library_id => $library_id, |
| 108 |
category_id => $category_id, |
| 109 |
item_type => $item_type, |
| 110 |
name => $name, |
| 111 |
} |
| 112 |
); |
| 113 |
is( $config->value, $server_1, |
| 114 |
'More specific rule is returned when item_type is given' ); |
| 115 |
|
| 116 |
$config = Koha::Configurations->get_effective_config( |
| 117 |
{ |
| 118 |
library_id => $library_id_2, |
| 119 |
category_id => undef, |
| 120 |
item_type => undef, |
| 121 |
name => $name, |
| 122 |
} |
| 123 |
); |
| 124 |
is( $config->value, $default_value, |
| 125 |
'Default rule is returned if there is no rule for this library_id' ); |
| 126 |
|
| 127 |
Koha::Configurations->set_config( |
| 128 |
{ |
| 129 |
library_id => undef, |
| 130 |
category_id => $category_id, |
| 131 |
item_type => undef, |
| 132 |
name => $name, |
| 133 |
value => $server_2, |
| 134 |
} |
| 135 |
); |
| 136 |
|
| 137 |
$config = Koha::Configurations->get_effective_config( |
| 138 |
{ |
| 139 |
|
| 140 |
library_id => $library_id, |
| 141 |
category_id => $category_id, |
| 142 |
item_type => $item_type, |
| 143 |
name => $name, |
| 144 |
} |
| 145 |
); |
| 146 |
is( $config->value, $server_2, |
| 147 |
'More specific rule is returned when category_id exists' ); |
| 148 |
|
| 149 |
Koha::Configurations->set_config( |
| 150 |
{ |
| 151 |
library_id => undef, |
| 152 |
category_id => $category_id, |
| 153 |
item_type => $item_type, |
| 154 |
name => $name, |
| 155 |
value => $server_3, |
| 156 |
} |
| 157 |
); |
| 158 |
$config = Koha::Configurations->get_effective_config( |
| 159 |
{ |
| 160 |
library_id => $library_id, |
| 161 |
category_id => $category_id, |
| 162 |
item_type => $item_type, |
| 163 |
name => $name, |
| 164 |
} |
| 165 |
); |
| 166 |
is( $config->value, $server_3, |
| 167 |
'More specific rule is returned when category_id and item_type exist' ); |
| 168 |
|
| 169 |
Koha::Configurations->set_config( |
| 170 |
{ |
| 171 |
library_id => $library_id, |
| 172 |
category_id => undef, |
| 173 |
item_type => undef, |
| 174 |
name => $name, |
| 175 |
value => $server_4, |
| 176 |
} |
| 177 |
); |
| 178 |
$config = Koha::Configurations->get_effective_config( |
| 179 |
{ |
| 180 |
library_id => $library_id, |
| 181 |
category_id => $category_id, |
| 182 |
item_type => $item_type, |
| 183 |
name => $name, |
| 184 |
} |
| 185 |
); |
| 186 |
is( $config->value, $server_4, |
| 187 |
'More specific rule is returned when library_id exists' ); |
| 188 |
|
| 189 |
Koha::Configurations->set_config( |
| 190 |
{ |
| 191 |
library_id => $library_id, |
| 192 |
category_id => undef, |
| 193 |
item_type => $item_type, |
| 194 |
name => $name, |
| 195 |
value => $server_5, |
| 196 |
} |
| 197 |
); |
| 198 |
$config = Koha::Configurations->get_effective_config( |
| 199 |
{ |
| 200 |
library_id => $library_id, |
| 201 |
category_id => $category_id, |
| 202 |
item_type => $item_type, |
| 203 |
name => $name, |
| 204 |
} |
| 205 |
); |
| 206 |
is( $config->value, $server_5, |
| 207 |
'More specific rule is returned when library_id and item_type exists' ); |
| 208 |
|
| 209 |
Koha::Configurations->set_config( |
| 210 |
{ |
| 211 |
library_id => $library_id, |
| 212 |
category_id => $category_id, |
| 213 |
item_type => undef, |
| 214 |
name => $name, |
| 215 |
value => $server_6, |
| 216 |
} |
| 217 |
); |
| 218 |
$config = Koha::Configurations->get_effective_config( |
| 219 |
{ |
| 220 |
library_id => $library_id, |
| 221 |
category_id => $category_id, |
| 222 |
item_type => $item_type, |
| 223 |
name => $name, |
| 224 |
} |
| 225 |
); |
| 226 |
is( $config->value, $server_6, |
| 227 |
'More specific rule is returned when library_id and category_id exist' |
| 228 |
); |
| 229 |
|
| 230 |
Koha::Configurations->set_config( |
| 231 |
{ |
| 232 |
library_id => $library_id, |
| 233 |
category_id => $category_id, |
| 234 |
item_type => $item_type, |
| 235 |
name => $name, |
| 236 |
value => $server_7, |
| 237 |
} |
| 238 |
); |
| 239 |
$config = Koha::Configurations->get_effective_config( |
| 240 |
{ |
| 241 |
library_id => $library_id, |
| 242 |
category_id => $category_id, |
| 243 |
item_type => $item_type, |
| 244 |
name => $name, |
| 245 |
} |
| 246 |
); |
| 247 |
is( $config->value, $server_7, |
| 248 |
'More specific rule is returned when library_id, category_id and item_type exist' |
| 249 |
); |
| 250 |
|
| 251 |
my $library_configs = Koha::Configurations->search({ library_id => $library_id }); |
| 252 |
is( $library_configs->count, 4, "We added 8 rules"); |
| 253 |
$library_configs->delete; |
| 254 |
is( $library_configs->count, 0, "We deleted 8 rules"); |
| 255 |
|
| 256 |
throws_ok { |
| 257 |
Koha::Configurations->set_config( |
| 258 |
{ |
| 259 |
library_id => $library_id, |
| 260 |
category_id => $category_id, |
| 261 |
item_type => $item_type, |
| 262 |
value => $server_7, |
| 263 |
} |
| 264 |
); |
| 265 |
} 'Koha::Exceptions::MissingParameter', 'Exceptions thrown on missing parameter'; |
| 266 |
|
| 267 |
is( "$@", "Required parameter 'name' missing", "Expected exception message" ); |
| 268 |
|
| 269 |
throws_ok { |
| 270 |
Koha::Configurations->set_config( |
| 271 |
{ |
| 272 |
library_id => $library_id, |
| 273 |
category_id => $category_id, |
| 274 |
item_type => $item_type, |
| 275 |
name => $name, |
| 276 |
} |
| 277 |
); |
| 278 |
} 'Koha::Exceptions::MissingParameter', 'Exceptions thrown on missing parameter'; |
| 279 |
|
| 280 |
is( "$@", "Required parameter 'value' missing", "Expected exception message" ); |
| 281 |
|
| 282 |
$schema->storage->txn_rollback; |
| 283 |
}; |