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 * Test class for Group API - civicrm_group_*
15 * @package CiviCRM_APIv3
18 class api_v3_GroupTest
extends CiviUnitTestCase
{
25 public function setUp() {
27 $this->_groupID
= $this->groupCreate();
28 $config = CRM_Core_Config
::singleton();
29 $config->userPermissionClass
->permissions
= [];
33 * Clean up after test.
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']);
46 * Test missing required title parameter results in an error.
50 * @dataProvider versionThreeAndFour
52 public function testGroupCreateNoTitle($version) {
53 $this->_apiversion
= $version;
55 'name' => 'Test Group No title ',
57 'description' => 'New Test Group Created',
59 'visibility' => 'Public Pages',
66 $this->callAPIFailure('group', 'create', $params, 'title');
72 * @dataProvider versionThreeAndFour
74 public function testGetGroupWithEmptyParams($version) {
75 $this->_apiversion
= $version;
76 $group = $this->callAPISuccess('group', 'get', []);
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');
86 * Test ability to get active, inactive and both.
88 * Default is active only.
92 * @dataProvider versionThreeAndFour
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);
102 * @param int $version
104 * @dataProvider versionThreeAndFour
106 public function testGetGroupParamsWithGroupId($version) {
107 $this->_apiversion
= $version;
108 $params = ['id' => $this->_groupID
];
109 $group = $this->callAPISuccess('group', 'get', $params);
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');
121 * @param int $version
123 * @dataProvider versionThreeAndFour
125 public function testGetGroupParamsWithGroupName($version) {
126 $this->_apiversion
= $version;
128 'name' => "Test Group 1",
130 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__
, __FILE__
);
131 $group = $group['values'];
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');
143 * @param int $version
145 * @dataProvider versionThreeAndFour
147 public function testGetGroupParamsWithReturnName($version) {
148 $this->_apiversion
= $version;
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'],
159 * @param int $version
161 * @dataProvider versionThreeAndFour
163 public function testGetGroupParamsWithGroupTitle($version) {
164 $this->_apiversion
= $version;
166 $params['title'] = 'New Test Group Created';
167 $group = $this->callAPISuccess('group', 'get', $params);
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');
179 * Test Group create with Group Type and Parent
182 public function testGroupCreateWithTypeAndParent() {
184 'name' => 'Test Group type',
185 'title' => 'Test Group Type',
186 'description' => 'Test Group with Group Type',
188 //check for empty parent
190 'visibility' => 'Public Pages',
191 'group_type' => [1, 2],
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']);
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],
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']));
212 //assert single value for group_type and parent
213 $params = array_merge($params, [
214 'name' => 'Test Group 2',
215 'title' => 'Test Group 2',
217 'parents' => $result['id'],
220 $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
222 $this->assertEquals($group2['group_type'], [$params['group_type']]);
223 $this->assertEquals($params['parents'], $group2['parents']);
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']],
231 $group3 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
232 $parents = $this->callAPISuccess('Group', 'getvalue', ['return' => 'parents', 'id' => $group3['id']]);
234 $this->assertAPIArrayComparison("{$result['id']},{$group2['id']}", $parents);
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']);
244 * Test that an array of valid values works for group_type field.
247 public function testGroupTypeWithPseudoconstantArray() {
249 'name' => 'Test Group 2',
250 'title' => 'Test Group 2',
251 'group_type' => ['Mailing List', 'Access Control'],
254 $group = $this->callAPISuccess('Group', 'create', $params);
255 $groupType = $this->callAPISuccess('Group', 'getvalue', ['return' => 'group_type', 'id' => $group['id']]);
257 $this->assertAPIArrayComparison([2, 1], $groupType);
261 * Test / demonstrate behaviour when attempting to filter by group_type.
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'.
266 * @throws \CRM_Core_Exception
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']);
277 * @param int $version
279 * @dataProvider versionThreeAndFour
281 public function testGetNonExistingGroup($version) {
282 $this->_apiversion
= $version;
284 $params['title'] = 'No such group Exist';
285 $group = $this->callAPISuccess('group', 'get', $params);
286 $this->assertEquals(0, $group['count']);
290 * @param int $version
292 * @dataProvider versionThreeAndFour
294 public function testgroupdeleteParamsnoId($version) {
295 $this->_apiversion
= $version;
296 $group = $this->callAPIFailure('group', 'delete', []);
300 * @param int $version
302 * @dataProvider versionThreeAndFour
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']);
312 public function testIllegalParentsParams() {
314 'title' => 'Test illegal Group',
316 'description' => 'Testing illegal Parents params',
318 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)",
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],
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');
338 * Test that ACLs are applied to group.get calls.
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);
351 * Implement hook to restrict to test group 1.
353 * @param string $type
354 * @param int $contactID
355 * @param string $tableName
356 * @param array $allGroups
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']);