Merge pull request #14840 from MegaphoneJon/core-1130
[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 * @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 }