From: Eileen McNaughton Date: Tue, 3 May 2022 00:48:07 +0000 (+1200) Subject: [Import] [Ref] [Contact] Extract row mapping and add test X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4b3ef37139a43ea150b8762710bfbd44f3b8d993;p=civicrm-core.git [Import] [Ref] [Contact] Extract row mapping and add test --- diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index cbdaedf826..cbc7b7173b 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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; + } + } diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 8b6030c3b7..c6229cd2ab 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -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. *