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

(-)a/C4/Letters.pm (-11 / +21 lines)
Lines 911-919 sub EnqueueLetter { Link Here
911
    my $dbh       = C4::Context->dbh();
911
    my $dbh       = C4::Context->dbh();
912
    my $statement = << 'ENDSQL';
912
    my $statement = << 'ENDSQL';
913
INSERT INTO message_queue
913
INSERT INTO message_queue
914
( borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type )
914
( borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type, delivery_note )
915
VALUES
915
VALUES
916
( ?,              ?,       ?,       ?,        ?,           ?,                      ?,      NOW(),       ?,          ?,            ? )
916
( ?,              ?,       ?,       ?,        ?,           ?,                      ?,      NOW(),       ?,          ?,            ?,            ? )
917
ENDSQL
917
ENDSQL
918
918
919
    my $sth    = $dbh->prepare($statement);
919
    my $sth    = $dbh->prepare($statement);
Lines 928-933 ENDSQL Link Here
928
        $params->{'to_address'},                  # to_address
928
        $params->{'to_address'},                  # to_address
929
        $params->{'from_address'},                # from_address
929
        $params->{'from_address'},                # from_address
930
        $params->{'letter'}->{'content-type'},    # content_type
930
        $params->{'letter'}->{'content-type'},    # content_type
931
        $params->{'delivery_note'}        || '',  # delivery_note
931
    );
932
    );
932
    return $dbh->last_insert_id(undef,undef,'message_queue', undef);
933
    return $dbh->last_insert_id(undef,undef,'message_queue', undef);
933
}
934
}
Lines 1017-1023 sub GetQueuedMessages { Link Here
1017
1018
1018
    my $dbh = C4::Context->dbh();
1019
    my $dbh = C4::Context->dbh();
1019
    my $statement = << 'ENDSQL';
1020
    my $statement = << 'ENDSQL';
1020
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued
1021
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued, delivery_note
1021
FROM message_queue
1022
FROM message_queue
1022
ENDSQL
1023
ENDSQL
1023
1024
Lines 1150-1156 sub _send_message_by_email { Link Here
1150
        unless ($member) {
1151
        unless ($member) {
1151
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1152
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1152
            _set_message_status( { message_id => $message->{'message_id'},
1153
            _set_message_status( { message_id => $message->{'message_id'},
1153
                                   status     => 'failed' } );
1154
                                   status     => 'failed',
1155
                                   delivery_note => 'Invalid borrowernumber '.$message->{borrowernumber} } );
1154
            return;
1156
            return;
1155
        }
1157
        }
1156
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
1158
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
Lines 1158-1164 sub _send_message_by_email { Link Here
1158
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1160
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1159
            # warning too verbose for this more common case?
1161
            # warning too verbose for this more common case?
1160
            _set_message_status( { message_id => $message->{'message_id'},
1162
            _set_message_status( { message_id => $message->{'message_id'},
1161
                                   status     => 'failed' } );
1163
                                   status     => 'failed',
1164
                                   delivery_note => 'Unable to find an email address for this borrower' } );
1162
            return;
1165
            return;
1163
        }
1166
        }
1164
    }
1167
    }
