From 2acf8c583cf03c9efd471abc83272db946db08be Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 5 Jun 2018 15:35:32 +1000 Subject: [PATCH] dev/core#163 Fix issue where disabling a group would block access to any mailing reports that group was used for --- CRM/Mailing/BAO/Mailing.php | 2 +- tests/phpunit/CRM/Mailing/BAO/MailingTest.php | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 09983f45bf..fc82a36b06 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -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), diff --git a/tests/phpunit/CRM/Mailing/BAO/MailingTest.php b/tests/phpunit/CRM/Mailing/BAO/MailingTest.php index b0fc690efd..5e6ae5d900 100644 --- a/tests/phpunit/CRM/Mailing/BAO/MailingTest.php +++ b/tests/phpunit/CRM/Mailing/BAO/MailingTest.php @@ -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 -- 2.25.1