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

(-)a/Koha/Patron/Message/Preference.pm (-42 / +53 lines)
Lines 228-233 sub message_transport_types { Link Here
228
    }
228
    }
229
}
229
}
230
230
231
=head3 mtt_deliverable
232
233
$preference->mtt_deliverable('sms'[, $borrowernumer]);
234
235
Returns true if given message transport type can be used to deliver message to
236
patron.
237
238
By default, uses the borrowernumber bound to C<$preference>, but this may be
239
overridden by providing optional C<$borrowernumber> parameter.
240
241
=cut
242
243
sub mtt_deliverable {
244
    my ( $self, $mtt, $borrowernumber ) = @_;
245
246
    $borrowernumber //= $self->borrowernumber;
247
248
    return 0 unless ($borrowernumber);
249
250
    my $patron = Koha::Patrons->find($self->borrowernumber);
251
252
    return (( $mtt eq 'email' and $patron->notice_email_address ) # No email address
253
         or ( $mtt eq 'sms'   and $patron->smsalertnumber ) # No SMS number
254
         or ( $mtt eq 'itiva' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
255
         or ( $mtt eq 'phone' and $patron->phone )) # No phone number to call
256
    ? 1 : 0;
257
}
258
231
=head3 set
259
=head3 set
232
260
233
$preference->set({
261
$preference->set({
Lines 404-451 sub _set_message_transport_types { Link Here
404
                );
432
                );
405
            }
433
            }
406
            if (defined $self->borrowernumber) {
434
            if (defined $self->borrowernumber) {
407
                my $patron = Koha::Patrons->find($self->borrowernumber);
435
                if ( ! $self->mtt_deliverable($type) ) {
408
                if ($type eq 'email') {
436
                    Koha::Exceptions::Patron::Message::Preference::EmailAddressRequired->throw(
409
                    if ( !$patron->email )
437
                        error => 'Patron has not set email address, '.
410
                    {
438
                                  'cannot use email as message transport',
411
                        Koha::Exceptions::Patron::Message::Preference::EmailAddressRequired->throw(
439
                        message_name => $self->message_name,
412
                            error => 'Patron has not set email address, '.
440
                        borrowernumber => $self->borrowernumber,
413
                                     'cannot use email as message transport',
441
                    ) if $type eq 'email';
414
                            message_name => $self->message_name,
442
                    Koha::Exceptions::Patron::Message::Preference::TalkingTechItivaPhoneNotificationRequired->throw(
415
                            borrowernumber => $self->borrowernumber,
443
                        error => 'System preference TalkingTechItivaPhoneNotification disabled'.
416
                        );
444
                                 'cannot use itiva as message transport',
417
                    }
445
                        message_name => $self->message_name,
418
                }
446
                        borrowernumber => $self->borrowernumber,
419
                elsif ($type eq 'itiva') {
447
                    ) if $type eq 'itiva';
420
                    if (!C4::Context->preference('TalkingTechItivaPhoneNotification')) {
448
                    Koha::Exceptions::Patron::Message::Preference::PhoneNumberRequired->throw(
421
                        Koha::Exceptions::Patron::Message::Preference::TalkingTechItivaPhoneNotificationRequired->throw(
449
                        error => 'Patron has not set phone number'.
422
                            error => 'System preference TalkingTechItivaPhoneNotification disabled'.
450
                                 'cannot use phone as message transport',
423
                                     'cannot use itiva as message transport',
451
                        message_name => $self->message_name,
424
                            message_name => $self->message_name,
452
                        borrowernumber => $self->borrowernumber,
425
                            borrowernumber => $self->borrowernumber,
453
                    ) if $type eq 'phone';
426
                        );
454
                    Koha::Exceptions::Patron::Message::Preference::SMSNumberRequired->throw(
427
                    }
455
                        error => 'Patron has not set SMS number'.
428
                }
456
                                 'cannot use sms as message transport',
429
                elsif ($type eq 'phone') {
457
                        message_name => $self->message_name,
430
                    if ( !$patron->phone ) {
458
                        borrowernumber => $self->borrowernumber,
431
                        Koha::Exceptions::Patron::Message::Preference::PhoneNumberRequired->throw(
459
                    ) if $type eq 'sms';
432
                            error => 'Patron has not set phone number'.
433
                                     'cannot use phone as message transport',
434
                            message_name => $self->message_name,
435
                            borrowernumber => $self->borrowernumber,
436
                        );
437
                    }
438
439
                }
440
                elsif ($type eq 'sms') {
441
                    if ( !$patron->smsalertnumber ){
442
                        Koha::Exceptions::Patron::Message::Preference::SMSNumberRequired->throw(
443
                            error => 'Patron has not set SMS number'.
444
                                     'cannot use sms as message transport',
445
                            message_name => $self->message_name,
446
                            borrowernumber => $self->borrowernumber,
447
                        );
448
                    }
449
                }
460
                }
450
            }
461
            }
451
            $self->{'_message_transport_types'}->{$type}
462
            $self->{'_message_transport_types'}->{$type}
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-3 / +80 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 7;
22
use Test::More tests => 8;
23
23
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
Lines 373-379 HERE Link Here
373
            is_digest              => 1
373
            is_digest              => 1
374
        })->store;
