From 5d9b686f88fd2f3a2e9f531196277b807d26ac46 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 6 Sep 2019 11:36:53 +1200 Subject: [PATCH] Use metadata for dataPatterns This is a bit harder to add a test for than columnHeaders but the principle of the change is very much the same so I'm thinking it can get a free ride on that test --- CRM/Contact/Import/Form/MapField.php | 10 ++++------ CRM/Contact/Import/Parser.php | 1 - tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php | 5 +---- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index e674d4ea81..6d46ae4fde 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -239,7 +239,6 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $defaults = []; $mapperKeys = array_keys($this->_mapperFields); $hasColumnNames = !empty($this->_columnNames); - $dataPatterns = $this->get('dataPatterns'); $hasLocationTypes = $this->get('fieldTypes'); $this->_location_types = ['Primary' => ts('Primary')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); @@ -404,7 +403,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL); if ($this->get('savedMapping')) { - list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $defaults, $js, $hasColumnNames, $dataPatterns); + list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $defaults, $js, $hasColumnNames); } else { $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; @@ -426,7 +425,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { else { // Otherwise guess the default from the form of the data $defaults["mapper[$i]"] = [ - $this->defaultFromData($dataPatterns, $i), + $this->defaultFromData($this->getDataPatterns(), $i), // $defaultLocationType->id 0, ]; @@ -847,12 +846,11 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * @param array $defaults * @param string $js * @param bool $hasColumnNames - * @param array $dataPatterns * * @return array * @throws \CiviCRM_API3_Exception */ - public function loadSavedMapping($processor, $mappingName, $i, $defaults, $js, $hasColumnNames, $dataPatterns) { + public function loadSavedMapping($processor, $mappingName, $i, $defaults, $js, $hasColumnNames) { $formName = $processor->getFormName(); if (isset($mappingName[$i])) { if ($mappingName[$i] != ts('- do not import -')) { @@ -874,7 +872,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $defaults["mapper[$i]"] = [$this->defaultFromColumnName($this->_columnNames[$i])]; } else { - $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)]; + $defaults["mapper[$i]"] = [$this->defaultFromData($this->getDataPatterns(), $i)]; } } return [$defaults, $js]; diff --git a/CRM/Contact/Import/Parser.php b/CRM/Contact/Import/Parser.php index 3813d90c5e..067582ace3 100644 --- a/CRM/Contact/Import/Parser.php +++ b/CRM/Contact/Import/Parser.php @@ -617,7 +617,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { $store->set('fields', $this->getSelectValues()); $store->set('fieldTypes', $this->getSelectTypes()); - $store->set('dataPatterns', $this->getDataPatterns()); $store->set('columnCount', $this->_activeFieldCount); $store->set('totalRowCount', $this->_totalCount); diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php index 927833c5cb..a6fcc0219f 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php @@ -376,16 +376,13 @@ document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", $js = ''; $hasColumnNames = TRUE; - // @todo - can use metadata trait once https://github.com/civicrm/civicrm-core/pull/15018 is merged. - $dataPatterns = []; - $columnPatterns = []; $processor = new CRM_Import_ImportProcessor(); $processor->setMappingID($mappingID); $processor->setFormName('document.forms.MapField'); $processor->setMetadata($this->getContactImportMetadata()); $processor->setContactTypeByConstant(CRM_Import_Parser::CONTACT_INDIVIDUAL); - $return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns); + $return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $defaults, $js, $hasColumnNames); return ['defaults' => $return[0], 'js' => $return[1]]; } -- 2.25.1