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

(-)a/C4/Letters.pm (-12 / +22 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 1071-1077 sub GetMessage { Link Here
1071
    return unless $message_id;
1072
    return unless $message_id;
1072
    my $dbh = C4::Context->dbh;
1073
    my $dbh = C4::Context->dbh;
1073
    return $dbh->selectrow_hashref(q|
1074
    return $dbh->selectrow_hashref(q|
1074
        SELECT message_id, borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type
1075
        SELECT message_id, borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type, delivery_note
1075
        FROM message_queue
1076
        FROM message_queue
1076
        WHERE message_id = ?
1077
        WHERE message_id = ?
1077
    |, {}, $message_id );
1078
    |, {}, $message_id );
Lines 1195-1201 sub _send_message_by_email { Link Here
1195
        unless ($member) {
1196
        unless ($member) {
1196
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1197
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1197
            _set_message_status( { message_id => $message->{'message_id'},
1198
            _set_message_status( { message_id => $message->{'message_id'},
1198
                                   status     => 'failed' } );
1199
                                   status     => 'failed',
1200
                                   delivery_note => 'Invalid borrowernumber '.$message->{borrowernumber} } );
1199
            return;
1201
            return;
1200
        }
1202
        }
1201
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
1203
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
Lines 1203-1209 sub _send_message_by_email { Link Here
1203
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1205
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1204
            # warning too verbose for this more common case?
1206
            # warning too verbose for this more common case?
1205
            _set_message_status( { message_id => $message->{'message_id'},
1207
            _set_message_status( { message_id => $message->{'message_id'},
1206
                                   status     => 'failed' } );
1208
                                   status     => 'failed',
1209
                                   delivery_note => 'Unable to find an email address for this borrower' } );
1207
            return;
1210
            return;
1208
        }
1211
        }
1209
    }
1212
    }
Lines 1244-1254 sub _send_message_by_email { Link Here
1244
    _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
1247
    _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
1245
    if ( sendmail( %sendmail_params ) ) {
1248
    if ( sendmail( %sendmail_params ) ) {
1246
        _set_message_status( { message_id => $message->{'message_id'},
1249
        _set_message_status( { message_id => $message->{'message_id'},
1247
                status     => 'sent' } );
1250
                status     => 'sent',
1251
                delivery_note => '' } );
1248
        return 1;
1252
        return 1;
1249
    } else {
1253
    } else {
1250
        _set_message_status( { message_id => $message->{'message_id'},
1254
        _set_message_status( { message_id => $message->{'message_id'},
1251
                status     => 'failed' } );
1255
                status     => 'failed',
1256
                delivery_note => $Mail::Sendmail::error } );
1252
        carp $Mail::Sendmail::error;
1257
        carp $Mail::Sendmail::error;
1253
        return;
1258
        return;
1254
    }
1259
    }
Lines 1297-1309 sub _send_message_by_sms { Link Here
1297
1302
1298
    unless ( $member->{smsalertnumber} ) {
1303
    unless ( $member->{smsalertnumber} ) {
1299
        _set_message_status( { message_id => $message->{'message_id'},
1304
        _set_message_status( { message_id => $message->{'message_id'},
1300
                               status     => 'failed' } );
1305
                               status     => 'failed',
1306
                               delivery_note => 'Missing SMS number' } );
1301
        return;
1307
        return;
1302
    }
1308
    }
1303
1309
1304
    if ( _is_duplicate( $message ) ) {
1310
    if ( _is_duplicate( $message ) ) {
1305
        _set_message_status( { message_id => $message->{'message_id'},
1311
        _set_message_status( { message_id => $message->{'message_id'},
1306
                               status     => 'failed' } );
1312
                               status     => 'failed',
1313
                               delivery_note => 'Message is duplicate' } );
1307
        return;
1314
        return;
1308
    }
1315
    }
1309
1316
Lines 1311-1317 sub _send_message_by_sms { Link Here
1311
                                       message     => $message->{'content'},
1318
                                       message     => $message->{'content'},
1312
                                     } );
1319
                                     } );
