CRM-17597 test to attempt to replicate bug (could not replicate in UI
authoreileen <emcnaughton@wikimedia.org>
Tue, 21 Jun 2016 02:21:06 +0000 (14:21 +1200)
committereileen <emcnaughton@wikimedia.org>
Sat, 25 Jun 2016 01:07:04 +0000 (13:07 +1200)
But in the test I found a related bug where 0 was not treated as a conflict.
I suspect this bug is broader & will dig more.

CRM/Dedupe/Merger.php
tests/phpunit/api/v3/JobTestCustomDataTest.php

index 5da3c9a4425fdd5f2a556e46cf2888ddc5b7e330..44a75533721fec7801f36df3a984eeb9f6673985 100644 (file)
@@ -863,7 +863,14 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
         // Rule: If both main-contact, and other-contact have a field with a
         // different value, then let $mode decide if to merge it or not
         if (
-          !empty($migrationInfo['rows'][$key]['main'])
+          (!empty($migrationInfo['rows'][$key]['main'])
+            // For custom fields a 0 (e.g in an int field) could be a true conflict. This
+            // is probably true for other fields too - e.g. 'do_not_email' but
+            // leaving that investigation as a @todo - until tests can be written.
+            // Note the handling of this has test coverage - although the data-typing
+            // of '0' feels flakey we have insurance.
+            || ($migrationInfo['rows'][$key]['main'] === '0' && substr($key, 0, 12) == 'move_custom_')
+          )
           && $migrationInfo['rows'][$key]['main'] != $migrationInfo['rows'][$key]['other']
         ) {
 
index 43001b7f5d65f5c6e7a616d1af28888ccf4357e4..794bf5f01c423c67d50f36d2ff402bbb73d8de8d 100644 (file)
@@ -181,6 +181,75 @@ class api_v3_JobTestCustomDataTest extends CiviUnitTestCase {
     $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
   }
 
+  /**
+   * Check we get a no conflict on the custom field & integer merges.
+   */
+  public function testBatchMergeIntCustomFieldNoConflict() {
+    $customGroup = $this->customGroupCreate();
+    $this->customGroupID = $customGroup['id'];
+    $customField = $this->customFieldCreate(array(
+      'custom_group_id' => $this->customGroupID,
+      'data_type' => 'Integer',
+      'html_type' => 'Text',
+      'default_value' => '',
+    ));
+    $this->customFieldID = $customField['id'];
+    $customFieldLabel = 'custom_' . $this->customFieldID;
+    $contactID = $this->individualCreate(array());
+    $this->individualCreate(array($customFieldLabel => 20));
+    $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
+    $this->assertEquals(1, count($result['values']['merged']));
+    $this->assertEquals(0, count($result['values']['skipped']));
+    $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+    $this->assertEquals(20, $contact[$customFieldLabel]);
+  }
+
+  /**
+   * Check we get a conflict on the integer custom field.
+   */
+  public function testBatchMergeIntCustomFieldConflict() {
+    $customGroup = $this->customGroupCreate();
+    $this->customGroupID = $customGroup['id'];
+    $customField = $this->customFieldCreate(array(
+      'custom_group_id' => $this->customGroupID,
+      'data_type' => 'Integer',
+      'html_type' => 'Text',
+      'default_value' => '',
+    ));
+    $this->customFieldID = $customField['id'];
+    $customFieldLabel = 'custom_' . $this->customFieldID;
+    $contactID = $this->individualCreate(array($customFieldLabel => 20));
+    $this->individualCreate(array($customFieldLabel => 1));
+    $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
+    $this->assertEquals(0, count($result['values']['merged']));
+    $this->assertEquals(1, count($result['values']['skipped']));
+    $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+    $this->assertEquals(20, $contact[$customFieldLabel]);
+  }
+
+  /**
+   * Check we get a conflict on the integer custom field when the conflicted field is 0.
+   */
+  public function testBatchMergeIntCustomFieldConflictZero() {
+    $customGroup = $this->customGroupCreate();
+    $this->customGroupID = $customGroup['id'];
+    $customField = $this->customFieldCreate(array(
+      'custom_group_id' => $this->customGroupID,
+      'data_type' => 'Integer',
+      'html_type' => 'Text',
+      'default_value' => '',
+    ));
+    $this->customFieldID = $customField['id'];
+    $customFieldLabel = 'custom_' . $this->customFieldID;
+    $contactID = $this->individualCreate(array($customFieldLabel => 0));
+    $this->individualCreate(array($customFieldLabel => 20));
+    $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
+    $this->assertEquals(0, count($result['values']['merged']));
+    $this->assertEquals(1, count($result['values']['skipped']));
+    $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+    $this->assertEquals(0, $contact[$customFieldLabel]);
+  }
+
   /**
    * Using the api with check perms set to off, make sure custom data is merged.git
    *