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

(-)a/t/db_dependent/Letters.t (-28 / +84 lines)
Lines 30-35 use MARC::Record; Link Here
30
use utf8;
30
use utf8;
31
31
32
my ( $email_object, $sendmail_params );
32
my ( $email_object, $sendmail_params );
33
our $send_or_die_count = 0;
33
34
34
my $email_sender_module = Test::MockModule->new('Email::Stuffer');
35
my $email_sender_module = Test::MockModule->new('Email::Stuffer');
35
$email_sender_module->mock(
36
$email_sender_module->mock(
Lines 38-43 $email_sender_module->mock( Link Here
38
        ( $email_object, $sendmail_params ) = @_;
39
        ( $email_object, $sendmail_params ) = @_;
39
        my $str = $email_object->email->as_string;
40
        my $str = $email_object->email->as_string;
40
        unlike $str, qr/I =C3=A2=C2=99=C2=A5 Koha=/, "Content is not double encoded";
41
        unlike $str, qr/I =C3=A2=C2=99=C2=A5 Koha=/, "Content is not double encoded";
42
        $send_or_die_count++;
41
        warn "Fake send_or_die";
43
        warn "Fake send_or_die";
42
    }
44
    }
43
);
45
);
Lines 55-63 use Koha::Acquisition::Booksellers; Link Here
55
use Koha::Acquisition::Bookseller::Contacts;
57
use Koha::Acquisition::Bookseller::Contacts;
56
use Koha::Acquisition::Orders;
58
use Koha::Acquisition::Orders;
57
use Koha::Libraries;
59
use Koha::Libraries;
60
use Koha::Notice::Messages;
58
use Koha::Notice::Templates;
61
use Koha::Notice::Templates;
62
use Koha::Notice::Util;
59
use Koha::Patrons;
63
use Koha::Patrons;
60
use Koha::Subscriptions;
64
use Koha::Subscriptions;
65
61
my $schema = Koha::Database->schema;
66
my $schema = Koha::Database->schema;
62
$schema->storage->txn_begin();
67
$schema->storage->txn_begin();
63
68
Lines 156-162 Koha::Notice::Messages->find($messages->[0]->{message_id})->time_queued($yesterd Link Here
156
my $messages_processed = C4::Letters::SendQueuedMessages( { type => 'email' });
161
my $messages_processed = C4::Letters::SendQueuedMessages( { type => 'email' });
157
is($messages_processed, 0, 'No queued messages processed if type limit passed with unused type');
162
is($messages_processed, 0, 'No queued messages processed if type limit passed with unused type');
158
$messages_processed = C4::Letters::SendQueuedMessages( { type => 'sms' });
163
$messages_processed = C4::Letters::SendQueuedMessages( { type => 'sms' });
159
is($messages_processed, 1, 'All queued messages processed, found correct number of messages with type limit');
164
is($messages_processed, 0, 'All queued messages processed, nothing sent');
160
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
165
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
161
is(
166
is(
162
    $messages->[0]->{status},
167
    $messages->[0]->{status},
Lines 958-964 EOF Link Here
958
};
963
};
959
964
960
subtest 'Test where parameter for SendQueuedMessages' => sub {
965
subtest 'Test where parameter for SendQueuedMessages' => sub {
961
    plan tests => 1;
966
    plan tests => 3;
962
967
963
    my $dbh = C4::Context->dbh;
968
    my $dbh = C4::Context->dbh;
964
969
Lines 968-974 subtest 'Test where parameter for SendQueuedMessages' => sub { Link Here
968
        categorycode => $patron_category,
973
        categorycode => $patron_category,
969
        branchcode   => $library->{branchcode},
974
        branchcode   => $library->{branchcode},
970
        dateofbirth  => $date,
975
        dateofbirth  => $date,
971
        smsalertnumber => undef,
972
    })->store->borrowernumber;
976
    })->store->borrowernumber;
973
977
974
    $dbh->do(q|DELETE FROM message_queue|);
978
    $dbh->do(q|DELETE FROM message_queue|);
Lines 1011-1025 subtest 'Test where parameter for SendQueuedMessages' => sub { Link Here
1011
        'message_transport_type' => 'sms',
1015
        'message_transport_type' => 'sms',
1012
        'from_address'           => 'from@example.com'
1016
        'from_address'           => 'from@example.com'
1013
    };
1017
    };
1014
    C4::Letters::EnqueueLetter($my_message);
1018
    my @id = ( C4::Letters::EnqueueLetter($my_message),
1015
    C4::Letters::EnqueueLetter($my_message2);
1019
        C4::Letters::EnqueueLetter($my_message2),
1016
    C4::Letters::EnqueueLetter($my_message3);
1020
        C4::Letters::EnqueueLetter($my_message3),
1017
    my $messages_processed = C4::Letters::SendQueuedMessages( { where => q{content NOT LIKE '%skipped%'} } );
1021
    );
1018
    is( $messages_processed, 2, "Correctly skipped processing one message containing the work 'skipped' in contents" );
