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

(-)a/Koha/Patron/Message/Preference.pm (-42 / +53 lines)
Lines 229-234 sub message_transport_types { Link Here
229
    }
229
    }
230
}
230
}
231
231
232
=head3 mtt_deliverable
233
234
$preference->mtt_deliverable('sms'[, $borrowernumer]);
235
236
Returns true if given message transport type can be used to deliver message to
237
patron.
238
239
By default, uses the borrowernumber bound to C<$preference>, but this may be
240
overridden by providing optional C<$borrowernumber> parameter.
241
242
=cut
243
244
sub mtt_deliverable {
245
    my ( $self, $mtt, $borrowernumber ) = @_;
246
247
    $borrowernumber //= $self->borrowernumber;
248
249
    return 0 unless ($borrowernumber);
250
251
    my $patron = Koha::Patrons->find($self->borrowernumber);
252
253
    return (( $mtt eq 'email' and $patron->notice_email_address ) # No email address
254
         or ( $mtt eq 'sms'   and $patron->smsalertnumber ) # No SMS number
255
         or ( $mtt eq 'itiva' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
256
         or ( $mtt eq 'phone' and $patron->phone )) # No phone number to call
257
    ? 1 : 0;
258
}
259
232
=head3 set
260
=head3 set
233
261
234
$preference->set({
262
$preference->set({
Lines 407-454 sub _set_message_transport_types { Link Here
407
                );
435
                );
408
            }
436
            }
409
            if (defined $self->borrowernumber) {
437
            if (defined $self->borrowernumber) {
410
                my $patron = Koha::Patrons->find($self->borrowernumber);
438
                if ( ! $self->mtt_deliverable($type) ) {
411
                if ($type eq 'email') {
439
                    Koha::Exceptions::Patron::Message::Preference::EmailAddressRequired->throw(
412
                    if ( !$patron->email )
440
                        error => 'Patron has not set email address, '.
413
                    {
441
                                  'cannot use email as message transport',
414
                        Koha::Exceptions::Patron::Message::Preference::EmailAddressRequired->throw(
442
                        message_name => $self->message_name,
415
                            error => 'Patron has not set email address, '.
443
                        borrowernumber => $self->borrowernumber,
416
                                     'cannot use email as message transport',
444
                    ) if $type eq 'email';
417
                            message_name => $self->message_name,
445
                    Koha::Exceptions::Patron::Message::Preference::TalkingTechItivaPhoneNotificationRequired->throw(
418
                            borrowernumber => $self->borrowernumber,
446
                        error => 'System preference TalkingTechItivaPhoneNotification disabled'.
419
                        );
447
                                 'cannot use itiva as message transport',
420
                    }
448
                        message_name => $self->message_name,
421
                }
449
                        borrowernumber => $self->borrowernumber,
422
                elsif ($type eq 'itiva') {
450
                    ) if $type eq 'itiva';
423
                    if (!C4::Context->preference('TalkingTechItivaPhoneNotification')) {
451
                    Koha::Exceptions::Patron::Message::Preference::PhoneNumberRequired->throw(
424
                        Koha::Exceptions::Patron::Message::Preference::TalkingTechItivaPhoneNotificationRequired->throw(
452
                        error => 'Patron has not set phone number'.
425
                            error => 'System preference TalkingTechItivaPhoneNotification disabled'.
453
                                 'cannot use phone as message transport',
426
                                     'cannot use itiva as message transport',
454
                        message_name => $self->message_name,
427
                            message_name => $self->message_name,
455
                        borrowernumber => $self->borrowernumber,
428
                            borrowernumber => $self->borrowernumber,
456
                    ) if $type eq 'phone';
429
                        );
457
                    Koha::Exceptions::Patron::Message::Preference::SMSNumberRequired->throw(
430
                    }
458
                        error => 'Patron has not set SMS number'.
431
                }
459
                                 'cannot use sms as message transport',
432
                elsif ($type eq 'phone') {
460
                        message_name => $self->message_name,
433
                    if ( !$patron->phone ) {
461
                        borrowernumber => $self->borrowernumber,
434
                        Koha::Exceptions::Patron::Message::Preference::PhoneNumberRequired->throw(
462
                    ) if $type eq 'sms';
435
                            error => 'Patron has not set phone number'.
436
                                     'cannot use phone as message transport',
437
                            message_name => $self->message_name,
438
                            borrowernumber => $self->borrowernumber,
439
                        );
440
                    }
441
                    
442
                }
443
                elsif ($type eq 'sms') {
444
                    if ( !$patron->smsalertnumber ){
445
                        Koha::Exceptions::Patron::Message::Preference::SMSNumberRequired->throw(
446
                            error => 'Patron has not set SMS number'.
447
                                     'cannot use sms as message transport',
448
                            message_name => $self->message_name,
449
                            borrowernumber => $self->borrowernumber,
450
                        );
451
                    }
452
                }
463
                }
453
            }
464
            }
454
            $self->{'_message_transport_types'}->{$type}
465
            $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