From dab64f21b937dbf42212b31b370ff9dd8fe1e7f3 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 4 May 2022 10:34:57 -0400 Subject: [PATCH] APIv3 - Fix testCustomDataGet errors caused by legacy entity naming issues --- CRM/Core/DAO.php | 2 +- CRM/Core/DAO/AllCoreTables.php | 14 +++++++------- api/v3/Rule.php | 4 ++-- api/v3/RuleGroup.php | 2 +- api/v3/utils.php | 8 ++++++-- tests/phpunit/api/v3/SyntaxConformanceTest.php | 10 +++++++--- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 959e9d9cd2..3a8ba271be 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -384,7 +384,7 @@ class CRM_Core_DAO extends DB_DataObject { else { $options = CRM_Core_PseudoConstant::get($daoName, $fieldName); if (is_array($options)) { - $this->$dbName = $options[0]; + $this->$dbName = $options[0] ?? NULL; } else { $defaultValues = explode(',', $options); diff --git a/CRM/Core/DAO/AllCoreTables.php b/CRM/Core/DAO/AllCoreTables.php index 51ffc8f1f3..19009e09fd 100644 --- a/CRM/Core/DAO/AllCoreTables.php +++ b/CRM/Core/DAO/AllCoreTables.php @@ -218,12 +218,11 @@ class CRM_Core_DAO_AllCoreTables { // This map only applies to APIv3 $map = [ 'acl' => 'Acl', - 'ACL' => 'Acl', 'im' => 'Im', - 'IM' => 'Im', + 'pcp' => 'Pcp', ]; - if ($legacyV3 && isset($map[$name])) { - return $map[$name]; + if ($legacyV3 && isset($map[strtolower($name)])) { + return $map[strtolower($name)]; } $fragments = explode('_', $name); @@ -234,9 +233,10 @@ class CRM_Core_DAO_AllCoreTables { $fragment = 'UF' . ucfirst(substr($fragment, 2)); } } - // Special case: UFGroup, UFJoin, UFMatch, UFField (if passed in underscore-separated) - if ($fragments[0] === 'Uf') { - $fragments[0] = 'UF'; + // Exceptions to CamelCase: UFGroup, UFJoin, UFMatch, UFField, ACL, IM, PCP + $exceptions = ['Uf', 'Acl', 'Im', 'Pcp']; + if (in_array($fragments[0], $exceptions)) { + $fragments[0] = strtoupper($fragments[0]); } return implode('', $fragments); } diff --git a/api/v3/Rule.php b/api/v3/Rule.php index 24a0f02ec5..f43db917b4 100644 --- a/api/v3/Rule.php +++ b/api/v3/Rule.php @@ -27,7 +27,7 @@ * API result array */ function civicrm_api3_rule_create($params) { - return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Rule'); + return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'DedupeRule'); } /** @@ -67,5 +67,5 @@ function civicrm_api3_rule_delete($params) { * API result array */ function civicrm_api3_rule_get($params) { - return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Rule'); + return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'DedupeRule'); } diff --git a/api/v3/RuleGroup.php b/api/v3/RuleGroup.php index 0fcfa1f1ae..fe40d28ae0 100644 --- a/api/v3/RuleGroup.php +++ b/api/v3/RuleGroup.php @@ -67,5 +67,5 @@ function civicrm_api3_rule_group_delete($params) { * API result array */ function civicrm_api3_rule_group_get($params) { - return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); + return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'DedupeRuleGroup'); } diff --git a/api/v3/utils.php b/api/v3/utils.php index a7ce83160e..4f0674e884 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -320,7 +320,7 @@ function _civicrm_api3_get_DAO($name) { if ($name === 'MailingRecipients') { return 'CRM_Mailing_DAO_Recipients'; } - if ($name === 'AclRole') { + if ($name === 'AclRole' || $name === 'ACLRole') { return 'CRM_ACL_DAO_ACLEntityRole'; } // FIXME: DAO should be renamed CRM_SMS_DAO_SmsProvider @@ -1128,6 +1128,10 @@ function _civicrm_api3_custom_format_params($params, &$values, $extends, $entity * @param string $entity */ function _civicrm_api3_format_params_for_create(&$params, $entity) { + if (!$entity) { + return; + } + $entity = CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel($entity); $nonGenericEntities = array_merge(['Contact'], CRM_Contact_BAO_ContactType::basicTypes(TRUE)); $customFieldEntities = array_diff_key(CRM_Core_SelectValues::customGroupExtends(), array_fill_keys($nonGenericEntities, 1)); @@ -1958,7 +1962,7 @@ function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = []) { * @return array */ function _civicrm_api_get_custom_fields($entity, &$params) { - $entity = _civicrm_api_get_camel_name($entity); + $entity = CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel($entity); if ($entity == 'Contact') { // Use sub-type if available, otherwise "NULL" to fetch from all contact types $entity = $params['contact_type'] ?? NULL; diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index 6dbaff805c..32379086fe 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -877,15 +877,19 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { $this->createLoggedInUser(); $entitiesWithNamingIssues = [ - 'SmsProvider' => 'Provider', + 'Acl' => 'ACL', 'AclRole' => 'ACLEntityRole', + 'Im' => 'IM', 'Dedupe' => 'PrevNextCache', 'Exception' => 'DedupeException', + 'Pcp' => 'PCP', + 'Rule' => 'DedupeRule', 'RuleGroup' => 'DedupeRuleGroup', + 'SmsProvider' => 'Provider', ]; - $usableName = !empty($entitiesWithNamingIssues[$entityName]) ? $entitiesWithNamingIssues[$entityName] : $entityName; - $optionName = CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($usableName)); + $usableName = $entitiesWithNamingIssues[$entityName] ?? $entityName; + $optionName = CRM_Core_DAO_AllCoreTables::getTableForEntityName($usableName); if (!isset(CRM_Core_BAO_CustomQuery::$extendsMap[$entityName])) { $createdValue = $this->callAPISuccess('OptionValue', 'create', [ -- 2.25.1