1022
    C4::Letters::SendQueuedMessages({
1023
        # Test scalar/arrayref in parameter too
1024
        letter_code => [ 'TEST_MESSAGE', 'SOMETHING_NOT_THERE' ],
1025
        type => 'sms',
1026
        where => q{content NOT LIKE '%skipped%'},
1027
    });
1028
    is( Koha::Notice::Messages->find( $id[0] )->status, 'failed', 'Processed but failed' );
1029
    is( Koha::Notice::Messages->find( $id[1] )->status, 'failed', 'Processed but failed' );
1030
    is( Koha::Notice::Messages->find( $id[2] )->status, 'pending', 'Skipped, still pending' );
1019
};
1031
};
1020
1032
1021
subtest 'Test limit parameter for SendQueuedMessages' => sub {
1033
subtest 'Test limit parameter for SendQueuedMessages' => sub {
1022
    plan tests => 3;
1034
    plan tests => 18;
1023
1035
1024
    my $dbh = C4::Context->dbh;
1036
    my $dbh = C4::Context->dbh;
1025
1037
Lines 1029-1035 subtest 'Test limit parameter for SendQueuedMessages' => sub { Link Here
1029
        categorycode => $patron_category,
1041
        categorycode => $patron_category,
1030
        branchcode   => $library->{branchcode},
1042
        branchcode   => $library->{branchcode},
1031
        dateofbirth  => $date,
1043
        dateofbirth  => $date,
1032
        smsalertnumber => undef,
1044
        email          => 'shouldnotwork@wrong.net',
1033
    })->store->borrowernumber;
1045
    })->store->borrowernumber;
1034
1046
1035
    $dbh->do(q|DELETE FROM message_queue|);
1047
    $dbh->do(q|DELETE FROM message_queue|);
Lines 1043-1070 subtest 'Test limit parameter for SendQueuedMessages' => sub { Link Here
1043
        },
1055
        },
1044
        'borrowernumber'         => $borrowernumber,
1056
        'borrowernumber'         => $borrowernumber,
1045
        'to_address'             => undef,
1057
        'to_address'             => undef,
1046
        'message_transport_type' => 'sms',
1058
        'message_transport_type' => 'email',
1047
        'from_address'           => 'from@example.com'
1059
        'from_address'           => 'from@example.com'
1048
    };
1060
    };
1049
    C4::Letters::EnqueueLetter($my_message);
1061
    C4::Letters::EnqueueLetter($my_message) for 1..5;
1050
    C4::Letters::EnqueueLetter($my_message);
1062
1051
    C4::Letters::EnqueueLetter($my_message);
1063
    $send_or_die_count = 0; # reset
1052
    C4::Letters::EnqueueLetter($my_message);
1064
    my $messages_sent;
1053
    C4::Letters::EnqueueLetter($my_message);
1065
    my $regex = qr|Fake send_or_die|;
1054
    my $messages_processed = C4::Letters::SendQueuedMessages( { limit => 1 } );
1066
    warning_like {
1055
    is( $messages_processed, 1,
1067
        $messages_sent = C4::Letters::SendQueuedMessages( { limit => 1 } ) }
1056
        'Processed 1 message with limit of 1 and 5 unprocessed messages' );
1068
        $regex,
1057
    $messages_processed = C4::Letters::SendQueuedMessages( { limit => 2 } );
1069
        "SendQueuedMessages with limit 1";
1058
    is( $messages_processed, 2,
1070
    is( $messages_sent, 1,
1059
        'Processed 2 message with limit of 2 and 4 unprocessed messages' );
1071
        'Sent 1 message with limit of 1 and 5 unprocessed messages' );
1060
    $messages_processed = C4::Letters::SendQueuedMessages( { limit => 3 } );
1072
1061
    is( $messages_processed, 2,
1073
    warnings_like {
1062
        'Processed 2 message with limit of 3 and 2 unprocessed messages' );
1074
        $messages_sent = C4::Letters::SendQueuedMessages( { limit => 2 } ) }
1075
        [ map { $regex } 1..2 ],
1076
        "SendQueuedMessages with limit 2";
1077
    is( $messages_sent, 2,
1078
        'Sent 2 messages with limit of 2 and 4 unprocessed messages' );
1079
1080
    warnings_like {
1081
        $messages_sent = C4::Letters::SendQueuedMessages( { limit => 3 } ) }
1082
        [ map { $regex } 1..2 ],
1083
        "SendQueuedMessages with limit 3";
1084
    is( $messages_sent, 2,
1085
        'Sent 2 messages with limit of 3 and 2 unprocessed messages' );
1086
1087
    is( $send_or_die_count, 5, '5 messages sent' );
1088
    # Mimic correct status in queue for next tests
1089
    Koha::Notice::Messages->search({ to_address => { 'LIKE', '%wrong.net' }})->update({ status => 'sent' });
1090
1091
    # Now add a few domain limits too, sending 2 more mails to wrongnet, 2 to fake1, 2 to fake2