Lines 1199-1209 sub _send_message_by_email { Link Here
1199
    _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
1202
    _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
1200
    if ( sendmail( %sendmail_params ) ) {
1203
    if ( sendmail( %sendmail_params ) ) {
1201
        _set_message_status( { message_id => $message->{'message_id'},
1204
        _set_message_status( { message_id => $message->{'message_id'},
1202
                status     => 'sent' } );
1205
                status     => 'sent',
1206
                delivery_note => '' } );
1203
        return 1;
1207
        return 1;
1204
    } else {
1208
    } else {
1205
        _set_message_status( { message_id => $message->{'message_id'},
1209
        _set_message_status( { message_id => $message->{'message_id'},
1206
                status     => 'failed' } );
1210
                status     => 'failed',
1211
                delivery_note => $Mail::Sendmail::error } );
1207
        carp $Mail::Sendmail::error;
1212
        carp $Mail::Sendmail::error;
1208
        return;
1213
        return;
1209
    }
1214
    }
Lines 1252-1264 sub _send_message_by_sms { Link Here
1252
1257
1253
    unless ( $member->{smsalertnumber} ) {
1258
    unless ( $member->{smsalertnumber} ) {
1254
        _set_message_status( { message_id => $message->{'message_id'},
1259
        _set_message_status( { message_id => $message->{'message_id'},
1255
                               status     => 'failed' } );
1260
                               status     => 'failed',
1261
                               delivery_note => 'Missing SMS number' } );
1256
        return;
1262
        return;
1257
    }
1263
    }
1258
1264
1259
    if ( _is_duplicate( $message ) ) {
1265
    if ( _is_duplicate( $message ) ) {
1260
        _set_message_status( { message_id => $message->{'message_id'},
1266
        _set_message_status( { message_id => $message->{'message_id'},
1261
                               status     => 'failed' } );
1267
                               status     => 'failed',
1268
                               delivery_note => 'Message is duplicate' } );
1262
        return;
1269
        return;
1263
    }
1270
    }
1264
1271
Lines 1266-1272 sub _send_message_by_sms { Link Here
1266
                                       message     => $message->{'content'},
1273
                                       message     => $message->{'content'},
1267
                                     } );
1274
                                     } );
1268
    _set_message_status( { message_id => $message->{'message_id'},
1275
    _set_message_status( { message_id => $message->{'message_id'},
1269
                           status     => ($success ? 'sent' : 'failed') } );
1276
                           status     => ($success ? 'sent' : 'failed'),
1277
                           delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1278
1270
    return $success;
1279
    return $success;
1271
}
1280
}
1272
1281
Lines 1284-1292 sub _set_message_status { Link Here
1284
    }
1293
    }
1285
1294
1286
    my $dbh = C4::Context->dbh();
1295
    my $dbh = C4::Context->dbh();
1287
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1296
    my $statement = 'UPDATE message_queue SET status= ?, delivery_note= ? WHERE message_id = ?';
1288
    my $sth = $dbh->prepare( $statement );
1297
    my $sth = $dbh->prepare( $statement );
1289
    my $result = $sth->execute( $params->{'status'},
1298
    my $result = $sth->execute( $params->{'status'},
1299
                                $params->{'delivery_note'} || '',
1290
                                $params->{'message_id'} );
1300
                                $params->{'message_id'} );
1291
    return $result;
1301
    return $result;
1292
}
1302
}
(-)a/installer/data/mysql/atomicupdate/Bug-14723_-_Additional_delivery_notes_to_messages.pl (+14 lines)
Line 0 Link Here
1
#! /usr/bin/perl
2
3
use strict;
4
use warnings;
5
use C4::Context;
6
use Koha::AtomicUpdater;
7
8
my $dbh = C4::Context->dbh;
9
my $atomicUpdater = Koha::AtomicUpdater->new();
10
11
unless($atomicUpdater->find('Bug14723')) {
12
    $dbh->do("ALTER TABLE message_queue ADD delivery_note TEXT");
13
    print "Upgrade to done (Bug 14723 - Additional delivery notes to messages)\n";
14
}
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt (+4 lines)
Lines 49-54 Link Here
49
		<th>Type</th>
49
		<th>Type</th>
50
		<th>Status</th>
50
		<th>Status</th>
51
		<th>Time</th>
51
		<th>Time</th>
52
		<th>Delivery note</th>
52
	    </tr>
53
	    </tr>
53
	</thead>
54
	</thead>
54
	<tbody>
55
	<tbody>
Lines 75-80 Link Here
75
            [% ELSE %][% QUEUED_MESSAGE.status %][% END %]
76
            [% ELSE %][% QUEUED_MESSAGE.status %][% END %]
76
        </td>
77
        </td>
77
        <td><span title="[% QUEUED_MESSAGE.time_queued %]">[% QUEUED_MESSAGE.time_queued | $KohaDates with_hours => 1 %]</span></td>
