Skip to content

Commit

Permalink
Merge pull request #12604 from tschuettler/296-custom-value-return-field
Browse files Browse the repository at this point in the history
api - CustomValue::get - add handling for comma separated return fields
  • Loading branch information
colemanw authored Jul 31, 2018
2 parents 315dbc2 + 55000cc commit 58603fb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
5 changes: 4 additions & 1 deletion api/v3/CustomValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ function civicrm_api3_custom_value_get($params) {
if (!empty(substr($id, 7))) {
$returnVal = substr($id, 7);
}
foreach ((array) $returnVal as $value) {
if (!is_array($returnVal)) {
$returnVal = explode(',', $returnVal);
}
foreach ($returnVal as $value) {
list($c, $i) = CRM_Utils_System::explode('_', $value, 2);
if ($c == 'custom' && is_numeric($i)) {
$names['custom_' . $i] = 'custom_' . $i;
Expand Down
68 changes: 68 additions & 0 deletions tests/phpunit/api/v3/CustomValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,72 @@ public function testUpdateCustomGreetings() {

}

/**
* Creates a multi-valued custom field set and creates a contact with mutliple values for it.
*
* @return array
*/
private function _testGetCustomValueMultiple() {
$fieldIDs = $this->CustomGroupMultipleCreateWithFields();
$customFieldValues = [];
foreach ($fieldIDs['custom_field_id'] as $id) {
$customFieldValues["custom_{$id}"] = "field_{$id}_value_1";
}
$this->assertNotEmpty($customFieldValues);
$contactParams = [
'first_name' => 'Jane',
'last_name' => 'Doe',
'contact_type' => 'Individual',
];
$contact = $this->callAPISuccess('Contact', 'create', array_merge($contactParams, $customFieldValues));
foreach ($fieldIDs['custom_field_id'] as $id) {
$customFieldValues["custom_{$id}"] = "field_{$id}_value_2";
}
$result = $this->callAPISuccess('Contact', 'create', array_merge(['id' => $contact['id']], $customFieldValues));
return [
$contact['id'],
$customFieldValues,
];
}

/**
* Test that specific custom values can be retrieved while using return with comma separated values as genererated by the api explorer.
* ['return' => 'custom_1,custom_2']
*/
public function testGetCustomValueReturnMultipleApiExplorer() {
list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
$result = $this->callAPISuccess('CustomValue', 'get', [
'return' => implode(',', array_keys($customFieldValues)),
'entity_id' => $cid,
]);
$this->assertEquals(count($customFieldValues), $result['count']);
}

/**
* Test that specific custom values can be retrieved while using return with array style syntax.
* ['return => ['custom_1', 'custom_2']]
*/
public function testGetCustomValueReturnMultipleArray() {
list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
$result = $this->callAPISuccess('CustomValue', 'get', [
'return' => array_keys($customFieldValues),
'entity_id' => $cid,
]);
$this->assertEquals(count($customFieldValues), $result['count']);
}

/**
* Test that specific custom values can be retrieved while using a list of return parameters.
* [['return.custom_1' => '1'], ['return.custom_2' => '1']]
*/
public function testGetCustomValueReturnMultipleList() {
list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
$returnArray = [];
foreach ($customFieldValues as $field => $value) {
$returnArray["return.{$field}"] = 1;
}
$result = $this->callAPISuccess('CustomValue', 'get', array_merge($returnArray, ['entity_id' => $cid]));
$this->assertEquals(count($customFieldValues), $result['count']);
}

}

0 comments on commit 58603fb

Please sign in to comment.