From b1498fdac6ad37161424c6b48f3facf7936c8666 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 24 Aug 2019 12:01:58 +1200 Subject: [PATCH] Extend tests & simpler retrieval to relationship_type_id --- CRM/Contact/Import/Form/MapField.php | 29 +++++-------------- CRM/Import/ImportProcessor.php | 2 +- .../CRM/Contact/Import/Form/MapFieldTest.php | 16 +++++----- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index f439890c74..7cab30413d 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -209,11 +209,10 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { else { $savedMapping = $this->get('savedMapping'); - list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping, TRUE); + list($mappingName) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping, TRUE); //get loaded Mapping Fields $mappingName = CRM_Utils_Array::value(1, $mappingName); - $mappingRelation = CRM_Utils_Array::value(1, $mappingRelation); $this->assign('loadedMapping', $savedMapping); $this->set('loadedMapping', $savedMapping); @@ -400,12 +399,13 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $processor->setMappingID($savedMappingID); $processor->setFormName($formName); $processor->setMetadata($this->getContactImportMetadata()); + $processor->setContactTypeByConstant($this->get('contactType')); for ($i = 0; $i < $this->_columnCount; $i++) { $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, $mappingRelation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns); + list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns); } else { $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; @@ -847,7 +847,6 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * @param \CRM_Import_ImportProcessor $processor * @param $mappingName * @param int $i - * @param array $mappingRelation * @param array $defaults * @param string $js * @param bool $hasColumnNames @@ -857,26 +856,14 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * @return array * @throws \CiviCRM_API3_Exception */ - public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns) { + public function loadSavedMapping($processor, $mappingName, $i, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns) { $jsSet = FALSE; $formName = $processor->getFormName(); if (isset($mappingName[$i])) { if ($mappingName[$i] != ts('- do not import -')) { - if (isset($mappingRelation[$i])) { - // relationship mapping - switch ($this->get('contactType')) { - case CRM_Import_Parser::CONTACT_INDIVIDUAL: - $contactType = 'Individual'; - break; - - case CRM_Import_Parser::CONTACT_HOUSEHOLD: - $contactType = 'Household'; - break; - - case CRM_Import_Parser::CONTACT_ORGANIZATION: - $contactType = 'Organization'; - } + if ($processor->getRelationshipKey($i)) { + $contactType = $processor->getContactType(); //CRM-5125 $contactSubType = NULL; if ($this->get('contactSubType')) { @@ -888,8 +875,8 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { ); foreach ($relations as $key => $var) { - if ($key == $mappingRelation[$i]) { - $relation = $key; + if ($processor->getValidRelationshipKey($i)) { + $relation = $processor->getValidRelationshipKey($i); break; } } diff --git a/CRM/Import/ImportProcessor.php b/CRM/Import/ImportProcessor.php index d7835714cd..534226a162 100644 --- a/CRM/Import/ImportProcessor.php +++ b/CRM/Import/ImportProcessor.php @@ -104,7 +104,7 @@ class CRM_Import_ImportProcessor { * @return string */ public function getContactSubType(): string { - return $this->contactSubType; + return $this->contactSubType ?? ''; } /** diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php index d66191eff4..d65cf61e91 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php @@ -301,6 +301,12 @@ document.forms.MapField['mapper[1][3]'].style.display = 'none';\n", ['name' => 'Website', 'contact_type' => 'Individual', 'column_number' => 0, 'website_type_id'], "document.forms.MapField['mapper[0][1]'].style.display = 'none'; document.forms.MapField['mapper[0][2]'].style.display = 'none'; +document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", + ], + [ + // Yes, the relationship mapping really does use url whereas non relationship uses website because... legacy + ['name' => 'Url', 'contact_type' => 'Individual', 'column_number' => 0, 'website_type_id', 'relationship_type_id' => 1, 'relationship_direction' => 'a_b'], + "document.forms.MapField['mapper[0][2]'].style.display = 'none'; document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", ], ]; @@ -321,15 +327,10 @@ document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", * @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); + list($mappingName) = 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 = []; $js = ''; @@ -341,8 +342,9 @@ document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", $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, $mappingRelation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns); + $return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns); return ['defaults' => $return[0], 'js' => $return[1]]; } -- 2.25.1