Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
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 | |
6a488035 TO |
12 | /** |
13 | * Test class for Group API - civicrm_group_* | |
14 | * | |
6c6e6187 | 15 | * @package CiviCRM_APIv3 |
acb109b7 | 16 | * @group headless |
6a488035 | 17 | */ |
6a488035 | 18 | class api_v3_GroupTest extends CiviUnitTestCase { |
f78dbf0b | 19 | |
6a488035 | 20 | protected $_groupID; |
6a488035 | 21 | |
6d054a8e | 22 | /** |
23 | * Set up for tests. | |
24 | */ | |
00be9182 | 25 | public function setUp() { |
6a488035 | 26 | parent::setUp(); |
fadb804f | 27 | $this->_groupID = $this->groupCreate(); |
6d054a8e | 28 | $config = CRM_Core_Config::singleton(); |
f78dbf0b | 29 | $config->userPermissionClass->permissions = []; |
6a488035 TO |
30 | } |
31 | ||
6d054a8e | 32 | /** |
33 | * Clean up after test. | |
fda18dc3 | 34 | * |
35 | * @throws \Exception | |
6d054a8e | 36 | */ |
00be9182 | 37 | public function tearDown() { |
6d054a8e | 38 | CRM_Utils_Hook::singleton()->reset(); |
39 | $config = CRM_Core_Config::singleton(); | |
40 | unset($config->userPermissionClass->permissions); | |
a3b559ee | 41 | $this->quickCleanup(['civicrm_group', 'civicrm_group_contact']); |
fda18dc3 | 42 | parent::tearDown(); |
6a488035 TO |
43 | } |
44 | ||
6d054a8e | 45 | /** |
46 | * Test missing required title parameter results in an error. | |
f78dbf0b | 47 | * |
2d932085 | 48 | * @param int $version |
f78dbf0b | 49 | * |
2d932085 | 50 | * @dataProvider versionThreeAndFour |
6d054a8e | 51 | */ |
2d932085 CW |
52 | public function testGroupCreateNoTitle($version) { |
53 | $this->_apiversion = $version; | |
f78dbf0b | 54 | $params = [ |
6a488035 TO |
55 | 'name' => 'Test Group No title ', |
56 | 'domain_id' => 1, | |
57 | 'description' => 'New Test Group Created', | |
58 | 'is_active' => 1, | |
59 | 'visibility' => 'Public Pages', | |
f78dbf0b | 60 | 'group_type' => [ |
6a488035 TO |
61 | '1' => 1, |
62 | '2' => 1, | |
f78dbf0b | 63 | ], |
64 | ]; | |
6a488035 | 65 | |
2d932085 | 66 | $this->callAPIFailure('group', 'create', $params, 'title'); |
6a488035 TO |
67 | } |
68 | ||
2d932085 CW |
69 | /** |
70 | * @param int $version | |
f78dbf0b | 71 | * |
2d932085 CW |
72 | * @dataProvider versionThreeAndFour |
73 | */ | |
74 | public function testGetGroupWithEmptyParams($version) { | |
75 | $this->_apiversion = $version; | |
f78dbf0b | 76 | $group = $this->callAPISuccess('group', 'get', []); |
6a488035 TO |
77 | |
78 | $group = $group["values"]; | |
79 | $this->assertNotNull(count($group)); | |
5667b84b | 80 | $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1"); |
6a488035 TO |
81 | $this->assertEquals($group[$this->_groupID]['is_active'], 1); |
82 | $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages'); | |
83 | } | |
84 | ||
a3b559ee | 85 | /** |
86 | * Test ability to get active, inactive and both. | |
87 | * | |
88 | * Default is active only. | |
f78dbf0b | 89 | * |
2d932085 | 90 | * @param int $version |
f78dbf0b | 91 | * |
2d932085 | 92 | * @dataProvider versionThreeAndFour |
a3b559ee | 93 | */ |
2d932085 CW |
94 | public function testGetGroupActiveAndInactive($version) { |
95 | $this->_apiversion = $version; | |
a3b559ee | 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 | ||
2d932085 CW |
101 | /** |
102 | * @param int $version | |
f78dbf0b | 103 | * |
2d932085 CW |
104 | * @dataProvider versionThreeAndFour |
105 | */ | |
106 | public function testGetGroupParamsWithGroupId($version) { | |
107 | $this->_apiversion = $version; | |
f78dbf0b | 108 | $params = ['id' => $this->_groupID]; |
b26e776b | 109 | $group = $this->callAPISuccess('group', 'get', $params); |
6a488035 TO |
110 | |
111 | foreach ($group['values'] as $v) { | |
5667b84b | 112 | $this->assertEquals($v['name'], "Test Group 1"); |
6a488035 TO |
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 | ||
2d932085 CW |
120 | /** |
121 | * @param int $version | |
f78dbf0b | 122 | * |
2d932085 CW |
123 | * @dataProvider versionThreeAndFour |
124 | */ | |
125 | public function testGetGroupParamsWithGroupName($version) { | |
126 | $this->_apiversion = $version; | |
f78dbf0b | 127 | $params = [ |
5667b84b | 128 | 'name' => "Test Group 1", |
f78dbf0b | 129 | ]; |
b26e776b | 130 | $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
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 | ||
2d932085 CW |
142 | /** |
143 | * @param int $version | |
f78dbf0b | 144 | * |
2d932085 CW |
145 | * @dataProvider versionThreeAndFour |
146 | */ | |
147 | public function testGetGroupParamsWithReturnName($version) { | |
148 | $this->_apiversion = $version; | |
f78dbf0b | 149 | $params = []; |
6a488035 TO |
150 | $params['id'] = $this->_groupID; |
151 | $params['return.name'] = 1; | |
b26e776b | 152 | $group = $this->callAPISuccess('group', 'get', $params); |
6a488035 | 153 | $this->assertEquals($group['values'][$this->_groupID]['name'], |
5667b84b | 154 | "Test Group 1" |
6a488035 TO |
155 | ); |
156 | } | |
157 | ||
2d932085 CW |
158 | /** |
159 | * @param int $version | |
f78dbf0b | 160 | * |
2d932085 CW |
161 | * @dataProvider versionThreeAndFour |
162 | */ | |
163 | public function testGetGroupParamsWithGroupTitle($version) { | |
164 | $this->_apiversion = $version; | |
f78dbf0b | 165 | $params = []; |
6a488035 | 166 | $params['title'] = 'New Test Group Created'; |
5667b84b | 167 | $group = $this->callAPISuccess('group', 'get', $params); |
6a488035 TO |
168 | |
169 | foreach ($group['values'] as $v) { | |
170 | $this->assertEquals($v['id'], $this->_groupID); | |
5667b84b | 171 | $this->assertEquals($v['name'], "Test Group 1"); |
6a488035 TO |
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 | ||
140609dc | 178 | /** |
ec7846f5 | 179 | * Test Group create with Group Type and Parent |
2d932085 | 180 | * FIXME: Api4 |
140609dc | 181 | */ |
ec7846f5 | 182 | public function testGroupCreateWithTypeAndParent() { |
f78dbf0b | 183 | $params = [ |
140609dc | 184 | 'name' => 'Test Group type', |
185 | 'title' => 'Test Group Type', | |
186 | 'description' => 'Test Group with Group Type', | |
187 | 'is_active' => 1, | |
ec7846f5 | 188 | //check for empty parent |
189 | 'parents' => "", | |
140609dc | 190 | 'visibility' => 'Public Pages', |
30208fab | 191 | 'group_type' => [1, 2], |
f78dbf0b | 192 | ]; |
140609dc | 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); | |
ec7846f5 | 198 | $this->assertEquals($group['parents'], ""); |
140609dc | 199 | $this->assertEquals($group['group_type'], $params['group_type']); |
140609dc | 200 | |
fb413e82 | 201 | //Pass group_type param in checkbox format. |
f78dbf0b | 202 | $params = array_merge($params, [ |
39b959db SL |
203 | 'name' => 'Test Checkbox Format', |
204 | 'title' => 'Test Checkbox Format', | |
f78dbf0b | 205 | 'group_type' => [2 => 1], |
206 | ]); | |
fb413e82 | 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 | ||
ec7846f5 | 212 | //assert single value for group_type and parent |
f78dbf0b | 213 | $params = array_merge($params, [ |
39b959db SL |
214 | 'name' => 'Test Group 2', |
215 | 'title' => 'Test Group 2', | |
216 | 'group_type' => 2, | |
217 | 'parents' => $result['id'], | |
218 | 'sequential' => 1, | |
f78dbf0b | 219 | ]); |
30208fab | 220 | $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0]; |
221 | ||
f78dbf0b | 222 | $this->assertEquals($group2['group_type'], [$params['group_type']]); |
30208fab | 223 | $this->assertEquals($params['parents'], $group2['parents']); |
224 | ||
225 | // Test array format for parents. | |
f78dbf0b | 226 | $params = array_merge($params, [ |
39b959db SL |
227 | 'name' => 'Test Group 3', |
228 | 'title' => 'Test Group 3', | |
229 | 'parents' => [$result['id'], $group2['id']], | |
f78dbf0b | 230 | ]); |
30208fab | 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. | |
2d932085 | 245 | * FIXME: Api4 |
30208fab | 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); | |
140609dc | 258 | } |
259 | ||
4f92030f | 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 | ||
2d932085 CW |
276 | /** |
277 | * @param int $version | |
f78dbf0b | 278 | * |
2d932085 CW |
279 | * @dataProvider versionThreeAndFour |
280 | */ | |
281 | public function testGetNonExistingGroup($version) { | |
282 | $this->_apiversion = $version; | |
f78dbf0b | 283 | $params = []; |
6a488035 | 284 | $params['title'] = 'No such group Exist'; |
5667b84b | 285 | $group = $this->callAPISuccess('group', 'get', $params); |
d0e1eff2 | 286 | $this->assertEquals(0, $group['count']); |
6a488035 TO |
287 | } |
288 | ||
2d932085 CW |
289 | /** |
290 | * @param int $version | |
f78dbf0b | 291 | * |
2d932085 CW |
292 | * @dataProvider versionThreeAndFour |
293 | */ | |
294 | public function testgroupdeleteParamsnoId($version) { | |
295 | $this->_apiversion = $version; | |
f78dbf0b | 296 | $group = $this->callAPIFailure('group', 'delete', []); |
6a488035 TO |
297 | } |
298 | ||
2d932085 CW |
299 | /** |
300 | * @param int $version | |
f78dbf0b | 301 | * |
2d932085 CW |
302 | * @dataProvider versionThreeAndFour |
303 | */ | |
304 | public function testgetfields($version) { | |
305 | $this->_apiversion = $version; | |
5c49fee0 | 306 | $description = "Demonstrate use of getfields to interrogate api."; |
f78dbf0b | 307 | $params = ['action' => 'create']; |
a828d7b8 | 308 | $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description); |
2d932085 | 309 | $this->assertEquals('is_active', $result['values']['is_active']['name']); |
6a488035 | 310 | } |
96025800 | 311 | |
1514f450 | 312 | public function testIllegalParentsParams() { |
f78dbf0b | 313 | $params = [ |
1514f450 SL |
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)", | |
f78dbf0b | 319 | ]; |
52202a20 SL |
320 | $this->callAPIFailure('group', 'create', $params); |
321 | unset($params['parents']); | |
1514f450 | 322 | $this->callAPISuccess('group', 'create', $params); |
f78dbf0b | 323 | $group1 = $this->callAPISuccess('group', 'get', [ |
1514f450 | 324 | 'title' => 'Test illegal Group', |
f78dbf0b | 325 | 'parents' => ['IS NOT NULL' => 1], |
326 | ]); | |
1514f450 SL |
327 | $this->assertEquals(0, $group1['count']); |
328 | $params['title'] = 'Test illegal Group 2'; | |
f78dbf0b | 329 | $params['parents'] = []; |
1514f450 SL |
330 | $params['parents'][$this->_groupID] = 'test Group'; |
331 | $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test"; | |
6d054a8e | 332 | $this->callAPIFailure('group', 'create', $params); |
52202a20 | 333 | unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]); |
1678a63b | 334 | $this->callAPIFailure('group', 'create', $params, '\'test Group\' is not a valid option for field parents'); |
1514f450 SL |
335 | } |
336 | ||
6d054a8e | 337 | /** |
338 | * Test that ACLs are applied to group.get calls. | |
2d932085 | 339 | * FIXME: Api4 |
6d054a8e | 340 | */ |
341 | public function testGroupGetACLs() { | |
342 | $this->createLoggedInUser(); | |
f78dbf0b | 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']); | |
6d054a8e | 346 | unset(Civi::$statics['CRM_ACL_API']['group_permission']); |
f78dbf0b | 347 | $this->callAPISuccessGetCount('Group', ['check_permissions' => 1], 1); |
6d054a8e | 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) { | |
f78dbf0b | 360 | $group = $this->callAPISuccess('Group', 'get', ['name' => 'Test Group 1']); |
6d054a8e | 361 | $ids = array_keys($group['values']); |
362 | } | |
363 | ||
6a488035 | 364 | } |