From 7e17c526179d5cd5f77066ebe75a1949777c2b1b Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Wed, 7 Feb 2024 17:11:58 +0100 Subject: [PATCH] Bug 36022: Add default recipient SMS number country code Add SMSSendDefaultCountryCode syspref to prepend country code to non international SMS numbers. To test: 1) Apply patch and run updatedatabase.pl 2) Verify that SMSSendDefaultCountryCode syspref can be set in staff interface system preferences 3) Verify all tests pass in t/SMS.t Sponsored-by: Gothenburg University Library --- C4/SMS.pm | 15 ++++++- ...2-add-SMSSendDefaultCountryCode-syspref.pl | 14 ++++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../en/modules/admin/preferences/patrons.pref | 5 +++ t/SMS.t | 29 +++++++++++- t/lib/SMS/Send/KohaTest.pm | 45 +++++++++++++++++++ 6 files changed, 107 insertions(+), 2 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug-36022-add-SMSSendDefaultCountryCode-syspref.pl create mode 100644 t/lib/SMS/Send/KohaTest.pm diff --git a/C4/SMS.pm b/C4/SMS.pm index ce3358f66ab..6a9ac750d9a 100644 --- a/C4/SMS.pm +++ b/C4/SMS.pm @@ -116,9 +116,22 @@ sub send_sms { %args, ); + my $default_country_code = C4::Context->preference("SMSSendDefaultCountryCode"); + my $destination = $params->{destination}; + + # Default country code set and is local phone number + if ($default_country_code && $destination =~ /^[^+]/) { + # Ensure country code has leading '+' + $default_country_code =~ s/^[^+]/+$&/; + # Strip leading zeroes + $destination =~ s/^0+//; + # Prepend default country code + $destination = $default_country_code . $destination; + } + # Send a message $sent = $sender->send_sms( - to => $params->{destination}, + to => $destination, text => $params->{message}, ); }; diff --git a/installer/data/mysql/atomicupdate/bug-36022-add-SMSSendDefaultCountryCode-syspref.pl b/installer/data/mysql/atomicupdate/bug-36022-add-SMSSendDefaultCountryCode-syspref.pl new file mode 100755 index 00000000000..e34ac28996d --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug-36022-add-SMSSendDefaultCountryCode-syspref.pl @@ -0,0 +1,14 @@ +use Modern::Perl; + +return { + bug_number => "36022", + description => "Add SMSSendDefaultCountryCode system preference", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + $dbh->do(q{ INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES ('SMSSendDefaultCountryCode ', NULL, NULL, 'Default SMS::Send driver recipient phone number country code', 'Integer') }); + # sysprefs + say $out "Added new system preference 'SMSSendDefaultCountryCode'"; + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index fad32d896e5..684f02a71ed 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -680,6 +680,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('SIP2SortBinMapping','',NULL,'Use the following mappings to determine the sort_bin of a returned item. The mapping should be on the form \"branchcode:item field:item field value:sort bin number\", with one mapping per line.','free'), ('SkipHoldTrapOnNotForLoanValue','',NULL,'If set, Koha will never trap items for hold with this notforloan value','Integer'), ('SlipCSS','',NULL,'Slips CSS url.','free'), +('SMSSendDefaultCountryCode ',NULL,NULL,'Default SMS::Send driver recipient phone number country code','Integer'), ('SMSSendDriver','','','Sets which SMS::Send driver is used to send SMS messages.','free'), ('SMSSendPassword', '', '', 'Password used to send SMS messages', 'free'), ('SMSSendUsername', '', '', 'Username/Login used to send SMS messages', 'free'), 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 0adb1ae1d7b..5140f7ceaeb 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 @@ -217,6 +217,11 @@ 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." + - + - "Prepend the country code" + - pref: SMSSendDefaultCountryCode + - class: integer + - "for SMS::Driver recipient phone numbers. The country code shound not include a leading \"+\"." - - "Define a username/login" - pref: SMSSendUsername diff --git a/t/SMS.t b/t/SMS.t index bf8de651526..dafc500fcda 100755 --- a/t/SMS.t +++ b/t/SMS.t @@ -17,14 +17,18 @@ use Modern::Perl; +use FindBin; +use lib "$FindBin::Bin/lib"; + use t::lib::Mocks; -use Test::More tests => 7; +use Test::More tests => 9; BEGIN { use_ok('C4::SMS', qw( driver send_sms )); } +t::lib::Mocks::mock_preference('SMSSendDefaultCountryCode', ''); my $driver = 'my mock driver'; t::lib::Mocks::mock_preference('SMSSendDriver', $driver); @@ -60,3 +64,26 @@ $send_sms = C4::SMS->send_sms({ }); is( $send_sms, 1, 'send_sms returns 1' ); + +t::lib::Mocks::mock_preference('SMSSendDriver', 'KohaTest'); +t::lib::Mocks::mock_preference('SMSSendDefaultCountryCode', '46'); + +my $driver_arguments = C4::SMS->send_sms({ + destination => '+33123456789', + message => 'my message' +}); + +is( $driver_arguments->{to}, + '+33123456789', + 'country code has been preserved when SMSSendDefaultCountryCode is set and destination is international phone number' +); + +$driver_arguments = C4::SMS->send_sms({ + destination => '0123456789', + message => 'my message' +}); + +is( $driver_arguments->{to}, + '+46123456789', + 'default country code has been prepended when SMSSendDefaultCountryCode is set and destination is non international phone number' +); diff --git a/t/lib/SMS/Send/KohaTest.pm b/t/lib/SMS/Send/KohaTest.pm new file mode 100644 index 00000000000..bb3494b8a66 --- /dev/null +++ b/t/lib/SMS/Send/KohaTest.pm @@ -0,0 +1,45 @@ +package SMS::Send::KohaTest; + +=pod + +=head1 NAME + +SMS::Send::KohaTest - SMS::Sent Testing Driver for C4::SMS tests + +=head1 SYNOPSIS + + my $send = SMS::Send->new( 'KohaTest' ) + $message = $send->send_sms( + text => 'Hi there', + to => '+61 (4) 1234 5678', + ) + +=head1 DESCRIPTION + +When writing tests for L we are unable to trap messages using the +L driver since the L instance cannot be accessed +directly. We do however have access to the return value of the driver's +send_sms method, so by returning the incoming arguments here we are able +verify these have been set correctly. + +=cut + +use 5.006; +use strict; +use parent 'SMS::Send::Driver'; + +sub new { + my $class = shift; + my $self = bless { + }, $class; + $self; +} + +sub send_sms { + # Shift $self + shift; + use Data::Dumper; + # Return arguments + return { @_ }; +} +1; -- 2.43.0