From: eileen Date: Thu, 6 Jun 2013 03:35:24 +0000 (+1200) Subject: CRM-12768 fix side-problem where handling of 'return' param inconsistent X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=3c70d50199a9edb718f83acf0deae68b469db1a7;p=civicrm-core.git CRM-12768 fix side-problem where handling of 'return' param inconsistent --- diff --git a/api/v3/utils.php b/api/v3/utils.php index da71cdfbb2..8a32d08189 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -459,6 +459,8 @@ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) { //if entity_id is set then treat it as ID (will be overridden by id if set) $dao->id = $params[$entity . "_id"]; } + + $options = _civicrm_api3_get_options_from_params($params); //apply options like sort _civicrm_api3_apply_options_to_dao($params, $dao, $entity); @@ -543,13 +545,14 @@ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) { } } } - if (!empty($params['return']) && is_array($params['return'])) { + if (!empty($options['return']) && is_array($options['return'])) { $dao->selectAdd(); + $options['return']['id'] = TRUE;// ensure 'id' is included $allfields = _civicrm_api3_get_unique_name_array($dao); - $returnMatched = array_intersect($params['return'], $allfields); + $returnMatched = array_intersect(array_keys($options['return']), $allfields); $returnUniqueMatched = array_intersect( array_diff(// not already matched on the field names - $params['return'], + $options['return'], $returnMatched), array_flip($allfields)// but a match for the field keys ); @@ -561,7 +564,6 @@ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) { $dao->selectAdd($allfields[$uniqueVal]); } - $dao->selectAdd('id'); } } diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index 9deb78b5b2..8c2a0dee6d 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -253,6 +253,55 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { } + /** + * Test custom field get works & return param works + */ + function testCustomFieldGetReturnOptions(){ + $customGroup = $this->customGroupCreate('Individual', 'test_group'); + $customField = $this->customFieldCreate($customGroup['id'], 'test_name'); + + $result = civicrm_api('custom_field', 'getsingle', array( + 'version' => 3, + 'id' => $customField['id'], + 'return' => 'data_type', + )); + $this->assertTrue(array_key_exists('data_type', $result)); + $this->assertFalse(array_key_exists('custom_group_id', $result)); + } + + /** + * Test custom field get works & return param works + */ + function testCustomFieldGetReturnArray(){ + $customGroup = $this->customGroupCreate('Individual', 'test_group'); + $customField = $this->customFieldCreate($customGroup['id'], 'test_name'); + + $result = civicrm_api('custom_field', 'getsingle', array( + 'version' => 3, + 'id' => $customField['id'], + 'return' => array('data_type'), + )); + $this->assertTrue(array_key_exists('data_type', $result)); + $this->assertFalse(array_key_exists('custom_group_id', $result)); + } + + /** + * Test custom field get works & return param works + */ + function testCustomFieldGetReturnTwoOptions(){ + $customGroup = $this->customGroupCreate('Individual', 'test_group'); + $customField = $this->customFieldCreate($customGroup['id'], 'test_name'); + + $result = civicrm_api('custom_field', 'getsingle', array( + 'version' => 3, + 'id' => $customField['id'], + 'return' => 'data_type, custom_group_id', + )); + $this->assertTrue(array_key_exists('data_type', $result)); + $this->assertTrue(array_key_exists('custom_group_id', $result)); + $this->assertFalse(array_key_exists('label', $result)); + } + function testCustomFieldCreateWithOptionValues() { $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);