Extend tests & simpler retrieval to relationship_type_id
authoreileen <emcnaughton@wikimedia.org>
Sat, 24 Aug 2019 00:01:58 +0000 (12:01 +1200)
committereileen <emcnaughton@wikimedia.org>
Sat, 24 Aug 2019 00:50:01 +0000 (12:50 +1200)
CRM/Contact/Import/Form/MapField.php
CRM/Import/ImportProcessor.php
tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php

index f439890c74aafe20da0bdc513ae5c3296adbd8c6..7cab30413d6137c29167c0530838d5c3ddffd878 100644 (file)
@@ -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;
             }
           }
index d7835714cd1f9789f7bfa27aefa79c92811d8bd9..534226a162ba780e43fccc5852d20e4f1134787b 100644 (file)
@@ -104,7 +104,7 @@ class CRM_Import_ImportProcessor {
    * @return string
    */
   public function getContactSubType(): string {
-    return $this->contactSubType;
+    return $this->contactSubType ?? '';
   }
 
   /**
index d66191eff4c1b34640feaf05be929e6e8d602ada..d65cf61e915f38855408c0a26ddfcffdf76a9db6 100644 (file)
@@ -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]];
   }