From a9fdce6ab252256dce229243b91427500e239309 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 31 May 2019 12:23:26 +1200 Subject: [PATCH] Standardise custom fields metadata --- CRM/Core/BAO/CustomField.php | 34 ++--- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 127 +++++++++++++++++- .../phpunit/CRM/Core/BAO/CustomQueryTest.php | 6 + 3 files changed, 141 insertions(+), 26 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 2b1e74ad4c7..8d22e52f219 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -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; @@ -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())) { @@ -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 && @@ -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; diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 83284700f5d..3e90b928819 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -399,14 +399,17 @@ protected function createCustomField($groupTitle = 'new custom group') { } /** - * Tet the getFieldsForImport function. + * 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') , + 'name' => $this->getCustomFieldName('country'), 'type' => 1, 'title' => 'Country', 'headerPattern' => '//', @@ -417,8 +420,27 @@ public function testGetFieldsForImport() { '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') => [ + $this->getCustomFieldName('file') => [ 'name' => $this->getCustomFieldName('file'), 'type' => 2, 'title' => 'Custom Field', @@ -430,6 +452,25 @@ public function testGetFieldsForImport() { 'data_type' => 'File', 'html_type' => 'File', 'is_search_range' => '0', + 'id' => '5', + '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_5', + 'where' => 'civicrm_value_custom_group_1.custom_field_5', + 'extends_table' => 'civicrm_contact', + 'search_table' => 'contact_a', ], $this->getCustomFieldName('text') => [ 'name' => $this->getCustomFieldName('text'), @@ -443,9 +484,28 @@ public function testGetFieldsForImport() { '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'), + 'name' => $this->getCustomFieldName('select_string'), 'type' => 2, 'title' => 'Pick Color', 'headerPattern' => '//', @@ -456,6 +516,29 @@ public function testGetFieldsForImport() { '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'), @@ -471,6 +554,23 @@ public function testGetFieldsForImport() { '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_3', + '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'), @@ -484,6 +584,25 @@ public function testGetFieldsForImport() { '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()); diff --git a/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php b/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php index ab266df34a4..ce496704d84 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php @@ -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']]); }