Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core996 Ensure that the oldest created_date is retained no matter… #14368

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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