3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test class for Group API - civicrm_group_*
31 * @package CiviCRM_APIv3
34 class api_v3_GroupTest
extends CiviUnitTestCase
{
41 public function setUp() {
43 $this->_groupID
= $this->groupCreate();
44 $config = CRM_Core_Config
::singleton();
45 $config->userPermissionClass
->permissions
= [];
49 * Clean up after test.
53 public function tearDown() {
54 CRM_Utils_Hook
::singleton()->reset();
55 $config = CRM_Core_Config
::singleton();
56 unset($config->userPermissionClass
->permissions
);
57 $this->quickCleanup(['civicrm_group', 'civicrm_group_contact']);
62 * Test missing required title parameter results in an error.
66 * @dataProvider versionThreeAndFour
68 public function testGroupCreateNoTitle($version) {
69 $this->_apiversion
= $version;
71 'name' => 'Test Group No title ',
73 'description' => 'New Test Group Created',
75 'visibility' => 'Public Pages',
82 $this->callAPIFailure('group', 'create', $params, 'title');
88 * @dataProvider versionThreeAndFour
90 public function testGetGroupWithEmptyParams($version) {
91 $this->_apiversion
= $version;
92 $group = $this->callAPISuccess('group', 'get', []);
94 $group = $group["values"];
95 $this->assertNotNull(count($group));
96 $this->assertEquals($group[$this->_groupID
]['name'], "Test Group 1");
97 $this->assertEquals($group[$this->_groupID
]['is_active'], 1);
98 $this->assertEquals($group[$this->_groupID
]['visibility'], 'Public Pages');
102 * Test ability to get active, inactive and both.
104 * Default is active only.
106 * @param int $version
108 * @dataProvider versionThreeAndFour
110 public function testGetGroupActiveAndInactive($version) {
111 $this->_apiversion
= $version;
112 $this->groupCreate(['is_active' => 0, 'name' => 'group_2', 'title' => 2]);
113 $group1 = $this->callAPISuccessGetSingle('Group', ['is_active' => 1]);
114 $this->callAPISuccessGetCount('Group', [], 2);
118 * @param int $version
120 * @dataProvider versionThreeAndFour
122 public function testGetGroupParamsWithGroupId($version) {
123 $this->_apiversion
= $version;
124 $params = ['id' => $this->_groupID
];
125 $group = $this->callAPISuccess('group', 'get', $params);
127 foreach ($group['values'] as $v) {
128 $this->assertEquals($v['name'], "Test Group 1");
129 $this->assertEquals($v['title'], 'New Test Group Created');
130 $this->assertEquals($v['description'], 'New Test Group Created');
131 $this->assertEquals($v['is_active'], 1);
132 $this->assertEquals($v['visibility'], 'Public Pages');
137 * @param int $version
139 * @dataProvider versionThreeAndFour
141 public function testGetGroupParamsWithGroupName($version) {
142 $this->_apiversion
= $version;
144 'name' => "Test Group 1",
146 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__
, __FILE__
);
147 $group = $group['values'];
149 foreach ($group as $v) {
150 $this->assertEquals($v['id'], $this->_groupID
);
151 $this->assertEquals($v['title'], 'New Test Group Created');
152 $this->assertEquals($v['description'], 'New Test Group Created');
153 $this->assertEquals($v['is_active'], 1);
154 $this->assertEquals($v['visibility'], 'Public Pages');
159 * @param int $version
161 * @dataProvider versionThreeAndFour
163 public function testGetGroupParamsWithReturnName($version) {
164 $this->_apiversion
= $version;
166 $params['id'] = $this->_groupID
;
167 $params['return.name'] = 1;
168 $group = $this->callAPISuccess('group', 'get', $params);
169 $this->assertEquals($group['values'][$this->_groupID
]['name'],
175 * @param int $version
177 * @dataProvider versionThreeAndFour
179 public function testGetGroupParamsWithGroupTitle($version) {
180 $this->_apiversion
= $version;
182 $params['title'] = 'New Test Group Created';
183 $group = $this->callAPISuccess('group', 'get', $params);
185 foreach ($group['values'] as $v) {
186 $this->assertEquals($v['id'], $this->_groupID
);
187 $this->assertEquals($v['name'], "Test Group 1");
188 $this->assertEquals($v['description'], 'New Test Group Created');
189 $this->assertEquals($v['is_active'], 1);
190 $this->assertEquals($v['visibility'], 'Public Pages');
195 * Test Group create with Group Type and Parent
198 public function testGroupCreateWithTypeAndParent() {
200 'name' => 'Test Group type',
201 'title' => 'Test Group Type',
202 'description' => 'Test Group with Group Type',
204 //check for empty parent
206 'visibility' => 'Public Pages',
207 'group_type' => [1, 2],
210 $result = $this->callAPISuccess('Group', 'create', $params);
211 $group = $result['values'][$result['id']];
212 $this->assertEquals($group['name'], "Test Group type");
213 $this->assertEquals($group['is_active'], 1);
214 $this->assertEquals($group['parents'], "");
215 $this->assertEquals($group['group_type'], $params['group_type']);
217 //Pass group_type param in checkbox format.
218 $params = array_merge($params, [
219 'name' => 'Test Checkbox Format',
220 'title' => 'Test Checkbox Format',
221 'group_type' => [2 => 1],
223 $result = $this->callAPISuccess('Group', 'create', $params);
224 $group = $result['values'][$result['id']];
225 $this->assertEquals($group['name'], "Test Checkbox Format");
226 $this->assertEquals($group['group_type'], array_keys($params['group_type']));
228 //assert single value for group_type and parent
229 $params = array_merge($params, [
230 'name' => 'Test Group 2',
231 'title' => 'Test Group 2',
233 'parents' => $result['id'],
236 $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
238 $this->assertEquals($group2['group_type'], [$params['group_type']]);
239 $this->assertEquals($params['parents'], $group2['parents']);
241 // Test array format for parents.
242 $params = array_merge($params, [
243 'name' => 'Test Group 3',
244 'title' => 'Test Group 3',
245 'parents' => [$result['id'], $group2['id']],
247 $group3 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
248 $parents = $this->callAPISuccess('Group', 'getvalue', ['return' => 'parents', 'id' => $group3['id']]);
250 $this->assertAPIArrayComparison("{$result['id']},{$group2['id']}", $parents);
252 $groupNesting = $this->callAPISuccess('GroupNesting', 'get', ['child_group_id' => $group3['id']]);
253 // 2 Group nesting entries - one for direct parent & one for grandparent.
254 $this->assertEquals(2, $groupNesting['count']);
255 $this->groupDelete($group2['id']);
256 $this->groupDelete($group3['id']);
260 * Test that an array of valid values works for group_type field.
263 public function testGroupTypeWithPseudoconstantArray() {
265 'name' => 'Test Group 2',
266 'title' => 'Test Group 2',
267 'group_type' => ['Mailing List', 'Access Control'],
270 $group = $this->callAPISuccess('Group', 'create', $params);
271 $groupType = $this->callAPISuccess('Group', 'getvalue', ['return' => 'group_type', 'id' => $group['id']]);
273 $this->assertAPIArrayComparison([2, 1], $groupType);
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']);