From 5a551455edd574b2d5fde9ecd726487649f01702 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 5 Dec 2023 21:56:15 +0000 Subject: [PATCH] (dev/core#4839) WorkflowMessage - Send email. Don't overwrite $to. --- .../Traits/AddressingTrait.php | 2 +- .../CRM/Core/BAO/MessageTemplateTest.php | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Civi/WorkflowMessage/Traits/AddressingTrait.php b/Civi/WorkflowMessage/Traits/AddressingTrait.php index ac60155bd5..a1dc7b73bf 100644 --- a/Civi/WorkflowMessage/Traits/AddressingTrait.php +++ b/Civi/WorkflowMessage/Traits/AddressingTrait.php @@ -249,7 +249,7 @@ trait AddressingTrait { * @see \Civi\WorkflowMessage\Traits\ReflectiveWorkflowTrait::import */ protected function importExtraEnvelope_toAddress(array &$values): void { - if (array_key_exists('toEmail', $values) || array_key_exists('toName', $values)) { + if (isset($values['toEmail']) || isset($values['toName'])) { $this->setTo(['name' => $values['toName'] ?? NULL, 'email' => $values['toEmail'] ?? NULL]); unset($values['toName']); unset($values['toEmail']); diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 8b51e6f0eb..eef95cc01e 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -432,6 +432,52 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { $this->assertStringContainsString('Case ID : 1234', $message); } + public function testSendToEmail_variantA(): void { + $mut = new CiviMailUtils($this, TRUE); + $cid = $this->individualCreate(); + + $msg = \Civi\WorkflowMessage\WorkflowMessage::create('petition_sign', [ + 'from' => '"The Sender" ', + 'toEmail' => 'demo-a@example.com', + 'contactId' => 204, + ]); + $msg->sendTemplate([ + 'messageTemplate' => [ + 'msg_subject' => 'Hello world', + 'msg_text' => 'Hello', + 'msg_html' => '

Hello

', + ], + ]); + $mut->checkMailLog([ + 'From: "The Sender" ', + 'To: ', + "Subject: Hello world", + ]); + $mut->stop(); + } + + public function testSendToEmail_variantB(): void { + $mut = new CiviMailUtils($this, TRUE); + $cid = $this->individualCreate(); + + \Civi\WorkflowMessage\WorkflowMessage::create('petition_sign') + ->setFrom(['name' => 'The Sender', 'email' => 'sender-b@example.com']) + ->setTo(['name' => 'The Recipient', 'email' => 'demo-b@example.com']) + ->setContactID($cid) + ->setTemplate([ + 'msg_subject' => 'Bonjour le monde', + 'msg_text' => 'Ça va', + 'msg_html' => '

Ça va

', + ]) + ->sendTemplate(); + $mut->checkMailLog([ + 'From: The Sender ', + 'To: The Recipient ', + "Subject: Bonjour le monde", + ]); + $mut->stop(); + } + /** * Test rendering of domain tokens. * -- 2.25.1