Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
15a4309a | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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() { |
6a488035 | 52 | $this->groupDelete($this->_groupID); |
6d054a8e | 53 | CRM_Utils_Hook::singleton()->reset(); |
54 | $config = CRM_Core_Config::singleton(); | |
55 | unset($config->userPermissionClass->permissions); | |
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 | ||
00be9182 | 88 | public function testGetGroupParamsWithGroupId() { |
b26e776b | 89 | $params = array('id' => $this->_groupID); |
90 | $group = $this->callAPISuccess('group', 'get', $params); | |
6a488035 TO |
91 | |
92 | foreach ($group['values'] as $v) { | |
5667b84b | 93 | $this->assertEquals($v['name'], "Test Group 1"); |
6a488035 TO |
94 | $this->assertEquals($v['title'], 'New Test Group Created'); |
95 | $this->assertEquals($v['description'], 'New Test Group Created'); | |
96 | $this->assertEquals($v['is_active'], 1); | |
97 | $this->assertEquals($v['visibility'], 'Public Pages'); | |
98 | } | |
99 | } | |
100 | ||
00be9182 | 101 | public function testGetGroupParamsWithGroupName() { |
b26e776b | 102 | $params = array( |
5667b84b | 103 | 'name' => "Test Group 1", |
b26e776b | 104 | ); |
105 | $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__); | |
6a488035 TO |
106 | $group = $group['values']; |
107 | ||
108 | foreach ($group as $v) { | |
109 | $this->assertEquals($v['id'], $this->_groupID); | |
110 | $this->assertEquals($v['title'], 'New Test Group Created'); | |
111 | $this->assertEquals($v['description'], 'New Test Group Created'); | |
112 | $this->assertEquals($v['is_active'], 1); | |
113 | $this->assertEquals($v['visibility'], 'Public Pages'); | |
114 | } | |
115 | } | |
116 | ||
00be9182 | 117 | public function testGetGroupParamsWithReturnName() { |
b26e776b | 118 | $params = array(); |
6a488035 TO |
119 | $params['id'] = $this->_groupID; |
120 | $params['return.name'] = 1; | |
b26e776b | 121 | $group = $this->callAPISuccess('group', 'get', $params); |
6a488035 | 122 | $this->assertEquals($group['values'][$this->_groupID]['name'], |
5667b84b | 123 | "Test Group 1" |
6a488035 TO |
124 | ); |
125 | } | |
126 | ||
00be9182 | 127 | public function testGetGroupParamsWithGroupTitle() { |
5667b84b | 128 | $params = array(); |
6a488035 | 129 | $params['title'] = 'New Test Group Created'; |
5667b84b | 130 | $group = $this->callAPISuccess('group', 'get', $params); |
6a488035 TO |
131 | |
132 | foreach ($group['values'] as $v) { | |
133 | $this->assertEquals($v['id'], $this->_groupID); | |
5667b84b | 134 | $this->assertEquals($v['name'], "Test Group 1"); |
6a488035 TO |
135 | $this->assertEquals($v['description'], 'New Test Group Created'); |
136 | $this->assertEquals($v['is_active'], 1); | |
137 | $this->assertEquals($v['visibility'], 'Public Pages'); | |
138 | } | |
139 | } | |
140 | ||
140609dc | 141 | /** |
ec7846f5 | 142 | * Test Group create with Group Type and Parent |
140609dc | 143 | */ |
ec7846f5 | 144 | public function testGroupCreateWithTypeAndParent() { |
140609dc | 145 | $params = array( |
146 | 'name' => 'Test Group type', | |
147 | 'title' => 'Test Group Type', | |
148 | 'description' => 'Test Group with Group Type', | |
149 | 'is_active' => 1, | |
ec7846f5 | 150 | //check for empty parent |
151 | 'parents' => "", | |
140609dc | 152 | 'visibility' => 'Public Pages', |
30208fab | 153 | 'group_type' => [1, 2], |
140609dc | 154 | ); |
155 | ||
156 | $result = $this->callAPISuccess('Group', 'create', $params); | |
157 | $group = $result['values'][$result['id']]; | |
158 | $this->assertEquals($group['name'], "Test Group type"); | |
159 | $this->assertEquals($group['is_active'], 1); | |
ec7846f5 | 160 | $this->assertEquals($group['parents'], ""); |
140609dc | 161 | $this->assertEquals($group['group_type'], $params['group_type']); |
140609dc | 162 | |
fb413e82 | 163 | //Pass group_type param in checkbox format. |
164 | $params = array_merge($params, array( | |
165 | 'name' => 'Test Checkbox Format', | |
166 | 'title' => 'Test Checkbox Format', | |
167 | 'group_type' => array(2 => 1), | |
168 | ) | |
169 | ); | |
170 | $result = $this->callAPISuccess('Group', 'create', $params); | |
171 | $group = $result['values'][$result['id']]; | |
172 | $this->assertEquals($group['name'], "Test Checkbox Format"); | |
173 | $this->assertEquals($group['group_type'], array_keys($params['group_type'])); | |
174 | ||
ec7846f5 | 175 | //assert single value for group_type and parent |
176 | $params = array_merge($params, array( | |
177 | 'name' => 'Test Group 2', | |
178 | 'title' => 'Test Group 2', | |
179 | 'group_type' => 2, | |
180 | 'parents' => $result['id'], | |
30208fab | 181 | 'sequential' => 1, |
ec7846f5 | 182 | ) |
183 | ); | |
30208fab | 184 | $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0]; |
185 | ||
186 | $this->assertEquals($group2['group_type'], array($params['group_type'])); | |
187 | $this->assertEquals($params['parents'], $group2['parents']); | |
188 | ||
189 | // Test array format for parents. | |
190 | $params = array_merge($params, array( | |
191 | 'name' => 'Test Group 3', | |
192 | 'title' => 'Test Group 3', | |
193 | 'parents' => [$result['id'], $group2['id']], | |
194 | ) | |
195 | ); | |
196 | $group3 = $this->callAPISuccess('Group', 'create', $params)['values'][0]; | |
197 | $parents = $this->callAPISuccess('Group', 'getvalue', ['return' => 'parents', 'id' => $group3['id']]); | |
198 | ||
199 | $this->assertAPIArrayComparison("{$result['id']},{$group2['id']}", $parents); | |
200 | ||
201 | $groupNesting = $this->callAPISuccess('GroupNesting', 'get', ['child_group_id' => $group3['id']]); | |
202 | // 2 Group nesting entries - one for direct parent & one for grandparent. | |
203 | $this->assertEquals(2, $groupNesting['count']); | |
204 | $this->groupDelete($group2['id']); | |
205 | $this->groupDelete($group3['id']); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Test that an array of valid values works for group_type field. | |
210 | */ | |
211 | public function testGroupTypeWithPseudoconstantArray() { | |
212 | $params = [ | |
213 | 'name' => 'Test Group 2', | |
214 | 'title' => 'Test Group 2', | |
215 | 'group_type' => ['Mailing List', 'Access Control'], | |
216 | 'sequential' => 1, | |
217 | ]; | |
218 | $group = $this->callAPISuccess('Group', 'create', $params); | |
219 | $groupType = $this->callAPISuccess('Group', 'getvalue', ['return' => 'group_type', 'id' => $group['id']]); | |
220 | ||
221 | $this->assertAPIArrayComparison([2, 1], $groupType); | |
140609dc | 222 | } |
223 | ||
00be9182 | 224 | public function testGetNonExistingGroup() { |
5667b84b | 225 | $params = array(); |
6a488035 | 226 | $params['title'] = 'No such group Exist'; |
5667b84b | 227 | $group = $this->callAPISuccess('group', 'get', $params); |
d0e1eff2 | 228 | $this->assertEquals(0, $group['count']); |
6a488035 TO |
229 | } |
230 | ||
00be9182 | 231 | public function testgroupdeleteParamsnoId() { |
b26e776b | 232 | $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id'); |
6a488035 TO |
233 | } |
234 | ||
00be9182 | 235 | public function testgetfields() { |
5c49fee0 | 236 | $description = "Demonstrate use of getfields to interrogate api."; |
5667b84b | 237 | $params = array('action' => 'create'); |
a828d7b8 | 238 | $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description); |
6a488035 TO |
239 | $this->assertEquals(1, $result['values']['is_active']['api.default']); |
240 | } | |
96025800 | 241 | |
1514f450 SL |
242 | public function testIllegalParentsParams() { |
243 | $params = array( | |
244 | 'title' => 'Test illegal Group', | |
245 | 'domain_id' => 1, | |
246 | 'description' => 'Testing illegal Parents params', | |
247 | 'is_active' => 1, | |
248 | 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)", | |
249 | ); | |
52202a20 SL |
250 | $this->callAPIFailure('group', 'create', $params); |
251 | unset($params['parents']); | |
1514f450 SL |
252 | $this->callAPISuccess('group', 'create', $params); |
253 | $group1 = $this->callAPISuccess('group', 'get', array( | |
254 | 'title' => 'Test illegal Group', | |
255 | 'parents' => array('IS NOT NULL' => 1), | |
256 | )); | |
257 | $this->assertEquals(0, $group1['count']); | |
258 | $params['title'] = 'Test illegal Group 2'; | |
259 | $params['parents'] = array(); | |
260 | $params['parents'][$this->_groupID] = 'test Group'; | |
261 | $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test"; | |
6d054a8e | 262 | $this->callAPIFailure('group', 'create', $params); |
52202a20 | 263 | unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]); |
1678a63b | 264 | $this->callAPIFailure('group', 'create', $params, '\'test Group\' is not a valid option for field parents'); |
1514f450 SL |
265 | } |
266 | ||
6d054a8e | 267 | /** |
268 | * Test that ACLs are applied to group.get calls. | |
269 | */ | |
270 | public function testGroupGetACLs() { | |
271 | $this->createLoggedInUser(); | |
272 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
273 | $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 0); | |
274 | $this->hookClass->setHook('civicrm_aclGroup', array($this, 'aclGroupAllGroups')); | |
275 | unset(Civi::$statics['CRM_ACL_API']['group_permission']); | |
276 | $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 1); | |
277 | } | |
278 | ||
279 | /** | |
280 | * Implement hook to restrict to test group 1. | |
281 | * | |
282 | * @param string $type | |
283 | * @param int $contactID | |
284 | * @param string $tableName | |
285 | * @param array $allGroups | |
286 | * @param array $ids | |
287 | */ | |
288 | public function aclGroupAllGroups($type, $contactID, $tableName, $allGroups, &$ids) { | |
289 | $group = $this->callAPISuccess('Group', 'get', array('name' => 'Test Group 1')); | |
290 | $ids = array_keys($group['values']); | |
291 | } | |
292 | ||
6a488035 | 293 | } |