1313
    _set_message_status( { message_id => $message->{'message_id'},
1320
    _set_message_status( { message_id => $message->{'message_id'},
1314
                           status     => ($success ? 'sent' : 'failed') } );
1321
                           status     => ($success ? 'sent' : 'failed'),
1322
                           delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1323
1315
    return $success;
1324
    return $success;
1316
}
1325
}
1317
1326
Lines 1329-1337 sub _set_message_status { Link Here
1329
    }
1338
    }
1330
1339
1331
    my $dbh = C4::Context->dbh();
1340
    my $dbh = C4::Context->dbh();
1332
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1341
    my $statement = 'UPDATE message_queue SET status= ?, delivery_note= ? WHERE message_id = ?';
1333
    my $sth = $dbh->prepare( $statement );
1342
    my $sth = $dbh->prepare( $statement );
1334
    my $result = $sth->execute( $params->{'status'},
1343
    my $result = $sth->execute( $params->{'status'},
1344
                                $params->{'delivery_note'} || '',
1335
                                $params->{'message_id'} );
1345
                                $params->{'message_id'} );
1336
    return $result;
1346
    return $result;
1337
}
1347
}
(-)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 (-1 / +5 lines)
Lines 10-16 Link Here
10
    $(document).ready(function() {
10
    $(document).ready(function() {
11
    $("#noticestable").dataTable($.extend(true, {}, dataTablesDefaults, {
11
    $("#noticestable").dataTable($.extend(true, {}, dataTablesDefaults, {
12
        "aaSorting": [[ 3, "desc" ]],
12
        "aaSorting": [[ 3, "desc" ]],
13
        "aoColumns": [ null,null,null,{ "sType": "title-string" } ],
13
        "aoColumns": [ null,null,null,{ "sType": "title-string" }, null ],
14
        "sPaginationType": "four_button"
14
        "sPaginationType": "four_button"
15
    }));
15
    }));
16
16
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 85-90 Link Here
85
            [% END %]
86
            [% END %]
86
        </td>
87
        </td>
87
        <td><span title="[% QUEUED_MESSAGE.time_queued %]">[% QUEUED_MESSAGE.time_queued | $KohaDates with_hours => 1 %]</span></td>
88
        <td><span title="[% QUEUED_MESSAGE.time_queued %]">[% QUEUED_MESSAGE.time_queued | $KohaDates with_hours => 1 %]</span></td>
89
        <td>
90
            [% QUEUED_MESSAGE.delivery_note %]
91
        </td>
88
	    </tr>
92
	    </tr>
89
	    [% END %]
93
	    [% END %]
90
	</tbody>
94
	</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 => 69;
21
use Test::More tests => 71;
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 146-151 is( $resent, 0, 'The message should not have been resent again' ); Link Here
146
$resent = C4::Letters::ResendMessage();
146
$resent = C4::Letters::ResendMessage();
147
is( $resent, undef, 'ResendMessage should return undef if not message_id given' );
147
is( $resent, undef, 'ResendMessage should return undef if not message_id given' );
148
148
149
# Delivery notes
150
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
151
149
# GetLetters
152
# GetLetters
150
my $letters = C4::Letters::GetLetters();
153
my $letters = C4::Letters::GetLetters();
151
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
154
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
(-)a/t/db_dependent/MessageDeliveryNote.t (-1 / +202 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
            {subject => "Test title",
61
             content => "Tessst content",
62
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
63
             message_transport_type => 'sms',
64
             from_address => 'A001@example.com',
65
            },
66
            {subject => "Test title",
67
             content => "Tessst content",
68
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
69
             message_transport_type => 'sms',
70
             from_address => 'A002@example.com',
71
            },
72
            {subject => "Test title",
73
             content => "Tessst content",
74
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
75
             message_transport_type => 'email',
76
             to_address   => '',
77
             from_address => 'B001@example.com',
78
            },
79
            {subject => "Test title",
80
             content => "Tessst content",
81
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
82
             message_transport_type => 'email',
83
             to_address   => 'nobody@example.com',
84
             from_address => 'B002@example.com',
85
            },
86
            {subject => "Test title",
87
             content => "Tessst content",
88
             cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
89
             message_transport_type => 'email',
90
             to_address   => '',
91
             from_address => 'B003@example.com',
92
            },
93
        ], undef, $testContext);
94
my $systempreferences = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([
95
            {preference => 'SMSSendDriver',
96
             value      => 'nonexistentdriver'
97
            },
98
            {
99
             preference => 'EnhancedMessagingPreferences',
100
             value      => '1'
101
            }
102
        ], undef, $testContext);
103
104
my $permissionManager = Koha::Auth::PermissionManager->new();
105
$permissionManager->grantPermissions($borrowers->{'superuberadmin'}, {superlibrarian => 'superlibrarian'});
106
107
eval {
108
109
    # staff client
110
    my $notices = t::lib::Page::Members::Notices->new({borrowernumber => $borrowers->{'superuberadmin'}->borrowernumber});
111
112
    # let's send the messages
113
114
    # first, send one email message with invalid borrower number
115
    $messages->{'B003@example.com'}->{borrowernumber} = $invalidBorrowerNumber;
116
    C4::Letters::_send_message_by_email($messages->{'B003@example.com'});
117
118
    # the rest should produce errors automatically without our modificaitons
119
    C4::Letters::SendQueuedMessages();
120
121
    # then, login to intranet
122
    $notices->doPasswordLogin($borrowers->{'superuberadmin'}->userid(), $password);
123
124
    # test delivery notes for SMS messaging
125
    DoSMSTests($notices);
126
    # check delivery notes for email messaging
127
    CheckEmailMessages($notices);
128
129
};
130
if ($@) { #Catch all leaking errors and gracefully terminate.
131
    warn $@;
132
    tearDown();
133
    exit 1;
134
}
135
136
##All tests done, tear down test context
137
tearDown();
138
done_testing;
139
140
sub tearDown {
141
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext);
142
}
143
144
145
146
147
148
149
150
151
######################################################
152
    ###  STARTING TEST IMPLEMENTATIONS         ###
153
######################################################
154
155
sub DoSMSTests {
156
    my ($notices) = @_;
157
158
    # login and check that our table is displayed correctly
159
    $notices->
160
    hasDeliveryNoteColumnInNoticesTable()->
161
    hasTextInTableCell("Missing SMS number");
162
163
    # now let's give the user a SMS number so we proceed to next step of failure
164
    ModMember( borrowernumber => $borrowers->{superuberadmin}->borrowernumber,
165
                            smsalertnumber => 'just_some_number' );
166
167
    # set first to sent and second to pending to check "duplicate" (Letters::_is_duplicate())
168
    setMessageStatus($messages->{'A001@example.com'}->{message_id}, "sent");
169
    setMessageStatus($messages->{'A002@example.com'}->{message_id}, "pending");
170
171
    C4::Letters::SendQueuedMessages();
172
    # A002 should go into failed: message is duplicate status
173
174
    # set A001 back to pending to check that we get no reply from driver
175
    setMessageStatus($messages->{'A001@example.com'}->{message_id}, "pending");
176
177
    # let's send the message and check for delivery note, we should get a warning
178
    # for non-installed driver.
179
    C4::Letters::SendQueuedMessages();
180
181
    $notices->navigateToNotices()->
182
    hasTextInTableCell("Message is duplicate")->
183
    hasTextInTableCell("No notes from SMS driver");
184
}
185
186
sub CheckEmailMessages {
187
    my ($notices) = @_;
188
189
    $notices->
190
    hasTextInTableCell("Unable to find an email address")->
191
    hasTextInTableCell("Invalid borrowernumber")->
192
    hasTextInTableCell("Connection refused"); #sendmail should also fail
193
}
194
sub setMessageStatus {
195
    my ($message_id, $status) = @_;
196
    my $dbh = C4::Context->dbh;
197
198
    my $sth = $dbh->prepare("UPDATE message_queue SET status=?, delivery_note='' WHERE message_id=?");
199
    my $result = $sth->execute($status, $message_id);
200
201
    return $result;
202
}

Return to bug 14723