|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 4; |
| 21 |
use Test::Exception; |
| 22 |
|
| 23 |
use Koha::SMTP::Servers; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
use t::lib::Mocks; |
| 27 |
|
| 28 |
my $schema = Koha::Database->new->schema; |
| 29 |
my $builder = t::lib::TestBuilder->new; |
| 30 |
|
| 31 |
subtest 'get_default() tests' => sub { |
| 32 |
|
| 33 |
plan tests => 3; |
| 34 |
|
| 35 |
$schema->storage->txn_begin; |
| 36 |
|
| 37 |
Koha::SMTP::Servers->search->delete; |
| 38 |
|
| 39 |
my $default_server = $builder->build_object( |
| 40 |
{ |
| 41 |
class => 'Koha::SMTP::Servers', |
| 42 |
value => { |
| 43 |
library_id => undef |
| 44 |
} |
| 45 |
} |
| 46 |
); |
| 47 |
|
| 48 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 49 |
my $library_specific_server = $builder->build_object( |
| 50 |
{ |
| 51 |
class => 'Koha::SMTP::Servers', |
| 52 |
value => { |
| 53 |
library_id => $library->branchcode |
| 54 |
} |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
my $servers = Koha::SMTP::Servers->new; |
| 59 |
my $server = $servers->get_default; |
| 60 |
is( $server->id, $default_server->id, |
| 61 |
'The default server is correctly retrieved' ); |
| 62 |
|
| 63 |
# Delete the default server |
| 64 |
$server->delete; |
| 65 |
|
| 66 |
# Get the default |
| 67 |
$default_server = $servers->get_default; |
| 68 |
is( ref($default_server), 'Koha::SMTP::Server', |
| 69 |
'An object of the right type is returned' ); |
| 70 |
|
| 71 |
my $unblessed_server = $default_server->unblessed; |
| 72 |
delete $unblessed_server->{id}; |
| 73 |
is_deeply( |
| 74 |
$unblessed_server, |
| 75 |
Koha::SMTP::Servers::default_setting, |
| 76 |
'The default setting is returned if no user-defined default' |
| 77 |
); |
| 78 |
|
| 79 |
$schema->storage->txn_rollback; |
| 80 |
}; |
| 81 |
|
| 82 |
subtest 'set_default() tests' => sub { |
| 83 |
|
| 84 |
plan tests => 4; |
| 85 |
|
| 86 |
$schema->storage->txn_begin; |
| 87 |
|
| 88 |
Koha::SMTP::Servers->search->delete; |
| 89 |
|
| 90 |
my $default_server = $builder->build_object( |
| 91 |
{ |
| 92 |
class => 'Koha::SMTP::Servers', |
| 93 |
value => { |
| 94 |
library_id => undef |
| 95 |
} |
| 96 |
} |
| 97 |
); |
| 98 |
|
| 99 |
throws_ok { |
| 100 |
Koha::SMTP::Servers->new->set_default( |
| 101 |
{ |
| 102 |
name => 'A new default', |
| 103 |
library_id => 'Whatever', |
| 104 |
} |
| 105 |
); |
| 106 |
} |
| 107 |
'Koha::Exceptions::BadParameter', |
| 108 |
'Exception thrown when trying to set default SMTP server with a library_id'; |
| 109 |
|
| 110 |
is( |
| 111 |
"$@", |
| 112 |
'library_id must be undef when setting the default SMTP server', |
| 113 |
'Exception message is clear' |
| 114 |
); |
| 115 |
|
| 116 |
my $new_default = Koha::SMTP::Servers->new->set_default( |
| 117 |
{ |
| 118 |
name => 'A new default', |
| 119 |
library_id => undef, |
| 120 |
} |
| 121 |
); |
| 122 |
|
| 123 |
is( ref($new_default), 'Koha::SMTP::Server', 'Type is correct' ); |
| 124 |
is( |
| 125 |
$new_default->id, |
| 126 |
Koha::SMTP::Servers->get_default->id, |
| 127 |
'Default SMTP server is correctly set' |
| 128 |
); |
| 129 |
|
| 130 |
$schema->storage->txn_rollback; |
| 131 |
}; |
| 132 |
|
| 133 |
subtest 'get_effective_server() tests' => sub { |
| 134 |
|
| 135 |
plan tests => 4; |
| 136 |
|
| 137 |
$schema->storage->txn_begin; |
| 138 |
|
| 139 |
Koha::SMTP::Servers->search->delete; |
| 140 |
|
| 141 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 142 |
|
| 143 |
my $default_server = $builder->build_object( |
| 144 |
{ |
| 145 |
class => 'Koha::SMTP::Servers', |
| 146 |
value => { |
| 147 |
library_id => undef |
| 148 |
} |
| 149 |
} |
| 150 |
); |
| 151 |
|
| 152 |
throws_ok { Koha::SMTP::Servers->new->get_effective_server() } |
| 153 |
'Koha::Exceptions::MissingParameter', 'Exception thrown'; |
| 154 |
|
| 155 |
is( "$@", 'Mandatory parameter missing: library' ); |
| 156 |
|
| 157 |
is( |
| 158 |
Koha::SMTP::Servers->new->get_effective_server( |
| 159 |
{ library => $library } |
| 160 |
)->id, |
| 161 |
$default_server->id, |
| 162 |
'Fallback default server retrieved' |
| 163 |
); |
| 164 |
|
| 165 |
my $specific_server = $builder->build_object( |
| 166 |
{ |
| 167 |
class => 'Koha::SMTP::Servers', |
| 168 |
value => { |
| 169 |
library_id => $library->branchcode |
| 170 |
} |
| 171 |
} |
| 172 |
); |
| 173 |
|
| 174 |
is( |
| 175 |
Koha::SMTP::Servers->new->get_effective_server( |
| 176 |
{ library => $library } |
| 177 |
)->id, |
| 178 |
$specific_server->id, |
| 179 |
'Library specific server retrieved' |
| 180 |
); |
| 181 |
|
| 182 |
$schema->storage->txn_rollback; |
| 183 |
}; |
| 184 |
|
| 185 |
subtest 'set_library_server() tests' => sub { |
| 186 |
|
| 187 |
plan tests => 6; |
| 188 |
|
| 189 |
$schema->storage->txn_begin; |
| 190 |
|
| 191 |
Koha::SMTP::Servers->search->delete; |
| 192 |
|
| 193 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 194 |
|
| 195 |
my $default_server = $builder->build_object( |
| 196 |
{ |
| 197 |
class => 'Koha::SMTP::Servers', |
| 198 |
value => { |
| 199 |
library_id => undef |
| 200 |
} |
| 201 |
} |
| 202 |
); |
| 203 |
|
| 204 |
is( |
| 205 |
Koha::SMTP::Servers->new->get_effective_server( |
| 206 |
{ library => $library } |
| 207 |
)->id, |
| 208 |
$default_server->id, |
| 209 |
'Fallback default server retrieved' |
| 210 |
); |
| 211 |
|
| 212 |
my $specific_server = Koha::SMTP::Servers->new->set_library_server( |
| 213 |
{ |
| 214 |
name => 'Specific server 1', |
| 215 |
library_id => $library->id |
| 216 |
} |
| 217 |
); |
| 218 |
|
| 219 |
is( |
| 220 |
Koha::SMTP::Servers->new->get_effective_server( |
| 221 |
{ library => $library } |
| 222 |
)->id, |
| 223 |
$specific_server->id, |
| 224 |
'Library specific server retrieved' |
| 225 |
); |
| 226 |
|
| 227 |
throws_ok { |
| 228 |
Koha::SMTP::Servers->new->set_library_server( |
| 229 |
{ |
| 230 |
name => 'Specific server 2' |
| 231 |
} |
| 232 |
); |
| 233 |
} |
| 234 |
'Koha::Exceptions::MissingParameter', |
| 235 |
'Exception thrown on missing parameter'; |
| 236 |
|
| 237 |
is( "$@", 'Mandatory parameter missing: library_id' ); |
| 238 |
|
| 239 |
$specific_server = Koha::SMTP::Servers->new->set_library_server( |
| 240 |
{ |
| 241 |
name => 'Specific server 2', |
| 242 |
library_id => $library->id |
| 243 |
} |
| 244 |
); |
| 245 |
|
| 246 |
is( |
| 247 |
Koha::SMTP::Servers->new->get_effective_server( |
| 248 |
{ library => $library } |
| 249 |
)->id, |
| 250 |
$specific_server->id, |
| 251 |
'New library specific server retrieved' |
| 252 |
); |
| 253 |
|
| 254 |
is( Koha::SMTP::Servers->search( { library_id => $library->id } )->count, |
| 255 |
1, 'Only one SMTP server set' ); |
| 256 |
|
| 257 |
$schema->storage->txn_rollback; |
| 258 |
}; |