From f27d82ad117c77e973cd7d1f533cc92ffe9d9635 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 --- 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 | 21 +++++++++++++++++-- 5 files changed, 49 insertions(+), 2 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 47e1252648..e31f3f9961 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -940,6 +940,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 b58114d802..1532d64f55 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -697,6 +697,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('SkipHoldTrapOnNotForLoanValue','',NULL,'If set, Koha will never trap items for hold with this notforloan value','Integer'), ('SlipCSS','',NULL,'Slips CSS url.','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 c2043b85dd..8b8a9bb7e4 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 @@ -229,6 +229,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 ade28f89ad..4dbe84e0ce 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 => 99; +use Test::More tests => 101; use Test::MockModule; use Test::Warn; use Test::Exception; @@ -157,6 +157,23 @@ 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 { @@ -958,7 +975,7 @@ 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({ + $message = $schema->resultset('MessageQueue')->search({ borrowernumber => $borrowernumber, status => 'sent' })->next(); -- 2.37.1 (Apple Git-137.1)