CRM-19496 Fix removing and re-adding contacts from the groups tab on contact record
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 13 Oct 2016 20:08:02 +0000 (07:08 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 13 Oct 2016 20:08:02 +0000 (07:08 +1100)
api/v3/GroupContact.php
tests/phpunit/api/v3/GroupContactTest.php

index e296e18297ad20c8cf3c66468f35954fbdb0353d..52a45fbefc8a8e468aaa75299169c6023d4864b1 100644 (file)
@@ -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');
index 72b1876420fef0ee788d3b8409d45daab0e5c5d5..e05e7460f550900895e7737295a5c9a1e3d4f79e 100644 (file)
@@ -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.
    *