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

dev/core#389 [preliminary cleanup+ test] Standardise metadat for custom field #14401

Merged
merged 2 commits into from
Jun 3, 2019
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
34 changes: 12 additions & 22 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,18 @@ public static function &getFields(

$fields = array();
while (($dao->fetch()) != NULL) {
$regexp = preg_replace('/[.,;:!?]/', '', NULL);
$fields[$dao->id]['id'] = $dao->id;
$fields[$dao->id]['label'] = $dao->label;
// This seems broken, but not in a new way.
$fields[$dao->id]['headerPattern'] = '/' . preg_quote($regexp, '/') . '/';
// To support the consolidation of various functions & their expectations.
$fields[$dao->id]['title'] = $dao->label;
$fields[$dao->id]['custom_field_id'] = $dao->id;
$fields[$dao->id]['groupTitle'] = $dao->title;
$fields[$dao->id]['data_type'] = $dao->data_type;
$fields[$dao->id]['name'] = 'custom_' . $dao->id;
$fields[$dao->id]['type'] = CRM_Utils_Array::value($dao->data_type, self::dataToType());
$fields[$dao->id]['html_type'] = $dao->html_type;
$fields[$dao->id]['default_value'] = $dao->default_value;
$fields[$dao->id]['text_length'] = $dao->text_length;
Expand All @@ -656,6 +664,7 @@ public static function &getFields(
$fields[$dao->id]['is_required'] = $dao->is_required;
$fields[$dao->id]['table_name'] = $dao->table_name;
$fields[$dao->id]['column_name'] = $dao->column_name;
$fields[$dao->id]['where'] = $dao->table_name . '.' . $dao->column_name;
// Probably we should use a different fn to get the extends tables but this is a refactor so not changing that now.
$fields[$dao->id]['extends_table'] = array_key_exists($dao->extends, CRM_Core_BAO_CustomQuery::$extendsMap) ? CRM_Core_BAO_CustomQuery::$extendsMap[$dao->extends] : '';
if (in_array($dao->extends, CRM_Contact_BAO_ContactType::subTypes())) {
Expand Down Expand Up @@ -716,7 +725,7 @@ public static function getFieldsForImport(
$checkPermission
);

$importableFields = array();
$importableFields = [];
foreach ($fields as $id => $values) {
// for now we should not allow multiple fields in profile / export etc, hence unsetting
if (!$search &&
Expand All @@ -728,27 +737,8 @@ public static function getFieldsForImport(
/* generate the key for the fields array */

$key = "custom_$id";

$regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values));
$importableFields[$key] = array(
'name' => $key,
'type' => CRM_Utils_Array::value(CRM_Utils_Array::value('data_type', $values), self::dataToType()),
'title' => CRM_Utils_Array::value('label', $values),
'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
'import' => 1,
'custom_field_id' => $id,
'options_per_line' => CRM_Utils_Array::value('options_per_line', $values),
'text_length' => CRM_Utils_Array::value('text_length', $values, 255),
'data_type' => CRM_Utils_Array::value('data_type', $values),
'html_type' => CRM_Utils_Array::value('html_type', $values),
'is_search_range' => CRM_Utils_Array::value('is_search_range', $values),
);

// CRM-6681, pass date and time format when html_type = Select Date
if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') {
$importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values);
$importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values);
}
$importableFields[$key] = $values;
$importableFields[$key]['import'] = 1;
}

return $importableFields;
Expand Down
231 changes: 228 additions & 3 deletions tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

/**
* Class CRM_Core_BAO_CustomFieldTest
*
* @group headless
*/
class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {

use CRMTraits_Custom_CustomDataTrait;

protected $customFieldID;

public function setUp() {
parent::setUp();
}

/**
* Clean up after test.
*
* @throws \Exception
*/
public function tearDown() {
$this->quickCleanup([], TRUE);
parent::tearDown();
}

public function testCreateCustomField() {
$customGroup = $this->createCustomField();
$customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
Expand Down Expand Up @@ -353,6 +366,8 @@ public function testMoveField() {

/**
* Test get custom field id function.
*
* @throws \CiviCRM_API3_Exception
*/
public function testGetCustomFieldID() {
$this->createCustomField();
Expand Down Expand Up @@ -382,17 +397,227 @@ public function testGetCustomFieldID() {
protected function createCustomField($groupTitle = 'new custom group') {
$customGroup = $this->customGroupCreate([
'extends' => 'Individual',
'title' => $groupTitle,
'title' => $groupTitle,
]);
$fields = array(
$fields = [
'label' => 'testFld',
'data_type' => 'String',
'html_type' => 'Text',
'custom_group_id' => $customGroup['id'],
);
];
$field = CRM_Core_BAO_CustomField::create($fields);
$this->customFieldID = $field->id;
return $customGroup;
}

/**
* Test the getFieldsForImport function.
*
* @throws \Exception
*/
public function testGetFieldsForImport() {
$this->entity = 'Contact';
$this->createCustomGroupWithFieldsOfAllTypes();
$customGroupID = $this->ids['CustomGroup']['Custom Group'];
$expected = [
$this->getCustomFieldName('country') => [
'name' => $this->getCustomFieldName('country'),
'type' => 1,
'title' => 'Country',
'headerPattern' => '//',
'import' => 1,
'custom_field_id' => $this->getCustomFieldID('country'),
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'Int',
'html_type' => 'Select Country',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('country'),
'label' => 'Country',
'groupTitle' => 'Custom Group',
'default_value' => NULL,
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => NULL,
'extends_entity_column_id' => NULL,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => NULL,
'date_format' => NULL,
'time_format' => NULL,
'is_required' => '0',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'country_' . $this->getCustomFieldID('country'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('file') => [
'name' => $this->getCustomFieldName('file'),
'type' => 2,
'title' => 'Custom Field',
'headerPattern' => '//',
'import' => 1,
'custom_field_id' => $this->getCustomFieldID('file'),
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'File',
'html_type' => 'File',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('file'),
'label' => 'Custom Field',
'groupTitle' => 'Custom Group',
'default_value' => NULL,
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => NULL,
'extends_entity_column_id' => NULL,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => NULL,
'date_format' => NULL,
'time_format' => NULL,
'is_required' => '0',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'custom_field_' . $this->getCustomFieldID('file'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.custom_field_' . $this->getCustomFieldID('file'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('text') => [
'name' => $this->getCustomFieldName('text'),
'type' => 2,
'title' => 'Enter text here',
'headerPattern' => '//',
'import' => 1,
'custom_field_id' => $this->getCustomFieldID('text'),
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'String',
'html_type' => 'Text',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('text'),
'label' => 'Enter text here',
'groupTitle' => 'Custom Group',
'default_value' => 'xyz',
'custom_group_id' => '1',
'extends' => 'Contact',
'extends_entity_column_value' => NULL,
'extends_entity_column_id' => NULL,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => NULL,
'date_format' => NULL,
'time_format' => NULL,
'is_required' => '1',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('select_string') => [
'name' => $this->getCustomFieldName('select_string'),
'type' => 2,
'title' => 'Pick Color',
'headerPattern' => '//',
'import' => 1,
'custom_field_id' => $this->getCustomFieldID('select_string'),
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'String',
'html_type' => 'Select',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('select_string'),
'label' => 'Pick Color',
'groupTitle' => 'Custom Group',
'default_value' => NULL,
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => NULL,
'extends_entity_column_id' => NULL,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']),
'date_format' => NULL,
'time_format' => NULL,
'is_required' => '1',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'pseudoconstant' => [
'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
],
],
$this->getCustomFieldName('select_date') => [
'name' => $this->getCustomFieldName('select_date'),
'type' => 4,
'title' => 'test_date',
'headerPattern' => '//',
'import' => 1,
'custom_field_id' => $this->getCustomFieldID('select_date'),
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'Date',
'html_type' => 'Select Date',
'is_search_range' => '0',
'date_format' => 'mm/dd/yy',
'time_format' => '1',
'id' => $this->getCustomFieldID('select_date'),
'label' => 'test_date',
'groupTitle' => 'Custom Group',
'default_value' => '20090711',
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => NULL,
'extends_entity_column_id' => NULL,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => NULL,
'is_required' => '0',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'test_date_' . $this->getCustomFieldID('select_date'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('link') => [
'name' => $this->getCustomFieldName('link'),
'type' => 2,
'title' => 'test_link',
'headerPattern' => '//',
'import' => 1,
'custom_field_id' => $this->getCustomFieldID('link'),
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'Link',
'html_type' => 'Link',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('link'),
'label' => 'test_link',
'groupTitle' => 'Custom Group',
'default_value' => 'http://civicrm.org',
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => NULL,
'extends_entity_column_id' => NULL,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => NULL,
'date_format' => NULL,
'time_format' => NULL,
'is_required' => '1',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'test_link_' . $this->getCustomFieldID('link'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
];
$this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport());
}

}
6 changes: 6 additions & 0 deletions tests/phpunit/CRM/Core/BAO/CustomQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public function testSearchCustomDataDateRelative() {
'is_required' => '0',
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'headerPattern' => '//',
'title' => 'date field',
'custom_field_id' => $dateCustomField['id'],
'name' => 'custom_' . $dateCustomField['id'],
'type' => 4,
'where' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'] . '.date_field_' . $dateCustomField['id'],
], $queryObj->getFields()[$dateCustomField['id']]);

}
Expand Down
18 changes: 17 additions & 1 deletion tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,23 @@ public function createCustomFieldsOfAllTypes() {
* @return string
*/
protected function getCustomFieldName($key) {
$linkField = 'custom_' . $this->ids['CustomField'][$key];
$linkField = 'custom_' . $this->getCustomFieldID($key);
return $linkField;
}

/**
* Get the custom field name for the relevant key.
*
* e.g returns 'custom_5' where 5 is the id of the field using the key.
*
* Generally keys map to data types.
*
* @param string $key
*
* @return string
*/
protected function getCustomFieldID($key) {
$linkField = $this->ids['CustomField'][$key];
return $linkField;
}

Expand Down