smart groups as mailing lists were not taking unsubscribed users into account
authorThomas <tmannell@rnao.ca>
Tue, 16 Oct 2018 16:20:58 +0000 (12:20 -0400)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 18 Oct 2018 20:48:33 +0000 (07:48 +1100)
dev/core#448 Add in a Unit test to demonstrate the problem with smart groups and contats removed from smart groups not being properly checked

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

index 1f0f0fa94f8dbdfaeddc952675b057d4450b9351..3e97b47ea97a092f126d611584d90d6a783d1475 100644 (file)
@@ -296,9 +296,11 @@ class CRM_Mailing_BAO_Mailing extends CRM_Mailing_DAO_Mailing {
         ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
         ->join($entityTable, " INNER JOIN $entityTable ON $entityTable.contact_id = $contact.id ")
         ->join('gc', " INNER JOIN civicrm_group_contact_cache gc ON $contact.id = gc.contact_id ")
+        ->join('gcr', " LEFT JOIN civicrm_group_contact gcr ON gc.group_id = gcr.group_id AND gc.contact_id = gcr.contact_id")
         ->join('mg', " INNER JOIN civicrm_mailing_group mg  ON  gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
         ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
         ->where('gc.group_id IN (#groups)')
+        ->where('gcr.status IS NULL OR gcr.status != "Removed"')
         ->merge($criteria)
         ->replaceInto($includedTempTablename, array('contact_id', $entityColumn))
         ->param('#groups', $includeSmartGroupIDs)
index 4d3d77d2b9ee16f8bfc17208b2ebe7daacc56aa4..bfa0ed9e93d95a4a406ffdf23e9a88cc92c5dc62 100644 (file)
@@ -397,6 +397,21 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
     unset($expected[0], $expected[4], $expected[8]);
     $this->assertRecipientsCorrect($mailing['id'], $expected);
 
+    // Tear down: delete mailing, groups, contacts
+    $this->deleteMailing($mailing['id']);
+
+    // Create a New mailing, Testing contacts removed from smart group.
+    // In this case groupIDs6 will only pick up contacts[0] amd contacts[8] with it's
+    // criteria. However we are deliberly going to remove contactIds[8] from the group
+    // Which should mean the mainling only finds 1 contact that is contactIds[0]
+    $mailing = $this->callAPISuccess('Mailing', 'create', array());
+    $this->callAPISuccess('GroupContact', 'Create', array(
+      'group_id' => $groupIDs[6],
+      'contact_id' => $contactIDs[8],
+      'status' => 'Removed',
+    ));
+    $this->createMailingGroup($mailing['id'], $groupIDs[6]);
+    $this->assertRecipientsCorrect($mailing['id'], [$contactIDs[0]]);
     // Tear down: delete mailing, groups, contacts
     $this->deleteMailing($mailing['id']);
     foreach ($groupIDs as $groupID) {