3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class api_v3_GroupContactTest
16 class api_v3_GroupContactTest
extends CiviUnitTestCase
{
18 protected $_contactId;
19 protected $_contactId1;
20 protected $_apiversion = 3;
33 * Set up for group contact tests.
35 * @todo set up calls function that doesn't work @ the moment
37 public function setUp(): void
{
39 $this->useTransaction(TRUE);
41 $this->_contactId
= $this->individualCreate();
43 $this->_groupId1
= $this->groupCreate();
45 $this->callAPISuccess('group_contact', 'create', [
46 'contact_id' => $this->_contactId
,
47 'group_id' => $this->_groupId1
,
50 $this->_groupId2
= $this->groupCreate([
51 'name' => 'Test Group 2',
53 'title' => 'New Test Group2 Created',
54 'description' => 'New Test Group2 Created',
56 'visibility' => 'User and User Admin Only',
61 'title' => 'New Test Group Created',
62 'visibility' => 'Public Pages',
66 'title' => 'New Test Group2 Created',
67 'visibility' => 'User and User Admin Only',
74 * Test GroupContact.get by ID.
76 public function testGet() {
78 'contact_id' => $this->_contactId
,
80 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
);
81 foreach ($result['values'] as $v) {
82 $this->assertEquals($v['title'], $this->_group
[$v['group_id']]['title']);
83 $this->assertEquals($v['visibility'], $this->_group
[$v['group_id']]['visibility']);
84 $this->assertEquals($v['in_method'], $this->_group
[$v['group_id']]['in_method']);
88 public function testGetGroupID() {
89 $description = "Get all from group and display contacts.";
90 $subfile = "GetWithGroupID";
92 'group_id' => $this->_groupId1
,
96 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
97 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
99 $this->assertEquals($values['title'], $this->_group
[$key]['title']);
100 $this->assertEquals($values['visibility'], $this->_group
[$key]['visibility']);
104 public function testCreateWithEmptyParams() {
106 $groups = $this->callAPIFailure('group_contact', 'create', $params);
107 $this->assertEquals($groups['error_message'],
108 'Mandatory key(s) missing from params array: group_id, contact_id'
112 public function testCreateWithoutGroupIdParams() {
114 'contact_id' => $this->_contactId
,
117 $groups = $this->callAPIFailure('group_contact', 'create', $params);
118 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
121 public function testCreateWithoutContactIdParams() {
123 'group_id' => $this->_groupId1
,
125 $groups = $this->callAPIFailure('group_contact', 'create', $params);
126 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
129 public function testCreate() {
131 'first_name' => 'Amiteshwar',
132 'middle_name' => 'L.',
133 'last_name' => 'Prasad',
136 'email' => 'amiteshwar.prasad@civicrm.org',
137 'contact_type' => 'Individual',
140 $this->_contactId1
= $this->individualCreate($cont);
142 'contact_id' => $this->_contactId
,
143 'contact_id.2' => $this->_contactId1
,
144 'group_id' => $this->_groupId1
,
147 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__
, __FILE__
);
148 $this->assertEquals($result['not_added'], 1);
149 $this->assertEquals($result['added'], 1);
150 $this->assertEquals($result['total_count'], 2);
154 * Test GroupContact.delete by contact+group ID.
156 public function testDelete() {
158 'contact_id' => $this->_contactId
,
159 'group_id' => $this->_groupId1
,
162 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
163 $this->assertEquals($result['removed'], 1);
164 $this->assertEquals($result['total_count'], 1);
167 public function testDeletePermanent() {
168 $result = $this->callAPISuccess('group_contact', 'get', ['contact_id' => $this->_contactId
]);
170 'id' => $result['id'],
171 'skip_undelete' => TRUE,
173 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
174 $result = $this->callAPISuccess('group_contact', 'get', $params);
175 $this->assertEquals(0, $result['count']);
176 $this->assertArrayNotHasKey('id', $result);
180 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
183 public function testDeleteWithId() {
184 $groupContactParams = [
185 'contact_id' => $this->_contactId
,
186 'group_id' => $this->_groupId1
,
188 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
190 'id' => $groupContact['id'],
191 'status' => 'Removed',
193 $result = $this->callAPISuccess('group_contact', 'delete', $params);
194 $this->assertEquals($result['removed'], 1);
195 $this->assertEquals($result['total_count'], 1);
199 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
202 public function testDeleteAndReAddWithId() {
203 $groupContactParams = [
204 'contact_id' => $this->_contactId
,
205 'group_id' => $this->_groupId1
,
207 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
209 'id' => $groupContact['id'],
210 'status' => 'Removed',
212 $result = $this->callAPISuccess('group_contact', 'delete', $params);
213 $this->assertEquals($result['removed'], 1);
214 $this->assertEquals($result['total_count'], 1);
215 $params = array_merge($params, ['status' => 'Added']);
216 $result2 = $this->callAPISuccess('group_contact', 'delete', $params);
217 $this->assertEquals($result2['added'], 1);
218 $this->assertEquals($result2['total_count'], 1);
222 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin.
224 public function testDeleteWithPending() {
225 $groupId3 = $this->groupCreate([
226 'name' => 'Test Group 3',
228 'title' => 'New Test Group3 Created',
229 'description' => 'New Test Group3 Created',
231 'visibility' => 'User and User Admin Only',
233 $groupContactCreateParams = [
234 'contact_id' => $this->_contactId
,
235 'group_id' => $groupId3,
236 'status' => 'Pending',
238 $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams);
239 $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams);
240 $this->callAPISuccess('groupContact', 'delete', ['id' => $groupGetContact['id'], 'status' => 'Removed']);
241 $this->callAPISuccess('groupContact', 'delete', ['id' => $groupGetContact['id'], 'skip_undelete' => TRUE]);
242 $this->callAPISuccess('group', 'delete', ['id' => $groupId3]);
246 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin and is a permanent delete.
248 public function testPermanentDeleteWithPending() {
249 $groupId3 = $this->groupCreate([
250 'name' => 'Test Group 3',
252 'title' => 'New Test Group3 Created',
253 'description' => 'New Test Group3 Created',
255 'visibility' => 'User and User Admin Only',
257 $groupContactCreateParams = [
258 'contact_id' => $this->_contactId
,
259 'group_id' => $groupId3,
260 'status' => 'Pending',
262 $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams);
263 $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams);
264 $this->callAPISuccess('groupContact', 'delete', ['id' => $groupGetContact['id'], 'skip_undelete' => TRUE]);
265 $this->callAPISuccess('group', 'delete', ['id' => $groupId3]);
269 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
273 * Test illustrates this (& ensures once fixed it will stay fixed).
275 public function testAccurateCountWithSmartGroups() {
276 $childGroupID = $this->groupCreate([
277 'name' => 'Child group',
279 'title' => 'Child group',
280 'description' => 'Child group',
282 'parents' => $this->_groupId1
,
283 'visibility' => 'User and User Admin Only',
287 'name' => 'Individuals',
288 'title' => 'Individuals',
290 'parents' => $this->_groupId1
,
291 'formValues' => ['contact_type' => 'Goat'],
293 $smartGroup2 = CRM_Contact_BAO_Group
::createSmartGroup($params);
295 $this->callAPISuccess('GroupContact', 'create', ['contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $this->_groupId2
]);
296 $this->callAPISuccess('GroupContact', 'create', ['contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $smartGroup2->id
]);
297 $this->callAPISuccess('GroupContact', 'create', ['contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $childGroupID]);
298 $groups = $this->callAPISuccess('GroupContact', 'get', ['contact_id' => $this->_contactId
]);
300 // Although the contact is actually hard-added to 4 groups the smart groups are conventionally not returned by the api or displayed
301 // on the main part of the groups tab on the contact (which calls the same function. So, 3 groups is an OK number to return.
302 // However, as of writing this test 4 groups are returned (indexed by group_contact_id, but more seriously 3/4 of those have the group id 1
303 // so 2 on them have group ids that do not match the group contact id they have been keyed by.
304 foreach ($groups['values'] as $groupContactID => $groupContactRecord) {
305 $this->assertEquals($groupContactRecord['group_id'], CRM_Core_DAO
::singleValueQuery("SELECT group_id FROM civicrm_group_contact WHERE id = $groupContactID"), 'Group contact record mis-returned for id ' . $groupContactID);
307 $this->assertEquals(3, $groups['count']);