Merge pull request #23475 from eileenmcnaughton/imp_fold
authorcolemanw <coleman@civicrm.org>
Wed, 18 May 2022 00:37:02 +0000 (20:37 -0400)
committerGitHub <noreply@github.com>
Wed, 18 May 2022 00:37:02 +0000 (20:37 -0400)
[Import] Add tests for contact type & contact subtype handling

CRM/Import/Parser.php
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php

index 9e9465dfe1a910d1285fa8f200a6c8dbd0ca8ce5..d22959a4fcb7d7ffe981944b65ad63b32e29cda0 100644 (file)
@@ -1105,6 +1105,10 @@ abstract class CRM_Import_Parser {
    * @return array
    */
   protected function getIdsOfMatchingContacts(array $formatted):array {
+    if ($formatted['id'] ?? NULL) {
+      return [$formatted['id']];
+    }
+
     // the call to the deprecated function seems to add no value other that to do an additional
     // check for the contact_id & type.
     $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted);
index 5df22b64a3d474e41b5719580e70d89d2fea2365..462a32357ff2e20339e43581e920aac5dadc4e9a 100644 (file)
@@ -152,6 +152,42 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccessGetSingle('Contact', $originalValues);
   }
 
+  /**
+   * Test updating an existing contact with external_identifier match but subtype mismatch.
+   *
+   * @throws \Exception
+   */
+  public function testImportParserWithUpdateWithExternalIdentifierSubtypeMismatch(): void {
+    $contactID = $this->individualCreate(['external_identifier' => 'billy', 'first_name' => 'William', 'contact_sub_type' => 'Parent']);
+    $this->runImport([
+      'external_identifier' => 'billy',
+      'nick_name' => 'Old Bill',
+      'contact_sub_type' => 'Staff',
+    ], CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
+    $contact = $this->callAPISuccessGetSingle('Contact', ['id' => $contactID]);
+    $this->assertEquals('Old Bill', $contact['nick_name']);
+    $this->assertEquals('William', $contact['first_name']);
+    $this->assertEquals('billy', $contact['external_identifier']);
+    $this->assertEquals(['Staff'], $contact['contact_sub_type']);
+  }
+
+  /**
+   * Test updating an existing contact with external_identifier match but subtype mismatch.
+   *
+   * @throws \Exception
+   */
+  public function testImportParserWithUpdateWithExternalIdentifierTypeMismatch(): void {
+    $contactID = $this->organizationCreate(['external_identifier' => 'billy']);
+    $this->runImport([
+      'external_identifier' => 'billy',
+      'nick_name' => 'Old Bill',
+    ], CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::NO_MATCH);
+    $contact = $this->callAPISuccessGetSingle('Contact', ['id' => $contactID]);
+    $this->assertEquals('', $contact['nick_name']);
+    $this->assertEquals('billy', $contact['external_identifier']);
+    $this->assertEquals('Organization', $contact['contact_type']);
+  }
+
   /**
    * Test import parser will fallback to external identifier.
    *