[Import] [Ref] [Contact] Extract row mapping and add test
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 3 May 2022 00:48:07 +0000 (12:48 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 3 May 2022 17:47:25 +0000 (05:47 +1200)
CRM/Contact/Import/Parser/Contact.php
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php

index cbdaedf826a627b21a6615502c0fa32a8b886426..cbc7b7173b879caffdaa1b3b07bf09ab18fb84b7 100644 (file)
@@ -337,7 +337,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
    *   CRM_Import_Parser::ERROR or CRM_Import_Parser::VALID
    */
   public function summary(&$values): int {
-    $this->setActiveFieldValues($values);
+    $params = $this->getMappedRow($values);
     $rowNumber = (int) ($values[count($values) - 1]);
     $errorMessage = NULL;
     $errorRequired = FALSE;
@@ -435,9 +435,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
       $this->_allExternalIdentifiers[$externalID] = $this->_lineCount;
     }
 
-    //Checking error in custom data
-    $params = &$this->getActiveFieldParams();
-    $params['contact_type'] = $this->_contactType;
     //date-format part ends
 
     $errorMessage = NULL;
@@ -3641,4 +3638,29 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     return $this->relationshipLabels[$id . $direction];
   }
 
+  /**
+   * Transform the input parameters into the
+   *
+   * @param array $values
+   *   Input parameters as they come in from the datasource
+   *   eg. ['Bob', 'Smith', 'bob@example.org', '123-456']
+   *
+   * @return array
+   *   Parameters mapped to CiviCRM fields based on the mapping
+   *   and specified contact type. eg.
+   *   [
+   *     'contact_type' => 'Individual',
+   *     'first_name' => 'Bob',
+   *     'last_name' => 'Smith',
+   *     'phone' => ['phone' => '123', 'location_type_id' => 1, 'phone_type_id' => 1],
+   *     '5_a_b' => ['contact_type' => 'Organization', 'url' => ['url' => 'https://example.org', 'website_type_id' => 1]]
+   *     'im' => ['im' => 'my-handle', 'location_type_id' => 1, 'provider_id' => 1],
+   */
+  public function getMappedRow(array $values): array {
+    $this->setActiveFieldValues($values);
+    $params = $this->getActiveFieldParams();
+    $params['contact_type'] = $this->_contactType;
+    return $params;
+  }
+
 }
index 8b6030c3b7dd4c39cf9abcbb3b05859749b4a5f9..c6229cd2ab2bdd7c917e34f683e400619f0e5630 100644 (file)
@@ -940,6 +940,93 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test mapping fields within the Parser class.
+   *
+   * @throws \API_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   */
+  public function testMapFields(): void {
+    $parser = new CRM_Contact_Import_Parser_Contact(
+      // Array of field names
+      ['first_name', 'phone', NULL, 'im', NULL],
+      // Array of location types, ie columns 2 & 4 have types.
+      [NULL, 1, NULL, 1, NULL],
+      // Array of phone types
+      [NULL, 1, NULL, NULL, NULL],
+      // Array of im provider types
+      [NULL, NULL, NULL, 1, NULL],
+      // Array of filled in relationship values.
+      [NULL, NULL, '5_a_b', NULL, '5_a_b'],
+      // Array of the contact type to map to - note this can be determined from ^^
+      [NULL, NULL, 'Organization', NULL, 'Organization'],
+      // Related contact field names
+      [NULL, NULL, 'url', NULL, 'phone'],
+      // Related contact location types
+      [NULL, NULL, NULL, NULL, 1],
+      // Related contact phone types
+      [NULL, NULL, NULL, NULL, 1],
+      // Related contact im provider types
+      [NULL, NULL, NULL, NULL, NULL],
+      // Website types
+      [NULL, NULL, NULL, NULL, NULL],
+      // Related contact website types
+      [NULL, NULL, 1, NULL, NULL]
+    );
+    $parser->setUserJobID($this->getUserJobID([
+      'mapper' => [
+        ['first_name'],
+        ['phone', 1, 1],
+        ['5_a_b', 'url', 1],
+        ['im', 1, 1],
+        ['5_a_b', 'phone', 1, 1],
+      ],
+    ]));
+    $parser->init();
+    $params = $parser->getMappedRow(
+      ['Bob', '123', 'https://example.org', 'my-handle', '456']
+    );
+    $this->assertEquals([
+      'first_name' => 'Bob',
+      'phone' => [
+        [
+          'phone' => '123',
+          'location_type_id' => 1,
+          'phone_type_id' => 1,
+        ],
+      ],
+      '5_a_b' => [
+        'contact_type' => 'Organization',
+        'url' =>
+          [
+
+            [
+              'url' => 'https://example.org',
+              'website_type_id' => 1,
+            ],
+          ],
+        'phone' =>
+          [
+            [
+              'phone' => '456',
+              'location_type_id' => 1,
+              'phone_type_id' => 1,
+            ],
+          ],
+      ],
+      'im' =>
+        [
+
+          [
+            'im' => 'my-handle',
+            'location_type_id' => 1,
+            'provider_id' => 1,
+          ],
+        ],
+      'contact_type' => 'Individual',
+    ], $params);
+  }
+
   /**
    * Set up the underlying contact.
    *