dev/core#163 Fix issue where disabling a group would block access to any mailing...
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 5 Jun 2018 05:35:32 +0000 (15:35 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Tue, 5 Jun 2018 05:35:32 +0000 (15:35 +1000)
CRM/Mailing/BAO/Mailing.php
tests/phpunit/CRM/Mailing/BAO/MailingTest.php

index 09983f45bfede941540b3419c60fd72196b6c489..fc82a36b06f3b91800edbfcc91e1b7a304f0d7be 100644 (file)
@@ -2335,7 +2335,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
     // get all the groups that this user can access
     // if they dont have universal access
     $groupNames = civicrm_api3('Group', 'get', array(
-      'is_active' => 1,
+      'is_active' => '',
       'check_permissions' => TRUE,
       'return' => array('title', 'id'),
       'options' => array('limit' => 0),
index b0fc690efd28f2af5ba472799cf76b00c2c49f12..5e6ae5d90059ea095229109d3ebb63aadec817e0 100644 (file)
@@ -125,10 +125,48 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
     $this->assertRecipientsCorrect($mailingID, $expectedContactIDs);
 
     $this->cleanUpAfterACLs();
+    $this->callAPISuccess('Group', 'Delete', ['id' => $groupID]);
     $this->contactDelete($contactID1);
     $this->contactDelete($this->allowedContactId);
   }
 
+  /**
+   * Test verify that a disabled mailing group doesn't prvent access to the mailing generated with the group.
+   */
+  public function testGetMailingDisabledGroup() {
+    $this->prepareForACLs();
+    $this->createLoggedInUser();
+    // create hook to build ACL where clause which choses $this->allowedContactId as the only contact to be considered as mail recipient
+    $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereAllowedOnlyOne'));
+    $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'edit groups');
+    // Create dummy group and assign 2 contacts
+    $name = 'Test static group ' . substr(sha1(rand()), 0, 7);
+    $groupID = $this->groupCreate([
+      'name' => $name,
+      'title' => $name,
+      'is_active' => 1,
+    ]);
+    $contactID = $this->individualCreate(array(), 0);
+    $this->callAPISuccess('GroupContact', 'Create', array(
+      'group_id' => $groupID,
+      'contact_id' => $contactID,
+    ));
+
+    // Create dummy mailing
+    $mailingID = $this->callAPISuccess('Mailing', 'create', array())['id'];
+    $this->createMailingGroup($mailingID, $groupID);
+    // Now disable the group.
+    $this->callAPISuccess('group', 'create', [
+      'id' => $groupID,
+      'is_active' => 0,
+    ]);
+    $groups = CRM_Mailing_BAO_Mailing::mailingACLIDs();
+    $this->assertTrue(in_array($groupID, $groups));
+    $this->cleanUpAfterACLs();
+    $this->contactDelete($contactID);
+  }
+
   /**
    * Build ACL where clause
    *
@@ -144,6 +182,37 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
     $where = " contact_a.id = " . $this->allowedContactId;
   }
 
+  /**
+   * Implements ACLGroup hook.
+   *
+   * @implements CRM_Utils_Hook::aclGroup
+   *
+   * aclGroup function returns a list of permitted groups
+   * @param string $type
+   * @param int $contactID
+   * @param string $tableName
+   * @param array $allGroups
+   * @param array $currentGroups
+   */
+  public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
+    //don't use api - you will get a loop
+    $sql = " SELECT * FROM civicrm_group";
+    $groups = array();
+    $dao = CRM_Core_DAO::executeQuery($sql);
+    while ($dao->fetch()) {
+      $groups[] = $dao->id;
+    }
+    if (!empty($allGroups)) {
+      //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
+      // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
+      $currentGroups = array_intersect($groups, array_flip($allGroups));
+    }
+    else {
+      $currentGroups = $groups;
+    }
+  }
+
+
   /**
    * @todo Missing tests:
    * - Ensure opt out emails are not mailed