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 => 19; |
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 not available/, 'Exception s because of given' |
516 |
.' digest is not available for this transport.'); |
517 |
|
518 |
eval { |
519 |
Koha::Patron::Message::Preference->new({ |
520 |
borrowernumber => $patron->borrowernumber, |
521 |
message_attribute_id => -1, |
522 |
message_transport_types => [], |
523 |
})->store }; |
524 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
525 |
'Adding a message preference with invalid message_transport_type' |
526 |
.' => Koha::Exceptions::BadParameter'); |
527 |
is ($@->parameter, 'message_attribute_id', 'The previous exception tells' |
528 |
.' us it was the message_attribute_id'); |
529 |
like ($@->error, qr/^Message attribute with id -1 not found/, 'Exception ' |
530 |
.' is because of given message attribute id is not found.'); |
531 |
|
532 |
$schema->storage->txn_rollback; |
533 |
}; |
534 |
|
535 |
subtest 'Duplicate object' => sub { |
536 |
plan tests => 2; |
537 |
|
538 |
$schema->storage->txn_begin; |
539 |
|
540 |
my $attribute = build_a_test_attribute(); |
541 |
my $letter = build_a_test_letter(); |
542 |
my $mtt = build_a_test_transport_type(); |
543 |
Koha::Patron::Message::Transport->new({ |
544 |
message_attribute_id => $attribute->message_attribute_id, |
545 |
message_transport_type => $mtt->message_transport_type, |
546 |
is_digest => 0, |
547 |
letter_module => $letter->module, |
548 |
letter_code => $letter->code, |
549 |
})->store; |
550 |
my $patron = build_a_test_patron(); |
551 |
my $preference = Koha::Patron::Message::Preference->new({ |
552 |
borrowernumber => $patron->borrowernumber, |
553 |
message_attribute_id => $attribute->message_attribute_id, |
554 |
wants_digest => 0, |
555 |
days_in_advance => undef, |
556 |
})->store; |
557 |
ok($preference->borrower_message_preference_id, |
558 |
'Added a new messaging preference for patron.'); |
559 |
eval { Koha::Patron::Message::Preference->new({ |
560 |
borrowernumber => $patron->borrowernumber, |
561 |
message_attribute_id => $attribute->message_attribute_id, |
562 |
wants_digest => 0, |
563 |
days_in_advance => undef, |
564 |
})->store }; |
565 |
is(ref $@, 'Koha::Exceptions::DuplicateObject', |
566 |
'Adding a duplicate preference' |
567 |
.' => Koha::Exceptions::DuplicateObject'); |
568 |
|
569 |
$schema->storage->txn_rollback; |
570 |
}; |
571 |
}; |
572 |
|
573 |
sub build_a_test_attribute { |
574 |
my ($params) = @_; |
575 |
|
576 |
$params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
577 |
? 1 : 0; |
578 |
|
579 |
my $attribute = $builder->build({ |
580 |
source => 'MessageAttribute', |
581 |
value => $params, |
582 |
}); |
583 |
|
584 |
return Koha::Patron::Message::Attributes->find( |
585 |
$attribute->{message_attribute_id} |
586 |
); |
587 |
} |
588 |
|
589 |
sub build_a_test_category { |
590 |
my $categorycode = $builder->build({ |
591 |
source => 'Category' })->{categorycode}; |
592 |
|
593 |
return Koha::Patron::Categories->find($categorycode); |
594 |
} |
595 |
|
596 |
sub build_a_test_letter { |
597 |
my ($params) = @_; |
598 |
|
599 |
my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
600 |
my $branchcode = $builder->build({ |
601 |
source => 'Branch' })->{branchcode}; |
602 |
my $letter = $builder->build({ |
603 |
source => 'Letter', |
604 |
value => { |
605 |
branchcode => '', |
606 |
is_html => 0, |
607 |
message_transport_type => $mtt |
608 |
} |
609 |
}); |
610 |
|
611 |
return Koha::Notice::Templates->find({ |
612 |
module => $letter->{module}, |
613 |
code => $letter->{code}, |
614 |
branchcode => $letter->{branchcode}, |
615 |
}); |
616 |
} |
617 |
|
618 |
sub build_a_test_patron { |
619 |
my $categorycode = $builder->build({ |
620 |
source => 'Category' })->{categorycode}; |
621 |
my $branchcode = $builder->build({ |
622 |
source => 'Branch' })->{branchcode}; |
623 |
my $borrowernumber = $builder->build({ |
624 |
source => 'Borrower' })->{borrowernumber}; |
625 |
|
626 |
return Koha::Patrons->find($borrowernumber); |
627 |
} |
628 |
|
629 |
sub build_a_test_transport_type { |
630 |
my $mtt = $builder->build({ |
631 |
source => 'MessageTransportType' }); |
632 |
|
633 |
return Koha::Patron::Message::Transport::Types->find( |
634 |
$mtt->{message_transport_type} |
635 |
); |
636 |
} |
637 |
|
638 |
sub build_a_test_category_preference { |
639 |
my ($params) = @_; |
640 |
|
641 |
my $patron = $params->{patron}; |
642 |
my $attr = $params->{attr} |
643 |
? $params->{attr} |
644 |
: build_a_test_attribute($params->{days_in_advance}); |
645 |
|
646 |
my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter(); |
647 |
my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type(); |
648 |
my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type(); |
649 |
|
650 |
Koha::Patron::Message::Transport->new({ |
651 |
message_attribute_id => $attr->message_attribute_id, |
652 |
message_transport_type => $mtt1->message_transport_type, |
653 |
is_digest => $params->{digest} ? 1 : 0, |
654 |
letter_module => $letter->module, |
655 |
letter_code => $letter->code, |
656 |
})->store; |
657 |
|
658 |
Koha::Patron::Message::Transport->new({ |
659 |
message_attribute_id => $attr->message_attribute_id, |
660 |
message_transport_type => $mtt2->message_transport_type, |
661 |
is_digest => $params->{digest} ? 1 : 0, |
662 |
letter_module => $letter->module, |
663 |
letter_code => $letter->code, |
664 |
})->store; |
665 |
|
666 |
my $default = Koha::Patron::Message::Preference->new({ |
667 |
categorycode => $patron->categorycode, |
668 |
message_attribute_id => $attr->message_attribute_id, |
669 |
wants_digest => $params->{digest} ? 1 : 0, |
670 |
days_in_advance => $params->{days_in_advance} |
671 |
? $params->{days_in_advance} : undef, |
672 |
})->store; |
673 |
|
674 |
Koha::Patron::Message::Transport::Preference->new({ |
675 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
676 |
message_transport_type => $mtt1->message_transport_type, |
677 |
})->store; |
678 |
Koha::Patron::Message::Transport::Preference->new({ |
679 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
680 |
message_transport_type => $mtt2->message_transport_type, |
681 |
})->store; |
682 |
|
683 |
return ($default, $mtt1, $mtt2); |
684 |
} |
685 |
|
686 |
sub build_a_test_complete_preference { |
687 |
my ($params) = @_; |
688 |
|
689 |
my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params); |
690 |
my $patron = $params->{patron}; |
691 |
$patron->set_default_messaging_preferences; |
692 |
return (Koha::Patron::Message::Preferences->search({ |
693 |
borrowernumber => $patron->borrowernumber |
694 |
})->next, $mtt1, $mtt2); |
695 |
} |
696 |
|
697 |
sub mytempfile { |
698 |
my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 ); |
699 |
print $fh $_[0]//''; |
700 |
close $fh; |
701 |
return $fn; |
702 |
} |
703 |
|
704 |
1; |