From: Coleman Watts Date: Sat, 8 Jun 2013 03:02:50 +0000 (-0700) Subject: Fix custom option tests X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a4c5e9a3c74cb9405674dd1ba9a355abcc7ada96;p=civicrm-core.git Fix custom option tests --- diff --git a/api/v3/Generic.php b/api/v3/Generic.php index 6a70b28999..b624621030 100644 --- a/api/v3/Generic.php +++ b/api/v3/Generic.php @@ -108,7 +108,7 @@ function civicrm_api3_generic_getfields($apiRequest) { $helper($metadata); } - $fieldsToResolve = CRM_Utils_Array::value('get_options', $apiOptions, array()); + $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array()); foreach ($metadata as $fieldname => $fieldSpec) { _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest['entity'], $fieldname, $fieldSpec, $fieldsToResolve); @@ -241,7 +241,7 @@ function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldn $metadata[$fieldname]['api.aliases'][] = substr($fieldname, 0, -3); } - if (!in_array($fieldname, $fieldsToResolve)) { + if (!empty($metadata[$fieldname]['options']) || !in_array($fieldname, $fieldsToResolve)) { return; } diff --git a/api/v3/utils.php b/api/v3/utils.php index 6748651257..9b4d47bf72 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1322,10 +1322,12 @@ function _civicrm_api_get_custom_fields($entity, &$params) { } foreach ($customfields as $key => $value) { + // Regular fields have a 'name' property + $value['name'] = 'custom_' . $key; $customfields['custom_' . $key] = $value; - if(in_array('custom_' . $key, $getoptions)){ - $customfields['custom_' . $key]['options'] = CRM_Core_BAO_CustomOption::valuesByID($key); - } + if (in_array('custom_' . $key, $getoptions)) { + $customfields['custom_' . $key]['options'] = CRM_Core_BAO_CustomOption::valuesByID($key); + } unset($customfields[$key]); } return $customfields; @@ -1568,6 +1570,12 @@ function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldN return; } + // Translate value into key + $newValue = array_search($value, $options); + if ($newValue !== FALSE) { + $value = $newValue; + return; + } // Case-insensitive matching $newValue = strtolower($value); $options = array_map("strtolower", $options); @@ -1586,6 +1594,12 @@ function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldN * @return (string|bool) fieldName or FALSE if the field does not exist */ function _civicrm_api3_api_resolve_alias($entity, $fieldName) { + if (strpos($fieldName, 'custom') === 0 && is_numeric($fieldName[7])) { + return $fieldName; + } + if ($fieldName == "{$entity}_id") { + return 'id'; + } $result = civicrm_api($entity, 'getfields', array( 'version' => 3, 'action' => 'create', @@ -1594,9 +1608,6 @@ function _civicrm_api3_api_resolve_alias($entity, $fieldName) { if (isset($meta[$fieldName])) { return $meta[$fieldName]['name']; } - if ($fieldName == "{$entity}_id") { - return 'id'; - } foreach ($meta as $info) { if ($fieldName == CRM_Utils_Array::value('uniqueName', $info)) { return $info['name']; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index d6cc15a539..27529a161e 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -319,7 +319,6 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // FIXME: look at it closer in second stage // initialize the object once db is loaded - require_once 'CRM/Core/Config.php'; $config = CRM_Core_Config::singleton(); // when running unit tests, use mockup user framework diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index d1931a7dd1..4d46010dc1 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -341,7 +341,7 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { 'version' => 3, 'action' => 'create', ); - $description = "Demonstrate retrieving custom field options"; + $description = "Demonstrate retrieving metadata with custom field options"; $subfile = "GetFieldsOptions"; $fields = civicrm_api('contact', 'getfields', $getFieldsParams); $this->documentMe($getFieldsParams, $fields, __FUNCTION__, 'ContactTest.php', $description,$subfile,'GetFields');