From b0cd0026cf9c253226d489d3e19d1ad290be768d 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 | 30 ++++++++++++- t/lib/SMS/Send/KohaTest.pm | 45 +++++++++++++++++++ 6 files changed, 108 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 eba203154ca..6672a484f0d 100644 --- a/C4/SMS.pm +++ b/C4/SMS.pm @@ -128,9 +128,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 336f05bd294..d910782757c 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -739,6 +739,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'), ('SMSSendAdditionalOptions', '', '', 'Additional SMS::Send parameters used to send SMS messages', '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'), ('SMSSendMaxChar', '', NULL, 'Add a limit for the number of characters in SMS messages', 'Integer'), ('SMSSendPassword', '', '', 'Password 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 3c5d5fb1542..117339e3b34 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 @@ -245,6 +245,11 @@ Patrons: - pref: SMSSendMaxChar class: integer - "characters (no limitation if empty)." + - + - "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 763251a7137..d1e8979d439 100755 --- a/t/SMS.t +++ b/t/SMS.t @@ -17,14 +17,19 @@ use Modern::Perl; +use FindBin; +use lib "$FindBin::Bin/lib"; + use t::lib::Mocks; -use Test::More tests => 8; +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 ); is( C4::SMS->driver(), $driver, 'driver returns the SMSSendDriver correctly' ); @@ -68,3 +73,26 @@ is( $error, 'SMS_SEND_DRIVER_MISSING', 'Error code returned is SMS_SEND_DRIVE ); 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.48.1