From 91f7e6cb825d25865a954bf321391588650b6f90 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 7 Sep 2023 11:04:32 -0400 Subject: [PATCH] Bug 34731: Throw exception if SendQueuedMessages is passed a bad message_id Test Plan: 1) Apply this patch 2) prove t/db_dependent/Letters.t Signed-off-by: David Nind Signed-off-by: Martin Renvoize --- C4/Letters.pm | 11 ++++++++--- t/db_dependent/Letters.t | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index fc948c05cc2..ee6af309a0c 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -25,18 +25,20 @@ use Module::Load::Conditional qw( can_load ); use Try::Tiny; -use C4::Members; use C4::Log qw( logaction ); +use C4::Members; use C4::SMS; use C4::Templates; -use Koha::SMS::Providers; - +use Koha::Auth::TwoFactorAuth; +use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Email; +use Koha::Exceptions; use Koha::Notice::Messages; use Koha::Notice::Templates; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Auth::TwoFactorAuth; use Koha::Patrons; +use Koha::SMS::Providers; use Koha::SMTP::Servers; use Koha::Subscriptions; @@ -973,6 +975,9 @@ Returns number of messages sent. sub SendQueuedMessages { my $params = shift; + Koha::Exceptions::BadParameter->throw("Parameter message_id cannot be empty if passed.") + if ( exists( $params->{message_id} ) && !$params->{message_id} ); + my $which_unsent_messages = { 'message_id' => $params->{'message_id'}, 'limit' => $params->{'limit'} // 0, diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 4c01696286f..84f09a82f9d 100755 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 89; +use Test::More tests => 92; use Test::MockModule; use Test::Warn; use Test::Exception; @@ -153,6 +153,22 @@ my $yesterday = dt_from_string->subtract( days => 1 ); Koha::Notice::Messages->find($messages->[0]->{message_id})->time_queued($yesterday)->store; # SendQueuedMessages + +throws_ok { + C4::Letters::SendQueuedMessages( { message_id => undef } ); +} +'Koha::Exceptions::BadParameter', 'Undef message_id throws an exception'; + +throws_ok { + C4::Letters::SendQueuedMessages( { message_id => 0 } ); +} +'Koha::Exceptions::BadParameter', 'message_id of 0 throws an exception'; + +throws_ok { + C4::Letters::SendQueuedMessages( { message_id => q{} } ); +} +'Koha::Exceptions::BadParameter', 'Empty string message_id throws an exception'; + my $messages_processed = C4::Letters::SendQueuedMessages( { type => 'email' }); is($messages_processed, 0, 'No queued messages processed if type limit passed with unused type'); $messages_processed = C4::Letters::SendQueuedMessages( { type => 'sms' }); -- 2.30.2