diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 2b1e74ad4c75..8d22e52f2198 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 625fded6cad8..89bb6be83fd6 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -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', @@ -353,6 +366,8 @@ public function testMoveField() { /** * Test get custom field id function. + * + * @throws \CiviCRM_API3_Exception */ public function testGetCustomFieldID() { $this->createCustomField(); @@ -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()); + } + } diff --git a/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php b/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php index ab266df34a44..ce496704d841 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']]); } diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 3b5d9a197fe1..7bcc1069fc70 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -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; }