APIv4 - Improve entity name lookup & add test
authorColeman Watts <coleman@civicrm.org>
Wed, 14 Apr 2021 12:35:58 +0000 (08:35 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 14 Apr 2021 13:26:58 +0000 (09:26 -0400)
Civi/Api4/Utils/CoreUtil.php
tests/phpunit/api/v4/Utils/CoreUtilTest.php [new file with mode: 0644]

index f7195d0d178c92dc5553069e93de762a1f60e8d0..c5b0d5e82957f3d2022586a2c92be1f053b30bea 100644 (file)
@@ -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 (file)
index 0000000..2f81dd1
--- /dev/null
@@ -0,0 +1,75 @@
+<?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']));
+
+  }
+
+}