View | Details | Raw Unified | Return to bug 17499
Collapse All | Expand All

(-)a/Koha/Patron/Message/Preference.pm (-28 / +38 lines)
Lines 198-208 sub message_transport_types { Link Here
198
        if ($self->{'_message_transport_types'}) {
198
        if ($self->{'_message_transport_types'}) {
199
            return $self->{'_message_transport_types'};
199
            return $self->{'_message_transport_types'};
200
        }
200
        }
201
        map { $self->{'_message_transport_types'}->{$_->message_transport_type} =
201
        map {
202
                Koha::Patron::Message::Transports->find({
202
            my $transport = Koha::Patron::Message::Transports->find({
203
                    message_attribute_id => $self->message_attribute_id,
203
                message_attribute_id => $self->message_attribute_id,
204
                    message_transport_type => $_->message_transport_type,
204
                message_transport_type => $_->message_transport_type,
205
                    is_digest => $self->wants_digest})->letter_code }
205
                is_digest => $self->wants_digest
206
            });
207
            unless ($transport) {
208
                my $logger = Koha::Logger->get;
209
                $logger->warn(
210
                    $self->message_name . ' has no transport with '.
211
                    $_->message_transport_type . ' (digest: '.
212
                    ($self->wants_digest ? 'yes':'no').').'
213
                );
214
            }
215
            $self->{'_message_transport_types'}->{$_->message_transport_type}
216
                = $transport ? $transport->letter_code : ' ';
217
        }
206
        Koha::Patron::Message::Transport::Preferences->search({
218
        Koha::Patron::Message::Transport::Preferences->search({
207
            borrower_message_preference_id => $self->borrower_message_preference_id,
219
            borrower_message_preference_id => $self->borrower_message_preference_id,
208
        })->as_list;
220
        })->as_list;
Lines 228-239 Sets preference object values and additionally message_transport_types if given. Link Here
228
sub set {
240
sub set {
229
    my ($self, $params) = @_;
241
    my ($self, $params) = @_;
230
242
231
    if ($params && $params->{'message_transport_types'}) {
243
    my $mtt = $params->{'message_transport_types'};
232
        $self->message_transport_types($params->{'message_transport_types'});
244
    delete $params->{'message_transport_types'};
233
        delete $params->{'message_transport_types'};
245
246
    $self->SUPER::set($params) if $params;
247
    if ($mtt) {
248
        $self->message_transport_types($mtt);
234
    }
249
    }
235
250
236
    return $self->SUPER::set($params) if $params;
251
    return $self;
237
}
252
}
238
253
239
=head3 store
254
=head3 store
Lines 354-360 sub validate { Link Here
354
    if (defined $self->wants_digest) {
369
    if (defined $self->wants_digest) {
355
        my $transports = Koha::Patron::Message::Transports->search({
370
        my $transports = Koha::Patron::Message::Transports->search({
356
            message_attribute_id => $self->message_attribute_id,
371
            message_attribute_id => $self->message_attribute_id,
357
            is_digest            => $self->wants_digest,
372
            is_digest            => $self->wants_digest ? 1 : 0,
358
        });
373
        });
359
        Koha::Exceptions::BadParameter->throw(
374
        Koha::Exceptions::BadParameter->throw(
360
            error => (!$self->wants_digest ? 'No d' : 'D').'igest not available '.
375
            error => (!$self->wants_digest ? 'No d' : 'D').'igest not available '.
Lines 377-387 sub _set_message_transport_types { Link Here
377
    $self->_validate_message_transport_types({ message_transport_types => $types });
392
    $self->_validate_message_transport_types({ message_transport_types => $types });
378
    foreach my $type (@$types) {
393
    foreach my $type (@$types) {
379
        unless (exists $self->{'_message_transport_types'}->{$type}) {
394
        unless (exists $self->{'_message_transport_types'}->{$type}) {
380
            $self->{'_message_transport_types'}->{$type} =
395
            my $transport = Koha::Patron::Message::Transports->find({
381
            Koha::Patron::Message::Transports->find({
382
                message_attribute_id => $self->message_attribute_id,
396
                message_attribute_id => $self->message_attribute_id,
383
                message_transport_type => $type,
397
                message_transport_type => $type
384
                is_digest => $self->wants_digest})->letter_code;
398
            });
399
            unless ($transport) {
400
                Koha::Exceptions::BadParameter->throw(
401
                    error => 'No transport configured for '.$self->message_name.
402
                        " transport type $type.",
403
                    parameter => 'message_transport_types'
404
                );
405
            }
406
            $self->{'_message_transport_types'}->{$type}
407
                = $transport->letter_code;
385
        }
408
        }
386
    }
409
    }
387
    return $self;
410
    return $self;
Lines 402-421 sub _validate_message_transport_types { Link Here
402
            })) {
425
            })) {
403
                Koha::Exceptions::BadParameter->throw(
426
                Koha::Exceptions::BadParameter->throw(
404
                    error => "Message transport type '$type' does not exist",
427
                    error => "Message transport type '$type' does not exist",
405
                    parameter => 'message_transport_type',
428
                    parameter => 'message_transport_types',
406
                );
407
            }
408
            my $tmp = (ref($self) eq __PACKAGE__) ? $self->unblessed : $params;
409
            unless (Koha::Patron::Message::Transports->find({
410
                    message_transport_type => $type,
411
                    message_attribute_id => $tmp->{'message_attribute_id'},
412
                    is_digest => $tmp->{'wants_digest'},
413
            })) {
414
                Koha::Exceptions::BadParameter->throw(
415
                    error => "Message transport option '$type' (".
416
                    ($tmp->{'wants_digest'} ? 'digest':'no digest').") for ".
417
                    $self->message_name . 'does not exist',
418
                    parameter => 'message_transport_type',
419
                );
429
                );
420
            }
430
            }
421
        }
431
        }
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-10 / +67 lines)
Lines 24-29 use Test::More tests => 7; Link Here
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
26
26
27
use C4::Context;
28
27
use Koha::Notice::Templates;
29
use Koha::Notice::Templates;
28
use Koha::Patron::Categories;
30
use Koha::Patron::Categories;
29
use Koha::Patron::Message::Attributes;
31
use Koha::Patron::Message::Attributes;
Lines 31-36 use Koha::Patron::Message::Transport::Types; Link Here
31
use Koha::Patron::Message::Transports;
33
use Koha::Patron::Message::Transports;
32
use Koha::Patrons;
34
use Koha::Patrons;
33
35
36
use File::Temp qw/tempfile/;
37
use Log::Log4perl;
38
34
my $schema  = Koha::Database->new->schema;
39
my $schema  = Koha::Database->new->schema;
35
my $builder = t::lib::TestBuilder->new;
40
my $builder = t::lib::TestBuilder->new;
36
41
Lines 189-195 subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub Link Here
189
       'Method message_transport_types available');