78
        <td><span title="[% QUEUED_MESSAGE.time_queued %]">[% QUEUED_MESSAGE.time_queued | $KohaDates with_hours => 1 %]</span></td>
79
        <td>
80
            [% QUEUED_MESSAGE.delivery_note %]
81
        </td>
78
	    </tr>
82
	    </tr>
79
	    [% END %]
83
	    [% END %]
80
	</tbody>
84
	</tbody>
(-)a/t/db_dependent/Letters.t (-2 / +5 lines)
Lines 18-24 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 65;
21
use Test::More tests => 67;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 123-129 is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter st Link Here
123
is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' );
123
is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' );
124
is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' );
124
is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' );
125
is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' );
125
is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' );
126
126
is( $messages->[0]->{delivery_note}, '', 'Delivery note for successful message correctly empty');
127
127
128
# SendQueuedMessages
128
# SendQueuedMessages
129
my $messages_processed = C4::Letters::SendQueuedMessages();
129
my $messages_processed = C4::Letters::SendQueuedMessages();
Lines 136-141 is( Link Here
136
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
136
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
137
);
137
);
138
138
139
# Delivery notes
140
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
141
139
# GetLetters
142
# GetLetters
140
my $letters = C4::Letters::GetLetters();
143
my $letters = C4::Letters::GetLetters();
141
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
144
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
(-)a/t/db_dependent/MessageDeliveryNote.t (-1 / +212 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# Copyright 2015 Open Source Freedom Fighters
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
$ENV{KOHA_PAGEOBJECT_DEBUG} = 1;
20
use Modern::Perl;
21
22
use Test::More;
23
use Try::Tiny; #Even Selenium::Remote::Driver uses Try::Tiny :)
24
25
use Koha::Auth::PermissionManager;
26
27
use t::lib::Page::Mainpage;
28
use t::lib::Page::Opac::OpacMain;
29
use t::lib::Page::Opac::OpacMemberentry;
30
use t::lib::Page::Members::Memberentry;
31
use t::lib::Page::Members::Moremember;
32
use t::lib::Page::Members::Notices;
33
34
use t::lib::TestObjects::BorrowerFactory;
35
use t::lib::TestObjects::MessageQueueFactory;
36
use t::lib::TestObjects::SystemPreferenceFactory;
37
38
use C4::Context;
39
use C4::Members;
40
41
##Setting up the test context
42
my $testContext = {};
43
44
my $password = '1234';
45
my $invalidBorrowerNumber = '000';
46
my $borrowerFactory = t::lib::TestObjects::BorrowerFactory->new();
47
my $borrowers = $borrowerFactory->createTestGroup([
48
            {firstname  => 'Testthree',
49
             surname    => 'Testfour',
50
             cardnumber => 'superuberadmin',
51
             branchcode => 'CPL',
52
             userid     => 'god',
53
             address    => 'testi',
54
             city       => 'joensuu',
55
             zipcode    => '80100',
56
             password   => $password,
57
            },
58
        ], undef, $testContext);
59
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup([
60
            {letter => {
61
                title => "Test title",
62
                content => "Tessst content",
63
                },
64
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
65
             message_transport_type => 'sms',
66
             from_address => 'A001@example.com',
67
            },
68
            {letter => {
69
                title => "Test title",
70
                content => "Tessst content",
71
                },
72
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
73
             message_transport_type => 'sms',
74
             from_address => 'A002@example.com',
75
            },
76
            {letter => {
77
                title => "Test title",
78
                content => "Tessst content",
79
                },
80
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
81
             message_transport_type => 'email',
82
             to_address   => '',
83
             from_address => 'B001@example.com',
84
            },
85
            {letter => {
86
                title => "Test title",
87
                content => "Tessst content",
88
                },
89
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
90
             message_transport_type => 'email',
91
             to_address   => 'nobody@example.com',
92
             from_address => 'B002@example.com',
93
            },
94
            {letter => {
95
                title => "INVALID USER",
96
                content => "INVALID USER",
97
                },
98
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
99
             message_transport_type => 'email',
100
             to_address   => '',
101
             from_address => 'B003@example.com',
102
            },
103
        ], undef, $testContext);
104
my $systempreferences = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([
105
            {preference => 'SMSSendDriver',
106
             value      => 'nonexistentdriver'
107
            },
108
            {
109
             preference => 'EnhancedMessagingPreferences',
110
             value      => '1'
111
            }
112
        ], undef, $testContext);
113
114
my $permissionManager = Koha::Auth::PermissionManager->new();
115
$permissionManager->grantPermissions($borrowers->{'superuberadmin'}, {superlibrarian => 'superlibrarian'});
116
117
eval {
118
119
    # staff client
120
    my $notices = t::lib::Page::Members::Notices->new({borrowernumber => $borrowers->{'superuberadmin'}->borrowernumber});
121
122
    # let's send the messages
123
124
    # first, send one email message with invalid borrower number
125
    $messages->{'B003@example.com'}->{borrowernumber} = $invalidBorrowerNumber;
126
    C4::Letters::_send_message_by_email($messages->{'B003@example.com'});
127
128
    # the rest should produce errors automatically without our modificaitons
129
    C4::Letters::SendQueuedMessages();
130
131
    # then, login to intranet
132
    $notices->doPasswordLogin($borrowers->{'superuberadmin'}->userid(), $password);
133
134
    # test delivery notes for SMS messaging
135
    DoSMSTests($notices);
136
    # check delivery notes for email messaging
137
    CheckEmailMessages($notices);
138
139
};
140
if ($@) { #Catch all leaking errors and gracefully terminate.
141
    warn $@;
142
    tearDown();
143
    exit 1;
144
}
145
146
##All tests done, tear down test context
147
tearDown();
148
done_testing;
149
150
sub tearDown {
151
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext);
152
}
153
154
155
156
157
158
159
160
161
######################################################
162
    ###  STARTING TEST IMPLEMENTATIONS         ###
