Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 - Fix output of CustomValue create/save/update #18195

Merged
merged 1 commit into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 CRM_Core_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 CRM_Core_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 CRM_Core_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 CRM_Core_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