Merge pull request #13926 from pradpnayak/NoticeErrorProfile
[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 protected $_apiversion = 3;
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 = array();
46 }
47
48 /**
49 * Clean up after test.
50 */
51 public function tearDown() {
52 CRM_Utils_Hook::singleton()->reset();
53 $config = CRM_Core_Config::singleton();
54 unset($config->userPermissionClass->permissions);
55 $this->quickCleanup(['civicrm_group', 'civicrm_group_contact']);
56 }
57
58 /**
59 * Test missing required title parameter results in an error.
60 */
61 public function testGroupCreateNoTitle() {
62 $params = array(
63 'name' => 'Test Group No title ',
64 'domain_id' => 1,
65 'description' => 'New Test Group Created',
66 'is_active' => 1,
67 'visibility' => 'Public Pages',
68 'group_type' => array(
69 '1' => 1,
70 '2' => 1,
71 ),
72 );
73
74 $this->callAPIFailure('group', 'create', $params, 'Mandatory key(s) missing from params array: title');
75 }
76
77
78 public function testGetGroupWithEmptyParams() {
79 $group = $this->callAPISuccess('group', 'get', array());
80
81 $group = $group["values"];
82 $this->assertNotNull(count($group));
83 $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1");
84 $this->assertEquals($group[$this->_groupID]['is_active'], 1);
85 $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages');
86 }
87
88 /**
89 * Test ability to get active, inactive and both.
90 *
91 * Default is active only.
92 */
93 public function testGetGroupActiveAndInactive() {
94 $this->groupCreate(['is_active' => 0, 'name' => 'group_2', 'title' => 2]);
95 $group1 = $this->callAPISuccessGetSingle('Group', ['is_active' => 1]);
96 $this->callAPISuccessGetCount('Group', [], 2);
97 }
98
99 public function testGetGroupParamsWithGroupId() {
100 $params = array('id' => $this->_groupID);
101 $group = $this->callAPISuccess('group', 'get', $params);
102
103 foreach ($group['values'] as $v) {
104 $this->assertEquals($v['name'], "Test Group 1");
105 $this->assertEquals($v['title'], 'New Test Group Created');
106 $this->assertEquals($v['description'], 'New Test Group Created');
107 $this->assertEquals($v['is_active'], 1);
108 $this->assertEquals($v['visibility'], 'Public Pages');
109 }
110 }
111
112 public function testGetGroupParamsWithGroupName() {
113 $params = array(
114 'name' => "Test Group 1",
115 );
116 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__);
117 $group = $group['values'];
118
119 foreach ($group as $v) {
120 $this->assertEquals($v['id'], $this->_groupID);
121 $this->assertEquals($v['title'], 'New Test Group Created');
122 $this->assertEquals($v['description'], 'New Test Group Created');
123 $this->assertEquals($v['is_active'], 1);
124 $this->assertEquals($v['visibility'], 'Public Pages');
125 }
126 }
127
128 public function testGetGroupParamsWithReturnName() {
129 $params = array();
130 $params['id'] = $this->_groupID;
131 $params['return.name'] = 1;
132 $group = $this->callAPISuccess('group', 'get', $params);
133 $this->assertEquals($group['values'][$this->_groupID]['name'],
134 "Test Group 1"
135 );
136 }
137
138 public function testGetGroupParamsWithGroupTitle() {
139 $params = array();
140 $params['title'] = 'New Test Group Created';
141 $group = $this->callAPISuccess('group', 'get', $params);
142
143 foreach ($group['values'] as $v) {
144 $this->assertEquals($v['id'], $this->_groupID);
145 $this->assertEquals($v['name'], "Test Group 1");
146 $this->assertEquals($v['description'], 'New Test Group Created');
147 $this->assertEquals($v['is_active'], 1);
148 $this->assertEquals($v['visibility'], 'Public Pages');
149 }
150 }
151
152 /**
153 * Test Group create with Group Type and Parent
154 */
155 public function testGroupCreateWithTypeAndParent() {
156 $params = array(
157 'name' => 'Test Group type',
158 'title' => 'Test Group Type',
159 'description' => 'Test Group with Group Type',
160 'is_active' => 1,
161 //check for empty parent
162 'parents' => "",
163 'visibility' => 'Public Pages',
164 'group_type' => [1, 2],
165 );
166
167 $result = $this->callAPISuccess('Group', 'create', $params);
168 $group = $result['values'][$result['id']];
169 $this->assertEquals($group['name'], "Test Group type");
170 $this->assertEquals($group['is_active'], 1);
171 $this->assertEquals($group['parents'], "");
172 $this->assertEquals($group['group_type'], $params['group_type']);
173
174 //Pass group_type param in checkbox format.
175 $params = array_merge($params, array(
176 'name' => 'Test Checkbox Format',
177 'title' => 'Test Checkbox Format',
178 'group_type' => array(2 => 1),
179 )
180 );
181 $result = $this->callAPISuccess('Group', 'create', $params);
182 $group = $result['values'][$result['id']];
183 $this->assertEquals($group['name'], "Test Checkbox Format");
184 $this->assertEquals($group['group_type'], array_keys($params['group_type']));
185
186 //assert single value for group_type and parent
187 $params = array_merge($params, array(
188 'name' => 'Test Group 2',
189 'title' => 'Test Group 2',
190 'group_type' => 2,
191 'parents' => $result['id'],
192 'sequential' => 1,
193 )
194 );
195 $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
196
197 $this->assertEquals($group2['group_type'], array($params['group_type']));
198 $this->assertEquals($params['parents'], $group2['parents']);
199
200 // Test array format for parents.
201 $params = array_merge($params, array(
202 'name' => 'Test Group 3',
203 'title' => 'Test Group 3',
204 'parents' => [$result['id'], $group2['id']],
205 )
206 );
207 $group3 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
208 $parents = $this->callAPISuccess('Group', 'getvalue', ['return' => 'parents', 'id' => $group3['id']]);
209
210 $this->assertAPIArrayComparison("{$result['id']},{$group2['id']}", $parents);
211
212 $groupNesting = $this->callAPISuccess('GroupNesting', 'get', ['child_group_id' => $group3['id']]);
213 // 2 Group nesting entries - one for direct parent & one for grandparent.
214 $this->assertEquals(2, $groupNesting['count']);
215 $this->groupDelete($group2['id']);
216 $this->groupDelete($group3['id']);
217 }
218
219 /**
220 * Test that an array of valid values works for group_type field.
221 */
222 public function testGroupTypeWithPseudoconstantArray() {
223 $params = [
224 'name' => 'Test Group 2',
225 'title' => 'Test Group 2',
226 'group_type' => ['Mailing List', 'Access Control'],
227 'sequential' => 1,
228 ];
229 $group = $this->callAPISuccess('Group', 'create', $params);
230 $groupType = $this->callAPISuccess('Group', 'getvalue', ['return' => 'group_type', 'id' => $group['id']]);
231
232 $this->assertAPIArrayComparison([2, 1], $groupType);
233 }
234
235 public function testGetNonExistingGroup() {
236 $params = array();
237 $params['title'] = 'No such group Exist';
238 $group = $this->callAPISuccess('group', 'get', $params);
239 $this->assertEquals(0, $group['count']);
240 }
241
242 public function testgroupdeleteParamsnoId() {
243 $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id');
244 }
245
246 public function testgetfields() {
247 $description = "Demonstrate use of getfields to interrogate api.";
248 $params = array('action' => 'create');
249 $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description);
250 $this->assertEquals(1, $result['values']['is_active']['api.default']);
251 }
252
253 public function testIllegalParentsParams() {
254 $params = array(
255 'title' => 'Test illegal Group',
256 'domain_id' => 1,
257 'description' => 'Testing illegal Parents params',
258 'is_active' => 1,
259 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)",
260 );
261 $this->callAPIFailure('group', 'create', $params);
262 unset($params['parents']);
263 $this->callAPISuccess('group', 'create', $params);
264 $group1 = $this->callAPISuccess('group', 'get', array(
265 'title' => 'Test illegal Group',
266 'parents' => array('IS NOT NULL' => 1),
267 ));
268 $this->assertEquals(0, $group1['count']);
269 $params['title'] = 'Test illegal Group 2';
270 $params['parents'] = array();
271 $params['parents'][$this->_groupID] = 'test Group';
272 $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test";
273 $this->callAPIFailure('group', 'create', $params);
274 unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]);
275 $this->callAPIFailure('group', 'create', $params, '\'test Group\' is not a valid option for field parents');
276 }
277
278 /**
279 * Test that ACLs are applied to group.get calls.
280 */
281 public function testGroupGetACLs() {
282 $this->createLoggedInUser();
283 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
284 $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 0);
285 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'aclGroupAllGroups'));
286 unset(Civi::$statics['CRM_ACL_API']['group_permission']);
287 $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 1);
288 }
289
290 /**
291 * Implement hook to restrict to test group 1.
292 *
293 * @param string $type
294 * @param int $contactID
295 * @param string $tableName
296 * @param array $allGroups
297 * @param array $ids
298 */
299 public function aclGroupAllGroups($type, $contactID, $tableName, $allGroups, &$ids) {
300 $group = $this->callAPISuccess('Group', 'get', array('name' => 'Test Group 1'));
301 $ids = array_keys($group['values']);
302 }
303
304 }