CRM-19246 acl test for group.get api
[civicrm-core.git] / tests / phpunit / api / v3 / GroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 $this->groupDelete($this->_groupID);
53 CRM_Utils_Hook::singleton()->reset();
54 $config = CRM_Core_Config::singleton();
55 unset($config->userPermissionClass->permissions);
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 public function testGetGroupParamsWithGroupId() {
89 $params = array('id' => $this->_groupID);
90 $group = $this->callAPISuccess('group', 'get', $params);
91
92 foreach ($group['values'] as $v) {
93 $this->assertEquals($v['name'], "Test Group 1");
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
101 public function testGetGroupParamsWithGroupName() {
102 $params = array(
103 'name' => "Test Group 1",
104 );
105 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__);
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
117 public function testGetGroupParamsWithReturnName() {
118 $params = array();
119 $params['id'] = $this->_groupID;
120 $params['return.name'] = 1;
121 $group = $this->callAPISuccess('group', 'get', $params);
122 $this->assertEquals($group['values'][$this->_groupID]['name'],
123 "Test Group 1"
124 );
125 }
126
127 public function testGetGroupParamsWithGroupTitle() {
128 $params = array();
129 $params['title'] = 'New Test Group Created';
130 $group = $this->callAPISuccess('group', 'get', $params);
131
132 foreach ($group['values'] as $v) {
133 $this->assertEquals($v['id'], $this->_groupID);
134 $this->assertEquals($v['name'], "Test Group 1");
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
141 /**
142 * Test Group create with Group Type and Parent
143 */
144 public function testGroupCreateWithTypeAndParent() {
145 $params = array(
146 'name' => 'Test Group type',
147 'title' => 'Test Group Type',
148 'description' => 'Test Group with Group Type',
149 'is_active' => 1,
150 //check for empty parent
151 'parents' => "",
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);
160 $this->assertEquals($group['parents'], "");
161 $this->assertEquals($group['group_type'], $params['group_type']);
162
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
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 );
183 $result = $this->callAPISuccess('Group', 'create', $params);
184 $group = $result["values"][$result['id']];
185 $this->assertEquals($group['group_type'], array($params['group_type']));
186 $this->assertEquals($group['parents'], $params['parents']);
187 }
188
189 public function testGetNonExistingGroup() {
190 $params = array();
191 $params['title'] = 'No such group Exist';
192 $group = $this->callAPISuccess('group', 'get', $params);
193 $this->assertEquals(0, $group['count']);
194 }
195
196 public function testgroupdeleteParamsnoId() {
197 $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id');
198 }
199
200 public function testgetfields() {
201 $description = "Demonstrate use of getfields to interrogate api.";
202 $params = array('action' => 'create');
203 $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description);
204 $this->assertEquals(1, $result['values']['is_active']['api.default']);
205 }
206
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 );
215 $this->callAPIFailure('group', 'create', $params);
216 unset($params['parents']);
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";
227 $this->callAPIFailure('group', 'create', $params);
228 unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]);
229 $group2 = $this->callAPISuccess('group', 'create', $params);
230 $this->assertEquals(count($group2['values'][$group2['id']]['parents']), 1);
231 }
232
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
259 }