From aa680b8df399aa632f3af9cbaf57a76a4758b6b0 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 14 Apr 2021 08:35:58 -0400 Subject: [PATCH] APIv4 - Improve entity name lookup & add test --- Civi/Api4/Utils/CoreUtil.php | 26 +++++-- tests/phpunit/api/v4/Utils/CoreUtilTest.php | 75 +++++++++++++++++++++ 2 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/api/v4/Utils/CoreUtilTest.php diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index f7195d0d178c..c5b0d5e82957 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -50,7 +50,8 @@ public static function getBAOFromApiName($entityName) { */ 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 @@ public static function getTableName($entityName) { */ 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 @@ public static function getCustomGroupExtends(string $entityName) { 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 000000000000..2f81dd15fd48 --- /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'])); + + } + +}