Skip to content

Commit

Permalink
APIv4 - Fix output of CustomValue create/save/update
Browse files Browse the repository at this point in the history
Before: output contained no useful data
After: output contains values and id
  • Loading branch information
colemanw committed Aug 19, 2020
1 parent b4559ab commit 365f37f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
8 changes: 4 additions & 4 deletions CRM/Core/BAO/CustomValueTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ public static function setValues(&$params) {
}

if (!isset($params['entityID']) || !CRM_Utils_Type::validate($params['entityID'], 'Integer', FALSE)) {
return CRM_Core_Error::createAPIError(ts('entity_id needs to be set and of type Integer'));
throw new Exception(ts('entity_id needs to be set and of type Integer'));
}

// first collect all the id/value pairs. The format is:
Expand All @@ -550,7 +550,7 @@ public static function setValues(&$params) {
if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($n, TRUE)) {
$fieldID = (int ) $customFieldInfo[0];
if (CRM_Utils_Type::escape($fieldID, 'Integer', FALSE) === NULL) {
return CRM_Core_Error::createAPIError(ts('field ID needs to be of type Integer for index %1',
throw new Exception(ts('field ID needs to be of type Integer for index %1',
[1 => $fieldID]
));
}
Expand Down Expand Up @@ -620,7 +620,7 @@ public static function setValues(&$params) {
}
// Ensure that value is of the right data type
elseif (CRM_Utils_Type::escape($fieldValue['value'], $dataType, FALSE) === NULL) {
return CRM_Core_Error::createAPIError(ts('value: %1 is not of the right field data type: %2',
throw new Exception(ts('value: %1 is not of the right field data type: %2',
[
1 => $fieldValue['value'],
2 => $dao->data_type,
Expand Down Expand Up @@ -668,7 +668,7 @@ public static function setValues(&$params) {
return ['is_error' => 0, 'result' => 1];
}

return CRM_Core_Error::createAPIError(ts('Unknown error'));
throw new Exception(ts('Unknown error'));
}

/**
Expand Down
14 changes: 10 additions & 4 deletions Civi/Api4/Generic/Traits/CustomValueActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public function getEntityName() {
* @inheritDoc
*/
protected function writeObjects($items) {
$result = [];
$fields = $this->entityFields();
foreach ($items as $item) {
foreach ($items as $idx => $item) {
FormattingUtil::formatWriteParams($item, $fields);

// Convert field names to custom_xx format
Expand All @@ -65,9 +64,16 @@ protected function writeObjects($items) {
}
}

$result[] = \CRM_Core_BAO_CustomValueTable::setValues($item);
\CRM_Core_BAO_CustomValueTable::setValues($item);

// Darn setValues function doesn't return an id.
if (empty($item['id'])) {
$tableName = CoreUtil::getTableName($this->getEntityName());
$items[$idx]['id'] = (int) \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM ' . $tableName);
}
}
return $result;
FormattingUtil::formatOutputValues($items, $this->entityFields(), $this->getEntityName(), 'create');
return $items;
}

/**
Expand Down
35 changes: 20 additions & 15 deletions tests/phpunit/api/v4/Action/CustomValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,40 +135,45 @@ public function testCRUD() {

// CASE 1: Test CustomValue::create
// Create two records for a single contact and using CustomValue::get ensure that two records are created
CustomValue::create($group)
->addValue($colorField, 'g')
->addValue("entity_id", $this->contactID)
->execute();
CustomValue::create($group)
->addValue($colorField, 'r')
->addValue("entity_id", $this->contactID)
->execute();
$created = [
CustomValue::create($group)
->addValue($colorField, 'g')
->addValue("entity_id", $this->contactID)
->execute()->first(),
CustomValue::create($group)
->addValue($colorField . ':label', 'Red')
->addValue("entity_id", $this->contactID)
->execute()->first(),
];
// fetch custom values using API4 CustomValue::get
$result = CustomValue::get($group)
->addSelect('id', 'entity_id', $colorField, $colorField . ':label')
->addOrderBy($colorField, 'DESC')
->addOrderBy($colorField, 'ASC')
->execute();

// check if two custom values are created
$this->assertEquals(2, count($result));
$expectedResult = [
[
'id' => 2,
$colorField => 'r',
$colorField . ':label' => 'Red',
'entity_id' => $this->contactID,
],
[
'id' => 1,
$colorField => 'g',
$colorField . ':label' => 'Green',
'entity_id' => $this->contactID,
],
[
'id' => 2,
$colorField => 'r',
$colorField . ':label' => 'Red',
'entity_id' => $this->contactID,
],
];
// match the data
foreach ($expectedResult as $key => $field) {
foreach ($field as $attr => $value) {
$this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
if (!strpos($attr, ':')) {
$this->assertEquals($expectedResult[$key][$attr], $created[$key][$attr]);
}
}
}

Expand Down

0 comments on commit 365f37f

Please sign in to comment.