From c08fae408d9aa8ba4d769bd299071c4296e5677c Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 13 May 2016 11:47:26 +1200 Subject: [PATCH] CRM-18546 Duplicate email addresses created when merging in 4.7 --- CRM/Dedupe/Merger.php | 17 +++++++++-------- tests/phpunit/api/v3/JobTest.php | 23 ++++++++++++++++++++--- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index e1b5e1c6d8..2ea57025bb 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -829,11 +829,7 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m */ public static function skipMerge($mainId, $otherId, &$migrationInfo, $mode = 'safe', &$conflicts = array()) { - $migrationData = array( - 'old_migration_info' => $migrationInfo, - 'mode' => $mode, - ); - + $originalMigrationInfo = $migrationInfo; foreach ($migrationInfo as $key => $val) { if ($val === "null") { // Rule: Never overwrite with an empty value (in any mode) @@ -890,12 +886,17 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m // there's a conflict (to handle "gotchas"). fields_in_conflict could be modified here // merge happens with new values filled in here. For a particular field / row not to be merged // field should be unset from fields_in_conflict. - $migrationData['fields_in_conflict'] = $conflicts; - $migrationData['merge_mode'] = $mode; + $migrationData = array( + 'old_migration_info' => $originalMigrationInfo, + 'mode' => $mode, + 'fields_in_conflict' => $conflicts, + 'merge_mode' => $mode, + 'migration_info' => $migrationInfo, + ); CRM_Utils_Hook::merge('batch', $migrationData, $mainId, $otherId); $conflicts = $migrationData['fields_in_conflict']; // allow hook to override / manipulate migrationInfo as well - $migrationInfo = $migrationData['old_migration_info']; + $migrationInfo = $migrationData['migration_info']; if (!empty($conflicts)) { foreach ($conflicts as $key => $val) { diff --git a/tests/phpunit/api/v3/JobTest.php b/tests/phpunit/api/v3/JobTest.php index 9cf8a0c16c..ddc56e97fd 100644 --- a/tests/phpunit/api/v3/JobTest.php +++ b/tests/phpunit/api/v3/JobTest.php @@ -330,10 +330,27 @@ class api_v3_JobTest extends CiviUnitTestCase { } /** - * Test that in aggressive mode our conflicted contacts are merged. + * Test the batch merge does not create duplicate emails. + * + * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails. */ - public function testBatchMergeAggressive() { - + public function testBatchMergeEmailHandling() { + for ($x = 0; $x <= 4; $x++) { + $id = $this->individualCreate(array('email' => 'batman@gotham.met')); + } + $result = $this->callAPISuccess('Job', 'process_batch_merge', array()); + $this->assertEquals(4, count($result['values']['merged'])); + $this->callAPISuccessGetCount('Contact', array('email' => 'batman@gotham.met'), 1); + $contacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 0)); + $deletedContacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 1)); + $this->callAPISuccessGetCount('Email', array( + 'email' => 'batman@gotham.met', + 'contact_id' => array('IN' => array_keys($contacts['values'])), + ), 1); + $this->callAPISuccessGetCount('Email', array( + 'email' => 'batman@gotham.met', + 'contact_id' => array('IN' => array_keys($deletedContacts['values'])), + ), 4); } /** -- 2.25.1