Merge pull request #16574 from civicrm/5.23
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / DedupeFinderTest.php
index 00ff33f9d53c159703788abcb1331a7b339dc058..0e5f599a35c31a3a924771629e0e172565bc516f 100644 (file)
@@ -22,6 +22,8 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
 
   /**
    * Clean up after the test.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function tearDown() {
 
@@ -60,6 +62,8 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
 
   /**
    * Test duplicate contact retrieval with 2 email fields.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testUnsupervisedWithTwoEmailFields() {
     $this->setupForGroupDedupe();
@@ -84,6 +88,8 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
    * Test that a rule set to is_reserved = 0 works.
    *
    * There is a different search used dependent on this variable.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testCustomRule() {
     $this->setupForGroupDedupe();
@@ -113,6 +119,8 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
 
   /**
    * Test a custom rule with a non-default field.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testCustomRuleWithAddress() {
     $this->setupForGroupDedupe();
@@ -140,6 +148,65 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
 
   }
 
+  /**
+   * Test rule from Richard
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testRuleThreeContactFieldsEqualWeightWIthThresholdtheTotalSumOfAllWeight() {
+    $this->setupForGroupDedupe();
+
+    $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
+      'contact_type' => 'Individual',
+      'threshold' => 30,
+      'used' => 'General',
+      'name' => 'TestRule',
+      'title' => 'TestRule',
+      'is_reserved' => 0,
+    ]);
+
+    foreach (['first_name', 'last_name', 'birth_date'] as $field) {
+      $rules[$field] = $this->callAPISuccess('Rule', 'create', [
+        'dedupe_rule_group_id' => $ruleGroup['id'],
+        'rule_table' => 'civicrm_contact',
+        'rule_weight' => 10,
+        'rule_field' => $field,
+      ]);
+    }
+    $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID);
+    $this->assertCount(1, $foundDupes);
+  }
+
+  /**
+   * Test a custom rule with a non-default field.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testInclusiveRule() {
+    $this->setupForGroupDedupe();
+
+    $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
+      'contact_type' => 'Individual',
+      'threshold' => 8,
+      'used' => 'General',
+      'name' => 'TestRule',
+      'title' => 'TestRule',
+      'is_reserved' => 0,
+    ]);
+    $rules = [];
+    foreach (['first_name', 'last_name'] as $field) {
+      $rules[$field] = $this->callAPISuccess('Rule', 'create', [
+        'dedupe_rule_group_id' => $ruleGroup['id'],
+        'rule_table' => 'civicrm_contact',
+        'rule_weight' => 4,
+        'rule_field' => $field,
+      ]);
+    }
+    $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID);
+    $this->assertCount(4, $foundDupes);
+    CRM_Dedupe_Finder::dupes($ruleGroup['id']);
+  }
+
   /**
    * Test the supervised dedupe rule against a group.
    *
@@ -160,6 +227,8 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
 
   /**
    * Test dupesByParams function.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testDupesByParams() {
     // make dupe checks based on based on following contact sets:
@@ -291,6 +360,8 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
 
   /**
    * Set up a group of dedupable contacts.
+   *
+   * @throws \CRM_Core_Exception
    */
   protected function setupForGroupDedupe() {
     $params = [
@@ -311,7 +382,7 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
         'email' => 'robin@example.com',
         'contact_type' => 'Individual',
         'birth_date' => '2016-01-01',
-        'api.Address.create' => ['location_type_id' => 'Billing', 'postal_code' => '99999'],
+        'api.Address.create' => ['street_address' => '123 Happy world', 'location_type_id' => 'Billing', 'postal_code' => '99999'],
       ],
       [
         'first_name' => 'robin',
@@ -319,7 +390,7 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
         'email' => 'hood@example.com',
         'contact_type' => 'Individual',
         'birth_date' => '2016-01-01',
-        'api.Address.create' => ['location_type_id' => 'Billing', 'postal_code' => '99999'],
+        'api.Address.create' => ['street_address' => '123 Happy World', 'location_type_id' => 'Billing', 'postal_code' => '99999'],
       ],
       [
         'first_name' => 'robin',