Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class api_v3_GroupContactTest
14 * @group headless
15 */
16 class api_v3_GroupContactTest extends CiviUnitTestCase {
17
18 protected $_contactId;
19 protected $_contactId1;
20 protected $_apiversion = 3;
21
22 /**
23 * @var int
24 */
25 protected $_groupId1;
26
27 /**
28 * @var int
29 */
30 protected $_groupId2;
31
32 /**
33 * Set up for group contact tests.
34 *
35 * @todo set up calls function that doesn't work @ the moment
36 */
37 public function setUp() {
38 parent::setUp();
39 $this->useTransaction(TRUE);
40
41 $this->_contactId = $this->individualCreate();
42
43 $this->_groupId1 = $this->groupCreate();
44
45 $this->callAPISuccess('group_contact', 'create', [
46 'contact_id' => $this->_contactId,
47 'group_id' => $this->_groupId1,
48 ]);
49
50 $this->_groupId2 = $this->groupCreate([
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',
57 ]);
58
59 $this->_group = [
60 $this->_groupId1 => [
61 'title' => 'New Test Group Created',
62 'visibility' => 'Public Pages',
63 'in_method' => 'API',
64 ],
65 $this->_groupId2 => [
66 'title' => 'New Test Group2 Created',
67 'visibility' => 'User and User Admin Only',
68 'in_method' => 'API',
69 ],
70 ];
71 }
72
73 /**
74 * Test GroupContact.get by ID.
75 */
76 public function testGet() {
77 $params = [
78 'contact_id' => $this->_contactId,
79 ];
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']);
85 }
86 }
87
88 public function testGetGroupID() {
89 $description = "Get all from group and display contacts.";
90 $subfile = "GetWithGroupID";
91 $params = [
92 'group_id' => $this->_groupId1,
93 'api.group.get' => 1,
94 'sequential' => 1,
95 ];
96 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
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
104 public function testCreateWithEmptyParams() {
105 $params = [];
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'
109 );
110 }
111
112 public function testCreateWithoutGroupIdParams() {
113 $params = [
114 'contact_id' => $this->_contactId,
115 ];
116
117 $groups = $this->callAPIFailure('group_contact', 'create', $params);
118 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
119 }
120
121 public function testCreateWithoutContactIdParams() {
122 $params = [
123 'group_id' => $this->_groupId1,
124 ];
125 $groups = $this->callAPIFailure('group_contact', 'create', $params);
126 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
127 }
128
129 public function testCreate() {
130 $cont = [
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',
137 'contact_type' => 'Individual',
138 ];
139
140 $this->_contactId1 = $this->individualCreate($cont);
141 $params = [
142 'contact_id' => $this->_contactId,
143 'contact_id.2' => $this->_contactId1,
144 'group_id' => $this->_groupId1,
145 ];
146
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);
151 }
152
153 /**
154 * Test GroupContact.delete by contact+group ID.
155 */
156 public function testDelete() {
157 $params = [
158 'contact_id' => $this->_contactId,
159 'group_id' => $this->_groupId1,
160 ];
161
162 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
163 $this->assertEquals($result['removed'], 1);
164 $this->assertEquals($result['total_count'], 1);
165 }
166
167 public function testDeletePermanent() {
168 $result = $this->callAPISuccess('group_contact', 'get', ['contact_id' => $this->_contactId]);
169 $params = [
170 'id' => $result['id'],
171 'skip_undelete' => TRUE,
172 ];
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);
177 }
178
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() {
184 $groupContactParams = [
185 'contact_id' => $this->_contactId,
186 'group_id' => $this->_groupId1,
187 ];
188 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
189 $params = [
190 'id' => $groupContact['id'],
191 'status' => 'Removed',
192 ];
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() {
203 $groupContactParams = [
204 'contact_id' => $this->_contactId,
205 'group_id' => $this->_groupId1,
206 ];
207 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
208 $params = [
209 'id' => $groupContact['id'],
210 'status' => 'Removed',
211 ];
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);
219 }
220
221 /**
222 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin.
223 */
224 public function testDeleteWithPending() {
225 $groupId3 = $this->groupCreate([
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',
232 ]);
233 $groupContactCreateParams = [
234 'contact_id' => $this->_contactId,
235 'group_id' => $groupId3,
236 'status' => 'Pending',
237 ];
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]);
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() {
249 $groupId3 = $this->groupCreate([
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',
256 ]);
257 $groupContactCreateParams = [
258 'contact_id' => $this->_contactId,
259 'group_id' => $groupId3,
260 'status' => 'Pending',
261 ];
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]);
266 }
267
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() {
276 $childGroupID = $this->groupCreate([
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',
284 ]);
285
286 $params = [
287 'name' => 'Individuals',
288 'title' => 'Individuals',
289 'is_active' => 1,
290 'parents' => $this->_groupId1,
291 'formValues' => ['contact_type' => 'Goat'],
292 ];
293 $smartGroup2 = CRM_Contact_BAO_Group::createSmartGroup($params);
294
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]);
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
311 }