Add test for getting columns from header
authoreileen <emcnaughton@wikimedia.org>
Thu, 5 Sep 2019 03:21:42 +0000 (15:21 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 5 Sep 2019 21:56:42 +0000 (09:56 +1200)
CRM/Contact/Import/Form/MapField.php
tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php

index 69f111bbc267181d89cece750396a27762ec1e4d..82aa20167d8ebafb74985101921850d40ca42252 100644 (file)
@@ -70,7 +70,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
   public function defaultFromColumnName($columnName, $patterns) {
 
     if (!preg_match('/^[a-z0-9 ]$/i', $columnName)) {
-      if ($columnKey = array_search($columnName, $this->_mapperFields)) {
+      if ($columnKey = array_search($columnName, $this->getFieldTitles())) {
         $this->_fieldUsed[$columnKey] = TRUE;
         return $columnKey;
       }
index 50ebd39fcb30099c2cd2f22ea4815032500b22a3..927833c5cb779af87133f8f69859d875774dab42 100644 (file)
@@ -322,6 +322,37 @@ document.forms.MapField['mapper[0][3]'].style.display = 'none';\n",
     ];
   }
 
+  /**
+   * 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'],
+    ];
+  }
+
   /**
    * Wrapper for loadSavedMapping.
    *