1092
    # Since we already sent 5 to wrong.net, we expect one deferral when limiting to 6
1093
    # Similarly we arrange for 1 deferral for fake1, and 2 for fake2
1094
    # We set therefore limit to 3 sent messages: we expect 2 sent, 4 deferred (so 6 processed)
1095
    t::lib::Mocks::mock_config( 'message_domain_limits', { domain => [
1096
        { name => 'wrong.net',    limit => 6, unit => '1m' },
1097
        { name => 'fake1.domain', limit => 1, unit => '1m' },
1098
        { name => 'fake2.domain', limit => 0, unit => '1m' },
1099
    ]});
1100
    C4::Letters::EnqueueLetter($my_message) for 1..2;
1101
    $my_message->{to_address} = 'someone@fake1.domain';
1102
    C4::Letters::EnqueueLetter($my_message) for 1..2;
1103
    $my_message->{to_address} = 'another@fake2.domain';
1104
    C4::Letters::EnqueueLetter($my_message) for 1..2;
1105
    my $mocked_util = Test::MockModule->new('Koha::Notice::Util');
1106
    my $count_exceeds_calls = 0;
1107
    $mocked_util->mock( 'exceeds_limit', sub {
1108
        $count_exceeds_calls++;
1109
        $mocked_util->original('exceeds_limit')->(@_);
1110
    });
1111
    warnings_like {
1112
        $messages_sent = C4::Letters::SendQueuedMessages({ limit => 3 }) }
1113
        [ qr/wrong.net reached limit/, $regex, qr/fake1.domain reached limit/, $regex ],
1114
        "SendQueuedMessages with limit 2 and domain limits";
1115
    is( $messages_sent, 2, 'Only expecting 2 sent messages' );
1116
    is(  Koha::Notice::Messages->search({ status => 'pending' })->count, 4, 'Still 4 pending' );
1117
    is( $count_exceeds_calls, 6, 'We saw 6 messages while checking domain limits: so we deferred 4' );
1063
};
1118
};
1064
1119
1065
subtest 'Test message_id parameter for SendQueuedMessages' => sub {
1120
subtest 'Test message_id parameter for SendQueuedMessages' => sub {
1066
1121
1067
    plan tests => 7;
1122
    plan tests => 8;
1068
1123
1069
    my $dbh = C4::Context->dbh;
1124
    my $dbh = C4::Context->dbh;
1070
1125
Lines 1092-1099 subtest 'Test message_id parameter for SendQueuedMessages' => sub { Link Here
1092
        'from_address'           => '@example.com' # invalid from_address
1147
        'from_address'           => '@example.com' # invalid from_address
1093
    };
1148
    };
1094
    my $message_id = C4::Letters::EnqueueLetter($my_message);
1149
    my $message_id = C4::Letters::EnqueueLetter($my_message);
1150
    $send_or_die_count = 0; # reset
1095
    my $processed = C4::Letters::SendQueuedMessages( { message_id => $message_id } );
1151
    my $processed = C4::Letters::SendQueuedMessages( { message_id => $message_id } );
1096
    is( $processed, 1, 'Processed 1 message when one message_id passed' );
1152
    is( $send_or_die_count, 0, 'Nothing sent when one message_id passed' );
1097
    my $message_1 = C4::Letters::GetMessage($message_id);
1153
    my $message_1 = C4::Letters::GetMessage($message_id);
1098
    is( $message_1->{status}, 'failed', 'Invalid from_address => status failed' );
1154
    is( $message_1->{status}, 'failed', 'Invalid from_address => status failed' );
1099
    is( $message_1->{failure_code}, 'INVALID_EMAIL:from', 'Failure code set correctly for invalid email parameter');
1155
    is( $message_1->{failure_code}, 'INVALID_EMAIL:from', 'Failure code set correctly for invalid email parameter');
Lines 1103-1108 subtest 'Test message_id parameter for SendQueuedMessages' => sub { Link Here
1103
    warning_like { C4::Letters::SendQueuedMessages( { message_id => $message_id } ); }
1159
    warning_like { C4::Letters::SendQueuedMessages( { message_id => $message_id } ); }
1104
        qr|Fake send_or_die|,
1160
        qr|Fake send_or_die|,
1105
        "SendQueuedMessages is using the mocked send_or_die routine";
1161
        "SendQueuedMessages is using the mocked send_or_die routine";
1162
    is( $send_or_die_count, 1, 'One message passed through' );
1106
    $message_1 = C4::Letters::GetMessage($message_1->{message_id});
1163
    $message_1 = C4::Letters::GetMessage($message_1->{message_id});
1107
    my $message_2 = C4::Letters::GetMessage($message_id);
1164
    my $message_2 = C4::Letters::GetMessage($message_id);
1108
    is( $message_1->{status}, 'failed', 'Message 1 status is unchanged' );
1165
    is( $message_1->{status}, 'failed', 'Message 1 status is unchanged' );
1109
- 

Return to bug 33360