From dceb29768ceaabcfc80300708e2ae88d98f48613 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 5 Mar 2021 10:01:30 +0100 Subject: [PATCH] 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.. --- C4/Letters.pm | 6 +++++ t/db_dependent/Letters.t | 50 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 5f1ab87a36..28b90fded0 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -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'}; diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 613f5696d3..3ba3361d28 100755 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -18,9 +18,10 @@ # along with Koha; if not, see . 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' ); +}; -- 2.20.1