From 3c70d50199a9edb718f83acf0deae68b469db1a7 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 6 Jun 2013 15:35:24 +1200 Subject: [PATCH] CRM-12768 fix side-problem where handling of 'return' param inconsistent --- api/v3/utils.php | 10 +++-- tests/phpunit/api/v3/CustomFieldTest.php | 49 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) 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); -- 2.25.1