Lines 19-25
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::More tests => 5; |
22 |
|
|
|
23 |
use Test::More tests => 6; |
23 |
|
24 |
|
24 |
use t::lib::Mocks; |
25 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::TestBuilder; |
Lines 173-178
subtest 'Add preferences from defaults' => sub {
Link Here
|
173 |
$schema->storage->txn_rollback; |
174 |
$schema->storage->txn_rollback; |
174 |
}; |
175 |
}; |
175 |
|
176 |
|
|
|
177 |
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub { |
178 |
plan tests => 4; |
179 |
|
180 |
ok(Koha::Patron::Message::Preference->can('message_transport_types'), |
181 |
'Method message_transport_types available'); |
182 |
|
183 |
subtest 'get message_transport_types' => sub { |
184 |
plan tests => 4; |
185 |
|
186 |
$schema->storage->txn_begin; |
187 |
|
188 |
my $patron = build_a_test_patron(); |
189 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
190 |
patron => $patron |
191 |
}); |
192 |
Koha::Patron::Message::Transport::Preferences->search({ |
193 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
194 |
})->delete; |
195 |
Koha::Patron::Message::Transport::Preference->new({ |
196 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
197 |
message_transport_type => $mtt1->message_transport_type, |
198 |
})->store; |
199 |
Koha::Patron::Message::Transport::Preference->new({ |
200 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
201 |
message_transport_type => $mtt2->message_transport_type, |
202 |
})->store; |
203 |
my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
204 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
205 |
}); |
206 |
my $transport1 = Koha::Patron::Message::Transports->find({ |
207 |
message_attribute_id => $preference->message_attribute_id, |
208 |
message_transport_type => $mtt1->message_transport_type, |
209 |
}); |
210 |
my $transport2 = Koha::Patron::Message::Transports->find({ |
211 |
message_attribute_id => $preference->message_attribute_id, |
212 |
message_transport_type => $mtt2->message_transport_type, |
213 |
}); |
214 |
my $transports = $preference->message_transport_types; |
215 |
is(keys %{$transports}, $stored_transports->count, |
216 |
'->message_transport_types gets correct amount of transport types.'); |
217 |
is($transports->{$stored_transports->next->message_transport_type}, |
218 |
$transport1->letter_code, 'Found correct message transport type and letter code.'); |
219 |
is($transports->{$stored_transports->next->message_transport_type}, |
220 |
$transport2->letter_code, 'Found correct message transport type and letter code.'); |
221 |
ok(!$preference->message_transport_types->{'nonexistent'}, |
222 |
'Didn\'t find nonexistent transport type.'); |
223 |
|
224 |
$schema->storage->txn_rollback; |
225 |
}; |
226 |
|
227 |
subtest 'set message_transport_types' => sub { |
228 |
plan tests => 6; |
229 |
|
230 |
$schema->storage->txn_begin; |
231 |
|
232 |
my $patron = build_a_test_patron(); |
233 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
234 |
patron => $patron |
235 |
}); |
236 |
|
237 |
my $mtt1_str = $mtt1->message_transport_type; |
238 |
my $mtt2_str = $mtt2->message_transport_type; |
239 |
# 1/3, use message_transport_types(list) |
240 |
Koha::Patron::Message::Transport::Preferences->search({ |
241 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
242 |
})->delete; |
243 |
ok($preference->message_transport_types($mtt1_str, $mtt2_str)->store, |
244 |
'1/3 Set returned true.'); |
245 |
my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
246 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
247 |
'-or' => [ |
248 |
message_transport_type => $mtt1_str, |
249 |
message_transport_type => $mtt2_str |
250 |
] |
251 |
}); |
252 |
is($stored_transports->count, 2, 'Two transports selected'); |
253 |
|
254 |
# 2/3, use message_transport_types(ARRAYREF) |
255 |
Koha::Patron::Message::Transport::Preferences->search({ |
256 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
257 |
})->delete; |
258 |
ok($preference->message_transport_types([$mtt1_str, $mtt2_str])->store, |
259 |
'2/3 Set returned true.'); |
260 |
$stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
261 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
262 |
'-or' => [ |
263 |
message_transport_type => $mtt1_str, |
264 |
message_transport_type => $mtt2_str |
265 |
] |
266 |
}); |
267 |
is($stored_transports->count, 2, 'Two transports selected'); |
268 |
|
269 |
# 3/3, use set({ message_transport_types => ARRAYREF }) |
270 |
Koha::Patron::Message::Transport::Preferences->search({ |
271 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
272 |
})->delete; |
273 |
ok($preference->set({ |
274 |
message_transport_types => [$mtt1_str, $mtt2_str]})->store, |
275 |
'3/3 Set returned true.'); |
276 |
$stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
277 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
278 |
'-or' => [ |
279 |
message_transport_type => $mtt1_str, |
280 |
message_transport_type => $mtt2_str |
281 |
] |
282 |
}); |
283 |
is($stored_transports->count, 2, 'Two transports selected'); |
284 |
|
285 |
$schema->storage->txn_rollback; |
286 |
}; |
287 |
|
288 |
subtest 'new message_transport_types' => sub { |
289 |
plan tests => 3; |
290 |
|
291 |
$schema->storage->txn_begin; |
292 |
|
293 |
my $patron = build_a_test_patron(); |
294 |
my $letter = build_a_test_letter(); |
295 |
my $attribute = build_a_test_attribute(); |
296 |
my $mtt = build_a_test_transport_type(); |
297 |
Koha::Patron::Message::Transport->new({ |
298 |
message_attribute_id => $attribute->message_attribute_id, |
299 |
message_transport_type => $mtt->message_transport_type, |
300 |
is_digest => 0, |
301 |
letter_module => $letter->module, |
302 |
letter_code => $letter->code, |
303 |
})->store; |
304 |
ok(my $preference = Koha::Patron::Message::Preference->new({ |
305 |
borrowernumber => $patron->borrowernumber, |
306 |
message_attribute_id => $attribute->message_attribute_id, |
307 |
wants_digest => 0, |
308 |
days_in_advance => undef, |
309 |
message_transport_types => $mtt->message_transport_type, |
310 |
})->store, 'Added a new messaging preference and transport types to patron.'); |
311 |
ok($preference->message_transport_types->{$mtt->message_transport_type}, |
312 |
'The transport type is stored in the object.'); |
313 |
my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
314 |
borrower_message_preference_id => $preference->borrower_message_preference_id, |
315 |
}); |
316 |
is($stored_transports->next->message_transport_type, $mtt->message_transport_type, |
317 |
'The transport type is stored in the database.'); |
318 |
|
319 |
$schema->storage->txn_rollback; |
320 |
}; |
321 |
}; |
322 |
|
176 |
subtest 'Test adding a new preference with invalid parameters' => sub { |
323 |
subtest 'Test adding a new preference with invalid parameters' => sub { |
177 |
plan tests => 4; |
324 |
plan tests => 4; |
178 |
|
325 |
|
Lines 203-209
subtest 'Test adding a new preference with invalid parameters' => sub {
Link Here
|
203 |
}; |
350 |
}; |
204 |
|
351 |
|
205 |
subtest 'Bad parameter' => sub { |
352 |
subtest 'Bad parameter' => sub { |
206 |
plan tests => 8; |
353 |
plan tests => 13; |
207 |
|
354 |
|
208 |
$schema->storage->txn_begin; |
355 |
$schema->storage->txn_begin; |
209 |
|
356 |
|
Lines 250-255
subtest 'Test adding a new preference with invalid parameters' => sub {
Link Here
|
250 |
is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
397 |
is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
251 |
.' was the days_in_advance.'); |
398 |
.' was the days_in_advance.'); |
252 |
|
399 |
|
|
|
400 |
eval { Koha::Patron::Message::Preference->new({ |
401 |
borrowernumber => $patron->{'borrowernumber'}, |
402 |
message_transport_types => ['nonexistent'] |
403 |
})->store }; |
404 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
405 |
'Adding a message preference with invalid message_transport_type' |
406 |
.' => Koha::Exceptions::BadParameter'); |
407 |
is ($@->parameter, 'message_transport_type', 'The previous exception tells us it' |
408 |
.' was the message_transport_type.'); |
409 |
eval { |
410 |
Koha::Patron::Message::Preference->new({ |
411 |
borrowernumber => $patron->{'borrowernumber'}, |
412 |
message_attribute_id => $attribute->message_attribute_id, |
413 |
message_transport_types => ['sms'], |
414 |
wants_digest => 1, |
415 |
})->store }; |
416 |
is (ref $@, 'Koha::Exceptions::BadParameter', |
417 |
'Adding a message preference with invalid message_transport_type' |
418 |
.' => Koha::Exceptions::BadParameter'); |
419 |
is ($@->parameter, 'message_transport_type', 'The previous exception tells us it' |
420 |
.' was the message_transport_type.'); |
421 |
like ($@->error, qr/^Message transport option/, 'Exception s because of given' |
422 |
.' message_transport_type is not a valid option.'); |
423 |
|
253 |
$schema->storage->txn_rollback; |
424 |
$schema->storage->txn_rollback; |
254 |
}; |
425 |
}; |
255 |
|
426 |
|
Lines 395-398
sub build_a_test_category_preference {
Link Here
|
395 |
return ($default, $mtt1, $mtt2); |
566 |
return ($default, $mtt1, $mtt2); |
396 |
} |
567 |
} |
397 |
|
568 |
|
|
|
569 |
sub build_a_test_complete_preference { |
570 |
my ($params) = @_; |
571 |
|
572 |
my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params); |
573 |
my $patron = $params->{patron}; |
574 |
$patron->set_default_messaging_preferences; |
575 |
return (Koha::Patron::Message::Preferences->search({ |
576 |
borrowernumber => $patron->borrowernumber |
577 |
})->next, $mtt1, $mtt2); |
578 |
} |
579 |
|
398 |
1; |
580 |
1; |
399 |
- |
|
|