|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright 2017 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 => 7; |
| 23 |
|
| 24 |
use t::lib::Mocks; |
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 27 |
use C4::Context; |
| 28 |
|
| 29 |
use Koha::Notice::Templates; |
| 30 |
use Koha::Patron::Categories; |
| 31 |
use Koha::Patron::Message::Attributes; |
| 32 |
use Koha::Patron::Message::Transport::Types; |
| 33 |
use Koha::Patron::Message::Transports; |
| 34 |
use Koha::Patrons; |
| 35 |
|
| 36 |
use File::Temp qw/tempfile/; |
| 37 |
use Log::Log4perl; |
| 38 |
|
| 39 |
my $schema = Koha::Database->new->schema; |
| 40 |
my $builder = t::lib::TestBuilder->new; |
| 41 |
|
| 42 |
subtest 'Test class imports' => sub { |
| 43 |
plan tests => 2; |
| 44 |
|
| 45 |
use_ok('Koha::Patron::Message::Preference'); |
| 46 |
use_ok('Koha::Patron::Message::Preferences'); |
| 47 |
}; |
| 48 |
|
| 49 |
subtest 'Test Koha::Patron::Message::Preferences' => sub { |
| 50 |
plan tests => 2; |
| 51 |
|
| 52 |
$schema->storage->txn_begin; |
| 53 |
|
| 54 |
my $attribute = build_a_test_attribute(); |
| 55 |
my $letter = build_a_test_letter(); |
| 56 |
my $mtt = build_a_test_transport_type(); |
| 57 |
Koha::Patron::Message::Transport->new({ |
| 58 |
message_attribute_id => $attribute->message_attribute_id, |
| 59 |
message_transport_type => $mtt->message_transport_type, |
| 60 |
is_digest => 0, |
| 61 |
letter_module => $letter->module, |
| 62 |
letter_code => $letter->code, |
| 63 |
})->store; |
| 64 |
|
| 65 |
subtest 'Test for a patron' => sub { |
| 66 |
plan tests => 3; |
| 67 |
|
| 68 |
my $patron = build_a_test_patron(); |
| 69 |
Koha::Patron::Message::Preference->new({ |
| 70 |
borrowernumber => $patron->borrowernumber, |
| 71 |
message_attribute_id => $attribute->message_attribute_id, |
| 72 |
wants_digest => 0, |
| 73 |
days_in_advance => undef, |
| 74 |
})->store; |
| 75 |
|
| 76 |
my $preference = Koha::Patron::Message::Preferences->find({ |
| 77 |
borrowernumber => $patron->borrowernumber, |
| 78 |
message_attribute_id => $attribute->message_attribute_id |
| 79 |
}); |
| 80 |
ok($preference->borrower_message_preference_id > 0, |
| 81 |
'Added a new messaging preference for patron.'); |
| 82 |
|
| 83 |
subtest 'Test set not throwing an exception on duplicate object' => sub { |
| 84 |
plan tests => 1; |
| 85 |
|
| 86 |
Koha::Patron::Message::Attributes->find({ |
| 87 |
message_attribute_id => $attribute->message_attribute_id |
| 88 |
})->set({ takes_days => 1 })->store; |
| 89 |
$preference->set({ days_in_advance => 1 })->store; |
| 90 |
is(ref($preference), 'Koha::Patron::Message::Preference', |
| 91 |
'Updating the preference does not cause duplicate object exception'); |
| 92 |
}; |
| 93 |
|
| 94 |
$preference->delete; |
| 95 |
is(Koha::Patron::Message::Preferences->search({ |
| 96 |
borrowernumber => $patron->borrowernumber, |
| 97 |
message_attribute_id => $attribute->message_attribute_id |
| 98 |
})->count, 0, 'Deleted the messaging preference.'); |
| 99 |
}; |
| 100 |
|
| 101 |
subtest 'Test for a category' => sub { |
| 102 |
my $category = build_a_test_category(); |
| 103 |
Koha::Patron::Message::Preference->new({ |
| 104 |
categorycode => $category->categorycode, |
| 105 |
message_attribute_id => $attribute->message_attribute_id, |
| 106 |
wants_digest => 0, |
| 107 |
days_in_advance => undef, |
| 108 |
})->store; |
| 109 |
|
| 110 |
my $preference = Koha::Patron::Message::Preferences->find({ |
| 111 |
categorycode => $category->categorycode, |
| 112 |
message_attribute_id => $attribute->message_attribute_id |
| 113 |
}); |
| 114 |
ok($preference->borrower_message_preference_id > 0, |
| 115 |
'Added a new messaging preference for category.'); |
| 116 |
|
| 117 |
$preference->delete; |
| 118 |
is(Koha::Patron::Message::Preferences->search({ |
| 119 |
categorycode => $category->categorycode, |
| 120 |
message_attribute_id => $attribute->message_attribute_id |
| 121 |
})->count, 0, 'Deleted the messaging preference.'); |
| 122 |
}; |
| 123 |
|
| 124 |
$schema->storage->txn_rollback; |
| 125 |
}; |
| 126 |
|
| 127 |
subtest 'Test Koha::Patron::Message::Preferences->get_options' => sub { |
| 128 |
plan tests => 2; |
| 129 |
|
| 130 |
subtest 'Test method availability and return value' => sub { |
| 131 |
plan tests => 3; |
| 132 |
|
| 133 |
ok(Koha::Patron::Message::Preferences->can('get_options'), |
| 134 |
'Method get_options is available.'); |
| 135 |
ok(my $options = Koha::Patron::Message::Preferences->get_options, |
| 136 |
'Called get_options successfully.'); |
| 137 |
is(ref($options), 'ARRAY', 'get_options returns a ARRAYref'); |
| 138 |
}; |
| 139 |
|
| 140 |
subtest 'Make sure options are correct' => sub { |
| 141 |
$schema->storage->txn_begin; |
| 142 |
my $options = Koha::Patron::Message::Preferences->get_options; |
| 143 |
|
| 144 |
foreach my $option (@$options) { |
| 145 |
my $n = $option->{'message_name'}; |
| 146 |
my $attr = Koha::Patron::Message::Attributes->find($option->{'message_attribute_id'}); |
| 147 |
is($option->{'message_attribute_id'}, $attr->message_attribute_id, |
| 148 |
'$n: message_attribute_id is set'); |
| 149 |
is($option->{'message_name'}, $attr->message_name, '$n: message_name is set'); |
| 150 |
is($option->{'takes_days'}, $attr->takes_days, '$n: takes_days is set'); |
| 151 |
my $transports = Koha::Patron::Message::Transports->search({ |
| 152 |
message_attribute_id => $option->{'message_attribute_id'}, |
| 153 |
is_digest => $option->{'has_digest'} || 0, |
| 154 |
}); |
| 155 |
while (my $trnzport = $transports->next) { |
| 156 |
is($option->{'has_digest'} || 0, $trnzport->is_digest, '$n: has_digest is set for '.$trnzport->message_transport_type); |
| 157 |
is($option->{'transport_'.$trnzport->message_transport_type}, ' ', '$n: transport_'.$trnzport->message_transport_type.' is set'); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
$schema->storage->txn_rollback; |
| 162 |
}; |
| 163 |
}; |
| 164 |
|
| 165 |
subtest 'Add preferences from defaults' => sub { |
| 166 |
plan tests => 3; |
| 167 |
|
| 168 |
$schema->storage->txn_begin; |
| 169 |
|
| 170 |
my $patron = build_a_test_patron(); |
| 171 |
my ($default, $mtt1, $mtt2) = build_a_test_category_preference({ |
| 172 |
patron => $patron, |
| 173 |
}); |
| 174 |
ok(Koha::Patron::Message::Preference->new_from_default({ |
| 175 |
borrowernumber => $patron->borrowernumber, |
| 176 |
categorycode => $patron->categorycode, |
| 177 |
message_attribute_id => $default->message_attribute_id, |
| 178 |
})->store, 'Added a default preference to patron.'); |
| 179 |
ok(my $pref = Koha::Patron::Message::Preferences->find({ |
| 180 |
borrowernumber => $patron->borrowernumber, |
| 181 |
message_attribute_id => $default->message_attribute_id, |
| 182 |
}), 'Found the default preference from patron.'); |
| 183 |
is(Koha::Patron::Message::Transport::Preferences->search({ |
| 184 |
borrower_message_preference_id => $pref->borrower_message_preference_id |
| 185 |
})->count, 2, 'Found the two transport types that we set earlier'); |
| 186 |
|
| 187 |
$schema->storage->txn_rollback; |
| 188 |
}; |
| 189 |
|
| 190 |
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub { |
| 191 |
plan tests => 4; |
| 192 |
|
| 193 |
ok(Koha::Patron::Message::Preference->can('message_transport_types'), |
| 194 |
'Method message_transport_types available'); |
| 195 |
|
| 196 |
subtest 'get message_transport_types' => sub { |
| 197 |
plan tests => 5; |
| 198 |
|
| 199 |
$schema->storage->txn_begin; |
| 200 |
|
| 201 |
my $patron = build_a_test_patron(); |
| 202 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
| 203 |
patron => $patron |
| 204 |
}); |
| 205 |
Koha::Patron::Message::Transport::Preferences->search({ |
| 206 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 207 |
})->delete; |
| 208 |
Koha::Patron::Message::Transport::Preference->new({ |
| 209 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 210 |
message_transport_type => $mtt1->message_transport_type, |
| 211 |
})->store; |
| 212 |
Koha::Patron::Message::Transport::Preference->new({ |
| 213 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 214 |
message_transport_type => $mtt2->message_transport_type, |
| 215 |
})->store; |
| 216 |
my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 217 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 218 |
}); |
| 219 |
my $transport1 = Koha::Patron::Message::Transports->find({ |
| 220 |
message_attribute_id => $preference->message_attribute_id, |
| 221 |
message_transport_type => $mtt1->message_transport_type, |
| 222 |
}); |
| 223 |
my $transport2 = Koha::Patron::Message::Transports->find({ |
| 224 |
message_attribute_id => $preference->message_attribute_id, |
| 225 |
message_transport_type => $mtt2->message_transport_type, |
| 226 |
}); |
| 227 |
my $transports = $preference->message_transport_types; |
| 228 |
is(keys %{$transports}, $stored_transports->count, |
| 229 |
'->message_transport_types gets correct amount of transport types.'); |
| 230 |
is($transports->{$stored_transports->next->message_transport_type}, |
| 231 |
$transport1->letter_code, 'Found correct message transport type and letter code.'); |
| 232 |
is($transports->{$stored_transports->next->message_transport_type}, |
| 233 |
$transport2->letter_code, 'Found correct message transport type and letter code.'); |
| 234 |
ok(!$preference->message_transport_types->{'nonexistent'}, |
| 235 |
'Didn\'t find nonexistent transport type.'); |
| 236 |
|
| 237 |
subtest 'test logging of warnings by invalid message transport type' => sub { |
| 238 |
plan tests => 2; |
| 239 |
|
| 240 |
my $log = mytempfile(); |
| 241 |
my $conf = mytempfile( <<"HERE" |
| 242 |
log4perl.logger.opac = WARN, OPAC |
| 243 |
log4perl.appender.OPAC=Log::Log4perl::Appender::TestBuffer |
| 244 |
log4perl.appender.OPAC.filename=$log |
| 245 |
log4perl.appender.OPAC.mode=append |
| 246 |
log4perl.appender.OPAC.layout=SimpleLayout |
| 247 |
log4perl.logger.intranet = WARN, INTRANET |
| 248 |
log4perl.appender.INTRANET=Log::Log4perl::Appender::TestBuffer |
| 249 |
log4perl.appender.INTRANET.filename=$log |
| 250 |
log4perl.appender.INTRANET.mode=append |
| 251 |
log4perl.appender.INTRANET.layout=SimpleLayout |
| 252 |
HERE |
| 253 |
); |
| 254 |
t::lib::Mocks::mock_config('log4perl_conf', $conf); |
| 255 |
my $appenders = Log::Log4perl->appenders; |
| 256 |
my $appender = Log::Log4perl->appenders->{OPAC}; |
| 257 |
|
| 258 |
my $pref = Koha::Patron::Message::Preferences->find( |
| 259 |
$preference->borrower_message_preference_id |
| 260 |
); |
| 261 |
my $transports = $pref->message_transport_types; |
| 262 |
is($appender, undef, 'Nothing in buffer yet'); |
| 263 |
|
| 264 |
my $mtt_new = build_a_test_transport_type(); |
| 265 |
Koha::Patron::Message::Transport::Preference->new({ |
| 266 |
borrower_message_preference_id => |
| 267 |
$pref->borrower_message_preference_id, |
| 268 |
message_transport_type => $mtt_new->message_transport_type, |
| 269 |
})->store; |
| 270 |
$pref = Koha::Patron::Message::Preferences->find( |
| 271 |
$pref->borrower_message_preference_id |
| 272 |
); |
| 273 |
$transports = $pref->message_transport_types; |
| 274 |
$appender = Log::Log4perl->appenders->{OPAC}; |
| 275 |
my $name = $pref->message_name; |
| 276 |
my $tt = $mtt_new->message_transport_type; |
| 277 |
like($appender->buffer, qr/WARN - $name has no transport with $tt/, |
| 278 |
'Logged invalid message transport type'); |
| 279 |
}; |
| 280 |
|
| 281 |
$schema->storage->txn_rollback; |
| 282 |
}; |
| 283 |
|
| 284 |
subtest 'set message_transport_types' => sub { |
| 285 |
plan tests => 6; |
| 286 |
|
| 287 |
$schema->storage->txn_begin; |
| 288 |
|
| 289 |
my $patron = build_a_test_patron(); |
| 290 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
| 291 |
patron => $patron |
| 292 |
}); |
| 293 |
|
| 294 |
my $mtt1_str = $mtt1->message_transport_type; |
| 295 |
my $mtt2_str = $mtt2->message_transport_type; |
| 296 |
# 1/3, use message_transport_types(list) |
| 297 |
Koha::Patron::Message::Transport::Preferences->search({ |
| 298 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 299 |
})->delete; |
| 300 |
ok($preference->message_transport_types($mtt1_str, $mtt2_str)->store, |
| 301 |
'1/3 Set returned true.'); |
| 302 |
my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 303 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 304 |
'-or' => [ |
| 305 |
message_transport_type => $mtt1_str, |
| 306 |
message_transport_type => $mtt2_str |
| 307 |
] |
| 308 |
}); |
| 309 |
is($stored_transports->count, 2, 'Two transports selected'); |
| 310 |
|
| 311 |
# 2/3, use message_transport_types(ARRAYREF) |
| 312 |
Koha::Patron::Message::Transport::Preferences->search({ |
| 313 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 314 |
})->delete; |
| 315 |
ok($preference->message_transport_types([$mtt1_str, $mtt2_str])->store, |
| 316 |
'2/3 Set returned true.'); |
| 317 |
$stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 318 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 319 |
'-or' => [ |
| 320 |
message_transport_type => $mtt1_str, |
| 321 |
message_transport_type => $mtt2_str |
| 322 |
] |
| 323 |
}); |
| 324 |
is($stored_transports->count, 2, 'Two transports selected'); |
| 325 |
|
| 326 |
# 3/3, use set({ message_transport_types => ARRAYREF }) |
| 327 |
Koha::Patron::Message::Transport::Preferences->search({ |
| 328 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 329 |
})->delete; |
| 330 |
ok($preference->set({ |
| 331 |
message_transport_types => [$mtt1_str, $mtt2_str]})->store, |
| 332 |
'3/3 Set returned true.'); |
| 333 |
$stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 334 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 335 |
'-or' => [ |
| 336 |
message_transport_type => $mtt1_str, |
| 337 |
message_transport_type => $mtt2_str |
| 338 |
] |
| 339 |
}); |
| 340 |
is($stored_transports->count, 2, 'Two transports selected'); |
| 341 |
|
| 342 |
$schema->storage->txn_rollback; |
| 343 |
}; |
| 344 |
|
| 345 |
subtest 'new message_transport_types' => sub { |
| 346 |
plan tests => 3; |
| 347 |
|
| 348 |
$schema->storage->txn_begin; |
| 349 |
|
| 350 |
my $patron = build_a_test_patron(); |
| 351 |
my $letter = build_a_test_letter(); |
| 352 |
my $attribute = build_a_test_attribute(); |
| 353 |
my $mtt = build_a_test_transport_type(); |
| 354 |
Koha::Patron::Message::Transport->new({ |
| 355 |
message_attribute_id => $attribute->message_attribute_id, |
| 356 |
message_transport_type => $mtt->message_transport_type, |
| 357 |
is_digest => 0, |
| 358 |
letter_module => $letter->module, |
| 359 |
letter_code => $letter->code, |
| 360 |
})->store; |
| 361 |
ok(my $preference = Koha::Patron::Message::Preference->new({ |
| 362 |
borrowernumber => $patron->borrowernumber, |
| 363 |
message_attribute_id => $attribute->message_attribute_id, |
| 364 |
wants_digest => 0, |
| 365 |
days_in_advance => undef, |
| 366 |
message_transport_types => $mtt->message_transport_type, |
| 367 |
})->store, 'Added a new messaging preference and transport types to patron.'); |
| 368 |
ok($preference->message_transport_types->{$mtt->message_transport_type}, |
| 369 |
'The transport type is stored in the object.'); |
| 370 |
my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 371 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 372 |
}); |
| 373 |
is($stored_transports->next->message_transport_type, $mtt->message_transport_type, |
| 374 |
'The transport type is stored in the database.'); |
| 375 |
|
| 376 |
$schema->storage->txn_rollback; |
| 377 |
}; |
| 378 |
}; |
| 379 |
|
| 380 |
subtest 'Test Koha::Patron::Message::Preference->message_name' => sub { |
| 381 |
plan tests => 1; |
| 382 |
|
| 383 |
$schema->storage->txn_begin; |
| 384 |
|
| 385 |
my $patron = build_a_test_patron(); |
| 386 |
my $attribute = build_a_test_attribute(); |
| 387 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
| 388 |
patron => $patron, |
| 389 |
attr => $attribute, |
| 390 |
}); |
| 391 |
my $message_name_pref = Koha::Patron::Message::Preferences->search_with_message_name({ |
| 392 |
borrowernumber => $patron->{'borrowernumber'}, |
| 393 |
message_name => $attribute->message_name, |
| 394 |
})->next; |
| 395 |
is($message_name_pref->message_name, $attribute->message_name, "Found preference with message_name"); |
| 396 |
|
| 397 |
$schema->storage->txn_rollback; |
| 398 |
}; |
| 399 |
|
| 400 |
subtest 'Test adding a new preference with invalid parameters' => sub { |
| 401 |
plan tests => 4; |
| 402 |
|
| 403 |
subtest 'Missing parameters' => sub { |
| 404 |
plan tests => 1; |
| 405 |
|
| 406 |
eval { Koha::Patron::Message::Preference->new->store }; |
| 407 |
is(ref $@, 'Koha::Exceptions::MissingParameter', |
| 408 |
'Adding a message preference without parameters' |
| 409 |
.' => Koha::Exceptions::MissingParameter'); |
| 410 |
}; |
| 411 |
|
| 412 |
subtest 'Too many parameters' => sub { |
| 413 |
plan tests => 1; |
| 414 |
|
| 415 |
$schema->storage->txn_begin; |
| 416 |
|
| 417 |
my $patron = build_a_test_patron(); |
| 418 |
eval { Koha::Patron::Message::Preference->new({ |
| 419 |
borrowernumber => $patron->borrowernumber, |
| 420 |
categorycode => $patron->categorycode, |
| 421 |
})->store }; |
| 422 |
is(ref $@, 'Koha::Exceptions::TooManyParameters', |
| 423 |
'Adding a message preference for both borrowernumber and categorycode' |
| 424 |
.' => Koha::Exceptions::TooManyParameters'); |
| 425 |
|
| 426 |
$schema->storage->txn_rollback; |
| 427 |
}; |
| 428 |
|
| 429 |
subtest 'Bad parameter' => sub { |
| 430 |
plan tests => 22; |
| 431 |
|
| 432 |
$schema->storage->txn_begin; |
| 433 |
|
| 434 |
eval { Koha::Patron::Message::Preference->new({ |
| 435 |
borrowernumber => -999, |
| 436 |
})->store }; |
| 437 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
| 438 |
'Adding a message preference with invalid borrowernumber' |
| 439 |
.' => Koha::Exceptions::BadParameter'); |
| 440 |
is ($@->parameter, 'borrowernumber', 'The previous exception tells us it' |
| 441 |
.' was the borrowernumber.'); |
| 442 |
|
| 443 |
eval { Koha::Patron::Message::Preference->new({ |
| 444 |
categorycode => 'nonexistent', |
| 445 |
})->store }; |
| 446 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
| 447 |
'Adding a message preference with invalid categorycode' |
| 448 |
.' => Koha::Exceptions::BadParameter'); |
| 449 |
is($@->parameter, 'categorycode', 'The previous exception tells us it' |
| 450 |
.' was the categorycode.'); |
| 451 |
|
| 452 |
my $attribute = build_a_test_attribute({ takes_days => 0 }); |
| 453 |
my $patron = build_a_test_patron(); |
| 454 |
eval { Koha::Patron::Message::Preference->new({ |
| 455 |
borrowernumber => $patron->borrowernumber, |
| 456 |
message_attribute_id => $attribute->message_attribute_id, |
| 457 |
days_in_advance => 10, |
| 458 |
})->store }; |
| 459 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
| 460 |
'Adding a message preference with days in advance option when not' |
| 461 |
.' available => Koha::Exceptions::BadParameter'); |
| 462 |
is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
| 463 |
.' was the days_in_advance.'); |
| 464 |
|
| 465 |
$attribute->set({ takes_days => 1 })->store; |
| 466 |
eval { Koha::Patron::Message::Preference->new({ |
| 467 |
borrowernumber => $patron->borrowernumber, |
| 468 |
message_attribute_id => $attribute->message_attribute_id, |
| 469 |
days_in_advance => 31, |
| 470 |
})->store }; |
| 471 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
| 472 |
'Adding a message preference with days in advance option too large' |
| 473 |
.' => Koha::Exceptions::BadParameter'); |
| 474 |
is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
| 475 |
.' was the days_in_advance.'); |
| 476 |
|
| 477 |
eval { Koha::Patron::Message::Preference->new({ |
| 478 |
borrowernumber => $patron->borrowernumber, |
| 479 |
message_transport_types => ['nonexistent'] |
| 480 |
})->store }; |
| 481 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
| 482 |
'Adding a message preference with invalid message_transport_type' |
| 483 |
.' => Koha::Exceptions::BadParameter'); |
| 484 |
is ($@->parameter, 'message_transport_types', 'The previous exception ' |
| 485 |
.'tells us it was the message_transport_types.'); |
| 486 |
|
| 487 |
my $mtt_new = build_a_test_transport_type(); |
| 488 |
eval { |
| 489 |
Koha::Patron::Message::Preference->new({ |
| 490 |
borrowernumber => $patron->borrowernumber, |
| 491 |
message_attribute_id => $attribute->message_attribute_id, |
| 492 |
message_transport_types => [$mtt_new->message_transport_type], |
| 493 |
wants_digest => 1, |
| 494 |
})->store }; |
| 495 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
| 496 |
'Adding a message preference with invalid message_transport_type' |
| 497 |
.' => Koha::Exceptions::BadParameter'); |
| 498 |
is ($@->parameter, 'message_transport_types', 'The previous exception ' |
| 499 |
.'tells us it was the message_transport_types.'); |
| 500 |
like ($@->error, qr/^No transport configured/, 'Exception is because of ' |
| 501 |
.'given message_transport_type is not a valid option.'); |
| 502 |
|
| 503 |
eval { |
| 504 |
Koha::Patron::Message::Preference->new({ |
| 505 |
borrowernumber => $patron->borrowernumber, |
| 506 |
message_attribute_id => $attribute->message_attribute_id, |
| 507 |
message_transport_types => [], |
| 508 |
wants_digest => 1, |
| 509 |
})->store }; |
| 510 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
| 511 |
'Adding a message preference with invalid message_transport_type' |
| 512 |
.' => Koha::Exceptions::BadParameter'); |
| 513 |
is ($@->parameter, 'wants_digest', 'The previous exception tells us it' |
| 514 |
.' was the wants_digest'); |
| 515 |
like ($@->error, qr/^Digest cannot be selected/, 'Exception s because of' |
| 516 |
.' given digest is not available for this transport.'); |
| 517 |
|
| 518 |
eval { |
| 519 |
Koha::Patron::Message::Preference->new({ |
| 520 |
borrowernumber => $patron->borrowernumber, |
| 521 |
message_attribute_id => $attribute->message_attribute_id, |
| 522 |
message_transport_types => [], |
| 523 |
wants_digest => 0, |
| 524 |
})->store }; |
| 525 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
| 526 |
'Adding a message preference with invalid message_transport_type' |
| 527 |
.' => Koha::Exceptions::BadParameter'); |
| 528 |
is ($@->parameter, 'wants_digest', 'The previous exception tells us it' |
| 529 |
.' was the wants_digest'); |
| 530 |
like ($@->error, qr/^Digest must be selected/, 'Exception s because of' |
| 531 |
.' digest has to be on for this transport.'); |
| 532 |
|
| 533 |
eval { |
| 534 |
Koha::Patron::Message::Preference->new({ |
| 535 |
borrowernumber => $patron->borrowernumber, |
| 536 |
message_attribute_id => -1, |
| 537 |
message_transport_types => [], |
| 538 |
})->store }; |
| 539 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
| 540 |
'Adding a message preference with invalid message_transport_type' |
| 541 |
.' => Koha::Exceptions::BadParameter'); |
| 542 |
is ($@->parameter, 'message_attribute_id', 'The previous exception tells' |
| 543 |
.' us it was the message_attribute_id'); |
| 544 |
like ($@->error, qr/^Message attribute with id -1 not found/, 'Exception ' |
| 545 |
.' is because of given message attribute id is not found.'); |
| 546 |
|
| 547 |
$schema->storage->txn_rollback; |
| 548 |
}; |
| 549 |
|
| 550 |
subtest 'Duplicate object' => sub { |
| 551 |
plan tests => 2; |
| 552 |
|
| 553 |
$schema->storage->txn_begin; |
| 554 |
|
| 555 |
my $attribute = build_a_test_attribute(); |
| 556 |
my $letter = build_a_test_letter(); |
| 557 |
my $mtt = build_a_test_transport_type(); |
| 558 |
Koha::Patron::Message::Transport->new({ |
| 559 |
message_attribute_id => $attribute->message_attribute_id, |
| 560 |
message_transport_type => $mtt->message_transport_type, |
| 561 |
is_digest => 0, |
| 562 |
letter_module => $letter->module, |
| 563 |
letter_code => $letter->code, |
| 564 |
})->store; |
| 565 |
my $patron = build_a_test_patron(); |
| 566 |
my $preference = Koha::Patron::Message::Preference->new({ |
| 567 |
borrowernumber => $patron->borrowernumber, |
| 568 |
message_attribute_id => $attribute->message_attribute_id, |
| 569 |
wants_digest => 0, |
| 570 |
days_in_advance => undef, |
| 571 |
})->store; |
| 572 |
ok($preference->borrower_message_preference_id, |
| 573 |
'Added a new messaging preference for patron.'); |
| 574 |
eval { Koha::Patron::Message::Preference->new({ |
| 575 |
borrowernumber => $patron->borrowernumber, |
| 576 |
message_attribute_id => $attribute->message_attribute_id, |
| 577 |
wants_digest => 0, |
| 578 |
days_in_advance => undef, |
| 579 |
})->store }; |
| 580 |
is(ref $@, 'Koha::Exceptions::DuplicateObject', |
| 581 |
'Adding a duplicate preference' |
| 582 |
.' => Koha::Exceptions::DuplicateObject'); |
| 583 |
|
| 584 |
$schema->storage->txn_rollback; |
| 585 |
}; |
| 586 |
}; |
| 587 |
|
| 588 |
sub build_a_test_attribute { |
| 589 |
my ($params) = @_; |
| 590 |
|
| 591 |
$params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
| 592 |
? 1 : 0; |
| 593 |
|
| 594 |
my $attribute = $builder->build({ |
| 595 |
source => 'MessageAttribute', |
| 596 |
value => $params, |
| 597 |
}); |
| 598 |
|
| 599 |
return Koha::Patron::Message::Attributes->find( |
| 600 |
$attribute->{message_attribute_id} |
| 601 |
); |
| 602 |
} |
| 603 |
|
| 604 |
sub build_a_test_category { |
| 605 |
my $categorycode = $builder->build({ |
| 606 |
source => 'Category' })->{categorycode}; |
| 607 |
|
| 608 |
return Koha::Patron::Categories->find($categorycode); |
| 609 |
} |
| 610 |
|
| 611 |
sub build_a_test_letter { |
| 612 |
my ($params) = @_; |
| 613 |
|
| 614 |
my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
| 615 |
my $branchcode = $builder->build({ |
| 616 |
source => 'Branch' })->{branchcode}; |
| 617 |
my $letter = $builder->build({ |
| 618 |
source => 'Letter', |
| 619 |
value => { |
| 620 |
branchcode => '', |
| 621 |
is_html => 0, |
| 622 |
message_transport_type => $mtt |
| 623 |
} |
| 624 |
}); |
| 625 |
|
| 626 |
return Koha::Notice::Templates->find({ |
| 627 |
module => $letter->{module}, |
| 628 |
code => $letter->{code}, |
| 629 |
branchcode => $letter->{branchcode}, |
| 630 |
}); |
| 631 |
} |
| 632 |
|
| 633 |
sub build_a_test_patron { |
| 634 |
my $categorycode = $builder->build({ |
| 635 |
source => 'Category' })->{categorycode}; |
| 636 |
my $branchcode = $builder->build({ |
| 637 |
source => 'Branch' })->{branchcode}; |
| 638 |
my $borrowernumber = $builder->build({ |
| 639 |
source => 'Borrower' })->{borrowernumber}; |
| 640 |
|
| 641 |
return Koha::Patrons->find($borrowernumber); |
| 642 |
} |
| 643 |
|
| 644 |
sub build_a_test_transport_type { |
| 645 |
my $mtt = $builder->build({ |
| 646 |
source => 'MessageTransportType' }); |
| 647 |
|
| 648 |
return Koha::Patron::Message::Transport::Types->find( |
| 649 |
$mtt->{message_transport_type} |
| 650 |
); |
| 651 |
} |
| 652 |
|
| 653 |
sub build_a_test_category_preference { |
| 654 |
my ($params) = @_; |
| 655 |
|
| 656 |
my $patron = $params->{patron}; |
| 657 |
my $attr = $params->{attr} |
| 658 |
? $params->{attr} |
| 659 |
: build_a_test_attribute($params->{days_in_advance}); |
| 660 |
|
| 661 |
my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter(); |
| 662 |
my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type(); |
| 663 |
my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type(); |
| 664 |
|
| 665 |
Koha::Patron::Message::Transport->new({ |
| 666 |
message_attribute_id => $attr->message_attribute_id, |
| 667 |
message_transport_type => $mtt1->message_transport_type, |
| 668 |
is_digest => $params->{digest} ? 1 : 0, |
| 669 |
letter_module => $letter->module, |
| 670 |
letter_code => $letter->code, |
| 671 |
})->store; |
| 672 |
|
| 673 |
Koha::Patron::Message::Transport->new({ |
| 674 |
message_attribute_id => $attr->message_attribute_id, |
| 675 |
message_transport_type => $mtt2->message_transport_type, |
| 676 |
is_digest => $params->{digest} ? 1 : 0, |
| 677 |
letter_module => $letter->module, |
| 678 |
letter_code => $letter->code, |
| 679 |
})->store; |
| 680 |
|
| 681 |
my $default = Koha::Patron::Message::Preference->new({ |
| 682 |
categorycode => $patron->categorycode, |
| 683 |
message_attribute_id => $attr->message_attribute_id, |
| 684 |
wants_digest => $params->{digest} ? 1 : 0, |
| 685 |
days_in_advance => $params->{days_in_advance} |
| 686 |
? $params->{days_in_advance} : undef, |
| 687 |
})->store; |
| 688 |
|
| 689 |
Koha::Patron::Message::Transport::Preference->new({ |
| 690 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
| 691 |
message_transport_type => $mtt1->message_transport_type, |
| 692 |
})->store; |
| 693 |
Koha::Patron::Message::Transport::Preference->new({ |
| 694 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
| 695 |
message_transport_type => $mtt2->message_transport_type, |
| 696 |
})->store; |
| 697 |
|
| 698 |
return ($default, $mtt1, $mtt2); |
| 699 |
} |
| 700 |
|
| 701 |
sub build_a_test_complete_preference { |
| 702 |
my ($params) = @_; |
| 703 |
|
| 704 |
my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params); |
| 705 |
my $patron = $params->{patron}; |
| 706 |
$patron->set_default_messaging_preferences; |
| 707 |
return (Koha::Patron::Message::Preferences->search({ |
| 708 |
borrowernumber => $patron->borrowernumber |
| 709 |
})->next, $mtt1, $mtt2); |
| 710 |
} |
| 711 |
|
| 712 |
sub mytempfile { |
| 713 |
my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 ); |
| 714 |
print $fh $_[0]//''; |
| 715 |
close $fh; |
| 716 |
return $fn; |
| 717 |
} |
| 718 |
|
| 719 |
1; |