Text user supplied dedupe rule is used during import.
authorDarrick Servis <darrick@davismedia.org>
Sat, 14 May 2022 22:07:35 +0000 (15:07 -0700)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 18 May 2022 20:17:43 +0000 (08:17 +1200)
CRM/Contact/Import/Parser/Contact.php
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php

index 5b72b261cbcc451f2ca2c7c6e983052129d9ee9f..e57df876cc9a632f471410480ef5058dec3f3f59 100644 (file)
@@ -1925,6 +1925,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     }
     $checkParams = ['check_permissions' => FALSE, 'match' => $params];
     $checkParams['match']['contact_type'] = $this->_contactType;
+    $checkParams['dedupe_rule_id'] = $this->_dedupeRuleGroupID ?? NULL;
 
     $possibleMatches = civicrm_api3('Contact', 'duplicatecheck', $checkParams);
     if (!$extIDMatch) {
index 4a4ddfbe1258b4d59df3e5ba4ef3ce7a93b903da..028f3d759822aa6742e7be1e7d4da1f55fb524c6 100644 (file)
@@ -120,6 +120,112 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccessGetSingle('Contact', $originalValues);
   }
 
+  /**
+   * Test import parser will update based on a custom rule match.
+   *
+   * In this case the contact has no external identifier.
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testImportParserWithUpdateWithCustomRule(): void {
+    $this->createCustomGroupWithFieldsOfAllTypes();
+
+    $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
+      'contact_type' => 'Individual',
+      'threshold' => 10,
+      'used' => 'General',
+      'name' => 'TestRule',
+      'title' => 'TestRule',
+      'is_reserved' => 0,
+    ]);
+    $this->callAPISuccess('Rule', 'create', [
+      'dedupe_rule_group_id' => $ruleGroup['id'],
+      'rule_table' => $this->getCustomGroupTable(),
+      'rule_weight' => 10,
+      'rule_field' => $this->getCustomFieldColumnName('text'),
+    ]);
+
+    $extra = [
+      $this->getCustomFieldName('select_string') => 'Yellow',
+      $this->getCustomFieldName('text') => 'Duplicate',
+    ];
+
+    [$originalValues, $result] = $this->setUpBaseContact($extra);
+
+    $contactValues = [
+      'first_name' => 'Tim',
+      'last_name' => 'Cook',
+      'email' => 'tim.cook@apple.com',
+      'nick_name' => 'Steve',
+      $this->getCustomFieldName('select_string') => 'Red',
+      $this->getCustomFieldName('text') => 'Duplicate',
+    ];
+
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [], NULL, $ruleGroup['id']);
+    $contactValues['id'] = $result['id'];
+    $this->assertEquals('R', $this->callAPISuccessGetValue('Contact', ['id' => $result['id'], 'return' => $this->getCustomFieldName('select_string')]));
+    $this->callAPISuccessGetSingle('Contact', $contactValues);
+
+    $foundDupes = CRM_Dedupe_Finder::dupes($ruleGroup['id']);
+    $this->assertCount(0, $foundDupes);
+  }
+
+  /**
+   * Test import parser will update based on a custom rule match.
+   *
+   * In this case the contact has no external identifier.
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testImportParserWithUpdateWithCustomRuleNoExternalIDMatch(): void {
+    $this->createCustomGroupWithFieldsOfAllTypes();
+
+    $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
+      'contact_type' => 'Individual',
+      'threshold' => 10,
+      'used' => 'General',
+      'name' => 'TestRule',
+      'title' => 'TestRule',
+      'is_reserved' => 0,
+    ]);
+    $this->callAPISuccess('Rule', 'create', [
+      'dedupe_rule_group_id' => $ruleGroup['id'],
+      'rule_table' => $this->getCustomGroupTable(),
+      'rule_weight' => 10,
+      'rule_field' => $this->getCustomFieldColumnName('text'),
+    ]);
+
+    $extra = [
+      $this->getCustomFieldName('select_string') => 'Yellow',
+      $this->getCustomFieldName('text') => 'Duplicate',
+      'external_identifier' => 'ext-2',
+    ];
+
+    [$originalValues, $result] = $this->setUpBaseContact($extra);
+
+    $contactValues = [
+      'first_name' => 'Tim',
+      'last_name' => 'Cook',
+      'email' => 'tim.cook@apple.com',
+      'nick_name' => 'Steve',
+      'external_identifier' => 'ext-1',
+      $this->getCustomFieldName('select_string') => 'Red',
+      $this->getCustomFieldName('text') => 'Duplicate',
+    ];
+
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [], NULL, $ruleGroup['id']);
+    $contactValues['id'] = $result['id'];
+    $this->assertEquals('R', $this->callAPISuccessGetValue('Contact', ['id' => $result['id'], 'return' => $this->getCustomFieldName('select_string')]));
+    $this->callAPISuccessGetSingle('Contact', $contactValues);
+
+    $foundDupes = CRM_Dedupe_Finder::dupes($ruleGroup['id']);
+    $this->assertCount(0, $foundDupes);
+  }
+
   /**
    * Test import parser will update contacts with an external identifier.
    *