Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Test class for Group API - civicrm_group_* | |
30 | * | |
6c6e6187 | 31 | * @package CiviCRM_APIv3 |
acb109b7 | 32 | * @group headless |
6a488035 | 33 | */ |
6a488035 | 34 | class api_v3_GroupTest extends CiviUnitTestCase { |
6d054a8e | 35 | protected $_apiversion = 3; |
6a488035 | 36 | protected $_groupID; |
6a488035 | 37 | |
6d054a8e | 38 | /** |
39 | * Set up for tests. | |
40 | */ | |
00be9182 | 41 | public function setUp() { |
6a488035 | 42 | parent::setUp(); |
fadb804f | 43 | $this->_groupID = $this->groupCreate(); |
6d054a8e | 44 | $config = CRM_Core_Config::singleton(); |
45 | $config->userPermissionClass->permissions = array(); | |
6a488035 TO |
46 | } |
47 | ||
6d054a8e | 48 | /** |
49 | * Clean up after test. | |
50 | */ | |
00be9182 | 51 | public function tearDown() { |
6d054a8e | 52 | CRM_Utils_Hook::singleton()->reset(); |
53 | $config = CRM_Core_Config::singleton(); | |
54 | unset($config->userPermissionClass->permissions); | |
a3b559ee | 55 | $this->quickCleanup(['civicrm_group', 'civicrm_group_contact']); |
6a488035 TO |
56 | } |
57 | ||
6d054a8e | 58 | /** |
59 | * Test missing required title parameter results in an error. | |
60 | */ | |
61 | public function testGroupCreateNoTitle() { | |
6a488035 TO |
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 | ||
6d054a8e | 74 | $this->callAPIFailure('group', 'create', $params, 'Mandatory key(s) missing from params array: title'); |
6a488035 TO |
75 | } |
76 | ||
6a488035 | 77 | |
00be9182 | 78 | public function testGetGroupWithEmptyParams() { |
6d054a8e | 79 | $group = $this->callAPISuccess('group', 'get', array()); |
6a488035 TO |
80 | |
81 | $group = $group["values"]; | |
82 | $this->assertNotNull(count($group)); | |
5667b84b | 83 | $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1"); |
6a488035 TO |
84 | $this->assertEquals($group[$this->_groupID]['is_active'], 1); |
85 | $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages'); | |
86 | } | |
87 | ||
a3b559ee | 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 | ||
00be9182 | 99 | public function testGetGroupParamsWithGroupId() { |
b26e776b | 100 | $params = array('id' => $this->_groupID); |
101 | $group = $this->callAPISuccess('group', 'get', $params); | |
6a488035 TO |
102 | |
103 | foreach ($group['values'] as $v) { | |
5667b84b | 104 | $this->assertEquals($v['name'], "Test Group 1"); |
6a488035 TO |
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 | ||
00be9182 | 112 | public function testGetGroupParamsWithGroupName() { |
b26e776b | 113 | $params = array( |
5667b84b | 114 | 'name' => "Test Group 1", |
b26e776b | 115 | ); |
116 | $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__); | |
6a488035 TO |
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 | ||
00be9182 | 128 | public function testGetGroupParamsWithReturnName() { |
b26e776b | 129 | $params = array(); |
6a488035 TO |
130 | $params['id'] = $this->_groupID; |
131 | $params['return.name'] = 1; | |
b26e776b | 132 | $group = $this->callAPISuccess('group', 'get', $params); |
6a488035 | 133 | $this->assertEquals($group['values'][$this->_groupID]['name'], |
5667b84b | 134 | "Test Group 1" |
6a488035 TO |
135 | ); |
136 | } | |
137 | ||
00be9182 | 138 | public function testGetGroupParamsWithGroupTitle() { |
5667b84b | 139 | $params = array(); |
6a488035 | 140 | $params['title'] = 'New Test Group Created'; |
5667b84b | 141 | $group = $this->callAPISuccess('group', 'get', $params); |
6a488035 TO |
142 | |
143 | foreach ($group['values'] as $v) { | |
144 | $this->assertEquals($v['id'], $this->_groupID); | |
5667b84b | 145 | $this->assertEquals($v['name'], "Test Group 1"); |
6a488035 TO |
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 | ||
140609dc | 152 | /** |
ec7846f5 | 153 | * Test Group create with Group Type and Parent |
140609dc | 154 | */ |
ec7846f5 | 155 | public function testGroupCreateWithTypeAndParent() { |
140609dc | 156 | $params = array( |
157 | 'name' => 'Test Group type', | |
158 | 'title' => 'Test Group Type', | |
159 | 'description' => 'Test Group with Group Type', | |
160 | 'is_active' => 1, | |
ec7846f5 | 161 | //check for empty parent |
162 | 'parents' => "", | |
140609dc | 163 | 'visibility' => 'Public Pages', |
30208fab | 164 | 'group_type' => [1, 2], |
140609dc | 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); | |
ec7846f5 | 171 | $this->assertEquals($group['parents'], ""); |
140609dc | 172 | $this->assertEquals($group['group_type'], $params['group_type']); |
140609dc | 173 | |
fb413e82 | 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 | ||
ec7846f5 | 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'], | |
30208fab | 192 | 'sequential' => 1, |
ec7846f5 | 193 | ) |
194 | ); | |
30208fab | 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); | |
140609dc | 233 | } |
234 | ||
00be9182 | 235 | public function testGetNonExistingGroup() { |
5667b84b | 236 | $params = array(); |
6a488035 | 237 | $params['title'] = 'No such group Exist'; |
5667b84b | 238 | $group = $this->callAPISuccess('group', 'get', $params); |
d0e1eff2 | 239 | $this->assertEquals(0, $group['count']); |
6a488035 TO |
240 | } |
241 | ||
00be9182 | 242 | public function testgroupdeleteParamsnoId() { |
b26e776b | 243 | $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id'); |
6a488035 TO |
244 | } |
245 | ||
00be9182 | 246 | public function testgetfields() { |
5c49fee0 | 247 | $description = "Demonstrate use of getfields to interrogate api."; |
5667b84b | 248 | $params = array('action' => 'create'); |
a828d7b8 | 249 | $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description); |
6a488035 TO |
250 | $this->assertEquals(1, $result['values']['is_active']['api.default']); |
251 | } | |
96025800 | 252 | |
1514f450 SL |
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 | ); | |
52202a20 SL |
261 | $this->callAPIFailure('group', 'create', $params); |
262 | unset($params['parents']); | |
1514f450 SL |
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"; | |
6d054a8e | 273 | $this->callAPIFailure('group', 'create', $params); |
52202a20 | 274 | unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]); |
1678a63b | 275 | $this->callAPIFailure('group', 'create', $params, '\'test Group\' is not a valid option for field parents'); |
1514f450 SL |
276 | } |
277 | ||
6d054a8e | 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 | ||
6a488035 | 304 | } |