Skip to content

Commit

Permalink
Merge pull request #14368 from seamuslee001/dev_core_996
Browse files Browse the repository at this point in the history
dev/core996 Ensure that the oldest created_date is retained no matter…
  • Loading branch information
eileenmcnaughton authored May 29, 2019
2 parents d53912b + 1f0138d commit 9bd1c7f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,22 @@ public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $che
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'))
) {
Expand Down
29 changes: 29 additions & 0 deletions tests/phpunit/CRM/Dedupe/MergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,34 @@ public function testCustomDataOverwrite() {
$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
Expand Down Expand Up @@ -894,6 +922,7 @@ public function setupMatchData() {
foreach ($fixtures as $fixture) {
$contactID = $this->individualCreate($fixture);
$this->contacts[] = array_merge($fixture, ['id' => $contactID]);
sleep(5);
}
$organizationFixtures = [
[
Expand Down

0 comments on commit 9bd1c7f

Please sign in to comment.