Bug 27860: Add message_id param to SendQueuedMessages

We could use C4::Letter::GetMessage, but the query in
_get_unsent_messages join on borrowers and return the branchcode.
To prevent any regressions it's preferable to not modify
SendQueuedMessages.

Ideally we obviously need a Koha methods to have better and clean code..

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2021-03-05 10:01:30 +01:00
parent 1ff9160a62
commit 2bad23f1c1
2 changed files with 55 additions and 1 deletions

View File

@ -1008,6 +1008,7 @@ sub SendQueuedMessages {
my $params = shift;
my $which_unsent_messages = {
'message_id' => $params->{'message_id'},
'limit' => $params->{'limit'} // 0,
'borrowernumber' => $params->{'borrowernumber'} // q{},
'letter_code' => $params->{'letter_code'} // q{},
@ -1263,6 +1264,7 @@ sub _add_attachments {
message_transport_type: method of message sending (e.g. email, sms, etc.)
borrowernumber : who the message is to be sent
letter_code : type of message being sent (e.g. PASSWORD_RESET)
message_id : the message_id of the message. In that case the sub will return only 1 result
limit : maximum number of messages to send
This function returns an array of matching hash referenced rows from
@ -1299,6 +1301,10 @@ sub _get_unsent_messages {
$statement .= ' AND message_transport_type = ? ';
push @query_params, $params->{'type'};
}
if ( $params->{message_id} ) {
$statement .= ' AND message_id = ?';
push @query_params, $params->{message_id};
}
if ( $params->{'limit'} ) {
$statement .= ' limit ? ';
push @query_params, $params->{'limit'};

View File

@ -18,9 +18,10 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 83;
use Test::More tests => 84;
use Test::MockModule;
use Test::Warn;
use Test::Exception;
use Email::Sender::Failure;
@ -958,3 +959,50 @@ subtest 'Test limit parameter for SendQueuedMessages' => sub {
is( $messages_processed, 2,
'Processed 2 message with limit of 3 and 2 unprocessed messages' );
};
subtest 'Test message_id parameter for SendQueuedMessages' => sub {
plan tests => 5;
my $dbh = C4::Context->dbh;
my $borrowernumber = Koha::Patron->new({
firstname => 'Jane',
surname => 'Smith',
categorycode => $patron_category,
branchcode => $library->{branchcode},
dateofbirth => $date,
smsalertnumber => undef,
})->store->borrowernumber;
$dbh->do(q|DELETE FROM message_queue|);
$my_message = {
'letter' => {
'content' => 'a message',
'metadata' => 'metadata',
'code' => 'TEST_MESSAGE',
'content_type' => 'text/plain',
'title' => 'message title'
},
'borrowernumber' => $borrowernumber,
'to_address' => 'to@example.org',
'message_transport_type' => 'email',
'from_address' => 'root@localhost' # invalid KohaAdminEmailAddress
};
my $message_id = C4::Letters::EnqueueLetter($my_message);
throws_ok {
C4::Letters::SendQueuedMessages( { message_id => $message_id } );
} 'Koha::Exceptions::BadParameter',
'Exception thrown if invalid email is passed';
my $message_1 = C4::Letters::GetMessage($message_id);
# FIXME must be 'failed'
is( $message_1->{status}, 'pending', 'Invalid KohaAdminEmailAddress => status pending' );
$my_message->{from_address} = 'root@example.org'; # valid KohaAdminEmailAddress
$message_id = C4::Letters::EnqueueLetter($my_message);
C4::Letters::SendQueuedMessages( { message_id => $message_id } );
$message_1 = C4::Letters::GetMessage($message_1->{message_id});
my $message_2 = C4::Letters::GetMessage($message_id);
is( $message_1->{status}, 'pending', 'Message 1 status is unchanged' ); # Must be 'failed'
is( $message_2->{status}, 'sent', 'Valid KohaAdminEmailAddress => status sent' );
};