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

Only set defaults when creating a custom field (not when editing one) #12240

Merged
merged 1 commit into from
Jun 1, 2018
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
16 changes: 9 additions & 7 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,15 @@ public static function create(&$params) {

$customField = new CRM_Core_DAO_CustomField();
$customField->copyValues($params);
$customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE);
$customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE);
$customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE);
$customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE);
//CRM-15792 - Custom field gets disabled if is_active not set
$customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
$customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
if ($op == 'create') {
$customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE);
$customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE);
$customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE);
$customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE);
//CRM-15792 - Custom field gets disabled if is_active not set
$customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
$customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
}
$customField->save();

// make sure all values are present in the object for further processing
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,31 @@ public function getCustomFieldKeys($getFieldsResult) {
return $r;
}

/**
* This test is designed to ensure that when a custom field is updated via the
* API, params that are not supplied do not revert to the defaults. This was
* happening with, for example, is_searchable
*/
public function testDisableSearchableContactReferenceField() {
$customGroup = $this->customGroupCreate(array(
'name' => 'testCustomGroup',
'title' => 'testCustomGroup',
'extends' => 'Individual',
));
$params = array(
'name' => 'testCustomField',
'label' => 'testCustomField',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'ContactReference',
'html_type' => 'Autocomplete-Select',
'is_searchable' => '1',
);
$result = $this->callAPISuccess('CustomField', 'create', $params);
$params = [
'id' => $result['id'],
'is_active' => 0,
];
$result = $this->callAPISuccess('CustomField', 'create', $params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelmcandrew looks like your not actually asserting anything other than the API works

What you probably want do to is add in $this->assertTrue($result['values'][$result['id']]['is_searchable'])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seamuslee001 - The test failed with an exception before the patch not applied. Just seeing it work was good enough for me. Though I take your point that I could be testing a bit more with the test.

}

}