From 8a9f02722e8a2d608c43e3affd4bfddb5975856f Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Wed, 20 Dec 2023 15:03:13 -0500 Subject: [PATCH] SearchKit - Unit test for illegal offset error when grouping by contact --- .../api/v4/SearchDisplay/SearchRunTest.php | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index d25f36ced9..c0ad02827e 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -7,6 +7,7 @@ require_once __DIR__ . '/../../../../../../../tests/phpunit/api/v4/Api4TestBase. use api\v4\Api4TestBase; use Civi\API\Exception\UnauthorizedException; use Civi\Api4\Activity; +use Civi\Api4\Address; use Civi\Api4\Contact; use Civi\Api4\ContactType; use Civi\Api4\Email; @@ -1435,6 +1436,66 @@ class SearchRunTest extends Api4TestBase implements TransactionalInterface { $this->assertEquals(1, $data['Household']); } + public function testGroupByAddress(): void { + $source = uniqid(__FUNCTION__); + $sampleData = [ + ['contact_type' => 'Individual'], + ['contact_type' => 'Individual'], + ['contact_type' => 'Individual'], + ['contact_type' => 'Organization'], + ['contact_type' => 'Organization'], + ['contact_type' => 'Household'], + ]; + Contact::save(FALSE) + ->addDefault('source', $source) + ->setRecords($sampleData) + ->addChain('address', Address::create() + ->addValue('contact_id', '$id') + ->addValue('street_address', '123') + ) + ->execute(); + + $params = [ + 'checkPermissions' => FALSE, + 'return' => 'page:1', + 'savedSearch' => [ + 'api_entity' => 'Contact', + 'api_params' => [ + 'version' => 4, + 'select' => [ + 'COUNT(id) AS COUNT_id', + 'Contact_Address_contact_id_01.street_address', + 'GROUP_CONCAT(DISTINCT sort_name) AS GROUP_CONCAT_sort_name', + 'GROUP_CONCAT(DISTINCT contact_type:label) AS GROUP_CONCAT_contact_type_label', + 'GROUP_CONCAT(DISTINCT contact_sub_type:label) AS GROUP_CONCAT_contact_sub_type_label', + ], + 'where' => [ + ['source', '=', $source], + ], + 'groupBy' => [ + 'Contact_Address_contact_id_01.street_address', + ], + 'join' => [ + [ + 'Address AS Contact_Address_contact_id_01', + 'LEFT', + ['id', '=', 'Contact_Address_contact_id_01.contact_id'], + ['Contact_Address_contact_id_01.is_primary', '=', TRUE], + ], + ], + ], + ], + 'display' => NULL, + ]; + + $result = civicrm_api4('SearchDisplay', 'run', $params); + $this->assertCount(1, $result); + $this->assertEquals(6, $result[0]['data']['COUNT_id']); + $this->assertEquals('123', $result[0]['data']['Contact_Address_contact_id_01.street_address']); + sort($result[0]['data']['GROUP_CONCAT_contact_type_label']); + $this->assertEquals(['Household', 'Individual', 'Organization'], $result[0]['data']['GROUP_CONCAT_contact_type_label']); + } + public function testGroupByFunction(): void { $source = uniqid(__FUNCTION__); $sampleData = [ -- 2.25.1