From 52356383a4f4d3526951551c6a3d5b866422d3f7 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 22 Jun 2017 20:35:02 +1200 Subject: [PATCH] Extract submit in mapFields to start adding unit testin (CRM-20759) --- CRM/Contact/Import/Form/MapField.php | 93 +++++++++++-------- .../CRM/Contact/Import/Form/MapFieldTest.php | 55 +++++++++++ 2 files changed, 108 insertions(+), 40 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 87827426fa..9ba6188f00 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -665,11 +665,59 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $this->controller->resetPage($this->_name); return; } - - $mapper = array(); - $mapperKeys = array(); $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); - $mapperKeysMain = array(); + + $parser = $this->submit($params, $mapperKeys); + + // add all the necessary variables to the form + $parser->set($this); + } + + /** + * Format custom field name. + * + * Combine group and field name to avoid conflict. + * + * @param array $fields + * + * @return array + */ + public function formatCustomFieldName(&$fields) { + //CRM-2676, replacing the conflict for same custom field name from different custom group. + $fieldIds = $formattedFieldNames = array(); + foreach ($fields as $key => $value) { + if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { + $fieldIds[] = $customFieldId; + } + } + + if (!empty($fieldIds) && is_array($fieldIds)) { + $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldIds); + + if (!empty($groupTitles)) { + foreach ($groupTitles as $fId => $values) { + $key = "custom_{$fId}"; + $groupTitle = $values['groupTitle']; + $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle; + } + } + } + + return $formattedFieldNames; + } + + /** + * Main submit function. + * + * Extracted to add testing & start refactoring. + * + * @param $params + * @param $mapperKeys + * + * @return \CRM_Contact_Import_Parser_Contact + */ + public function submit($params, $mapperKeys) { + $mapper = $mapperKeysMain = array(); $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); @@ -968,42 +1016,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $this->get('contactSubType'), $this->get('dedupe') ); - - // add all the necessary variables to the form - $parser->set($this); - } - - /** - * Format custom field name. - * - * Combine group and field name to avoid conflict. - * - * @param array $fields - * - * @return array - */ - public function formatCustomFieldName(&$fields) { - //CRM-2676, replacing the conflict for same custom field name from different custom group. - $fieldIds = $formattedFieldNames = array(); - foreach ($fields as $key => $value) { - if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { - $fieldIds[] = $customFieldId; - } - } - - if (!empty($fieldIds) && is_array($fieldIds)) { - $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldIds); - - if (!empty($groupTitles)) { - foreach ($groupTitles as $fId => $values) { - $key = "custom_{$fId}"; - $groupTitle = $values['groupTitle']; - $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle; - } - } - } - - return $formattedFieldNames; + return $parser; } } diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php new file mode 100644 index 0000000000..d0a438c8ad --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php @@ -0,0 +1,55 @@ +getFormObject('CRM_Contact_Import_Form_MapField'); + $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); + $form->set('fields', array()); + $form->_columnNames = array(); + $form->_importTableName = 'civicrm_cache'; + $form->submit(array(), array()); + } + +} -- 2.25.1