dev/core996 Ensure that the oldest created_date is retained no matter the ids of...
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 29 May 2019 01:37:14 +0000 (11:37 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Wed, 29 May 2019 08:35:13 +0000 (18:35 +1000)
Reduce sleep to 5

CRM/Dedupe/Merger.php
tests/phpunit/CRM/Dedupe/MergerTest.php

index 504b8211036d7592f28ad496d65b0cc561a7ed05..300626505c46d379f2f3aa35a8789bd7ee1c34d3 100644 (file)
@@ -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'))
     ) {
index c55f1883247d8a3e49c9270b085624401d9a2de3..a4c049927ae78ad1ecb29e99e6b4b1dcb89caaa4 100644 (file)
@@ -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 = [
       [