194
       'Method message_transport_types available');
190
195
191
    subtest 'get message_transport_types' => sub {
196
    subtest 'get message_transport_types' => sub {
192
        plan tests => 4;
197
        plan tests => 5;
193
198
194
        $schema->storage->txn_begin;
199
        $schema->storage->txn_begin;
195
200
Lines 229-234 subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub Link Here
229
        ok(!$preference->message_transport_types->{'nonexistent'},
234
        ok(!$preference->message_transport_types->{'nonexistent'},
230
           'Didn\'t find nonexistent transport type.');
235
           'Didn\'t find nonexistent transport type.');
231
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
232
        $schema->storage->txn_rollback;
281
        $schema->storage->txn_rollback;
233
    };
282
    };
234
283
Lines 432-453 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
432
        is (ref $@, 'Koha::Exceptions::BadParameter',
481
        is (ref $@, 'Koha::Exceptions::BadParameter',
433
            'Adding a message preference with invalid message_transport_type'
482
            'Adding a message preference with invalid message_transport_type'
434
            .' => Koha::Exceptions::BadParameter');
483
            .' => Koha::Exceptions::BadParameter');
435
        is ($@->parameter, 'message_transport_type', 'The previous exception tells us it'
484
        is ($@->parameter, 'message_transport_types', 'The previous exception '
436
            .' was the message_transport_type.');
485
            .'tells us it was the message_transport_types.');
486
487
        my $mtt_new = build_a_test_transport_type();
437
        eval {
488
        eval {
438
            Koha::Patron::Message::Preference->new({
489
            Koha::Patron::Message::Preference->new({
439
                borrowernumber => $patron->borrowernumber,
490
                borrowernumber => $patron->borrowernumber,
440
                message_attribute_id => $attribute->message_attribute_id,
491
                message_attribute_id => $attribute->message_attribute_id,
441
                message_transport_types => ['sms'],
492
                message_transport_types => [$mtt_new->message_transport_type],
442
                wants_digest => 1,
493
                wants_digest => 1,
443
            })->store };
494
            })->store };
444
        is (ref $@, 'Koha::Exceptions::BadParameter',
495
        is (ref $@, 'Koha::Exceptions::BadParameter',
445
            'Adding a message preference with invalid message_transport_type'
496
            'Adding a message preference with invalid message_transport_type'
446
            .' => Koha::Exceptions::BadParameter');
497
           .' => Koha::Exceptions::BadParameter');
447
        is ($@->parameter, 'message_transport_type', 'The previous exception tells us it'
498
        is ($@->parameter, 'message_transport_types', 'The previous exception '
448
            .' was the message_transport_type.');
499
            .'tells us it was the message_transport_types.');
449
        like ($@->error, qr/^Message transport option/, 'Exception s because of given'
500
        like ($@->error, qr/^No transport configured/, 'Exception is because of '
450
            .' message_transport_type is not a valid option.');
501
            .'given message_transport_type is not a valid option.');
451
502
452
        eval {
503
        eval {
453
            Koha::Patron::Message::Preference->new({
504
            Koha::Patron::Message::Preference->new({
Lines 643-646 sub build_a_test_complete_preference { Link Here
643
    })->next, $mtt1, $mtt2);
694
    })->next, $mtt1, $mtt2);
644
}
695
}
645
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
646
1;
704
1;
647
- 

Return to bug 17499