*/
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);
*/
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;
}
/**
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');
+ }
+
}
--- /dev/null
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved. |
+ | |
+ | This work is published under the GNU AGPLv3 license with some |
+ | permitted exceptions and without any warranty. For full license |
+ | and copyright information, see https://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+
+namespace api\v4\Utils;
+
+use api\v4\UnitTestCase;
+use Civi\Api4\CustomGroup;
+use Civi\Api4\Utils\CoreUtil;
+
+/**
+ * @group headless
+ */
+class CoreUtilTest extends UnitTestCase {
+
+ /**
+ */
+ public function testGetApiNameFromTableName() {
+ $this->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']));
+
+ }
+
+}