Skip to content

Commit

Permalink
APIv3 - Fix testCustomDataGet errors caused by legacy entity naming i…
Browse files Browse the repository at this point in the history
…ssues
  • Loading branch information
colemanw committed May 4, 2022
1 parent c100cf4 commit dab64f2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protected function assignTestValue($fieldName, &$fieldDef, $counter) {
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);
Expand Down
14 changes: 7 additions & 7 deletions CRM/Core/DAO/AllCoreTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,11 @@ public static function convertEntityNameToCamel(string $name, $legacyV3 = FALSE)
// 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);
Expand All @@ -234,9 +233,10 @@ public static function convertEntityNameToCamel(string $name, $legacyV3 = FALSE)
$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);
}
Expand Down
4 changes: 2 additions & 2 deletions api/v3/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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');
}
2 changes: 1 addition & 1 deletion api/v3/RuleGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
8 changes: 6 additions & 2 deletions api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions tests/phpunit/api/v3/SyntaxConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -877,15 +877,19 @@ public function testCustomDataGet($entityName) {
$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', [
Expand Down

0 comments on commit dab64f2

Please sign in to comment.