From: Coleman Watts Date: Sun, 6 Jun 2021 20:32:11 +0000 (-0400) Subject: APIv4 - Include child groups in contact group filter X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c6ffa626f572e1423140612be67d4a1f070f2f44;p=civicrm-core.git APIv4 - Include child groups in contact group filter --- diff --git a/CRM/Contact/BAO/GroupContactCache.php b/CRM/Contact/BAO/GroupContactCache.php index 7a9d8a0d7c..22d3339d7b 100644 --- a/CRM/Contact/BAO/GroupContactCache.php +++ b/CRM/Contact/BAO/GroupContactCache.php @@ -648,9 +648,10 @@ ORDER BY gc.contact_id, g.children * @throws \CiviCRM_API3_Exception */ public static function populateTemporaryTableWithContactsInGroups(array $groupIDs, string $temporaryTable): void { + $childAndParentGroupIDs = array_merge($groupIDs, CRM_Contact_BAO_GroupNesting::getDescendentGroupIds($groupIDs)); $groups = civicrm_api3('Group', 'get', [ 'is_active' => 1, - 'id' => ['IN' => $groupIDs], + 'id' => ['IN' => $childAndParentGroupIDs], 'saved_search_id' => ['>' => 0], 'return' => 'id', ]); @@ -659,7 +660,7 @@ ORDER BY gc.contact_id, g.children $query = " SELECT DISTINCT group_contact.contact_id as contact_id FROM civicrm_group_contact group_contact - WHERE group_contact.group_id IN (" . implode(', ', $groupIDs) . ") + WHERE group_contact.group_id IN (" . implode(', ', $childAndParentGroupIDs) . ") AND group_contact.status = 'Added' "; if (!empty($smartGroups)) { diff --git a/tests/phpunit/api/v4/Entity/SavedSearchTest.php b/tests/phpunit/api/v4/Entity/SavedSearchTest.php index 789e502020..67b92b723c 100644 --- a/tests/phpunit/api/v4/Entity/SavedSearchTest.php +++ b/tests/phpunit/api/v4/Entity/SavedSearchTest.php @@ -147,6 +147,10 @@ class SavedSearchTest extends UnitTestCase { ->execute()->first()['id']; } + $parentGroupId = \Civi\Api4\Group::create(FALSE) + ->setValues(['title' => uniqid()]) + ->execute()->first()['id']; + $savedSearchA = civicrm_api4('SavedSearch', 'create', [ 'values' => [ 'api_entity' => 'Contact', @@ -158,7 +162,7 @@ class SavedSearchTest extends UnitTestCase { ], ], 'chain' => [ - 'group' => ['Group', 'create', ['values' => ['title' => 'In A Test', 'saved_search_id' => '$id']], 0], + 'group' => ['Group', 'create', ['values' => ['parents' => [$parentGroupId], 'title' => 'In A Test', 'saved_search_id' => '$id']], 0], ], ])->first(); @@ -174,7 +178,7 @@ class SavedSearchTest extends UnitTestCase { ], ], 'chain' => [ - 'group' => ['Group', 'create', ['values' => ['title' => 'In B Test', 'saved_search_id' => '$id']], 0], + 'group' => ['Group', 'create', ['values' => ['parents' => [$parentGroupId], 'title' => 'In B Test', 'saved_search_id' => '$id']], 0], ], ])->first(); @@ -183,6 +187,12 @@ class SavedSearchTest extends UnitTestCase { ]); $this->assertCount(15, $bothGroups); + // Parent group includes both groups a & b so should give the same results as above + $parentGroup = civicrm_api4('Contact', 'get', [ + 'where' => [['groups', 'IN', [$parentGroupId]]], + ]); + $this->assertCount(15, $parentGroup); + $aNotB = civicrm_api4('Contact', 'get', [ 'where' => [ ['groups:name', 'IN', [$savedSearchA['group']['name']]],