From 85df4a81515e20dc512ad50de5ee3b7a1a31decf Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 7 Feb 2017 10:23:07 +1100 Subject: [PATCH 1/4] CRM-19979 Add test to demonstrate breakage --- tests/phpunit/api/v3/GroupContactTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/phpunit/api/v3/GroupContactTest.php b/tests/phpunit/api/v3/GroupContactTest.php index 3283d88b357f..f32317bb7671 100644 --- a/tests/phpunit/api/v3/GroupContactTest.php +++ b/tests/phpunit/api/v3/GroupContactTest.php @@ -238,6 +238,29 @@ public function testDeleteAndReAddWithId() { $this->assertEquals($result2['total_count'], 1); } + /** + * CRM-19979 test that group cotnact delete action works when contact is in status of pendin. + */ + public function testDeleteWithPending() { + $groupId3 = $this->groupCreate(array( + 'name' => 'Test Group 3', + 'domain_id' => 1, + 'title' => 'New Test Group3 Created', + 'description' => 'New Test Group3 Created', + 'is_active' => 1, + 'visibility' => 'User and User Admin Only', + )); + $groupContactCreateParams = array( + 'contact_id' => $this->_contactId, + 'group_id' => $groupId3, + 'status' => 'Pending', + ); + $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams); + $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams); + $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'status' => 'Removed')); + } + + /** * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups. * From 518dd0e7eadeffca7c4e67115d45ea68be0f743d Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 7 Feb 2017 10:36:03 +1100 Subject: [PATCH 2/4] CRM-19979 Also check to see if there is a record in the pending status as well as removed and added --- api/v3/GroupContact.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/v3/GroupContact.php b/api/v3/GroupContact.php index 50378c1d96fe..79e9e351e17e 100644 --- a/api/v3/GroupContact.php +++ b/api/v3/GroupContact.php @@ -161,7 +161,8 @@ function civicrm_api3_group_contact_delete($params) { $checkParams['status'] = 'removed'; } $groupContact2 = civicrm_api3('GroupContact', 'get', $checkParams); - if ($groupContact['count'] == 0 && $groupContact2['count'] == 0) { + $groupContact3 = civicrm_api3('GroupContact', 'get', array_merge($checkParams, array('status' => 'Pending'))); + if ($groupContact['count'] == 0 && $groupContact2['count'] == 0 && $groupContact3['count'] == 0) { throw new API_Exception('Cannot Delete GroupContact'); } $params['status'] = CRM_Utils_Array::value('status', $params, empty($params['skip_undelete']) ? 'Removed' : 'Deleted'); From 4e5d12a13979fadc365f134d2e6ef68f59605090 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 7 Feb 2017 11:12:27 +1100 Subject: [PATCH 3/4] Reduce number of API queries and use IN on the status check as per comment from Eileen --- api/v3/GroupContact.php | 7 +++---- tests/phpunit/api/v3/GroupContactTest.php | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/api/v3/GroupContact.php b/api/v3/GroupContact.php index 79e9e351e17e..2528c827cbb4 100644 --- a/api/v3/GroupContact.php +++ b/api/v3/GroupContact.php @@ -148,10 +148,10 @@ function civicrm_api3_group_contact_create($params) { function civicrm_api3_group_contact_delete($params) { $checkParams = $params; if (!empty($checkParams['status']) && in_array($checkParams['status'], array('Removed', 'Deleted'))) { - $checkParams['status'] = 'Added'; + $checkParams['status'] = array('IN' => array('Added', 'Pending')); } elseif (!empty($checkParams['status']) && $checkParams['status'] == 'Added') { - $checkParams['status'] = 'Removed'; + $checkParams['status'] = array('IN' => array('Pending', 'Removed')); } elseif (!empty($checkParams['status'])) { unset($checkParams['status']); @@ -161,8 +161,7 @@ function civicrm_api3_group_contact_delete($params) { $checkParams['status'] = 'removed'; } $groupContact2 = civicrm_api3('GroupContact', 'get', $checkParams); - $groupContact3 = civicrm_api3('GroupContact', 'get', array_merge($checkParams, array('status' => 'Pending'))); - if ($groupContact['count'] == 0 && $groupContact2['count'] == 0 && $groupContact3['count'] == 0) { + 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 f32317bb7671..c2c686871c90 100644 --- a/tests/phpunit/api/v3/GroupContactTest.php +++ b/tests/phpunit/api/v3/GroupContactTest.php @@ -260,7 +260,6 @@ public function testDeleteWithPending() { $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'status' => 'Removed')); } - /** * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups. * From bd9042db72bcc866415efd8f96374adce7517e2c Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 8 Feb 2017 10:00:58 +1100 Subject: [PATCH 4/4] Fix issue where a group contact record in status of pending cannot be immedately permanently deleted --- api/v3/GroupContact.php | 2 +- tests/phpunit/api/v3/GroupContactTest.php | 25 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/api/v3/GroupContact.php b/api/v3/GroupContact.php index 2528c827cbb4..53f055e82b85 100644 --- a/api/v3/GroupContact.php +++ b/api/v3/GroupContact.php @@ -158,7 +158,7 @@ function civicrm_api3_group_contact_delete($params) { } $groupContact = civicrm_api3('GroupContact', 'get', $checkParams); if ($groupContact['count'] == 0 && !empty($params['skip_undelete'])) { - $checkParams['status'] = 'removed'; + $checkParams['status'] = array('IN' => array('Removed', 'Pending')); } $groupContact2 = civicrm_api3('GroupContact', 'get', $checkParams); if ($groupContact['count'] == 0 && $groupContact2['count'] == 0) { diff --git a/tests/phpunit/api/v3/GroupContactTest.php b/tests/phpunit/api/v3/GroupContactTest.php index c2c686871c90..39509f095ae8 100644 --- a/tests/phpunit/api/v3/GroupContactTest.php +++ b/tests/phpunit/api/v3/GroupContactTest.php @@ -258,6 +258,31 @@ public function testDeleteWithPending() { $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams); $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams); $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'status' => 'Removed')); + $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE)); + $this->callAPISuccess('group', 'delete', array('id' => $groupId3)); + } + + /** + * CRM-19979 test that group cotnact delete action works when contact is in status of pendin and is a permanent delete. + */ + public function testPermanentDeleteWithPending() { + $groupId3 = $this->groupCreate(array( + 'name' => 'Test Group 3', + 'domain_id' => 1, + 'title' => 'New Test Group3 Created', + 'description' => 'New Test Group3 Created', + 'is_active' => 1, + 'visibility' => 'User and User Admin Only', + )); + $groupContactCreateParams = array( + 'contact_id' => $this->_contactId, + 'group_id' => $groupId3, + 'status' => 'Pending', + ); + $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams); + $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams); + $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE)); + $this->callAPISuccess('group', 'delete', array('id' => $groupId3)); } /**