From 12d73bba6ca7611b4f7f8688f0206ee20964fc20 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 27 Oct 2015 11:14:14 +1300 Subject: [PATCH] Add test for contact.merge action --- CRM/Dedupe/Merger.php | 6 +++++ api/v3/Contact.php | 38 +++++++++++++++++++++------- tests/phpunit/api/v3/ContactTest.php | 12 +++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 5ae6f01a05..305a7c8b91 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -43,6 +43,12 @@ class CRM_Dedupe_Merger { public static function relTables() { static $relTables; + // Setting these merely prevents enotices - but it may be more appropriate not to add the user table below + // if the url can't be retrieved. A more standardised way to retrieve them is. + // CRM_Core_Config::singleton()->userSystem->getUserRecordUrl() - however that function takes a contact_id & + // we may need a different function when it is not known. + $title = $userRecordUrl = ''; + $config = CRM_Core_Config::singleton(); if ($config->userSystem->is_drupal) { $userRecordUrl = CRM_Utils_System::url('user/%ufid'); diff --git a/api/v3/Contact.php b/api/v3/Contact.php index f425d781d0..c0b08bdde9 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -1010,23 +1010,43 @@ function _civicrm_api3_contact_deprecation() { * * @return array * API Result Array + * @throws CiviCRM_API3_Exception */ function civicrm_api3_contact_merge($params) { $mode = CRM_Utils_Array::value('mode', $params, 'safe'); $autoFlip = CRM_Utils_Array::value('auto_flip', $params, TRUE); - $dupePairs = array(array( - 'srcID' => CRM_Utils_Array::value('main_id', $params), + $dupePairs = array( + array( + 'srcID' => CRM_Utils_Array::value('main_id', $params), 'dstID' => CRM_Utils_Array::value('other_id', $params), - )); - $result = CRM_Dedupe_Merger::merge($dupePairs, array(), $mode, $autoFlip); + ), + ); - if ($result['is_error'] == 0) { - return civicrm_api3_create_success(); - } - else { - return civicrm_api3_create_error($result['messages']); + if (($result = CRM_Dedupe_Merger::merge($dupePairs, array(), $mode, $autoFlip)) != FALSE) { + return civicrm_api3_create_success($result, $params); } + throw new CiviCRM_API3_Exception('Merge failed'); +} + +/** + * Adjust metadata for contact_proximity api function. + * + * @param array $params + */ +function _civicrm_api3_contact_merge_spec(&$params) { + $params['main_id'] = array( + 'title' => 'ID of the contact to merge & remove', + 'description' => ts('Wow - these 2 params are the logical reverse of what I expect - but what to do?'), + 'api.required' => 1, + 'type' => CRM_Utils_Type::T_INT, + ); + $params['other_id'] = array( + 'title' => 'ID of the contact to keep', + 'description' => ts('Wow - these 2 params are the logical reverse of what I expect - but what to do?'), + 'api.required' => 1, + 'type' => CRM_Utils_Type::T_INT, + ); } /** diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 94465a73d0..5133c2efaa 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -2836,4 +2836,16 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->assertContains($household['id'], array_keys($result['values'])); } + /** + * Test merging 2 contacts. + */ + public function testMerge() { + $otherContact = $this->callAPISuccess('contact', 'create', $this->_params); + $mainContact = $this->callAPISuccess('contact', 'create', $this->_params); + $this->callAPISuccess('contact', 'merge', array('main_id' => $mainContact['id'], 'other_id' => $otherContact['id'])); + $contacts = $this->callAPISuccess('contact', 'get', $this->_params); + $this->assertEquals($otherContact['id'], $contacts['id']); + + } + } -- 2.25.1