Merge pull request #1 from civicrm/master
[civicrm-core.git] / tests / phpunit / api / v3 / GroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for Group API - civicrm_group_*
30 *
31 * @package CiviCRM_APIv3
32 * @group headless
33 */
34 class api_v3_GroupTest extends CiviUnitTestCase {
35
36 protected $_groupID;
37
38 /**
39 * Set up for tests.
40 */
41 public function setUp() {
42 parent::setUp();
43 $this->_groupID = $this->groupCreate();
44 $config = CRM_Core_Config::singleton();
45 $config->userPermissionClass->permissions = [];
46 }
47
48 /**
49 * Clean up after test.
50 *
51 * @throws \Exception
52 */
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']);
58 parent::tearDown();
59 }
60
61 /**
62 * Test missing required title parameter results in an error.
63 *
64 * @param int $version
65 *
66 * @dataProvider versionThreeAndFour
67 */
68 public function testGroupCreateNoTitle($version) {
69 $this->_apiversion = $version;
70 $params = [
71 'name' => 'Test Group No title ',
72 'domain_id' => 1,
73 'description' => 'New Test Group Created',
74 'is_active' => 1,
75 'visibility' => 'Public Pages',
76 'group_type' => [
77 '1' => 1,
78 '2' => 1,
79 ],
80 ];
81
82 $this->callAPIFailure('group', 'create', $params, 'title');
83 }
84
85 /**
86 * @param int $version
87 *
88 * @dataProvider versionThreeAndFour
89 */
90 public function testGetGroupWithEmptyParams($version) {
91 $this->_apiversion = $version;
92 $group = $this->callAPISuccess('group', 'get', []);
93
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');
99 }
100
101 /**
102 * Test ability to get active, inactive and both.
103 *
104 * Default is active only.
105 *
106 * @param int $version
107 *
108 * @dataProvider versionThreeAndFour
109 */
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);
115 }
116
117 /**
118 * @param int $version
119 *
120 * @dataProvider versionThreeAndFour
121 */
122 public function testGetGroupParamsWithGroupId($version) {
123 $this->_apiversion = $version;
124 $params = ['id' => $this->_groupID];
125 $group = $this->callAPISuccess('group', 'get', $params);
126
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');
133 }
134 }
135
136 /**
137 * @param int $version
138 *
139 * @dataProvider versionThreeAndFour
140 */
141 public function testGetGroupParamsWithGroupName($version) {
142 $this->_apiversion = $version;
143 $params = [
144 'name' => "Test Group 1",
145 ];
146 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__);
147 $group = $group['values'];
148
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');
155 }
156 }
157
158 /**
159 * @param int $version
160 *
161 * @dataProvider versionThreeAndFour
162 */
163 public function testGetGroupParamsWithReturnName($version) {
164 $this->_apiversion = $version;
165 $params = [];
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'],
170 "Test Group 1"
171 );
172 }
173
174 /**
175 * @param int $version
176 *
177 * @dataProvider versionThreeAndFour
178 */
179 public function testGetGroupParamsWithGroupTitle($version) {
180 $this->_apiversion = $version;
181 $params = [];
182 $params['title'] = 'New Test Group Created';
183 $group = $this->callAPISuccess('group', 'get', $params);
184
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');
191 }
192 }
193
194 /**
195 * Test Group create with Group Type and Parent
196 * FIXME: Api4
197 */
198 public function testGroupCreateWithTypeAndParent() {
199 $params = [
200 'name' => 'Test Group type',
201 'title' => 'Test Group Type',
202 'description' => 'Test Group with Group Type',
203 'is_active' => 1,
204 //check for empty parent
205 'parents' => "",
206 'visibility' => 'Public Pages',
207 'group_type' => [1, 2],
208 ];
209
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']);
216
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],
222 ]);
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']));
227
228 //assert single value for group_type and parent
229 $params = array_merge($params, [
230 'name' => 'Test Group 2',
231 'title' => 'Test Group 2',
232 'group_type' => 2,
233 'parents' => $result['id'],
234 'sequential' => 1,
235 ]);
236 $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
237
238 $this->assertEquals($group2['group_type'], [$params['group_type']]);
239 $this->assertEquals($params['parents'], $group2['parents']);
240
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']],
246 ]);
247 $group3 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
248 $parents = $this->callAPISuccess('Group', 'getvalue', ['return' => 'parents', 'id' => $group3['id']]);
249
250 $this->assertAPIArrayComparison("{$result['id']},{$group2['id']}", $parents);
251
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']);
257 }
258
259 /**
260 * Test that an array of valid values works for group_type field.
261 * FIXME: Api4
262 */
263 public function testGroupTypeWithPseudoconstantArray() {
264 $params = [
265 'name' => 'Test Group 2',
266 'title' => 'Test Group 2',
267 'group_type' => ['Mailing List', 'Access Control'],
268 'sequential' => 1,
269 ];
270 $group = $this->callAPISuccess('Group', 'create', $params);
271 $groupType = $this->callAPISuccess('Group', 'getvalue', ['return' => 'group_type', 'id' => $group['id']]);
272
273 $this->assertAPIArrayComparison([2, 1], $groupType);
274 }
275
276 /**
277 * Test / demonstrate behaviour when attempting to filter by group_type.
278 *
279 * Per https://lab.civicrm.org/dev/core/issues/1321 the group_type filter is deceptive
280 * - it only filters on exact match not 'is one of'.
281 *
282 * @throws \CRM_Core_Exception
283 */
284 public function testGroupWithGroupTypeFilter() {
285 $this->groupCreate(['group_type' => ['Access Control'], 'name' => 'access_list', 'title' => 'access list']);
286 $this->groupCreate(['group_type' => ['Mailing List'], 'name' => 'mailing_list', 'title' => 'mailing list']);
287 $this->groupCreate(['group_type' => ['Access Control', 'Mailing List'], 'name' => 'group', 'title' => 'group']);
288 $group = $this->callAPISuccessGetSingle('Group', ['return' => 'id,title,group_type', 'group_type' => 'Mailing List']);
289 $this->assertEquals('mailing list', $group['title']);
290 }
291
292 /**
293 * @param int $version
294 *
295 * @dataProvider versionThreeAndFour
296 */
297 public function testGetNonExistingGroup($version) {
298 $this->_apiversion = $version;
299 $params = [];
300 $params['title'] = 'No such group Exist';
301 $group = $this->callAPISuccess('group', 'get', $params);
302 $this->assertEquals(0, $group['count']);
303 }
304
305 /**
306 * @param int $version
307 *
308 * @dataProvider versionThreeAndFour
309 */
310 public function testgroupdeleteParamsnoId($version) {
311 $this->_apiversion = $version;
312 $group = $this->callAPIFailure('group', 'delete', []);
313 }
314
315 /**
316 * @param int $version
317 *
318 * @dataProvider versionThreeAndFour
319 */
320 public function testgetfields($version) {
321 $this->_apiversion = $version;
322 $description = "Demonstrate use of getfields to interrogate api.";
323 $params = ['action' => 'create'];
324 $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description);
325 $this->assertEquals('is_active', $result['values']['is_active']['name']);
326 }
327
328 public function testIllegalParentsParams() {
329 $params = [
330 'title' => 'Test illegal Group',
331 'domain_id' => 1,
332 'description' => 'Testing illegal Parents params',
333 'is_active' => 1,
334 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)",
335 ];
336 $this->callAPIFailure('group', 'create', $params);
337 unset($params['parents']);
338 $this->callAPISuccess('group', 'create', $params);
339 $group1 = $this->callAPISuccess('group', 'get', [
340 'title' => 'Test illegal Group',
341 'parents' => ['IS NOT NULL' => 1],
342 ]);
343 $this->assertEquals(0, $group1['count']);
344 $params['title'] = 'Test illegal Group 2';
345 $params['parents'] = [];
346 $params['parents'][$this->_groupID] = 'test Group';
347 $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test";
348 $this->callAPIFailure('group', 'create', $params);
349 unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]);
350 $this->callAPIFailure('group', 'create', $params, '\'test Group\' is not a valid option for field parents');
351 }
352
353 /**
354 * Test that ACLs are applied to group.get calls.
355 * FIXME: Api4
356 */
357 public function testGroupGetACLs() {
358 $this->createLoggedInUser();
359 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
360 $this->callAPISuccessGetCount('Group', ['check_permissions' => 1], 0);
361 $this->hookClass->setHook('civicrm_aclGroup', [$this, 'aclGroupAllGroups']);
362 unset(Civi::$statics['CRM_ACL_API']['group_permission']);
363 $this->callAPISuccessGetCount('Group', ['check_permissions' => 1], 1);
364 }
365
366 /**
367 * Implement hook to restrict to test group 1.
368 *
369 * @param string $type
370 * @param int $contactID
371 * @param string $tableName
372 * @param array $allGroups
373 * @param array $ids
374 */
375 public function aclGroupAllGroups($type, $contactID, $tableName, $allGroups, &$ids) {
376 $group = $this->callAPISuccess('Group', 'get', ['name' => 'Test Group 1']);
377 $ids = array_keys($group['values']);
378 }
379
380 }