APIv4 - Include child groups in contact group filter
authorColeman Watts <coleman@civicrm.org>
Sun, 6 Jun 2021 20:32:11 +0000 (16:32 -0400)
committercolemanw <coleman@civicrm.org>
Sun, 6 Jun 2021 21:14:11 +0000 (17:14 -0400)
CRM/Contact/BAO/GroupContactCache.php
tests/phpunit/api/v4/Entity/SavedSearchTest.php

index 7a9d8a0d7c8653ccd9d5f03c584218ab9521e337..22d3339d7bee10a0802fa96b2359d7ba5eabc3e8 100644 (file)
@@ -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)) {
index 789e502020a14eff3baa84086ae7654c1935eaac..67b92b723c75ff40268771bdc0721b3252c5b192 100644 (file)
@@ -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']]],