From: Coleman Watts Date: Wed, 14 Apr 2021 12:35:58 +0000 (-0400) Subject: APIv4 - Improve entity name lookup & add test X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=aa680b8df399aa632f3af9cbaf57a76a4758b6b0;p=civicrm-core.git APIv4 - Improve entity name lookup & add test --- diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index f7195d0d17..c5b0d5e829 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -50,7 +50,8 @@ class CoreUtil { */ public static function getApiClass($entityName) { if (strpos($entityName, 'Custom_') === 0) { - return 'Civi\Api4\CustomValue'; + $groupName = substr($entityName, 7); + return self::isCustomEntity($groupName) ? 'Civi\Api4\CustomValue' : NULL; } // Because "Case" is a reserved php keyword $className = 'Civi\Api4\\' . ($entityName === 'Case' ? 'CiviCase' : $entityName); @@ -80,12 +81,14 @@ class CoreUtil { */ public static function getApiNameFromTableName($tableName) { $entityName = AllCoreTables::getBriefName(AllCoreTables::getClassForTable($tableName)); - if (!$entityName) { - $customGroup = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $tableName, 'name', 'table_name'); - return $customGroup ? "Custom_$customGroup" : NULL; + // Real entities + if ($entityName) { + // Verify class exists + return self::getApiClass($entityName) ? $entityName : NULL; } - // Verify class exists - return self::getApiClass($entityName) ? $entityName : NULL; + // Multi-value custom group pseudo-entities + $customGroup = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $tableName, 'name', 'table_name'); + return self::isCustomEntity($customGroup) ? "Custom_$customGroup" : NULL; } /** @@ -137,4 +140,15 @@ class CoreUtil { return NULL; } + /** + * Checks if a custom group exists and is multivalued + * + * @param $customGroupName + * @return bool + * @throws \CRM_Core_Exception + */ + private static function isCustomEntity($customGroupName) { + return $customGroupName && \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupName, 'is_multiple', 'name'); + } + } diff --git a/tests/phpunit/api/v4/Utils/CoreUtilTest.php b/tests/phpunit/api/v4/Utils/CoreUtilTest.php new file mode 100644 index 0000000000..2f81dd15fd --- /dev/null +++ b/tests/phpunit/api/v4/Utils/CoreUtilTest.php @@ -0,0 +1,75 @@ +assertEquals('Contact', CoreUtil::getApiNameFromTableName('civicrm_contact')); + $this->assertNull(CoreUtil::getApiNameFromTableName('civicrm_nothing')); + + $singleGroup = CustomGroup::create(FALSE) + ->addValue('title', uniqid()) + ->addValue('extends', 'Contact') + ->execute()->first(); + + $this->assertNull(CoreUtil::getApiNameFromTableName($singleGroup['table_name'])); + + $multiGroup = CustomGroup::create(FALSE) + ->addValue('title', uniqid()) + ->addValue('extends', 'Contact') + ->addValue('is_multiple', TRUE) + ->execute()->first(); + + $this->assertEquals('Custom_' . $multiGroup['name'], CoreUtil::getApiNameFromTableName($multiGroup['table_name'])); + } + + public function testGetApiClass() { + $this->assertEquals('Civi\Api4\Contact', CoreUtil::getApiClass('Contact')); + $this->assertEquals('Civi\Api4\CiviCase', CoreUtil::getApiClass('Case')); + $this->assertNull(CoreUtil::getApiClass('NothingAtAll')); + + $singleGroup = CustomGroup::create(FALSE) + ->addValue('title', uniqid()) + ->addValue('extends', 'Contact') + ->execute()->first(); + + $this->assertNull(CoreUtil::getApiClass($singleGroup['name'])); + + $multiGroup = CustomGroup::create(FALSE) + ->addValue('title', uniqid()) + ->addValue('extends', 'Contact') + ->addValue('is_multiple', TRUE) + ->execute()->first(); + + $this->assertEquals('Civi\Api4\CustomValue', CoreUtil::getApiClass('Custom_' . $multiGroup['name'])); + + } + +}