From 3d182a0410cc3f52a3adaa7f8e36da216662048a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 15 May 2020 11:04:04 -0400 Subject: [PATCH] Fix CRM_Core_DAO_AllCoreTables::getBriefName to accept BAO name This function was overly strict about not accepting BAO names, prompting end-users of the function to do their own ad-hoc conversions before using it. --- CRM/Core/DAO.php | 2 +- CRM/Core/DAO/AllCoreTables.php | 3 ++- CRM/Core/PseudoConstant.php | 2 +- api/api.php | 5 ++--- api/v3/utils.php | 2 +- tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php | 5 +++++ 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index a72e5cdf33..b947740e0e 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1736,7 +1736,7 @@ FROM civicrm_domain if (!$blockCopyofCustomValues) { $newObject->copyCustomFields($object->id, $newObject->id); } - CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getBriefName(str_replace('_BAO_', '_DAO_', $daoName)), $newObject->id, $newObject); + CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject->id, $newObject); } return $newObject; diff --git a/CRM/Core/DAO/AllCoreTables.php b/CRM/Core/DAO/AllCoreTables.php index f6a8e2b657..6cce0eb92c 100644 --- a/CRM/Core/DAO/AllCoreTables.php +++ b/CRM/Core/DAO/AllCoreTables.php @@ -254,7 +254,8 @@ class CRM_Core_DAO_AllCoreTables { * Ex: 'Contact'. */ public static function getBriefName($className) { - return CRM_Utils_Array::value($className, array_flip(self::daoToClass())); + $className = self::getCanonicalClassName($className); + return array_search($className, self::daoToClass(), TRUE) ?: NULL; } /** diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index afbb0b5fbb..911189b5e1 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -194,7 +194,7 @@ class CRM_Core_PseudoConstant { 'fresh' => FALSE, 'context' => $context, ]; - $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getCanonicalClassName($daoName)); + $entity = CRM_Core_DAO_AllCoreTables::getBriefName($daoName); // Custom fields are not in the schema if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) { diff --git a/api/api.php b/api/api.php index 8208af08db..7b9dbd7717 100644 --- a/api/api.php +++ b/api/api.php @@ -302,12 +302,11 @@ function _civicrm_api_get_entity_name_from_camel($entity) { /** * Having a DAO object find the entity name. * - * @param object $bao + * @param CRM_Core_DAO $bao * DAO being passed in. * * @return string */ function _civicrm_api_get_entity_name_from_dao($bao) { - $daoName = str_replace("BAO", "DAO", get_class($bao)); - return CRM_Core_DAO_AllCoreTables::getBriefName($daoName); + return CRM_Core_DAO_AllCoreTables::getBriefName(get_class($bao)); } diff --git a/api/v3/utils.php b/api/v3/utils.php index 3101e29480..87f12b116e 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1237,7 +1237,7 @@ function formatCheckBoxField(&$checkboxFieldValue, $customFieldLabel, $entity) { * @return array */ function _civicrm_api3_basic_get($bao_name, $params, $returnAsSuccess = TRUE, $entity = "", $sql = NULL, $uniqueFields = FALSE) { - $entity = $entity ?: CRM_Core_DAO_AllCoreTables::getBriefName(str_replace('_BAO_', '_DAO_', $bao_name)); + $entity = $entity ?: CRM_Core_DAO_AllCoreTables::getBriefName($bao_name); $options = _civicrm_api3_get_options_from_params($params); $query = new \Civi\API\Api3SelectQuery($entity, CRM_Utils_Array::value('check_permissions', $params, FALSE)); diff --git a/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php b/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php index e6998025be..1bf6542772 100644 --- a/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php +++ b/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php @@ -213,4 +213,9 @@ class CRM_Core_DAO_AllCoreTablesTest extends CiviUnitTestCase { $this->assertFalse(CRM_Core_DAO_AllCoreTables::isCoreTable('civicrm_invalid_table'), 'civicrm_invalid_table should NOT be a core table'); } + public function testGetBriefName() { + $this->assertEquals('Contact', CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Contact_BAO_Contact')); + $this->assertEquals('Contact', CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Contact_DAO_Contact')); + } + } -- 2.25.1