Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
e9479dcf
EM
12/**
13 * Class api_v3_GroupContactTest
acb109b7 14 * @group headless
e9479dcf 15 */
6a488035
TO
16class api_v3_GroupContactTest extends CiviUnitTestCase {
17
18 protected $_contactId;
19 protected $_contactId1;
7fbb4198 20 protected $_apiversion = 3;
97089930 21
22 /**
23 * @var int
24 */
6a488035 25 protected $_groupId1;
6a488035 26
97089930 27 /**
28 * @var int
29 */
30 protected $_groupId2;
31
c490a46a 32 /**
eceb18cc 33 * Set up for group contact tests.
c490a46a
CW
34 *
35 * @todo set up calls function that doesn't work @ the moment
36 */
00be9182 37 public function setUp() {
6a488035 38 parent::setUp();
10f7c4d2 39 $this->useTransaction(TRUE);
6a488035 40
e4d5f1e2 41 $this->_contactId = $this->individualCreate();
6a488035 42
72e41e3a 43 $this->_groupId1 = $this->groupCreate();
97089930 44
9099cab3 45 $this->callAPISuccess('group_contact', 'create', [
6a488035
TO
46 'contact_id' => $this->_contactId,
47 'group_id' => $this->_groupId1,
9099cab3 48 ]);
6a488035 49
9099cab3 50 $this->_groupId2 = $this->groupCreate([
6a488035
TO
51 'name' => 'Test Group 2',
52 'domain_id' => 1,
53 'title' => 'New Test Group2 Created',
54 'description' => 'New Test Group2 Created',
55 'is_active' => 1,
56 'visibility' => 'User and User Admin Only',
9099cab3 57 ]);
6a488035 58
9099cab3
CW
59 $this->_group = [
60 $this->_groupId1 => [
92915c55 61 'title' => 'New Test Group Created',
6a488035
TO
62 'visibility' => 'Public Pages',
63 'in_method' => 'API',
9099cab3
CW
64 ],
65 $this->_groupId2 => [
6a488035
TO
66 'title' => 'New Test Group2 Created',
67 'visibility' => 'User and User Admin Only',
68 'in_method' => 'API',
9099cab3
CW
69 ],
70 ];
6a488035
TO
71 }
72
389bcebf 73 /**
36e590d4 74 * Test GroupContact.get by ID.
389bcebf 75 */
00be9182 76 public function testGet() {
9099cab3 77 $params = [
6a488035 78 'contact_id' => $this->_contactId,
9099cab3 79 ];
7fbb4198 80 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
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']);
85 }
86 }
87
00be9182 88 public function testGetGroupID() {
5c49fee0 89 $description = "Get all from group and display contacts.";
92915c55 90 $subfile = "GetWithGroupID";
9099cab3 91 $params = [
6a488035 92 'group_id' => $this->_groupId1,
6a488035
TO
93 'api.group.get' => 1,
94 'sequential' => 1,
9099cab3 95 ];
7fbb4198 96 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
97 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
98 $key = $values['id'];
99 $this->assertEquals($values['title'], $this->_group[$key]['title']);
100 $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']);
101 }
102 }
103
00be9182 104 public function testCreateWithEmptyParams() {
9099cab3 105 $params = [];
d0e1eff2 106 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035 107 $this->assertEquals($groups['error_message'],
d0e1eff2 108 'Mandatory key(s) missing from params array: group_id, contact_id'
6a488035
TO
109 );
110 }
111
00be9182 112 public function testCreateWithoutGroupIdParams() {
9099cab3 113 $params = [
92915c55 114 'contact_id' => $this->_contactId,
9099cab3 115 ];
6a488035 116
d0e1eff2 117 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
118 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
119 }
120
00be9182 121 public function testCreateWithoutContactIdParams() {
9099cab3 122 $params = [
92915c55 123 'group_id' => $this->_groupId1,
9099cab3 124 ];
d0e1eff2 125 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
126 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
127 }
128
00be9182 129 public function testCreate() {
9099cab3 130 $cont = [
6a488035
TO
131 'first_name' => 'Amiteshwar',
132 'middle_name' => 'L.',
133 'last_name' => 'Prasad',
134 'prefix_id' => 3,
135 'suffix_id' => 3,
136 'email' => 'amiteshwar.prasad@civicrm.org',
92915c55 137 'contact_type' => 'Individual',
9099cab3 138 ];
6a488035
TO
139
140 $this->_contactId1 = $this->individualCreate($cont);
9099cab3 141 $params = [
6a488035
TO
142 'contact_id' => $this->_contactId,
143 'contact_id.2' => $this->_contactId1,
144 'group_id' => $this->_groupId1,
9099cab3 145 ];
6a488035 146
7fbb4198 147 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
97089930 148 $this->assertEquals($result['not_added'], 1);
149 $this->assertEquals($result['added'], 1);
150 $this->assertEquals($result['total_count'], 2);
6a488035
TO
151 }
152
389bcebf 153 /**
36e590d4 154 * Test GroupContact.delete by contact+group ID.
389bcebf 155 */
00be9182 156 public function testDelete() {
9099cab3 157 $params = [
6a488035 158 'contact_id' => $this->_contactId,
10f7c4d2 159 'group_id' => $this->_groupId1,
9099cab3 160 ];
6a488035 161
7fbb4198 162 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
97089930 163 $this->assertEquals($result['removed'], 1);
164 $this->assertEquals($result['total_count'], 1);
6a488035 165 }
3d700d00 166
00be9182 167 public function testDeletePermanent() {
9099cab3
CW
168 $result = $this->callAPISuccess('group_contact', 'get', ['contact_id' => $this->_contactId]);
169 $params = [
a9739e5d 170 'id' => $result['id'],
3d700d00 171 'skip_undelete' => TRUE,
9099cab3 172 ];
3d700d00
CW
173 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
174 $result = $this->callAPISuccess('group_contact', 'get', $params);
97089930 175 $this->assertEquals(0, $result['count']);
176 $this->assertArrayNotHasKey('id', $result);
3d700d00 177 }
96025800 178
3c64f1f6
SL
179 /**
180 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
181 *
182 */
183 public function testDeleteWithId() {
9099cab3 184 $groupContactParams = [
3c64f1f6
SL
185 'contact_id' => $this->_contactId,
186 'group_id' => $this->_groupId1,
9099cab3 187 ];
3c64f1f6 188 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
9099cab3 189 $params = [
3c64f1f6
SL
190 'id' => $groupContact['id'],
191 'status' => 'Removed',
9099cab3 192 ];
3c64f1f6
SL
193 $result = $this->callAPISuccess('group_contact', 'delete', $params);
194 $this->assertEquals($result['removed'], 1);
195 $this->assertEquals($result['total_count'], 1);
196 }
197
198 /**
199 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
200 *
201 */
202 public function testDeleteAndReAddWithId() {
9099cab3 203 $groupContactParams = [
3c64f1f6
SL
204 'contact_id' => $this->_contactId,
205 'group_id' => $this->_groupId1,
9099cab3 206 ];
3c64f1f6 207 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
9099cab3 208 $params = [
3c64f1f6
SL
209 'id' => $groupContact['id'],
210 'status' => 'Removed',
9099cab3 211 ];
3c64f1f6
SL
212 $result = $this->callAPISuccess('group_contact', 'delete', $params);
213 $this->assertEquals($result['removed'], 1);
214 $this->assertEquals($result['total_count'], 1);
9099cab3 215 $params = array_merge($params, ['status' => 'Added']);
3c64f1f6
SL
216 $result2 = $this->callAPISuccess('group_contact', 'delete', $params);
217 $this->assertEquals($result2['added'], 1);
218 $this->assertEquals($result2['total_count'], 1);
219 }
220
85df4a81
SL
221 /**
222 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin.
223 */
224 public function testDeleteWithPending() {
9099cab3 225 $groupId3 = $this->groupCreate([
85df4a81
SL
226 'name' => 'Test Group 3',
227 'domain_id' => 1,
228 'title' => 'New Test Group3 Created',
229 'description' => 'New Test Group3 Created',
230 'is_active' => 1,
231 'visibility' => 'User and User Admin Only',
9099cab3
CW
232 ]);
233 $groupContactCreateParams = [
85df4a81
SL
234 'contact_id' => $this->_contactId,
235 'group_id' => $groupId3,
236 'status' => 'Pending',
9099cab3 237 ];
85df4a81
SL
238 $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams);
239 $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams);
9099cab3
CW
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]);
bd9042db
SL
243 }
244
245 /**
246 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin and is a permanent delete.
247 */
248 public function testPermanentDeleteWithPending() {
9099cab3 249 $groupId3 = $this->groupCreate([
bd9042db
SL
250 'name' => 'Test Group 3',
251 'domain_id' => 1,
252 'title' => 'New Test Group3 Created',
253 'description' => 'New Test Group3 Created',
254 'is_active' => 1,
255 'visibility' => 'User and User Admin Only',
9099cab3
CW
256 ]);
257 $groupContactCreateParams = [
bd9042db
SL
258 'contact_id' => $this->_contactId,
259 'group_id' => $groupId3,
260 'status' => 'Pending',
9099cab3 261 ];
bd9042db
SL
262 $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams);
263 $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams);
9099cab3
CW
264 $this->callAPISuccess('groupContact', 'delete', ['id' => $groupGetContact['id'], 'skip_undelete' => TRUE]);
265 $this->callAPISuccess('group', 'delete', ['id' => $groupId3]);
85df4a81
SL
266 }
267
242e73d4 268 /**
269 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
270 *
271 * Fix documented in
272 *
273 * Test illustrates this (& ensures once fixed it will stay fixed).
274 */
275 public function testAccurateCountWithSmartGroups() {
9099cab3 276 $childGroupID = $this->groupCreate([
242e73d4 277 'name' => 'Child group',
278 'domain_id' => 1,
279 'title' => 'Child group',
280 'description' => 'Child group',
281 'is_active' => 1,
282 'parents' => $this->_groupId1,
283 'visibility' => 'User and User Admin Only',
9099cab3 284 ]);
242e73d4 285
9099cab3 286 $params = [
242e73d4 287 'name' => 'Individuals',
288 'title' => 'Individuals',
289 'is_active' => 1,
290 'parents' => $this->_groupId1,
9099cab3
CW
291 'formValues' => ['contact_type' => 'Goat'],
292 ];
242e73d4 293 $smartGroup2 = CRM_Contact_BAO_Group::createSmartGroup($params);
294
9099cab3
CW
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]);
242e73d4 299
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);
306 }
307 $this->assertEquals(3, $groups['count']);
308
309 }
310
6a488035 311}