X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCRM%2FDedupe%2FMergerTest.php;h=97dd6d5a65c0a66b2b0db0c13a92506ecc6f8866;hb=53fff622a37bb4bccccdc839e0843dab36579125;hp=73b9bab41192e3f284885da7c4b55b9da26ae208;hpb=3f699c2d9969c2454a95e43b706c21c347e30330;p=civicrm-core.git diff --git a/tests/phpunit/CRM/Dedupe/MergerTest.php b/tests/phpunit/CRM/Dedupe/MergerTest.php index 73b9bab411..97dd6d5a65 100644 --- a/tests/phpunit/CRM/Dedupe/MergerTest.php +++ b/tests/phpunit/CRM/Dedupe/MergerTest.php @@ -163,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.'); @@ -226,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.'); @@ -284,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, @@ -302,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. * @@ -365,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)); + } }