(dev/core#4839) WorkflowMessage - Send email. Don't overwrite $to.
authorTim Otten <totten@civicrm.org>
Tue, 5 Dec 2023 21:56:15 +0000 (21:56 +0000)
committerTim Otten <totten@civicrm.org>
Tue, 5 Dec 2023 22:04:22 +0000 (22:04 +0000)
Civi/WorkflowMessage/Traits/AddressingTrait.php
tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php

index ac60155bd5835775f2e6ae0d946594b966d64931..a1dc7b73bfc4c3eb491ef3f9492ff3fc97d73f03 100644 (file)
@@ -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']);
index 8b51e6f0eb2c22b4ff8ffec25493000e20e0b306..eef95cc01e7f3d01c9dd26deae8035316a26361e 100644 (file)
@@ -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" <sender-a@example.com>',
+      'toEmail' => 'demo-a@example.com',
+      'contactId' => 204,
+    ]);
+    $msg->sendTemplate([
+      'messageTemplate' => [
+        'msg_subject' => 'Hello world',
+        'msg_text' => 'Hello',
+        'msg_html' => '<p>Hello</p>',
+      ],
+    ]);
+    $mut->checkMailLog([
+      'From: "The Sender" <sender-a@example.com>',
+      'To: <demo-a@example.com>',
+      "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' => '<p>Ça va</p>',
+      ])
+      ->sendTemplate();
+    $mut->checkMailLog([
+      'From: The Sender <sender-b@example.com>',
+      'To: The Recipient <demo-b@example.com>',
+      "Subject: Bonjour le monde",
+    ]);
+    $mut->stop();
+  }
+
   /**
    * Test rendering of domain tokens.
    *