Fix CRM_Core_DAO_AllCoreTables::getBriefName to accept BAO name
authorColeman Watts <coleman@civicrm.org>
Fri, 15 May 2020 15:04:04 +0000 (11:04 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 15 May 2020 15:07:36 +0000 (11:07 -0400)
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
CRM/Core/DAO/AllCoreTables.php
CRM/Core/PseudoConstant.php
api/api.php
api/v3/utils.php
tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php

index a72e5cdf33df691715fd599909023168ddca2011..b947740e0e595b59c44c180a7e94d3b4a33390e3 100644 (file)
@@ -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;
index f6a8e2b657f0cd4b4b113010e2bc4f17fa809f40..6cce0eb92cc3c0f582ccbc5c4b7c7ae744d95fa1 100644 (file)
@@ -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;
   }
 
   /**
index afbb0b5fbb4120b2515db85bd44c6f5d191809dd..911189b5e12f9b4ba7f64cb7b9c0df5dcc01ab76 100644 (file)
@@ -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])) {
index 8208af08db30d36f42b8a9e653f983f18bd69e62..7b9dbd77175e850577bff0a0eca013f94e519195 100644 (file)
@@ -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));
 }
index 3101e29480174cb7c6b0bc9222d6f418b61c0c63..87f12b116efd3f3e18a6783464ff45dc5f3ed57a 100644 (file)
@@ -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));
index e6998025be23f0d36f213d246e8aa029408c2c5f..1bf6542772ee61527e38c3432bb99e143244882e 100644 (file)
@@ -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'));
+  }
+
 }