dev/core#352 Ensure that contacts that are to be exluded are not added incorrectly...
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 22 Aug 2018 03:59:37 +0000 (13:59 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Mon, 27 Aug 2018 20:00:06 +0000 (06:00 +1000)
Add in DISTINCT clause as per Moonish's comment

CRM/Mailing/BAO/Mailing.php
tests/phpunit/CRM/Mailing/BAO/MailingTest.php

index 212284f00ee82f09cc36837396c9a07b98d16c7b..dd4ef5bc013ed71cd92dfe30153d805129e1b3f5 100644 (file)
@@ -282,8 +282,10 @@ class CRM_Mailing_BAO_Mailing extends CRM_Mailing_DAO_Mailing {
     // Get recipients selected in prior mailings
     if (!empty($priorMailingIDs['Include'])) {
       CRM_Utils_SQL_Select::from('civicrm_mailing_recipients')
-        ->select("contact_id, $entityColumn")
+        ->select("DISTINCT civicrm_mailing_recipients.contact_id, $entityColumn")
+        ->join('temp', " LEFT JOIN $excludeTempTablename temp ON civicrm_mailing_recipients.contact_id = temp.contact_id ")
         ->where('mailing_id IN (#mailings)')
+        ->where('temp.contact_id IS NULL')
         ->param('#mailings', $priorMailingIDs['Include'])
         ->insertIgnoreInto($includedTempTablename, array('contact_id', $entityColumn))
         ->execute();
index 5e999c45e8bce377d29206a14833340f0d9fa1d3..4d3d77d2b9ee16f8bfc17208b2ebe7daacc56aa4 100644 (file)
@@ -130,6 +130,59 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
     $this->contactDelete($this->allowedContactId);
   }
 
+  /**
+   * Test mailing receipients when using previous mailing as include and contact is in exclude as well
+   */
+  public function testMailingIncludePreviousMailingExcludeGroup() {
+    $groupName = 'Test static group ' . substr(sha1(rand()), 0, 7);
+    $groupName2 = 'Test static group 2' . substr(sha1(rand()), 0, 7);
+    $groupID = $this->groupCreate([
+      'name' => $groupName,
+      'title' => $groupName,
+      'is_active' => 1,
+    ]);
+    $groupID2 = $this->groupCreate([
+      'name' => $groupName2,
+      'title' => $groupName2,
+      'is_active' => 1,
+    ]);
+    $contactID = $this->individualCreate(array(), 0);
+    $contactID2 = $this->individualCreate(array(), 2);
+    $this->callAPISuccess('GroupContact', 'Create', array(
+      'group_id' => $groupID,
+      'contact_id' => $contactID,
+    ));
+    $this->callAPISuccess('GroupContact', 'Create', array(
+      'group_id' => $groupID,
+      'contact_id' => $contactID2,
+    ));
+    $this->callAPISuccess('GroupContact', 'Create', array(
+      'group_id' => $groupID2,
+      'contact_id' => $contactID2,
+    ));
+    // Create dummy mailing
+    $mailingID = $this->callAPISuccess('Mailing', 'create', array())['id'];
+    $this->createMailingGroup($mailingID, $groupID);
+    $expectedContactIDs = [$contactID, $contactID2];
+    $this->assertRecipientsCorrect($mailingID, $expectedContactIDs);
+    $mailingID2 = $this->callAPISuccess('Mailing', 'create', array())['id'];
+    $this->createMailingGroup($mailingID2, $groupID2, 'Exclude');
+    $this->callAPISuccess('MailingGroup', 'create', array(
+      'mailing_id' => $mailingID2,
+      'group_type' => 'Include',
+      'entity_table' => CRM_Mailing_BAO_Mailing::getTableName(),
+      'entity_id' => $mailingID,
+    ));
+    $expectedContactIDs = [$contactID];
+    $this->assertRecipientsCorrect($mailingID2, $expectedContactIDs);
+    $this->callAPISuccess('mailing', 'delete', ['id' => $mailingID2]);
+    $this->callAPISuccess('mailing', 'delete', ['id' => $mailingID]);
+    $this->callAPISuccess('group', 'delete', ['id' => $groupID]);
+    $this->callAPISuccess('group', 'delete', ['id' => $groupID2]);
+    $this->callAPISuccess('contact', 'delete', ['id' => $contactID, 'skip_undelete' => TRUE]);
+    $this->callAPISuccess('contact', 'delete', ['id' => $contactID2, 'skip_undelete' => TRUE]);
+  }
+
   /**
    * Test verify that a disabled mailing group doesn't prvent access to the mailing generated with the group.
    */