From d659649a132f8df2f6c294127f3c39d42bbbcaf8 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Wed, 7 Mar 2018 16:40:07 +0100 Subject: [PATCH] Bug 20356: Add EmailSMSSendDriverFromAddress system preference Add EmailSMSSendDriverFromAddress system preference for overriding Email SMS send driver from address. How to test: 1) Run tests in t/db_dependent/Letters.t 2) All tests should pass Sponsored-by: Gothenburg University Library Signed-off-by: Michal Denar Signed-off-by: Marcel de Rooy Signed-off-by: Nick Clemens --- C4/Letters.pm | 16 +++++++- ...-syspref-EmailSMSSendDriverFromAddress.sql | 1 + .../en/modules/admin/preferences/patrons.pref | 3 ++ t/db_dependent/Letters.t | 39 ++++++++++++++++--- 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_20356-add-syspref-EmailSMSSendDriverFromAddress.sql diff --git a/C4/Letters.pm b/C4/Letters.pm index 4954067427..5382819b8f 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -994,7 +994,15 @@ sub SendQueuedMessages { } $message->{to_address} = $patron->smsalertnumber; #Sometime this is set to email - sms should always use smsalertnumber $message->{to_address} .= '@' . $sms_provider->domain(); - _update_message_to_address($message->{'message_id'},$message->{to_address}); + + # Check for possible from_address override + my $from_address = C4::Context->preference('EmailSMSSendDriverFromAddress'); + if ($from_address && $message->{from_address} ne $from_address) { + $message->{from_address} = $from_address; + _update_message_from_address($message->{'message_id'}, $message->{from_address}); + } + + _update_message_to_address($message->{'message_id'}, $message->{to_address}); _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} ); } else { _send_message_by_sms( $message ); @@ -1389,6 +1397,12 @@ sub _update_message_to_address { $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id)); } +sub _update_message_from_address { + my ($message_id, $from_address) = @_; + my $dbh = C4::Context->dbh(); + $dbh->do('UPDATE message_queue SET from_address = ? WHERE message_id = ?', undef, ($from_address, $message_id)); +} + sub _set_message_status { my $params = shift or return; diff --git a/installer/data/mysql/atomicupdate/bug_20356-add-syspref-EmailSMSSendDriverFromAddress.sql b/installer/data/mysql/atomicupdate/bug_20356-add-syspref-EmailSMSSendDriverFromAddress.sql new file mode 100644 index 0000000000..e5636c3e87 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_20356-add-syspref-EmailSMSSendDriverFromAddress.sql @@ -0,0 +1 @@ +INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type`) VALUES ('EmailSMSSendDriverFromAddress', '', '', 'Email SMS send driver from address override', '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 9185d202a7..ecb1c4e6df 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 @@ -139,6 +139,9 @@ Patrons: - pref: SMSSendUsername - "and a password" - pref: SMSSendPassword + - ". Override from address with" + - pref: EmailSMSSendDriverFromAddress + - "for emails sent using \"Email\" send driver." - - pref: uppercasesurnames choices: diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index a66491881d..d6f8c79682 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -692,8 +692,11 @@ subtest 'TranslateNotices' => sub { subtest 'SendQueuedMessages' => sub { - plan tests => 4; + plan tests => 6; + t::lib::Mocks::mock_preference( 'SMSSendDriver', 'Email' ); + t::lib::Mocks::mock_preference('EmailSMSSendDriverFromAddress', ''); + my $patron = Koha::Patrons->find($borrowernumber); $dbh->do(q| INSERT INTO message_queue(borrowernumber, subject, content, message_transport_type, status, letter_code) @@ -707,11 +710,37 @@ subtest 'SendQueuedMessages' => sub { $patron->set( { smsalertnumber => '5555555555', sms_provider_id => $sms_pro->id() } )->store; $message_id = C4::Letters::EnqueueLetter($my_message); #using datas set around line 95 and forward C4::Letters::SendQueuedMessages(); - my $sms_message_address = $schema->resultset('MessageQueue')->search({ + + my $message = $schema->resultset('MessageQueue')->search({ borrowernumber => $borrowernumber, status => 'sent' - })->next()->to_address(); - is( $sms_message_address, '5555555555@kidclamp.rocks', 'SendQueuedMessages populates the to address correctly for SMS by email when to_address not set' ); + })->next(); + + is( $message->to_address(), '5555555555@kidclamp.rocks', 'SendQueuedMessages populates the to address correctly for SMS by email when to_address not set' ); + is( + $message->from_address(), + 'from@example.com', + 'SendQueuedMessages uses message queue item \"from address\" for SMS by email when EmailSMSSendDriverFromAddress system preference is not set' + ); + + $schema->resultset('MessageQueue')->search({borrowernumber => $borrowernumber, status => 'sent'})->delete(); #clear borrower queue + + t::lib::Mocks::mock_preference('EmailSMSSendDriverFromAddress', 'override@example.com'); + + $message_id = C4::Letters::EnqueueLetter($my_message); + C4::Letters::SendQueuedMessages(); + + $message = $schema->resultset('MessageQueue')->search({ + borrowernumber => $borrowernumber, + status => 'sent' + })->next(); + + is( + $message->from_address(), + 'override@example.com', + 'SendQueuedMessages uses EmailSMSSendDriverFromAddress value for SMS by email when EmailSMSSendDriverFromAddress is set' + ); + $schema->resultset('MessageQueue')->search({borrowernumber => $borrowernumber,status => 'sent'})->delete(); #clear borrower queue $my_message->{to_address} = 'fixme@kidclamp.iswrong'; $message_id = C4::Letters::EnqueueLetter($my_message); @@ -723,7 +752,7 @@ subtest 'SendQueuedMessages' => sub { is ( $number_attempted, 0, 'There were no password reset messages for SendQueuedMessages to attempt.' ); C4::Letters::SendQueuedMessages(); - $sms_message_address = $schema->resultset('MessageQueue')->search({ + my $sms_message_address = $schema->resultset('MessageQueue')->search({ borrowernumber => $borrowernumber, status => 'sent' })->next()->to_address(); -- 2.39.5