dev/core#861 Ensure that when processing mailings that no emails are sent to deceased...
authorSeamus Lee <seamuslee001@gmail.com>
Sun, 6 Oct 2019 16:26:46 +0000 (03:26 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Sun, 6 Oct 2019 16:26:46 +0000 (03:26 +1100)
CRM/Mailing/BAO/Recipients.php
tests/phpunit/api/v3/JobProcessMailingTest.php

index 608f83db67f5a43733b5cbd4553d89cc286a7659..b554f385c68db0f710ceceb6c88d3dfa8f32ce28 100644 (file)
@@ -73,10 +73,26 @@ WHERE  mailing_id = %1
       $limitString = "LIMIT $offset, $limit";
     }
 
+    $isSMSmode = CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $mailingID, 'sms_provider_id', 'id');
+    $additionalJoin = '';
+    if (!$isSMSmode) {
+      // mailing_recipients added when mailing is submitted in UI by user.
+      // if any email is marked on_hold =1 or contact is deceased after mailing is submitted
+      // then it should be get skipped while preparing event_queue
+      // event_queue list is prepared when mailing job gets started.
+      $additionalJoin = " INNER JOIN civicrm_email e ON (r.email_id = e.id AND e.on_hold = 0 AND e.is_primary = 1)
+                          INNER JOIN civicrm_contact c on (c.id = r.contact_id AND c.is_deceased <> 1 AND c.do_not_email = 0 AND c.is_opt_out = 0)
+";
+    }
+    else {
+      $additionalJoin = "INNER JOIN civicrm_contact c on (c.id = r.contact_id AND c.is_deceased <> 1 AND c.do_not_sms = 0 AND c.is_opt_out = 0)";
+    }
+
     $sql = "
-SELECT contact_id, email_id, phone_id
-FROM   civicrm_mailing_recipients
-WHERE  mailing_id = %1
+SELECT r.contact_id, r.email_id, r.phone_id
+FROM   civicrm_mailing_recipients r
+{$additionalJoin}
+WHERE  r.mailing_id = %1
        $limitString
 ";
     $params = [1 => [$mailingID, 'Integer']];
index 1d8fa229707699abd2981bdb36250c06e1eeb91d..6cdf544f88a4973df24078f86eeb61a6df5a73cb 100644 (file)
@@ -119,6 +119,30 @@ class api_v3_JobProcessMailingTest extends CiviUnitTestCase {
     $this->_mut->assertRecipients($this->getRecipients(1, 2));
   }
 
+  /**
+   * Test what happens when a contact is set to decesaed
+   */
+  public function testDecesasedRecepient() {
+    $contactID = $this->individualCreate(['first_name' => 'test dead recipeint', 'email' => 'mailtestdead@civicrm.org']);
+    $this->callAPISuccess('group_contact', 'create', [
+      'contact_id' => $contactID,
+      'group_id' => $this->_groupID,
+      'status' => 'Added',
+    ]);
+    $this->createContactsInGroup(2, $this->_groupID);
+    Civi::settings()->add([
+      'mailerBatchLimit' => 2,
+    ]);
+    $mailing = $this->callAPISuccess('mailing', 'create', $this->_params);
+    $this->assertEquals(3, $this->callAPISuccess('MailingRecipients', 'get', ['mailing_id' => $mailing['id']])['count']);
+    $this->_mut->assertRecipients([]);
+    $this->callAPISuccess('Contact', 'create', ['id' => $contactID, 'is_deceased' => 1, 'contact_type' => 'Individual']);
+    $this->callAPISuccess('job', 'process_mailing', []);
+    // Check that the deceased contact is not found in the mailing.
+    $this->_mut->assertRecipients($this->getRecipients(1, 2));
+
+  }
+
   /**
    * Test pause and resume on Mailing.
    */