Skip to content

Commit

Permalink
Fix obscure bug on updating custom fields (not necessarily hittable v…
Browse files Browse the repository at this point in the history
…ia UI)

This fixes a bug which I think only applies to programatically created custom fields -basically if the field has an empty option_group_id
and then you update it to have one it does a check to see if the existing option_group_id is in use and does not handle the
fact the field is empty - leading to a mysql error.
  • Loading branch information
eileenmcnaughton committed Jul 8, 2019
1 parent dba70c0 commit 808c81f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ public static function fixOptionGroups($customFieldId, $optionGroupId) {
);
// get the updated option group
// if both are same return
if ($currentOptionGroupId == $optionGroupId) {
if (!$currentOptionGroupId || $currentOptionGroupId == $optionGroupId) {
return;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ public function testCustomFieldExistingOptionGroup() {
$this->assertEquals($optionGroupID, 3);
}

/**
* Test adding an optionGroup to an existing field doesn't cause a fatal error.
*
* (this was happening due to a check running despite no existing option_group_id)
*/
public function testUpdateCustomFieldAddOptionGroup() {
$customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']);
$params = [
'custom_group_id' => $customGroup['id'],
'label' => 'Organization Gender',
'html_type' => 'Text',
'data_type' => 'Int',
];

$customField = $this->callAPISuccess('custom_field', 'create', $params);
$this->callAPISuccess('CustomField', 'create', [
'option_group_id' => 3,
'id' => $customField['id'],
'html_type' => 'Select',
]);
}

/**
* Test custom field get works & return param works
*/
Expand Down

0 comments on commit 808c81f

Please sign in to comment.