From 1f0138ddd854d4aeec89a0f75961ceba570ee520 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 29 May 2019 11:37:14 +1000 Subject: [PATCH] dev/core996 Ensure that the oldest created_date is retained no matter the ids of the contacts being merged Reduce sleep to 5 --- CRM/Dedupe/Merger.php | 16 ++++++++++++++ tests/phpunit/CRM/Dedupe/MergerTest.php | 29 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 504b821103..300626505c 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -1618,6 +1618,22 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m CRM_Core_BAO_CustomValueTable::setValues($viewOnlyCustomFields); } + // dev/core#996 Ensure that the earliest created date is stored against the kept contact id + $mainCreatedDate = civicrm_api3('Contact', 'getsingle', [ + 'id' => $mainId, + 'return' => ['created_date'], + ])['created_date']; + $otherCreatedDate = civicrm_api3('Contact', 'getsingle', [ + 'id' => $otherId, + 'return' => ['created_date'], + ])['created_date']; + if ($otherCreatedDate < $mainCreatedDate) { + CRM_Core_DAO::executeQuery("UPDATE civicrm_contact SET created_date = %1 WHERE id = %2", [ + 1 => [$otherCreatedDate, 'String'], + 2 => [$mainId, 'Positive'], + ]); + } + if (!$checkPermissions || (CRM_Core_Permission::check('merge duplicate contacts') && CRM_Core_Permission::check('delete contacts')) ) { diff --git a/tests/phpunit/CRM/Dedupe/MergerTest.php b/tests/phpunit/CRM/Dedupe/MergerTest.php index c55f188324..a4c049927a 100644 --- a/tests/phpunit/CRM/Dedupe/MergerTest.php +++ b/tests/phpunit/CRM/Dedupe/MergerTest.php @@ -674,6 +674,34 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase { $this->callAPISuccess('CustomGroup', 'delete', ['id' => $createGroup['id']]); } + /** + * Creatd Date merge cases + * @return array + */ + public function createdDateMergeCases() { + $cases = []; + // Normal pattern merge into the lower id + $cases[] = [0, 1]; + // Check if we flipped the contacts that it still does right thing + $cases[] = [1, 0]; + return $cases; + } + + /** + * dev/core#996 Ensure that the oldest created date is retained even if duplicates have been flipped + * @dataProvider createdDateMergeCases + */ + public function testCreatedDatePostMerge($keepContactKey, $duplicateContactKey) { + $this->setupMatchData(); + $lowerContactCreatedDate = $this->callAPISuccess('Contact', 'getsingle', [ + 'id' => $this->contacts[0]['id'], + 'return' => ['created_date'], + ])['created_date']; + // Assume contats have been flipped in the UL so merging into the higher id + $this->mergeContacts($this->contacts[$keepContactKey]['id'], $this->contacts[$duplicateContactKey]['id'], []); + $this->assertEquals($lowerContactCreatedDate, $this->callAPISuccess('Contact', 'getsingle', ['id' => $this->contacts[$keepContactKey]['id'], 'return' => ['created_date']])['created_date']); + } + /** * Verifies that when a contact with a custom field value is merged into a * contact without a record int its corresponding custom group table, and none @@ -894,6 +922,7 @@ class CRM_Dedupe_MergerTest extends CiviUnitTestCase { foreach ($fixtures as $fixture) { $contactID = $this->individualCreate($fixture); $this->contacts[] = array_merge($fixture, ['id' => $contactID]); + sleep(5); } $organizationFixtures = [ [ -- 2.25.1