Skip to content

Commit

Permalink
dev/core#352 Ensure that contacts that are to be exluded are not adde…
Browse files Browse the repository at this point in the history
…d incorrectly when adding recipients of past mailings
  • Loading branch information
seamuslee001 committed Aug 22, 2018
1 parent 3afc47d commit 3b34d0e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CRM/Mailing/BAO/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/CRM/Mailing/BAO/MailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 3b34d0e

Please sign in to comment.