extend unit test
authordeb.monish <monish.deb@jmaconsulting.biz>
Fri, 30 Mar 2018 17:48:33 +0000 (23:18 +0530)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Tue, 19 Jun 2018 07:44:00 +0000 (13:14 +0530)
CRM/Mailing/BAO/Mailing.php
tests/phpunit/CRM/Mailing/BAO/MailingTest.php

index 1f7965fb41a97d27dbdd56f7143edb5f9480d59a..f306c72770970b3b35cedba53cd993adff4a217c 100644 (file)
@@ -228,7 +228,6 @@ class CRM_Mailing_BAO_Mailing extends CRM_Mailing_DAO_Mailing {
     );
 
     if ($isSMSmode) {
-
       $criteria = array(
         'is_opt_out' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_opt_out = 0"),
         'is_deceased' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_deceased <> 1"),
@@ -250,6 +249,7 @@ class CRM_Mailing_BAO_Mailing extends CRM_Mailing_DAO_Mailing {
         'location_filter' => CRM_Utils_SQL_Select::fragment()->where($location_filter),
         'email_not_null' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.email IS NOT NULL"),
         'email_not_empty' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.email != ''"),
+        'email_not_on_hold' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.on_hold = 0"),
         'mailing_id' => CRM_Utils_SQL_Select::fragment()->where("mg.mailing_id = #mailingID"),
         'temp_contact_null' => CRM_Utils_SQL_Select::fragment()->where('temp.contact_id IS NULL'),
         'order_by' => CRM_Utils_SQL_Select::fragment()->orderBy($order_by),
index 09a0044326c00c37c9b28b4f3c39a69e60685e7b..5e999c45e8bce377d29206a14833340f0d9fa1d3 100644 (file)
@@ -463,11 +463,15 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
    */
   public function testAlterMailingRecipientsHook() {
     $groupID = $this->groupCreate();
+    $this->tagCreate(array('name' => 'Tagged'));
 
     // Create deseased Contact 1 and add in group
     $contactID1 = $this->individualCreate(array('email' => 'abc@test.com', 'is_deceased' => 1), 0);
     // Create deseased Contact 2 and add in group
     $contactID2 = $this->individualCreate(array('email' => 'def@test.com'), 1);
+    // Create deseased Contact 3 and add in group
+    $contactID3 = $this->individualCreate(array('email' => 'ghi@test.com', 'is_deceased' => 1), 2);
+
     // Add both the created contacts in group
     $this->callAPISuccess('GroupContact', 'Create', array(
       'group_id' => $groupID,
@@ -477,6 +481,12 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
       'group_id' => $groupID,
       'contact_id' => $contactID2,
     ));
+    $this->callAPISuccess('GroupContact', 'Create', array(
+      'group_id' => $groupID,
+      'contact_id' => $contactID3,
+    ));
+    $this->entityTagAdd(array('contact_id' => $contactID3, 'tag_id' => 'Tagged'));
+
     // trigger the alterMailingRecipients hook
     $this->hookClass->setHook('civicrm_alterMailingRecipients', array($this, 'alterMailingRecipients'));
 
@@ -497,13 +507,17 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
    * @implements CRM_Utils_Hook::alterMailingRecipients
    *
    * @param object $mailingObject
-   * @param array $params
+   * @param array $criteria
    * @param string $context
    */
-  public function alterMailingRecipients(&$mailingObject, &$params, $context) {
+  public function alterMailingRecipients(&$mailingObject, &$criteria, $context) {
     if ($context == 'pre') {
-      // modify the filter to include only deceased recipient(s)
-      $params['filters']['is_deceased'] = 'civicrm_contact.is_deceased = 1';
+      // modify the filter to include only deceased recipient(s) that is Tagged
+      $criteria['is_deceased'] = CRM_Utils_SQL_Select::fragment()->where("civicrm_contact.is_deceased = 1");
+      $criteria['tagged_contact'] = CRM_Utils_SQL_Select::fragment()
+                                      ->join('civicrm_entity_tag', "INNER JOIN civicrm_entity_tag et ON et.entity_id = civicrm_contact.id AND et.entity_table = 'civicrm_contact'")
+                                      ->join('civicrm_tag', "INNER JOIN civicrm_tag t ON t.id = et.tag_id")
+                                      ->where("t.name = 'Tagged'");
     }
     else {
       $mailingRecipients = $this->callAPISuccess('MailingRecipients', 'get', array(
@@ -514,7 +528,7 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
         ),
       ));
       $this->assertEquals(1, $mailingRecipients['count'], 'Check recipient count');
-      $this->assertEquals('abc@test.com', $mailingRecipients['values'][$mailingRecipients['id']]['api.Email.getvalue'], 'Check if recipient email belong to deceased contact');
+      $this->assertEquals('ghi@test.com', $mailingRecipients['values'][$mailingRecipients['id']]['api.Email.getvalue'], 'Check if recipient email belong to deceased contact');
     }
   }