374
        })->store;
375
        t::lib::Mocks::mock_preference('TalkingTechItivaPhoneNotification', 0);
375
        t::lib::Mocks::mock_preference('TalkingTechItivaPhoneNotification', 0);
376
        $patron->set({ email => '', phone => '', smsalertnumber => '' })->store;
376
        $patron->set({ email => '', emailpro => '', B_email => '', phone => '', smsalertnumber => '' })->store;
377
        eval {
377
        eval {
378
            $preference->message_transport_types('email')->store;
378
            $preference->message_transport_types('email')->store;
379
        };
379
        };
Lines 473-478 subtest 'Test Koha::Patron::Message::Preference->message_name' => sub { Link Here
473
    $schema->storage->txn_rollback;
473
    $schema->storage->txn_rollback;
474
};
474
};
475
475
476
subtest 'Test Koha::Patron::Message::Preference->mtt_deliverable' => sub {
477
    plan tests => 10;
478
479
    $schema->storage->txn_begin;
480
481
    my $patron      = $builder->build_object({ class => 'Koha::Patrons' });
482
    my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({
483
        patron => $patron
484
    });
485
486
    # Test email and smsalertnumber validation
487
    eval { Koha::Patron::Message::Transport::Types->new({
488
            message_transport_type => 'email'
489
        })->store };
490
    eval { Koha::Patron::Message::Transport::Types->new({
491
            message_transport_type => 'sms'
492
        })->store };
493
    eval { Koha::Patron::Message::Transport::Types->new({
494
            message_transport_type => 'phone'
495
        })->store };
496
    eval { Koha::Patron::Message::Transport::Types->new({
497
            message_transport_type => 'itiva'
498
        })->store };
499
    Koha::Patron::Message::Transport->new({
500
        message_attribute_id   => $preference->message_attribute_id,
501
        message_transport_type => 'email',
502
        is_digest              => 1
503
    })->store;
504
    Koha::Patron::Message::Transport->new({
505
        message_attribute_id   => $preference->message_attribute_id,
506
        message_transport_type => 'sms',
507
        is_digest              => 1
508
    })->store;
509
    Koha::Patron::Message::Transport->new({
510
        message_attribute_id   => $preference->message_attribute_id,
511
        message_transport_type => 'phone',
512
        is_digest              => 1
513
    })->store;
514
    Koha::Patron::Message::Transport->new({
515
        message_attribute_id   => $preference->message_attribute_id,
516
        message_transport_type => 'itiva',
517
        is_digest              => 1
518
    })->store;
519
    $patron->set({
520
        email => 'nobody@koha-community.org',
521
        emailpro => 'nobody@koha-community.org',
522
        B_email => 'nobody@koha-community.org',
523
        smsalertnumber => '123',
524
        phone => '123',
525
    })->store;
526
    t::lib::Mocks::mock_preference('TalkingTechItivaPhoneNotification', 1);
527
528
    is($preference->mtt_deliverable('email'), 1, 'mtt_deliverable - email');
529
    is($preference->mtt_deliverable('itiva'), 1, 'mtt_deliverable - itiva');
530
    is($preference->mtt_deliverable('phone'), 1, 'mtt_deliverable - phone');
531
    is($preference->mtt_deliverable('sms'), 1, 'mtt_deliverable - sms');
532
533
    $patron->set({email => '',})->store;
534
    is($preference->mtt_deliverable('email'), 1, 'mtt_deliverable - emailpro');
535
536
    $patron->set({emailpro => '',})->store;
537
    is($preference->mtt_deliverable('email'), 1, 'mtt_deliverable - B_email');
538
539
    $patron->set({B_email => '',})->store;
540
    is($preference->mtt_deliverable('email'), 0, 'mtt_deliverable - email false');
541
542
    t::lib::Mocks::mock_preference('TalkingTechItivaPhoneNotification', 0);
543
    is($preference->mtt_deliverable('itiva'), 0, 'mtt_deliverable - itiva false');
544
545
    $patron->set({phone => '',})->store;
546
    is($preference->mtt_deliverable('phone'), 0, 'mtt_deliverable - phone false');
547
548
    $patron->set({smsalertnumber => '',})->store;
549
    is($preference->mtt_deliverable('sms'), 0, 'mtt_deliverable - smsalertnumber false');
550
551
    $schema->storage->txn_rollback;
552
};
553
476
subtest 'Test adding a new preference with invalid parameters' => sub {
554
subtest 'Test adding a new preference with invalid parameters' => sub {
477
    plan tests => 4;
555
    plan tests => 4;
478
556
479
- 

Return to bug 17499