dev/core#2914 Fix for incorrect sent count in message
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 14 Oct 2021 01:21:29 +0000 (14:21 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 17 Oct 2021 23:25:02 +0000 (12:25 +1300)
CRM/Contact/Form/Task/EmailTrait.php
tests/phpunit/CRM/Contact/Form/Task/EmailTest.php

index 2f53b8f4767e4969c64b6b8938b2e9d091176d4a..743edf6173ed7a9e083df740f9ea53ae5a35e18b 100644 (file)
@@ -391,11 +391,11 @@ trait CRM_Contact_Form_Task_EmailTrait {
       // has no meaning for followup activities, and this doesn't prevent
       // creating more manually if desired.
       $followupStatus = $this->createFollowUpActivities($formValues, $activityIds[0]);
-      $count_success = count($this->_toContactDetails);
+
       CRM_Core_Session::setStatus(ts('One message was sent successfully. ', [
         'plural' => '%count messages were sent successfully. ',
-        'count' => $count_success,
-      ]) . $followupStatus, ts('Message Sent', ['plural' => 'Messages Sent', 'count' => $count_success]), 'success');
+        'count' => $sent,
+      ]) . $followupStatus, ts('Message Sent', ['plural' => 'Messages Sent', 'count' => $sent]), 'success');
     }
 
     if (!empty($this->suppressedEmails)) {
@@ -441,13 +441,33 @@ trait CRM_Contact_Form_Task_EmailTrait {
    * Get the emails from the added element.
    *
    * @return array
+   * @throws \API_Exception
    */
   protected function getEmails(): array {
     $allEmails = explode(',', $this->getSubmittedValue('to'));
     $return = [];
+    $contactIDs = [];
     foreach ($allEmails as $value) {
       $values = explode('::', $value);
-      $return[] = ['contact_id' => $values[0], 'email' => $values[1]];
+      $return[$values[0]] = ['contact_id' => $values[0], 'email' => $values[1]];
+      $contactIDs[] = $values[0];
+    }
+    $this->suppressedEmails = [];
+    $suppressionDetails = Email::get(FALSE)
+      ->addWhere('contact_id', 'IN', $contactIDs)
+      ->addWhere('is_primary', '=', TRUE)
+      ->addSelect('email', 'contact_id', 'contact_id.is_deceased', 'on_hold', 'contact_id.do_not_email', 'contact_id.display_name')
+      ->execute();
+    foreach ($suppressionDetails as $details) {
+      if (empty($details['email']) || $details['contact_id.is_deceased'] || $details['contact_id.do_not_email'] || $details['on_hold']) {
+        $this->setSuppressedEmail($details['contact_id'], [
+          'on_hold' => $details['on_hold'],
+          'is_deceased' => $details['contact_id.is_deceased'],
+          'email' => $details['email'],
+          'display_name' => $details['contact_id.display_name'],
+        ]);
+        unset($return[$details['contact_id']]);
+      }
     }
     return $return;
   }
@@ -737,7 +757,7 @@ trait CRM_Contact_Form_Task_EmailTrait {
 
     $userID = CRM_Core_Session::getLoggedInContactID();
 
-    $sent = $notSent = [];
+    $sent = 0;
     $attachmentFileIds = [];
     $activityIds = [];
     $firstActivityCreated = FALSE;
@@ -755,7 +775,6 @@ trait CRM_Contact_Form_Task_EmailTrait {
         'disableSmarty' => !CRM_Utils_Constant::value('CIVICRM_MAIL_SMARTY'),
       ]);
 
-      $sent = FALSE;
       // To minimize storage requirements, only one copy of any file attachments uploaded to CiviCRM is kept,
       // even when multiple contacts will receive separate emails from CiviCRM.
       if (!empty($attachmentFileIds)) {
@@ -785,7 +804,7 @@ trait CRM_Contact_Form_Task_EmailTrait {
         $bcc
       )
       ) {
-        $sent = TRUE;
+        $sent++;
       }
     }
 
index 444122827343f2bab5223fba7e63e020b8212e4c..950000e8ccce11c84e8ed3f2da321ae6d7293317 100644 (file)
@@ -94,7 +94,7 @@ class CRM_Contact_Form_Task_EmailTest extends CiviUnitTestCase {
       $to[] = $contactID . '::' . $email;
     }
     $deceasedContactID = $this->individualCreate(['is_deceased' => 1, 'email' => 'dead@example.com']);
-    $to[] = $deceasedContactID . '::' . 'email@example.com';
+    $to[] = $deceasedContactID . '::' . 'dead@example.com';
     /* @var CRM_Contact_Form_Task_Email $form*/
     $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [
       'to' => implode(',', $to),