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', |
153 | 'group_type' => array(1, 2), | |
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'], | |
181 | ) | |
182 | ); | |
140609dc | 183 | $result = $this->callAPISuccess('Group', 'create', $params); |
184 | $group = $result["values"][$result['id']]; | |
185 | $this->assertEquals($group['group_type'], array($params['group_type'])); | |
ec7846f5 | 186 | $this->assertEquals($group['parents'], $params['parents']); |
140609dc | 187 | } |
188 | ||
00be9182 | 189 | public function testGetNonExistingGroup() { |
5667b84b | 190 | $params = array(); |
6a488035 | 191 | $params['title'] = 'No such group Exist'; |
5667b84b | 192 | $group = $this->callAPISuccess('group', 'get', $params); |
d0e1eff2 | 193 | $this->assertEquals(0, $group['count']); |
6a488035 TO |
194 | } |
195 | ||
00be9182 | 196 | public function testgroupdeleteParamsnoId() { |
b26e776b | 197 | $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id'); |
6a488035 TO |
198 | } |
199 | ||
00be9182 | 200 | public function testgetfields() { |
5c49fee0 | 201 | $description = "Demonstrate use of getfields to interrogate api."; |
5667b84b | 202 | $params = array('action' => 'create'); |
a828d7b8 | 203 | $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description); |
6a488035 TO |
204 | $this->assertEquals(1, $result['values']['is_active']['api.default']); |
205 | } | |
96025800 | 206 | |
1514f450 SL |
207 | public function testIllegalParentsParams() { |
208 | $params = array( | |
209 | 'title' => 'Test illegal Group', | |
210 | 'domain_id' => 1, | |
211 | 'description' => 'Testing illegal Parents params', | |
212 | 'is_active' => 1, | |
213 | 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)", | |
214 | ); | |
52202a20 SL |
215 | $this->callAPIFailure('group', 'create', $params); |
216 | unset($params['parents']); | |
1514f450 SL |
217 | $this->callAPISuccess('group', 'create', $params); |
218 | $group1 = $this->callAPISuccess('group', 'get', array( | |
219 | 'title' => 'Test illegal Group', | |
220 | 'parents' => array('IS NOT NULL' => 1), | |
221 | )); | |
222 | $this->assertEquals(0, $group1['count']); | |
223 | $params['title'] = 'Test illegal Group 2'; | |
224 | $params['parents'] = array(); | |
225 | $params['parents'][$this->_groupID] = 'test Group'; | |
226 | $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test"; | |
6d054a8e | 227 | $this->callAPIFailure('group', 'create', $params); |
52202a20 | 228 | unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]); |
1514f450 SL |
229 | $group2 = $this->callAPISuccess('group', 'create', $params); |
230 | $this->assertEquals(count($group2['values'][$group2['id']]['parents']), 1); | |
231 | } | |
232 | ||
6d054a8e | 233 | /** |
234 | * Test that ACLs are applied to group.get calls. | |
235 | */ | |
236 | public function testGroupGetACLs() { | |
237 | $this->createLoggedInUser(); | |
238 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
239 | $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 0); | |
240 | $this->hookClass->setHook('civicrm_aclGroup', array($this, 'aclGroupAllGroups')); | |
241 | unset(Civi::$statics['CRM_ACL_API']['group_permission']); | |
242 | $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 1); | |
243 | } | |
244 | ||
245 | /** | |
246 | * Implement hook to restrict to test group 1. | |
247 | * | |
248 | * @param string $type | |
249 | * @param int $contactID | |
250 | * @param string $tableName | |
251 | * @param array $allGroups | |
252 | * @param array $ids | |
253 | */ | |
254 | public function aclGroupAllGroups($type, $contactID, $tableName, $allGroups, &$ids) { | |
255 | $group = $this->callAPISuccess('Group', 'get', array('name' => 'Test Group 1')); | |
256 | $ids = array_keys($group['values']); | |
257 | } | |
258 | ||
6a488035 | 259 | } |