Skip to content

Commit

Permalink
Merge pull request #18770 from civicrm/5.31
Browse files Browse the repository at this point in the history
5.31
  • Loading branch information
eileenmcnaughton authored Oct 14, 2020
2 parents 644d969 + 8af44ce commit 7ad12b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
35 changes: 15 additions & 20 deletions CRM/Mailing/BAO/Recipients.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,23 @@ public static function mailingQuery(
$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)
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)";
}
$isSMSMode = CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $mailingID, 'sms_provider_id', 'id');
$additionalJoin = $isSMSMode ? '' : " INNER JOIN civicrm_email e ON (r.email_id = e.id AND e.on_hold = 0)";

$sql = "
SELECT r.contact_id, r.email_id, r.phone_id
FROM civicrm_mailing_recipients r
{$additionalJoin}
WHERE r.mailing_id = %1
$limitString
";
SELECT r.contact_id, r.email_id, r.phone_id
FROM civicrm_mailing_recipients r
INNER JOIN civicrm_contact c on
(c.id = r.contact_id
AND c.is_deleted = 0
AND c.is_deceased = 0
AND c.do_not_" . ($isSMSMode ? 'sms' : 'email') . " = 0
AND c.is_opt_out = 0
)
{$additionalJoin}
WHERE r.mailing_id = %1
$limitString
";
$params = [1 => [$mailingID, 'Integer']];

return CRM_Core_DAO::executeQuery($sql, $params);
Expand Down
17 changes: 15 additions & 2 deletions tests/phpunit/api/v3/JobProcessMailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* @subpackage API_Job
*
* @copyright CiviCRM LLC https://civicrm.org/licensing
* @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
*
*/

Expand Down Expand Up @@ -100,10 +99,23 @@ public function testBasic() {
$this->_mut->assertRecipients($this->getRecipients(1, 2));
}

/**
* Test that a contact deleted after the mailing is queued is not emailed.
*
* @throws \CRM_Core_Exception
*/
public function testDeletedRecipient() {
$this->createContactsInGroup(2, $this->_groupID);
$this->callAPISuccess('Mailing', 'create', $this->_params);
$this->callAPISuccess('Contact', 'delete', ['id' => $this->callAPISuccessGetValue('GroupContact', ['return' => 'contact_id', 'options' => ['limit' => 1, 'sort' => 'id DESC']])]);
$this->callAPISuccess('job', 'process_mailing');
$this->_mut->assertRecipients($this->getRecipients(1, 1));
}

/**
* Test what happens when a contact is set to decesaed
*/
public function testDecesasedRecepient() {
public function testDeceasedRecipient() {
$contactID = $this->individualCreate(['first_name' => 'test dead recipeint', 'email' => 'mailtestdead@civicrm.org']);
$this->callAPISuccess('group_contact', 'create', [
'contact_id' => $contactID,
Expand Down Expand Up @@ -571,6 +583,7 @@ protected function cleanupMailingTest() {
'civicrm_activity_contact',
'civicrm_activity',
]);
Civi::settings()->set('mailerBatchLimit', 0);
}

/**
Expand Down

0 comments on commit 7ad12b2

Please sign in to comment.