[Ref] Extend email trait test, process more sanely
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Sep 2021 21:00:29 +0000 (09:00 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 21 Sep 2021 07:12:44 +0000 (19:12 +1200)
CRM/Contact/Form/Task/EmailTrait.php
tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php
tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php

index 0655e3cbdcfc2f8dac2d53d799485e8d085d888a..db6af3198c38e170fbe36c93db02bbf4a96d6b4e 100644 (file)
@@ -37,6 +37,13 @@ trait CRM_Contact_Form_Task_EmailTrait {
    */
   public $_templates;
 
+  /**
+   * Email addresses to send to.
+   *
+   * @var array
+   */
+  protected $emails = [];
+
   /**
    * Store "to" contact details.
    * @var array
@@ -190,10 +197,8 @@ trait CRM_Contact_Form_Task_EmailTrait {
     if ($to->getValue()) {
       foreach ($this->getEmails($to) as $value) {
         $contactId = $value['contact_id'];
-        $email = $value['email'];
         if ($contactId) {
           $this->_contactIds[] = $this->_toContactIds[] = $contactId;
-          $this->_toContactEmails[] = $email;
           $this->_allContactIds[] = $contactId;
         }
       }
@@ -366,8 +371,6 @@ trait CRM_Contact_Form_Task_EmailTrait {
    */
   public function postProcess() {
     $this->bounceIfSimpleMailLimitExceeded(count($this->_contactIds));
-
-    // check and ensure that
     $formValues = $this->controller->exportValues($this->getName());
     $this->submit($formValues);
   }
@@ -424,7 +427,7 @@ trait CRM_Contact_Form_Task_EmailTrait {
       if (!isset($this->_contactDetails[$contactId])) {
         continue;
       }
-      $email = $this->_toContactEmails[$key];
+      $email = $this->getEmail($key);
       // prevent duplicate emails if same email address is selected CRM-4067
       // we should allow same emails for different contacts
       $details = $this->_contactDetails[$contactId];
@@ -741,4 +744,22 @@ trait CRM_Contact_Form_Task_EmailTrait {
     return $fromEmailValues;
   }
 
+  /**
+   * Get the relevant emails.
+   *
+   * @param int $index
+   *
+   * @return string
+   */
+  protected function getEmail(int $index): string {
+    if (empty($this->emails)) {
+      $toEmails = explode(',', $this->getSubmittedValue('to'));
+      foreach ($toEmails as $value) {
+        $parts = explode('::', $value);
+        $this->emails[] = $parts[1];
+      }
+    }
+    return $this->emails[$index];
+  }
+
 }
index 589bcdf2e7e3d94e7616f5523c885af45796df50..930bfbebc2d39250227d00c74f1f06ccf3c02f79 100644 (file)
@@ -80,19 +80,6 @@ class CRM_Contact_Form_Task_EmailCommonTest extends CiviUnitTestCase {
 
     Civi::settings()->set('allow_mail_from_logged_in_contact', 1);
     $loggedInContactID = $this->createLoggedInUser();
-    /* @var CRM_Contact_Form_Task_Email $form*/
-    $form = $this->getFormObject('CRM_Contact_Form_Task_Email');
-
-    for ($i = 0; $i < 27; $i++) {
-      $email = 'spy' . $i . '@secretsquirrels.com';
-      $contactID = $this->individualCreate(['email' => $email]);
-      $form->_contactIds[$contactID] = $contactID;
-      $form->_toContactEmails[$this->callAPISuccessGetValue('Email', ['return' => 'id', 'email' => $email])] = $email;
-    }
-    $deceasedContactID = $this->individualCreate(['is_deceased' => 1, 'email' => 'dead@example.com']);
-    $form->_contactIds[$deceasedContactID] = $deceasedContactID;
-    $form->_toContactEmails[$this->callAPISuccessGetValue('Email', ['return' => 'id', 'email' => 'dead@example.com'])] = 'dead@example.com';
-
     $loggedInEmail = $this->callAPISuccess('Email', 'create', [
       'email' => 'mickey@mouse.com',
       'location_type_id' => 1,
@@ -101,15 +88,35 @@ class CRM_Contact_Form_Task_EmailCommonTest extends CiviUnitTestCase {
       'signature_text' => 'This is a test Signature',
       'signature_html' => '<p>This is a test Signature</p>',
     ]);
+
+    $to = $form_contactIds = $form_toContactEmails = [];
+    for ($i = 0; $i < 27; $i++) {
+      $email = 'spy' . $i . '@secretsquirrels.com';
+      $contactID = $this->individualCreate(['email' => $email]);
+      $form_contactIds[$contactID] = $contactID;
+      $to[] = $contactID . '::' . $email;
+    }
+    $deceasedContactID = $this->individualCreate(['is_deceased' => 1, 'email' => 'dead@example.com']);
+    $to[] = $deceasedContactID . '::' . 'email@example.com';
+    /* @var CRM_Contact_Form_Task_Email $form*/
+    $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [
+      'to' => implode(',', $to),
+    ]);
+    $form->_contactIds = $form_contactIds;
+    $form->_contactIds[$deceasedContactID] = $deceasedContactID;
+
     $form->_allContactIds = $form->_toContactIds = $form->_contactIds;
-    $form->_emails = [$loggedInEmail['id'] => 'mickey@mouse.com'];
     $form->_fromEmails = [$loggedInEmail['id'] => 'mickey@mouse.com'];
     // This rule somehow disappears if there's a form-related test before us,
     // so register it again. See packages/HTML/QuickForm/file.php.
+    // update - actually - it's never registered. Even in form made
+    // I can see it missing - It's really weird.
     $form->registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file');
     $form->isSearchContext = FALSE;
     $form->buildForm();
     $form->submit(array_merge($form->_defaultValues, [
+      // @todo - it's better to pass these into getForm
+      // and access them on the form using $this->getSubmittedValue().
       'from_email_address' => $loggedInEmail['id'],
       'subject' => 'Really interesting stuff',
       'bcc_id' => $bcc,
index 56b13ca7a37310e94d4da0ae090640919cbfe44c..f2061638c1413c0ff04f3bbc2f55e282e4e81c29 100644 (file)
@@ -46,15 +46,26 @@ class CRM_Contribute_Form_Task_EmailTest extends CiviUnitTestCase {
     ]);
     $contribution1 = $this->contributionCreate(['contact_id' => $contact2]);
     $contribution2 = $this->contributionCreate(['total_amount' => 999, 'contact_id' => $contact1]);
-    $form = $this->getFormObject('CRM_Contribute_Form_Task_Email', ['cc_id' => '', 'bcc_id' => ''], [], [
+    $form = $this->getFormObject('CRM_Contribute_Form_Task_Email', [
+      'cc_id' => '',
+      'bcc_id' => '',
+      'to' => implode(',', [
+        $contact1 . '::teresajensen-nielsen65@spamalot.co.in',
+        $contact2 . '::bob@example.com',
+      ]),
+      'subject' => '{contact.display_name}',
+      'text_message' => '{contribution.total_amount}',
+      'html_message' => '{domain.name}',
+    ], [], [
       'radio_ts' => 'ts_sel',
-      'task' => CRM_Contribute_Task::TASK_EMAIL,
+      'task' => CRM_Core_Task::TASK_EMAIL,
       'mark_x_' . $contribution1 => 1,
       'mark_x_' . $contribution2 => 1,
     ]);
     $form->set('cid', $contact1 . ',' . $contact2);
     $form->buildForm();
     $this->assertEquals('<br/><br/>--Benny, Benny', $form->_defaultValues['html_message']);
+    $form->postProcess();
   }
 
 }