From 1ab5cdf67461508bf8644ba369f04ea69b1746ea Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 16 Aug 2019 11:34:55 +1200 Subject: [PATCH] Initial test on loadSavedMapping function. To facilitate this I 1) made it public - in the end it will go anyway 2) removed a 'pass-through' variable from the return array - it is passed in & returned unchanged 3) made it cope without a notice when a class property is not defined - that will go too soon --- CRM/Contact/Import/Form/MapField.php | 8 +- .../CRM/Contact/Import/Form/MapFieldTest.php | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index b961eba5fc8d..83c54ff2baec 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -401,7 +401,7 @@ public function buildQuickForm() { $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL); if ($this->get('savedMapping')) { - list($mappingName, $defaults, $js) = $this->loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns); + list($defaults, $js) = $this->loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns); } else { $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; @@ -856,7 +856,7 @@ protected function saveMappingField($mapperKeys, array $saveMapping, string $cTy * * @return array */ - protected function loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns) { + public function loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns) { $jsSet = FALSE; if (isset($mappingName[$i])) { if ($mappingName[$i] != ts('- do not import -')) { @@ -935,7 +935,7 @@ protected function loadSavedMapping($mappingName, $i, $mappingRelation, $mapping $jsSet = TRUE; } else { - $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); + $mappingHeader = array_keys((array) $this->_mapperFields, $mappingName[$i]); $websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL; $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL; @@ -989,7 +989,7 @@ protected function loadSavedMapping($mappingName, $i, $mappingRelation, $mapping $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)]; } } - return [$mappingName, $defaults, $js]; + return [$defaults, $js]; } } diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php index 17f26c4f6317..2602ec97201f 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php @@ -149,4 +149,77 @@ public function getFormObject($class, $formValues = [], $pageName = '') { return $form; } + /** + * Test the function that loads saved field mappings. + * + * @dataProvider mapFieldDataProvider + * + * @param array $fieldSpec + * @param string $expectedJS + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testLoadSavedMapping($fieldSpec, $expectedJS) { + $form = $this->getFormObject('CRM_Contact_Import_Form_MapField'); + /* @var CRM_Contact_Import_Form_MapField $form */ + $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); + + $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test']); + $this->callAPISuccess('MappingField', 'create', array_merge(['mapping_id' => $mapping], $fieldSpec)); + $result = $this->loadSavedMapping($form, $mapping['id'], 1); + $this->assertEquals($expectedJS, $result['js']); + } + + /** + * Get data for map field tests. + */ + public function mapFieldDataProvider() { + return [ + [ + ['name' => 'First Name', 'contact_type' => 'Individual', 'column_number' => 1], + 'document.forms.MapField[\'mapper[1][1]\'].style.display = \'none\'; +document.forms.MapField[\'mapper[1][2]\'].style.display = \'none\'; +document.forms.MapField[\'mapper[1][3]\'].style.display = \'none\'; +', + ], + ]; + } + + /** + * Wrapper for loadSavedMapping. + * + * This signature of the function we are calling is funky as a new extraction & will be refined. + * + * @param \CRM_Contact_Import_Form_MapField $form + * + * @param int $mappingID + * @param int $columnNumber + * + * @return array + * + * @throws \CiviCRM_API3_Exception + */ + protected function loadSavedMapping($form, $mappingID, $columnNumber) { + list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping::getMappingFields($mappingID, TRUE); + + //get loaded Mapping Fields + $mappingName = CRM_Utils_Array::value(1, $mappingName); + $mappingLocation = CRM_Utils_Array::value(1, $mappingLocation); + $mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType); + $mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider); + $mappingRelation = CRM_Utils_Array::value(1, $mappingRelation); + $mappingWebsiteType = CRM_Utils_Array::value(1, $mappingWebsiteType); + $defaults = []; + $formName = 'document.forms.MapField'; + $js = ''; + $hasColumnNames = TRUE; + // @todo - can use metadata trait once https://github.com/civicrm/civicrm-core/pull/15018 is merged. + $dataPatterns = []; + $columnPatterns = []; + + $return = $form->loadSavedMapping($mappingName, $columnNumber, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns); + return ['defaults' => $return[0], 'js' => $return[1]]; + } + }