From e2cecb9c930b82480e53b5e3839be8bf3fc0fc89 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 21 Jun 2016 14:21:06 +1200 Subject: [PATCH] CRM-17597 test to attempt to replicate bug (could not replicate in UI 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 | 9 ++- .../phpunit/api/v3/JobTestCustomDataTest.php | 69 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 5da3c9a442..44a7553372 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -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'] ) { diff --git a/tests/phpunit/api/v3/JobTestCustomDataTest.php b/tests/phpunit/api/v3/JobTestCustomDataTest.php index 43001b7f5d..794bf5f01c 100644 --- a/tests/phpunit/api/v3/JobTestCustomDataTest.php +++ b/tests/phpunit/api/v3/JobTestCustomDataTest.php @@ -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 * -- 2.25.1