From 3b34d0e988dec83eaabfa97d55f82ec805f6bc13 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 22 Aug 2018 13:59:37 +1000 Subject: [PATCH] dev/core#352 Ensure that contacts that are to be exluded are not added incorrectly when adding recipients of past mailings --- CRM/Mailing/BAO/Mailing.php | 4 +- tests/phpunit/CRM/Mailing/BAO/MailingTest.php | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 212284f00ee8..d1f6a844539d 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -282,8 +282,10 @@ public static function getRecipients($mailingID) { // Get recipients selected in prior mailings if (!empty($priorMailingIDs['Include'])) { CRM_Utils_SQL_Select::from('civicrm_mailing_recipients') - ->select("contact_id, $entityColumn") + ->select("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(); diff --git a/tests/phpunit/CRM/Mailing/BAO/MailingTest.php b/tests/phpunit/CRM/Mailing/BAO/MailingTest.php index 5e999c45e8bc..4d3d77d2b9ee 100644 --- a/tests/phpunit/CRM/Mailing/BAO/MailingTest.php +++ b/tests/phpunit/CRM/Mailing/BAO/MailingTest.php @@ -130,6 +130,59 @@ public function testgetRecipientsUsingACL() { $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. */