Add test for getting columns from header
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Form / MapFieldTest.php
index d66191eff4c1b34640feaf05be929e6e8d602ada..927833c5cb779af87133f8f69859d875774dab42 100644 (file)
@@ -166,17 +166,19 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase {
    *
    * @param array $fieldSpec
    * @param string $expectedJS
+   * @param array $expectedDefaults
    *
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function testLoadSavedMapping($fieldSpec, $expectedJS) {
+  public function testLoadSavedMapping($fieldSpec, $expectedJS, $expectedDefaults) {
     $this->setUpMapFieldForm();
 
     $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test']);
     $this->callAPISuccess('MappingField', 'create', array_merge(['mapping_id' => $mapping['id']], $fieldSpec));
     $result = $this->loadSavedMapping($this->form, $mapping['id'], $fieldSpec['column_number']);
     $this->assertEquals($expectedJS, $result['js']);
+    $this->assertEquals($expectedDefaults, $result['defaults']);
   }
 
   /**
@@ -288,21 +290,66 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase {
         "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';\n",
+        ['mapper[1]' => ['first_name', 0, NULL]],
       ],
       [
         ['name' => 'Phone', 'contact_type' => 'Individual', 'column_number' => 8, 'phone_type_id' => 1, 'location_type_id' => 2],
         "document.forms.MapField['mapper[8][3]'].style.display = 'none';\n",
+        ['mapper[8]' => ['phone', 2, 1]],
       ],
       [
-        ['name' => 'Phone', 'contact_type' => 'Individual', 'column_number' => 0, 'im_provider_id' => 1, 'location_type_id' => 2],
+        ['name' => 'IM Screen Name', 'contact_type' => 'Individual', 'column_number' => 0, 'im_provider_id' => 1, 'location_type_id' => 2],
         "document.forms.MapField['mapper[0][3]'].style.display = 'none';\n",
+        ['mapper[0]' => ['im', 2, 1]],
       ],
       [
-        ['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';
+        ['name' => 'Website', 'contact_type' => 'Individual', 'column_number' => 0, 'website_type_id' => 1],
+        "document.forms.MapField['mapper[0][2]'].style.display = 'none';
 document.forms.MapField['mapper[0][3]'].style.display = 'none';\n",
+        ['mapper[0]' => ['url', 1]],
       ],
+      [
+        // 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' => 1, 'relationship_type_id' => 1, 'relationship_direction' => 'a_b'],
+        "document.forms.MapField['mapper[0][3]'].style.display = 'none';\n",
+        ['mapper[0]' => ['1_a_b', 'url', 1]],
+      ],
+      [
+        ['name' => 'Phone', 'contact_type' => 'Individual', 'column_number' => 0, 'phone_type_id' => 1, 'relationship_type_id' => 1, 'relationship_direction' => 'b_a'],
+        '',
+        ['mapper[0]' => ['1_b_a', 'phone', 'Primary', 1]],
+      ],
+    ];
+  }
+
+  /**
+   * Test the MapField function getting defaults from column names.
+   *
+   * @dataProvider getHeaderMatchDataProvider
+   *
+   * @throws \CiviCRM_API3_Exception
+   * @throws \CRM_Core_Exception
+   */
+  public function testDefaultFromColumnNames($columnHeader, $mapsTo) {
+    $this->setUpMapFieldForm();
+    $this->assertEquals($mapsTo, $this->form->defaultFromColumnName($columnHeader, $this->getHeaderPatterns()));
+  }
+
+  /**
+   * Get data to use for default from column names.
+   *
+   * @return array
+   */
+  public function getHeaderMatchDataProvider() {
+    return [
+      ['Contact Id', 'id'],
+      ['Contact ID', 'id'],
+      ['contact id', 'id'],
+      ['contact_id', 'id'],
+      // Yes, really... id wins the day here.
+      ['external id', 'id'],
+      ['external ident', 'external_identifier'],
+      ['external idg', 'external_identifier'],
     ];
   }
 
@@ -321,15 +368,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 +383,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]];
   }