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 => 12; |
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 |
# Test email and smsalertnumber validation |
343 |
eval { Koha::Patron::Message::Transport::Types->new({ |
344 |
message_transport_type => 'email' |
345 |
})->store }; |
346 |
eval { Koha::Patron::Message::Transport::Types->new({ |
347 |
message_transport_type => 'sms' |
348 |
})->store }; |
349 |
Koha::Patron::Message::Transport->new({ |
350 |
message_attribute_id => $preference->message_attribute_id, |
351 |
message_transport_type => 'email', |
352 |
is_digest => 1 |
353 |
})->store; |
354 |
Koha::Patron::Message::Transport->new({ |
355 |
message_attribute_id => $preference->message_attribute_id, |
356 |
message_transport_type => 'sms', |
357 |
is_digest => 1 |
358 |
})->store; |
359 |
$patron->set({ email => '', smsalertnumber => '' })->store; |
360 |
eval { |
361 |
$preference->message_transport_types('email')->store; |
362 |
}; |
363 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
364 |
'Adding a message preference with invalid message_transport_type' |
365 |
.' => Koha::Exceptions::BadParameter'); |
366 |
is ($@->parameter, 'message_transport_types', 'The previous exception ' |
367 |
.' tells us it was the message_transport_types'); |
368 |
like ($@->error, qr/^Patron has not set email address/, 'Exception ' |
369 |
.' is because of patron has not set email address.'); |
370 |
eval { |
371 |
$preference->message_transport_types('sms')->store; |
372 |
}; |
373 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
374 |
'Adding a message preference with invalid message_transport_type' |
375 |
.' => Koha::Exceptions::BadParameter'); |
376 |
is ($@->parameter, 'message_transport_types', 'The previous exception ' |
377 |
.' tells us it was the message_transport_types'); |
378 |
like ($@->error, qr/^Patron has not set sms number/, 'Exception ' |
379 |
.' is because of patron has not set sms number.'); |
380 |
|
381 |
|
382 |
|
383 |
$schema->storage->txn_rollback; |
384 |
}; |
385 |
|
386 |
subtest 'new message_transport_types' => sub { |
387 |
plan tests => 3; |
388 |
|
389 |
$schema->storage->txn_begin; |
390 |
|
391 |
my $patron = build_a_test_patron(); |
392 |
my $letter = build_a_test_letter(); |
393 |
my $attribute = build_a_test_attribute(); |
394 |
my $mtt = build_a_test_transport_type(); |
395 |
Koha::Patron::Message::Transport->new({ |
396 |
message_attribute_id => $attribute->message_attribute_id, |
397 |
message_transport_type => $mtt->message_transport_type, |
398 |
is_digest => 0, |
399 |
letter_module => $letter->module, |
400 |
letter_code => $letter->code, |
401 |
})->store; |
402 |
ok(my $preference = Koha::Patron::Message::Preference->new({ |
403 |
borrowernumber => $patron->borrowernumber, |
404 |
message_attribute_id => $attribute->message_attribute_id, |
405 |
wants_digest => 0, |
406 |
days_in_advance => undef, |
407 |
message_transport_types => $mtt->message_transport_type, |
408 |
})->store, 'Added a new messaging preference and transport types to patron.'); |
409 |
ok($preference->message_transport_types->{$mtt->message_transport_type}, |
410 |
'The transport type is stored in the object.'); |
411 |
my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
412 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
413 |
}); |
414 |
is($stored_transports->next->message_transport_type, $mtt->message_transport_type, |
415 |
'The transport type is stored in the database.'); |
416 |
|
417 |
$schema->storage->txn_rollback; |
418 |
}; |
419 |
}; |
420 |
|
421 |
subtest 'Test Koha::Patron::Message::Preference->message_name' => sub { |
422 |
plan tests => 1; |
423 |
|
424 |
$schema->storage->txn_begin; |
425 |
|
426 |
my $patron = build_a_test_patron(); |
427 |
my $attribute = build_a_test_attribute(); |
428 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
429 |
patron => $patron, |
430 |
attr => $attribute, |
431 |
}); |
432 |
my $message_name_pref = Koha::Patron::Message::Preferences->search_with_message_name({ |
433 |
borrowernumber => $patron->{'borrowernumber'}, |
434 |
message_name => $attribute->message_name, |
435 |
})->next; |
436 |
is($message_name_pref->message_name, $attribute->message_name, "Found preference with message_name"); |
437 |
|
438 |
$schema->storage->txn_rollback; |
439 |
}; |
440 |
|
441 |
subtest 'Test adding a new preference with invalid parameters' => sub { |
442 |
plan tests => 4; |
443 |
|
444 |
subtest 'Missing parameters' => sub { |
445 |
plan tests => 1; |
446 |
|
447 |
eval { Koha::Patron::Message::Preference->new->store }; |
448 |
is(ref $@, 'Koha::Exceptions::MissingParameter', |
449 |
'Adding a message preference without parameters' |
450 |
.' => Koha::Exceptions::MissingParameter'); |
451 |
}; |
452 |
|
453 |
subtest 'Too many parameters' => sub { |
454 |
plan tests => 1; |
455 |
|
456 |
$schema->storage->txn_begin; |
457 |
|
458 |
my $patron = build_a_test_patron(); |
459 |
eval { Koha::Patron::Message::Preference->new({ |
460 |
borrowernumber => $patron->borrowernumber, |
461 |
categorycode => $patron->categorycode, |
462 |
})->store }; |
463 |
is(ref $@, 'Koha::Exceptions::TooManyParameters', |
464 |
'Adding a message preference for both borrowernumber and categorycode' |
465 |
.' => Koha::Exceptions::TooManyParameters'); |
466 |
|
467 |
$schema->storage->txn_rollback; |
468 |
}; |
469 |
|
470 |
subtest 'Bad parameter' => sub { |
471 |
plan tests => 22; |
472 |
|
473 |
$schema->storage->txn_begin; |
474 |
|
475 |
eval { Koha::Patron::Message::Preference->new({ |
476 |
borrowernumber => -999, |
477 |
})->store }; |
478 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
479 |
'Adding a message preference with invalid borrowernumber' |
480 |
.' => Koha::Exceptions::BadParameter'); |
481 |
is ($@->parameter, 'borrowernumber', 'The previous exception tells us it' |
482 |
.' was the borrowernumber.'); |
483 |
|
484 |
eval { Koha::Patron::Message::Preference->new({ |
485 |
categorycode => 'nonexistent', |
486 |
})->store }; |
487 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
488 |
'Adding a message preference with invalid categorycode' |
489 |
.' => Koha::Exceptions::BadParameter'); |
490 |
is($@->parameter, 'categorycode', 'The previous exception tells us it' |
491 |
.' was the categorycode.'); |
492 |
|
493 |
my $attribute = build_a_test_attribute({ takes_days => 0 }); |
494 |
my $patron = build_a_test_patron(); |
495 |
eval { Koha::Patron::Message::Preference->new({ |
496 |
borrowernumber => $patron->borrowernumber, |
497 |
message_attribute_id => $attribute->message_attribute_id, |
498 |
days_in_advance => 10, |
499 |
})->store }; |
500 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
501 |
'Adding a message preference with days in advance option when not' |
502 |
.' available => Koha::Exceptions::BadParameter'); |
503 |
is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
504 |
.' was the days_in_advance.'); |
505 |
|
506 |
$attribute->set({ takes_days => 1 })->store; |
507 |
eval { Koha::Patron::Message::Preference->new({ |
508 |
borrowernumber => $patron->borrowernumber, |
509 |
message_attribute_id => $attribute->message_attribute_id, |
510 |
days_in_advance => 31, |
511 |
})->store }; |
512 |
is(ref $@, 'Koha::Exceptions::BadParameter', |
513 |
'Adding a message preference with days in advance option too large' |
514 |
.' => Koha::Exceptions::BadParameter'); |
515 |
is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
516 |
.' was the days_in_advance.'); |
517 |
|
518 |
eval { Koha::Patron::Message::Preference->new({ |
519 |
borrowernumber => $patron->borrowernumber, |
520 |
message_transport_types => ['nonexistent'] |
521 |
})->store }; |
522 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
523 |
'Adding a message preference with invalid message_transport_type' |
524 |
.' => Koha::Exceptions::BadParameter'); |
525 |
is ($@->parameter, 'message_transport_types', 'The previous exception ' |
526 |
.'tells us it was the message_transport_types.'); |
527 |
|
528 |
my $mtt_new = build_a_test_transport_type(); |
529 |
eval { |
530 |
Koha::Patron::Message::Preference->new({ |
531 |
borrowernumber => $patron->borrowernumber, |
532 |
message_attribute_id => $attribute->message_attribute_id, |
533 |
message_transport_types => [$mtt_new->message_transport_type], |
534 |
wants_digest => 1, |
535 |
})->store }; |
536 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
537 |
'Adding a message preference with invalid message_transport_type' |
538 |
.' => Koha::Exceptions::BadParameter'); |
539 |
is ($@->parameter, 'message_transport_types', 'The previous exception ' |
540 |
.'tells us it was the message_transport_types.'); |
541 |
like ($@->error, qr/^No transport configured/, 'Exception is because of ' |
542 |
.'given message_transport_type is not a valid option.'); |
543 |
|
544 |
eval { |
545 |
Koha::Patron::Message::Preference->new({ |
546 |
borrowernumber => $patron->borrowernumber, |
547 |
message_attribute_id => $attribute->message_attribute_id, |
548 |
message_transport_types => [], |
549 |
wants_digest => 1, |
550 |
})->store }; |
551 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
552 |
'Adding a message preference with invalid message_transport_type' |
553 |
.' => Koha::Exceptions::BadParameter'); |
554 |
is ($@->parameter, 'wants_digest', 'The previous exception tells us it' |
555 |
.' was the wants_digest'); |
556 |
like ($@->error, qr/^Digest cannot be selected/, 'Exception s because of' |
557 |
.' given digest is not available for this transport.'); |
558 |
|
559 |
eval { |
560 |
Koha::Patron::Message::Preference->new({ |
561 |
borrowernumber => $patron->borrowernumber, |
562 |
message_attribute_id => $attribute->message_attribute_id, |
563 |
message_transport_types => [], |
564 |
wants_digest => 0, |
565 |
})->store }; |
566 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
567 |
'Adding a message preference with invalid message_transport_type' |
568 |
.' => Koha::Exceptions::BadParameter'); |
569 |
is ($@->parameter, 'wants_digest', 'The previous exception tells us it' |
570 |
.' was the wants_digest'); |
571 |
like ($@->error, qr/^Digest must be selected/, 'Exception s because of' |
572 |
.' digest has to be on for this transport.'); |
573 |
|
574 |
eval { |
575 |
Koha::Patron::Message::Preference->new({ |
576 |
borrowernumber => $patron->borrowernumber, |
577 |
message_attribute_id => -1, |
578 |
message_transport_types => [], |
579 |
})->store }; |
580 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
581 |
'Adding a message preference with invalid message_transport_type' |
582 |
.' => Koha::Exceptions::BadParameter'); |
583 |
is ($@->parameter, 'message_attribute_id', 'The previous exception tells' |
584 |
.' us it was the message_attribute_id'); |
585 |
like ($@->error, qr/^Message attribute with id -1 not found/, 'Exception ' |
586 |
.' is because of given message attribute id is not found.'); |
587 |
|
588 |
$schema->storage->txn_rollback; |
589 |
}; |
590 |
|
591 |
subtest 'Duplicate object' => sub { |
592 |
plan tests => 2; |
593 |
|
594 |
$schema->storage->txn_begin; |
595 |
|
596 |
my $attribute = build_a_test_attribute(); |
597 |
my $letter = build_a_test_letter(); |
598 |
my $mtt = build_a_test_transport_type(); |
599 |
Koha::Patron::Message::Transport->new({ |
600 |
message_attribute_id => $attribute->message_attribute_id, |
601 |
message_transport_type => $mtt->message_transport_type, |
602 |
is_digest => 0, |
603 |
letter_module => $letter->module, |
604 |
letter_code => $letter->code, |
605 |
})->store; |
606 |
my $patron = build_a_test_patron(); |
607 |
my $preference = Koha::Patron::Message::Preference->new({ |
608 |
borrowernumber => $patron->borrowernumber, |
609 |
message_attribute_id => $attribute->message_attribute_id, |
610 |
wants_digest => 0, |
611 |
days_in_advance => undef, |
612 |
})->store; |
613 |
ok($preference->borrower_message_preference_id, |
614 |
'Added a new messaging preference for patron.'); |
615 |
eval { Koha::Patron::Message::Preference->new({ |
616 |
borrowernumber => $patron->borrowernumber, |
617 |
message_attribute_id => $attribute->message_attribute_id, |
618 |
wants_digest => 0, |
619 |
days_in_advance => undef, |
620 |
})->store }; |
621 |
is(ref $@, 'Koha::Exceptions::DuplicateObject', |
622 |
'Adding a duplicate preference' |
623 |
.' => Koha::Exceptions::DuplicateObject'); |
624 |
|
625 |
$schema->storage->txn_rollback; |
626 |
}; |
627 |
}; |
628 |
|
629 |
sub build_a_test_attribute { |
630 |
my ($params) = @_; |
631 |
|
632 |
$params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
633 |
? 1 : 0; |
634 |
|
635 |
my $attribute = $builder->build({ |
636 |
source => 'MessageAttribute', |
637 |
value => $params, |
638 |
}); |
639 |
|
640 |
return Koha::Patron::Message::Attributes->find( |
641 |
$attribute->{message_attribute_id} |
642 |
); |
643 |
} |
644 |
|
645 |
sub build_a_test_category { |
646 |
my $categorycode = $builder->build({ |
647 |
source => 'Category' })->{categorycode}; |
648 |
|
649 |
return Koha::Patron::Categories->find($categorycode); |
650 |
} |
651 |
|
652 |
sub build_a_test_letter { |
653 |
my ($params) = @_; |
654 |
|
655 |
my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
656 |
my $branchcode = $builder->build({ |
657 |
source => 'Branch' })->{branchcode}; |
658 |
my $letter = $builder->build({ |
659 |
source => 'Letter', |
660 |
value => { |
661 |
branchcode => '', |
662 |
is_html => 0, |
663 |
message_transport_type => $mtt |
664 |
} |
665 |
}); |
666 |
|
667 |
return Koha::Notice::Templates->find({ |
668 |
module => $letter->{module}, |
669 |
code => $letter->{code}, |
670 |
branchcode => $letter->{branchcode}, |
671 |
}); |
672 |
} |
673 |
|
674 |
sub build_a_test_patron { |
675 |
my $categorycode = $builder->build({ |
676 |
source => 'Category' })->{categorycode}; |
677 |
my $branchcode = $builder->build({ |
678 |
source => 'Branch' })->{branchcode}; |
679 |
my $borrowernumber = $builder->build({ |
680 |
source => 'Borrower' })->{borrowernumber}; |
681 |
|
682 |
return Koha::Patrons->find($borrowernumber); |
683 |
} |
684 |
|
685 |
sub build_a_test_transport_type { |
686 |
my $mtt = $builder->build({ |
687 |
source => 'MessageTransportType' }); |
688 |
|
689 |
return Koha::Patron::Message::Transport::Types->find( |
690 |
$mtt->{message_transport_type} |
691 |
); |
692 |
} |
693 |
|
694 |
sub build_a_test_category_preference { |
695 |
my ($params) = @_; |
696 |
|
697 |
my $patron = $params->{patron}; |
698 |
my $attr = $params->{attr} |
699 |
? $params->{attr} |
700 |
: build_a_test_attribute($params->{days_in_advance}); |
701 |
|
702 |
my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter(); |
703 |
my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type(); |
704 |
my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type(); |
705 |
|
706 |
Koha::Patron::Message::Transport->new({ |
707 |
message_attribute_id => $attr->message_attribute_id, |
708 |
message_transport_type => $mtt1->message_transport_type, |
709 |
is_digest => $params->{digest} ? 1 : 0, |
710 |
letter_module => $letter->module, |
711 |
letter_code => $letter->code, |
712 |
})->store; |
713 |
|
714 |
Koha::Patron::Message::Transport->new({ |
715 |
message_attribute_id => $attr->message_attribute_id, |
716 |
message_transport_type => $mtt2->message_transport_type, |
717 |
is_digest => $params->{digest} ? 1 : 0, |
718 |
letter_module => $letter->module, |
719 |
letter_code => $letter->code, |
720 |
})->store; |
721 |
|
722 |
my $default = Koha::Patron::Message::Preference->new({ |
723 |
categorycode => $patron->categorycode, |
724 |
message_attribute_id => $attr->message_attribute_id, |
725 |
wants_digest => $params->{digest} ? 1 : 0, |
726 |
days_in_advance => $params->{days_in_advance} |
727 |
? $params->{days_in_advance} : undef, |
728 |
})->store; |
729 |
|
730 |
Koha::Patron::Message::Transport::Preference->new({ |
731 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
732 |
message_transport_type => $mtt1->message_transport_type, |
733 |
})->store; |
734 |
Koha::Patron::Message::Transport::Preference->new({ |
735 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
736 |
message_transport_type => $mtt2->message_transport_type, |
737 |
})->store; |
738 |
|
739 |
return ($default, $mtt1, $mtt2); |
740 |
} |
741 |
|
742 |
sub build_a_test_complete_preference { |
743 |
my ($params) = @_; |
744 |
|
745 |
my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params); |
746 |
my $patron = $params->{patron}; |
747 |
$patron->set_default_messaging_preferences; |
748 |
return (Koha::Patron::Message::Preferences->search({ |
749 |
borrowernumber => $patron->borrowernumber |
750 |
})->next, $mtt1, $mtt2); |
751 |
} |
752 |
|
753 |
sub mytempfile { |
754 |
my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 ); |
755 |
print $fh $_[0]//''; |
756 |
close $fh; |
757 |
return $fn; |
758 |
} |
759 |
|
760 |
1; |