Skip to content

Commit

Permalink
Merge pull request #12225 from jitendrapurohit/core-149
Browse files Browse the repository at this point in the history
dev/core#149 - Fatal Error on customvalue get api
  • Loading branch information
colemanw authored Jun 1, 2018
2 parents 1efe6b6 + 0ec36ef commit 887ef31
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
31 changes: 18 additions & 13 deletions api/v3/CustomValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,27 @@ function civicrm_api3_custom_value_get($params) {
unset($params['entity_id'], $params['entity_table']);
foreach ($params as $id => $param) {
if ($param && substr($id, 0, 6) == 'return') {
$id = substr($id, 7);
list($c, $i) = CRM_Utils_System::explode('_', $id, 2);
if ($c == 'custom' && is_numeric($i)) {
$names['custom_' . $i] = 'custom_' . $i;
$id = $i;
$returnVal = $param;
if (!empty(substr($id, 7))) {
$returnVal = substr($id, 7);
}
else {
// Lookup names if ID was not supplied
list($group, $field) = CRM_Utils_System::explode(':', $id, 2);
$id = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
if (!$id) {
continue;
foreach ((array) $returnVal as $value) {
list($c, $i) = CRM_Utils_System::explode('_', $value, 2);
if ($c == 'custom' && is_numeric($i)) {
$names['custom_' . $i] = 'custom_' . $i;
$fldId = $i;
}
else {
// Lookup names if ID was not supplied
list($group, $field) = CRM_Utils_System::explode(':', $value, 2);
$fldId = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
if (!$fldId) {
continue;
}
$names['custom_' . $fldId] = 'custom_' . $i;
}
$names['custom_' . $id] = 'custom_' . $i;
$getParams['custom_' . $fldId] = 1;
}
$getParams['custom_' . $id] = 1;
}
}

Expand Down
36 changes: 35 additions & 1 deletion tests/phpunit/api/v3/CustomValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public function tearDown() {

public function testCreateCustomValue() {
$this->_populateOptionAndCustomGroup();
$this->_customField = $this->customFieldCreate(array('custom_group_id' => $this->ids['string']['custom_group_id']));
$this->_customFieldID = $this->_customField['id'];

$customFieldDataType = CRM_Core_BAO_CustomField::dataType();
$dataToHtmlTypes = CRM_Core_BAO_CustomField::dataToHtml();
Expand Down Expand Up @@ -227,9 +229,41 @@ public function _testCustomValue($customField, $sqlOps, $type) {
}
}

$params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue);
$params = [
'entity_id' => $contactId,
'custom_' . $customId => $selectedValue,
"custom_{$this->_customFieldID}" => "Test String Value for {$this->_customFieldID}",
];
$this->callAPISuccess('CustomValue', 'create', $params);

//Test for different return value syntax.
$returnValues = [
['return' => "custom_{$customId}"],
['return' => ["custom_{$customId}"]],
["return.custom_{$customId}" => 1],
['return' => ["custom_{$customId}", "custom_{$this->_customFieldID}"]],
["return.custom_{$customId}" => 1, "return.custom_{$this->_customFieldID}" => 1],
];
foreach ($returnValues as $key => $val) {
$params = array_merge($val, [
'entity_id' => $contactId,
]);
$customValue = $this->callAPISuccess('CustomValue', 'get', $params);
if (is_array($selectedValue)) {
$expected = array_values($selectedValue);
$this->checkArrayEquals($expected, $customValue['values'][$customId]['latest']);
}
elseif ($type == 'date') {
$this->assertEquals($selectedValue, date('Ymd', strtotime(str_replace('.', '/', $customValue['values'][$customId]['latest']))));
}
else {
$this->assertEquals($selectedValue, $customValue['values'][$customId]['latest']);
}
if ($key > 2) {
$this->assertEquals("Test String Value for {$this->_customFieldID}", $customValue['values'][$this->_customFieldID]['latest']);
}
}

foreach ($sqlOps as $op) {
$qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
switch ($op) {
Expand Down

0 comments on commit 887ef31

Please sign in to comment.