Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / GroupTest.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 * Test class for Group API - civicrm_group_*
14 *
15 * @package CiviCRM_APIv3
16 * @group headless
17 */
18 class api_v3_GroupTest extends CiviUnitTestCase {
19
20 protected $_groupID;
21
22 /**
23 * Set up for tests.
24 */
25 public function setUp() {
26 parent::setUp();
27 $this->_groupID = $this->groupCreate();
28 $config = CRM_Core_Config::singleton();
29 $config->userPermissionClass->permissions = [];
30 }
31
32 /**
33 * Clean up after test.
34 *
35 * @throws \Exception
36 */
37 public function tearDown() {
38 CRM_Utils_Hook::singleton()->reset();
39 $config = CRM_Core_Config::singleton();
40 unset($config->userPermissionClass->permissions);
41 $this->quickCleanup(['civicrm_group', 'civicrm_group_contact']);
42 parent::tearDown();
43 }
44
45 /**
46 * Test missing required title parameter results in an error.
47 *
48 * @param int $version
49 *
50 * @dataProvider versionThreeAndFour
51 */
52 public function testGroupCreateNoTitle($version) {
53 $this->_apiversion = $version;
54 $params = [
55 'name' => 'Test Group No title ',
56 'domain_id' => 1,
57 'description' => 'New Test Group Created',
58 'is_active' => 1,
59 'visibility' => 'Public Pages',
60 'group_type' => [
61 '1' => 1,
62 '2' => 1,
63 ],
64 ];
65
66 $this->callAPIFailure('group', 'create', $params, 'title');
67 }
68
69 /**
70 * @param int $version
71 *
72 * @dataProvider versionThreeAndFour
73 */
74 public function testGetGroupWithEmptyParams($version) {
75 $this->_apiversion = $version;
76 $group = $this->callAPISuccess('group', 'get', []);
77
78 $group = $group["values"];
79 $this->assertNotNull(count($group));
80 $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1");
81 $this->assertEquals($group[$this->_groupID]['is_active'], 1);
82 $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages');
83 }
84
85 /**
86 * Test ability to get active, inactive and both.
87 *
88 * Default is active only.
89 *
90 * @param int $version
91 *
92 * @dataProvider versionThreeAndFour
93 */
94 public function testGetGroupActiveAndInactive($version) {
95 $this->_apiversion = $version;
96 $this->groupCreate(['is_active' => 0, 'name' => 'group_2', 'title' => 2]);
97 $group1 = $this->callAPISuccessGetSingle('Group', ['is_active' => 1]);
98 $this->callAPISuccessGetCount('Group', [], 2);
99 }
100
101 /**
102 * @param int $version
103 *
104 * @dataProvider versionThreeAndFour
105 */
106 public function testGetGroupParamsWithGroupId($version) {
107 $this->_apiversion = $version;
108 $params = ['id' => $this->_groupID];
109 $group = $this->callAPISuccess('group', 'get', $params);
110
111 foreach ($group['values'] as $v) {
112 $this->assertEquals($v['name'], "Test Group 1");
113 $this->assertEquals($v['title'], 'New Test Group Created');
114 $this->assertEquals($v['description'], 'New Test Group Created');
115 $this->assertEquals($v['is_active'], 1);
116 $this->assertEquals($v['visibility'], 'Public Pages');
117 }
118 }
119
120 /**
121 * @param int $version
122 *
123 * @dataProvider versionThreeAndFour
124 */
125 public function testGetGroupParamsWithGroupName($version) {
126 $this->_apiversion = $version;
127 $params = [
128 'name' => "Test Group 1",
129 ];
130 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__);
131 $group = $group['values'];
132
133 foreach ($group as $v) {
134 $this->assertEquals($v['id'], $this->_groupID);
135 $this->assertEquals($v['title'], 'New Test Group Created');
136 $this->assertEquals($v['description'], 'New Test Group Created');
137 $this->assertEquals($v['is_active'], 1);
138 $this->assertEquals($v['visibility'], 'Public Pages');
139 }
140 }
141
142 /**
143 * @param int $version
144 *
145 * @dataProvider versionThreeAndFour
146 */
147 public function testGetGroupParamsWithReturnName($version) {
148 $this->_apiversion = $version;
149 $params = [];
150 $params['id'] = $this->_groupID;
151 $params['return.name'] = 1;
152 $group = $this->callAPISuccess('group', 'get', $params);
153 $this->assertEquals($group['values'][$this->_groupID]['name'],
154 "Test Group 1"
155 );
156 }
157
158 /**
159 * @param int $version
160 *
161 * @dataProvider versionThreeAndFour
162 */
163 public function testGetGroupParamsWithGroupTitle($version) {
164 $this->_apiversion = $version;
165 $params = [];
166 $params['title'] = 'New Test Group Created';
167 $group = $this->callAPISuccess('group', 'get', $params);
168
169 foreach ($group['values'] as $v) {
170 $this->assertEquals($v['id'], $this->_groupID);
171 $this->assertEquals($v['name'], "Test Group 1");
172 $this->assertEquals($v['description'], 'New Test Group Created');
173 $this->assertEquals($v['is_active'], 1);
174 $this->assertEquals($v['visibility'], 'Public Pages');
175 }
176 }
177
178 /**
179 * Test Group create with Group Type and Parent
180 * FIXME: Api4
181 */
182 public function testGroupCreateWithTypeAndParent() {
183 $params = [
184 'name' => 'Test Group type',
185 'title' => 'Test Group Type',
186 'description' => 'Test Group with Group Type',
187 'is_active' => 1,
188 //check for empty parent
189 'parents' => "",
190 'visibility' => 'Public Pages',
191 'group_type' => [1, 2],
192 ];
193
194 $result = $this->callAPISuccess('Group', 'create', $params);
195 $group = $result['values'][$result['id']];
196 $this->assertEquals($group['name'], "Test Group type");
197 $this->assertEquals($group['is_active'], 1);
198 $this->assertEquals($group['parents'], "");
199 $this->assertEquals($group['group_type'], $params['group_type']);
200
201 //Pass group_type param in checkbox format.
202 $params = array_merge($params, [
203 'name' => 'Test Checkbox Format',
204 'title' => 'Test Checkbox Format',
205 'group_type' => [2 => 1],
206 ]);
207 $result = $this->callAPISuccess('Group', 'create', $params);
208 $group = $result['values'][$result['id']];
209 $this->assertEquals($group['name'], "Test Checkbox Format");
210 $this->assertEquals($group['group_type'], array_keys($params['group_type']));
211
212 //assert single value for group_type and parent
213 $params = array_merge($params, [
214 'name' => 'Test Group 2',
215 'title' => 'Test Group 2',
216 'group_type' => 2,
217 'parents' => $result['id'],
218 'sequential' => 1,
219 ]);
220 $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
221
222 $this->assertEquals($group2['group_type'], [$params['group_type']]);
223 $this->assertEquals($params['parents'], $group2['parents']);
224
225 // Test array format for parents.
226 $params = array_merge($params, [
227 'name' => 'Test Group 3',
228 'title' => 'Test Group 3',
229 'parents' => [$result['id'], $group2['id']],
230 ]);
231 $group3 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
232 $parents = $this->callAPISuccess('Group', 'getvalue', ['return' => 'parents', 'id' => $group3['id']]);
233
234 $this->assertAPIArrayComparison("{$result['id']},{$group2['id']}", $parents);
235
236 $groupNesting = $this->callAPISuccess('GroupNesting', 'get', ['child_group_id' => $group3['id']]);
237 // 2 Group nesting entries - one for direct parent & one for grandparent.
238 $this->assertEquals(2, $groupNesting['count']);
239 $this->groupDelete($group2['id']);
240 $this->groupDelete($group3['id']);
241 }
242
243 /**
244 * Test that an array of valid values works for group_type field.
245 * FIXME: Api4
246 */
247 public function testGroupTypeWithPseudoconstantArray() {
248 $params = [
249 'name' => 'Test Group 2',
250 'title' => 'Test Group 2',
251 'group_type' => ['Mailing List', 'Access Control'],
252 'sequential' => 1,
253 ];
254 $group = $this->callAPISuccess('Group', 'create', $params);
255 $groupType = $this->callAPISuccess('Group', 'getvalue', ['return' => 'group_type', 'id' => $group['id']]);
256
257 $this->assertAPIArrayComparison([2, 1], $groupType);
258 }
259
260 /**
261 * Test / demonstrate behaviour when attempting to filter by group_type.
262 *
263 * Per https://lab.civicrm.org/dev/core/issues/1321 the group_type filter is deceptive
264 * - it only filters on exact match not 'is one of'.
265 *
266 * @throws \CRM_Core_Exception
267 */
268 public function testGroupWithGroupTypeFilter() {
269 $this->groupCreate(['group_type' => ['Access Control'], 'name' => 'access_list', 'title' => 'access list']);
270 $this->groupCreate(['group_type' => ['Mailing List'], 'name' => 'mailing_list', 'title' => 'mailing list']);
271 $this->groupCreate(['group_type' => ['Access Control', 'Mailing List'], 'name' => 'group', 'title' => 'group']);
272 $group = $this->callAPISuccessGetSingle('Group', ['return' => 'id,title,group_type', 'group_type' => 'Mailing List']);
273 $this->assertEquals('mailing list', $group['title']);
274 }
275
276 /**
277 * @param int $version
278 *
279 * @dataProvider versionThreeAndFour
280 */
281 public function testGetNonExistingGroup($version) {
282 $this->_apiversion = $version;
283 $params = [];
284 $params['title'] = 'No such group Exist';
285 $group = $this->callAPISuccess('group', 'get', $params);
286 $this->assertEquals(0, $group['count']);
287 }
288
289 /**
290 * @param int $version
291 *
292 * @dataProvider versionThreeAndFour
293 */
294 public function testgroupdeleteParamsnoId($version) {
295 $this->_apiversion = $version;
296 $group = $this->callAPIFailure('group', 'delete', []);
297 }
298
299 /**
300 * @param int $version
301 *
302 * @dataProvider versionThreeAndFour
303 */
304 public function testgetfields($version) {
305 $this->_apiversion = $version;
306 $description = "Demonstrate use of getfields to interrogate api.";
307 $params = ['action' => 'create'];
308 $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description);
309 $this->assertEquals('is_active', $result['values']['is_active']['name']);
310 }
311
312 public function testIllegalParentsParams() {
313 $params = [
314 'title' => 'Test illegal Group',
315 'domain_id' => 1,
316 'description' => 'Testing illegal Parents params',
317 'is_active' => 1,
318 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)",
319 ];
320 $this->callAPIFailure('group', 'create', $params);
321 unset($params['parents']);
322 $this->callAPISuccess('group', 'create', $params);
323 $group1 = $this->callAPISuccess('group', 'get', [
324 'title' => 'Test illegal Group',
325 'parents' => ['IS NOT NULL' => 1],
326 ]);
327 $this->assertEquals(0, $group1['count']);
328 $params['title'] = 'Test illegal Group 2';
329 $params['parents'] = [];
330 $params['parents'][$this->_groupID] = 'test Group';
331 $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test";
332 $this->callAPIFailure('group', 'create', $params);
333 unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]);
334 $this->callAPIFailure('group', 'create', $params, '\'test Group\' is not a valid option for field parents');
335 }
336
337 /**
338 * Test that ACLs are applied to group.get calls.
339 * FIXME: Api4
340 */
341 public function testGroupGetACLs() {
342 $this->createLoggedInUser();
343 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
344 $this->callAPISuccessGetCount('Group', ['check_permissions' => 1], 0);
345 $this->hookClass->setHook('civicrm_aclGroup', [$this, 'aclGroupAllGroups']);
346 unset(Civi::$statics['CRM_ACL_API']['group_permission']);
347 $this->callAPISuccessGetCount('Group', ['check_permissions' => 1], 1);
348 }
349
350 /**
351 * Implement hook to restrict to test group 1.
352 *
353 * @param string $type
354 * @param int $contactID
355 * @param string $tableName
356 * @param array $allGroups
357 * @param array $ids
358 */
359 public function aclGroupAllGroups($type, $contactID, $tableName, $allGroups, &$ids) {
360 $group = $this->callAPISuccess('Group', 'get', ['name' => 'Test Group 1']);
361 $ids = array_keys($group['values']);
362 }
363
364 }