From ca38a45bfdc5ffe11b1cf9768f98debc40d02301 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 6 Feb 2018 18:36:02 +1300 Subject: [PATCH] CRM-21738 fix transfer of view_only custom data. View only custom data was not being transferred during the merge despite code in order to achieve that. The reason was the metadata was being used from the (hateful) getGroupTree function, but without passing $permissions = FALSE it was not returning viewOnly fields. Note that this metadata retrieval function is a bad place for filtering - they should be in separate functions. --- CRM/Dedupe/Merger.php | 21 ++++++++++++++++----- tests/phpunit/api/v3/JobTest.php | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 12c3903d8c..381488563b 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -1528,9 +1528,20 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m CRM_Core_OptionGroup::lookupValues($submitted, $names, TRUE); // fix custom fields so they're edible by createProfileContact() - static $treeCache = array(); + $treeCache = array(); if (!array_key_exists($migrationInfo['main_details']['contact_type'], $treeCache)) { - $treeCache[$migrationInfo['main_details']['contact_type']] = CRM_Core_BAO_CustomGroup::getTree($migrationInfo['main_details']['contact_type'], NULL, NULL, -1); + $treeCache[$migrationInfo['main_details']['contact_type']] = CRM_Core_BAO_CustomGroup::getTree( + $migrationInfo['main_details']['contact_type'], + NULL, + NULL, + -1, + array(), + NULL, + TRUE, + NULL, + FALSE, + FALSE + ); } $cFields = array(); @@ -1696,12 +1707,12 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m // move view only custom fields CRM-5362 $viewOnlyCustomFields = array(); foreach ($submitted as $key => $value) { - $fid = (int) substr($key, 7); - if (array_key_exists($fid, $cFields) && !empty($cFields[$fid]['attributes']['is_view'])) { + $fid = CRM_Core_BAO_CustomField::getKeyID($key); + if ($fid && array_key_exists($fid, $cFields) && !empty($cFields[$fid]['attributes']['is_view']) + ) { $viewOnlyCustomFields[$key] = $value; } } - // special case to set values for view only, CRM-5362 if (!empty($viewOnlyCustomFields)) { $viewOnlyCustomFields['entityID'] = $mainId; diff --git a/tests/phpunit/api/v3/JobTest.php b/tests/phpunit/api/v3/JobTest.php index ff5af1fd0c..17358a1cd3 100644 --- a/tests/phpunit/api/v3/JobTest.php +++ b/tests/phpunit/api/v3/JobTest.php @@ -882,6 +882,28 @@ class api_v3_JobTest extends CiviUnitTestCase { } + /** + * Test the batch merge copes with view only custom data field. + */ + public function testBatchMergeCustomDataViewOnlyField() { + CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'edit my contact'); + $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com']; + $this->individualCreate($mouseParams); + + $customGroup = $this->CustomGroupCreate(); + $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'], 'is_view' => 1)); + $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 'blah'])); + + $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 0, 'mode' => 'safe')); + $this->assertEquals(1, count($result['values']['merged'])); + $mouseParams['return'] = 'custom_' . $customField['id']; + $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams); + $this->assertEquals('blah', $mouse['custom_' . $customField['id']]); + + $this->customFieldDelete($customGroup['id']); + $this->customGroupDelete($customGroup['id']); + } + /** * Test the batch merge function actually works! * -- 2.25.1