From acaec9760a75f5d830bd52576c3eb47ec61f5592 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 7 Oct 2019 03:26:46 +1100 Subject: [PATCH] dev/core#861 Ensure that when processing mailings that no emails are sent to deceased contacts or those with emails now on hold --- CRM/Mailing/BAO/Recipients.php | 22 ++++++++++++++--- .../phpunit/api/v3/JobProcessMailingTest.php | 24 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CRM/Mailing/BAO/Recipients.php b/CRM/Mailing/BAO/Recipients.php index 608f83db67..b554f385c6 100644 --- a/CRM/Mailing/BAO/Recipients.php +++ b/CRM/Mailing/BAO/Recipients.php @@ -73,10 +73,26 @@ WHERE mailing_id = %1 $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 AND e.is_primary = 1) + 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)"; + } + $sql = " -SELECT contact_id, email_id, phone_id -FROM civicrm_mailing_recipients -WHERE mailing_id = %1 +SELECT r.contact_id, r.email_id, r.phone_id +FROM civicrm_mailing_recipients r +{$additionalJoin} +WHERE r.mailing_id = %1 $limitString "; $params = [1 => [$mailingID, 'Integer']]; diff --git a/tests/phpunit/api/v3/JobProcessMailingTest.php b/tests/phpunit/api/v3/JobProcessMailingTest.php index 1d8fa22970..6cdf544f88 100644 --- a/tests/phpunit/api/v3/JobProcessMailingTest.php +++ b/tests/phpunit/api/v3/JobProcessMailingTest.php @@ -119,6 +119,30 @@ class api_v3_JobProcessMailingTest extends CiviUnitTestCase { $this->_mut->assertRecipients($this->getRecipients(1, 2)); } + /** + * Test what happens when a contact is set to decesaed + */ + public function testDecesasedRecepient() { + $contactID = $this->individualCreate(['first_name' => 'test dead recipeint', 'email' => 'mailtestdead@civicrm.org']); + $this->callAPISuccess('group_contact', 'create', [ + 'contact_id' => $contactID, + 'group_id' => $this->_groupID, + 'status' => 'Added', + ]); + $this->createContactsInGroup(2, $this->_groupID); + Civi::settings()->add([ + 'mailerBatchLimit' => 2, + ]); + $mailing = $this->callAPISuccess('mailing', 'create', $this->_params); + $this->assertEquals(3, $this->callAPISuccess('MailingRecipients', 'get', ['mailing_id' => $mailing['id']])['count']); + $this->_mut->assertRecipients([]); + $this->callAPISuccess('Contact', 'create', ['id' => $contactID, 'is_deceased' => 1, 'contact_type' => 'Individual']); + $this->callAPISuccess('job', 'process_mailing', []); + // Check that the deceased contact is not found in the mailing. + $this->_mut->assertRecipients($this->getRecipients(1, 2)); + + } + /** * Test pause and resume on Mailing. */ -- 2.25.1