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