Skip to content

Commit

Permalink
Merge pull request #20056 from colemanw/api4GetApiFix
Browse files Browse the repository at this point in the history
APIv4 - Improve entity name lookup
  • Loading branch information
eileenmcnaughton authored Apr 14, 2021
2 parents 3e4762e + aa680b8 commit 8cd618a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
26 changes: 20 additions & 6 deletions Civi/Api4/Utils/CoreUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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');
}

}
75 changes: 75 additions & 0 deletions tests/phpunit/api/v4/Utils/CoreUtilTest.php
Original file line number Diff line number Diff line change
@@ -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']));

}

}

0 comments on commit 8cd618a

Please sign in to comment.