Merge pull request #9124 from mlutfy/master-crm17991
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / MergerTest.php
index a48b0fda910d3da69439064bfa46b1c9ae48178c..97dd6d5a65c0a66b2b0db0c13a92506ecc6f8866 100644 (file)
@@ -22,10 +22,9 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
       'domain_id'  => 1,
       'is_active'  => 1,
       'visibility' => 'Public Pages',
-      'version'    => 3,
     );
-    // TODO: This is not an API test!!
-    $result = civicrm_api('group', 'create', $params);
+
+    $result = $this->callAPISuccess('group', 'create', $params);
     $this->_groupId = $result['id'];
 
     // contact data set
@@ -110,7 +109,7 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
         'group_id'   => $this->_groupId,
         'version'    => 3,
       );
-      $res = civicrm_api('group_contact', 'create', $grpParams);
+      $this->callAPISuccess('group_contact', 'create', $grpParams);
     }
   }
 
@@ -121,10 +120,7 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
     foreach ($this->_contactIds as $contactId) {
       $this->contactDelete($contactId);
     }
-
-    // delete dupe group
-    $params = array('id' => $this->_groupId, 'version' => 3);
-    civicrm_api('group', 'delete', $params);
+    $this->groupDelete($this->_groupId);
   }
 
   /**
@@ -167,7 +163,7 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
 
     // Retrieve pairs from prev next cache table
     $select = array('pn.is_selected' => 'is_selected');
-    $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
+    $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($dao->id, $this->_groupId);
     $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
 
     $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
@@ -230,7 +226,7 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
 
     // Retrieve pairs from prev next cache table
     $select = array('pn.is_selected' => 'is_selected');
-    $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
+    $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($dao->id, $this->_groupId);
     $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
 
     $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
@@ -288,17 +284,17 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
 
     $this->assertEquals(array(
       0 => array(
-        'srcID' => $this->contacts[0]['id'],
+        'srcID' => $this->contacts[1]['id'],
         'srcName' => 'Mr. Mickey Mouse II',
-        'dstID' => $this->contacts[1]['id'],
+        'dstID' => $this->contacts[0]['id'],
         'dstName' => 'Mr. Mickey Mouse II',
         'weight' => 20,
         'canMerge' => TRUE,
       ),
       1 => array(
-        'srcID' => $this->contacts[2]['id'],
+        'srcID' => $this->contacts[3]['id'],
         'srcName' => 'Mr. Minnie Mouse II',
-        'dstID' => $this->contacts[3]['id'],
+        'dstID' => $this->contacts[2]['id'],
         'dstName' => 'Mr. Minnie Mouse II',
         'weight' => 20,
         'canMerge' => TRUE,
@@ -306,6 +302,117 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
     ), $pairs);
   }
 
+  /**
+   * Test function that gets organization pairs.
+   *
+   * Note the rule will match on organization_name OR email - hence lots of matches.
+   */
+  public function testGetOrganizationMatches() {
+    $this->setupMatchData();
+    $ruleGroups = $this->callAPISuccessGetSingle('RuleGroup', array('contact_type' => 'Organization', 'used' => 'Supervised'));
+
+    $pairs = CRM_Dedupe_Merger::getDuplicatePairs(
+      $ruleGroups['id'],
+      NULL,
+      TRUE,
+      25,
+      FALSE
+    );
+
+    $expectedPairs = array(
+      0 => array(
+        'srcID' => $this->contacts[5]['id'],
+        'srcName' => 'Walt Disney Ltd',
+        'dstID' => $this->contacts[4]['id'],
+        'dstName' => 'Walt Disney Ltd',
+        'weight' => 20,
+        'canMerge' => TRUE,
+      ),
+      1 => array(
+        'srcID' => $this->contacts[7]['id'],
+        'srcName' => 'Walt Disney',
+        'dstID' => $this->contacts[6]['id'],
+        'dstName' => 'Walt Disney',
+        'weight' => 10,
+        'canMerge' => TRUE,
+      ),
+      2 => array(
+        'srcID' => $this->contacts[6]['id'],
+        'srcName' => 'Walt Disney',
+        'dstID' => $this->contacts[4]['id'],
+        'dstName' => 'Walt Disney Ltd',
+        'weight' => 10,
+        'canMerge' => TRUE,
+      ),
+      3 => array(
+        'srcID' => $this->contacts[6]['id'],
+        'srcName' => 'Walt Disney',
+        'dstID' => $this->contacts[5]['id'],
+        'dstName' => 'Walt Disney Ltd',
+        'weight' => 10,
+        'canMerge' => TRUE,
+      ),
+    );
+    usort($pairs, array(__CLASS__, 'compareDupes'));
+    usort($expectedPairs, array(__CLASS__, 'compareDupes'));
+    $this->assertEquals($expectedPairs, $pairs);
+  }
+
+  /**
+   * Function to sort $duplicate records in a stable way.
+   *
+   * @param array $a
+   * @param array $b
+   * @return int
+   */
+  public static function compareDupes($a, $b) {
+    foreach (array('srcName', 'dstName', 'srcID', 'dstID') as $field) {
+      if ($a[$field] != $b[$field]) {
+        return ($a[$field] < $b[$field]) ? 1 : -1;
+      }
+    }
+    return 0;
+  }
+
+  /**
+   *  Test function that gets organization duplicate pairs.
+   */
+  public function testGetOrganizationMatchesInGroup() {
+    $this->setupMatchData();
+    $ruleGroups = $this->callAPISuccessGetSingle('RuleGroup', array('contact_type' => 'Organization', 'used' => 'Supervised'));
+
+    $groupID = $this->groupCreate(array('title' => 'she-mice'));
+
+    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $this->contacts[4]['id']));
+
+    $pairs = CRM_Dedupe_Merger::getDuplicatePairs(
+      $ruleGroups['id'],
+      $groupID,
+      TRUE,
+      25,
+      FALSE
+    );
+
+    $this->assertEquals(array(
+      0 => array(
+        'srcID' => $this->contacts[5]['id'],
+        'srcName' => 'Walt Disney Ltd',
+        'dstID' => $this->contacts[4]['id'],
+        'dstName' => 'Walt Disney Ltd',
+        'weight' => 20,
+        'canMerge' => TRUE,
+      ),
+      1 => array(
+        'srcID' => $this->contacts[6]['id'],
+        'srcName' => 'Walt Disney',
+        'dstID' => $this->contacts[4]['id'],
+        'dstName' => 'Walt Disney Ltd',
+        'weight' => 10,
+        'canMerge' => TRUE,
+      ),
+    ), $pairs);
+  }
+
   /**
    * Test function that gets duplicate pairs.
    *
@@ -369,6 +476,28 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
       $contactID = $this->individualCreate($fixture);
       $this->contacts[] = array_merge($fixture, array('id' => $contactID));
     }
+    $organizationFixtures = array(
+      array(
+        'organization_name' => 'Walt Disney Ltd',
+        'email' => 'walt@disney.com',
+      ),
+      array(
+        'organization_name' => 'Walt Disney Ltd',
+        'email' => 'walt@disney.com',
+      ),
+      array(
+        'organization_name' => 'Walt Disney',
+        'email' => 'walt@disney.com',
+      ),
+      array(
+        'organization_name' => 'Walt Disney',
+        'email' => 'walter@disney.com',
+      ),
+    );
+    foreach ($organizationFixtures as $fixture) {
+      $contactID = $this->organizationCreate($fixture);
+      $this->contacts[] = array_merge($fixture, array('id' => $contactID));
+    }
   }