From bde8387b32a06914d3112263dcba60b41a6ce1fa Mon Sep 17 00:00:00 2001 From: Emily-Rose Francoeur Date: Fri, 22 Dec 2023 08:41:39 -0500 Subject: [PATCH] Bug 35639: Trim the messages that are too long before sending them via SMS I created a new system preference, SMSSendMaxChar, which allows you to set a limit for the number of characters in SMS messages to send. When a limit is set, messages that exceed it will be trimed. TEST PLAN 1) Apply the patch 2) Run prove t/db_dependent/Letters.t Signed-off-by: Matt Blenkinsop Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize --- C4/Letters.pm | 6 ++++ ...bug_35639-add_sms_characters_limitation.pl | 19 ++++++++++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../en/modules/admin/preferences/patrons.pref | 4 +++ t/db_dependent/Letters.t | 30 +++++++++++++++---- 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_35639-add_sms_characters_limitation.pl diff --git a/C4/Letters.pm b/C4/Letters.pm index dfb7b78694..a41815fc97 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -952,6 +952,12 @@ sub EnqueueLetter { ); } + if ( $params->{message_transport_type} eq 'sms' ) { + my $limit = C4::Context->preference('SMSSendMaxChar'); + $params->{letter}->{content} = substr( $params->{letter}->{content}, 0, $limit - 3 ) . '...' + if ( $limit && length( $params->{letter}->{content} ) > $limit ); + } + my $message = Koha::Notice::Message->new( { letter_id => $params->{letter}->{id} || undef, diff --git a/installer/data/mysql/atomicupdate/bug_35639-add_sms_characters_limitation.pl b/installer/data/mysql/atomicupdate/bug_35639-add_sms_characters_limitation.pl new file mode 100755 index 0000000000..7c646bad07 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_35639-add_sms_characters_limitation.pl @@ -0,0 +1,19 @@ +use Modern::Perl; + +return { + bug_number => "35639", + description => "Add the SMSSendMaxChar system preference to limit the number of characters in a SMS message", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + # Do you stuffs here + $dbh->do(q{ + INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) + VALUES ('SMSSendMaxChar','','NULL','Add a limit for the number of characters in SMS messages','Integer'); + }); + + # sysprefs + say $out "Added new system preference 'SMSSendMaxChar'"; + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 4f93d5b6bd..d6a450e514 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -723,6 +723,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('SlipCSS','',NULL,'Slips CSS url.','free'), ('SMSSendAdditionalOptions', '', '', 'Additional SMS::Send parameters used to send SMS messages', 'free'), ('SMSSendDriver','','','Sets which SMS::Send driver is used to send SMS messages.','free'), +('SMSSendMaxChar', '', NULL, 'Add a limit for the number of characters in SMS messages', 'Integer'), ('SMSSendPassword', '', '', 'Password used to send SMS messages', 'free'), ('SMSSendUsername', '', '', 'Username/Login used to send SMS messages', 'free'), ('SocialNetworks','','facebook|linkedin|email','Enable/Disable social networks links in opac detail pages','Choice'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 0c280f302b..d2976e2d43 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -239,6 +239,10 @@ Patrons: - driver to send SMS messages. - "
If you would prefer to send SMS via E-mail, set SMSSendDriver to: Email" - "
NOTE: Many mobile providers have deprecated support for this feature and it is not recommended for use unless you have a dedicated SMS to Email gateway." + - "
Limit messages to" + - pref: SMSSendMaxChar + class: integer + - "characters (no limitation if empty)." - - "Define a username/login" - pref: SMSSendUsername diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index ea1e859456..e546009534 100755 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -19,7 +19,7 @@ use Modern::Perl; use File::Basename qw(dirname); -use Test::More tests => 100; +use Test::More tests => 102; use Test::MockModule; use Test::Warn; use Test::Exception; @@ -158,6 +158,22 @@ is( $messages->[0]->{failure_code}, '', 'Failure code for successful message cor my $yesterday = dt_from_string->subtract( days => 1 ); Koha::Notice::Messages->find($messages->[0]->{message_id})->time_queued($yesterday)->store; + +# EnqueueLetter - Test characters limitation for SMS +$my_message->{letter}->{content} = "a" x 2000; + +t::lib::Mocks::mock_preference( 'SMSSendMaxChar', '' ); +$message_id = C4::Letters::EnqueueLetter($my_message); +my $message = $schema->resultset('MessageQueue')->search( { message_id => $message_id } )->next(); +is( length( $message->content() ), 2000, "EnqueueLetter doesn't resize the message when SMSSendMaxChar is empty" ); +$message->delete(); + +t::lib::Mocks::mock_preference( 'SMSSendMaxChar', 100 ); +$message_id = C4::Letters::EnqueueLetter($my_message); +$message = $schema->resultset('MessageQueue')->search( { message_id => $message_id } )->next(); +is( length( $message->content() ), 100, "EnqueueLetter resizes the message according to the value of SMSSendMaxChar" ); +$message->delete(); + # SendQueuedMessages throws_ok { @@ -195,7 +211,7 @@ is(dt_from_string($messages->[0]->{time_queued}), $yesterday, 'Time queued remai # ResendMessage my $resent = C4::Letters::ResendMessage($messages->[0]->{message_id}); -my $message = C4::Letters::GetMessage( $messages->[0]->{message_id}); +$message = C4::Letters::GetMessage( $messages->[0]->{message_id}); is( $resent, 1, 'The message should have been resent' ); is($message->{status},'pending', 'ResendMessage sets status to pending correctly (bug 12426)'); $resent = C4::Letters::ResendMessage($messages->[0]->{message_id}); @@ -1027,10 +1043,12 @@ subtest 'Test SMS handling in SendQueuedMessages' => sub { qr|Fake send_or_die|, "SendAlerts is using the mocked send_or_die routine (claimissues)"; - my $message = $schema->resultset('MessageQueue')->search({ - borrowernumber => $borrowernumber, - status => 'sent' - })->next(); + $message = $schema->resultset('MessageQueue')->search( + { + borrowernumber => $borrowernumber, + status => 'sent' + } + )->next(); is( $message->letter_id, $messages->[0]->{id}, "Message letter_id is set correctly" ); is( $message->to_address(), '5555555555@kidclamp.rocks', 'SendQueuedMessages populates the to address correctly for SMS by email when to_address not set' ); -- 2.39.5