163
######################################################
164
165
sub DoSMSTests {
166
    my ($notices) = @_;
167
168
    # login and check that our table is displayed correctly
169
    $notices->
170
    hasDeliveryNoteColumnInNoticesTable()->
171
    hasTextInTableCell("Missing SMS number");
172
173
    # now let's give the user a SMS number so we proceed to next step of failure
174
    ModMember( borrowernumber => $borrowers->{superuberadmin}->borrowernumber,
175
                            smsalertnumber => 'just_some_number' );
176
177
    # set first to sent and second to pending to check "duplicate" (Letters::_is_duplicate())
178
    setMessageStatus($messages->{'A001@example.com'}->{message_id}, "sent");
179
    setMessageStatus($messages->{'A002@example.com'}->{message_id}, "pending");
180
181
    C4::Letters::SendQueuedMessages();
182
    # A002 should go into failed: message is duplicate status
183
184
    # set A001 back to pending to check that we get no reply from driver
185
    setMessageStatus($messages->{'A001@example.com'}->{message_id}, "pending");
186
187
    # let's send the message and check for delivery note, we should get a warning
188
    # for non-installed driver.
189
    C4::Letters::SendQueuedMessages();
190
191
    $notices->navigateToNotices()->
192
    hasTextInTableCell("Message is duplicate")->
193
    hasTextInTableCell("No notes from SMS driver");
194
}
195
196
sub CheckEmailMessages {
197
    my ($notices) = @_;
198
199
    $notices->
200
    hasTextInTableCell("Unable to find an email address")->
201
    hasTextInTableCell("Invalid borrowernumber")->
202
    hasTextInTableCell("Connection refused"); #sendmail should also fail
203
}
204
sub setMessageStatus {
205
    my ($message_id, $status) = @_;
206
    my $dbh = C4::Context->dbh;
207
208
    my $sth = $dbh->prepare("UPDATE message_queue SET status=?, delivery_note='' WHERE message_id=?");
209
    my $result = $sth->execute($status, $message_id);
210
211
    return $result;
212
}

Return to bug 14723