From 3c64f1f62ee451c88f3f7e83ddd00645b37c75f8 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 14 Oct 2016 07:08:02 +1100 Subject: [PATCH] CRM-19496 Fix removing and re-adding contacts from the groups tab on contact record --- api/v3/GroupContact.php | 18 ++++++++-- tests/phpunit/api/v3/GroupContactTest.php | 42 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/api/v3/GroupContact.php b/api/v3/GroupContact.php index e296e18297..52a45fbefc 100644 --- a/api/v3/GroupContact.php +++ b/api/v3/GroupContact.php @@ -146,8 +146,22 @@ function civicrm_api3_group_contact_create($params) { * @deprecated */ function civicrm_api3_group_contact_delete($params) { - $groupContact = civicrm_api3('GroupContact', 'get', $params); - if ($groupContact['count'] == 0) { + $checkParams = $params; + if (!empty($checkParams['status']) && in_array($checkParams['status'], array('Removed', 'Deleted'))) { + $checkParams['status'] = 'Added'; + } + elseif (!empty($checkParams['status']) && $checkParams['status'] == 'Added') { + $checkParams['status'] = 'Removed'; + } + elseif (!empty($checkParams['status'])) { + unset($checkParams['status']); + } + $groupContact = civicrm_api3('GroupContact', 'get', $checkParams); + if ($groupContact['count'] == 0 && !empty($params['skip_undelete'])) { + $checkParams['status'] = 'removed'; + } + $groupContact2 = civicrm_api3('GroupContact', 'get', $checkParams); + if ($groupContact['count'] == 0 && $groupContact2['count'] == 0) { throw new API_Exception('Cannot Delete GroupContact'); } $params['status'] = CRM_Utils_Array::value('status', $params, empty($params['skip_undelete']) ? 'Removed' : 'Deleted'); diff --git a/tests/phpunit/api/v3/GroupContactTest.php b/tests/phpunit/api/v3/GroupContactTest.php index 72b1876420..e05e7460f5 100644 --- a/tests/phpunit/api/v3/GroupContactTest.php +++ b/tests/phpunit/api/v3/GroupContactTest.php @@ -196,6 +196,48 @@ class api_v3_GroupContactTest extends CiviUnitTestCase { $this->assertArrayNotHasKey('id', $result); } + /** + * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works. + * + */ + public function testDeleteWithId() { + $groupContactParams = array( + 'contact_id' => $this->_contactId, + 'group_id' => $this->_groupId1, + ); + $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams); + $params = array( + 'id' => $groupContact['id'], + 'status' => 'Removed', + ); + $result = $this->callAPISuccess('group_contact', 'delete', $params); + $this->assertEquals($result['removed'], 1); + $this->assertEquals($result['total_count'], 1); + } + + /** + * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works. + * + */ + public function testDeleteAndReAddWithId() { + $groupContactParams = array( + 'contact_id' => $this->_contactId, + 'group_id' => $this->_groupId1, + ); + $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams); + $params = array( + 'id' => $groupContact['id'], + 'status' => 'Removed', + ); + $result = $this->callAPISuccess('group_contact', 'delete', $params); + $this->assertEquals($result['removed'], 1); + $this->assertEquals($result['total_count'], 1); + $params = array_merge($params, array('status' => 'Added')); + $result2 = $this->callAPISuccess('group_contact', 'delete', $params); + $this->assertEquals($result2['added'], 1); + $this->assertEquals($result2['total_count'], 1); + } + /** * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups. * -- 2.25.1