2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Group_Page_AjaxTest
extends CiviUnitTestCase
{
5 * Permissioned group is used both as an active group the contact can see and as a group that allows
6 * logged in user to see contacts
9 protected $_permissionedGroup;
11 * AS disabled group the contact has permission to
14 protected $_permissionedDisabledGroup;
18 'name' => 'Contact BAOs',
19 'description' => 'Test all Contact_BAO_Contact methods.',
20 'group' => 'CiviCRM BAO Tests',
26 $this->_params
= array(
33 'is_unit_test' => TRUE,
35 $this->hookClass
= CRM_Utils_Hook
::singleton();
36 $this->createLoggedInUser();
37 $this->_permissionedDisabledGroup
= $this->groupCreate(array('title' => 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
38 $this->_permissionedGroup
= $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
39 $this->groupCreate(array('title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled'));
40 $this->groupCreate(array('title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active'));
44 CRM_Utils_Hook
::singleton()->reset();
45 $this->quickCleanup(array('civicrm_group'));
46 $config = CRM_Core_Config
::singleton();
47 unset($config->userPermissionClass
->permissions
);
52 * retrieve groups as 'view all contacts'
54 function testGroupListViewAllContacts() {
55 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
57 $_REQUEST = $this->_params
;
58 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
59 $this->assertEquals(2, $total);
60 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
61 $this->assertEquals('not-me-active', $groups[4]['group_name']);
65 * retrieve groups as 'view all contacts' permissioned user
66 * Without setting params the default is both enabled & disabled
67 * (if you do set default it is enabled only)
69 function testGroupListViewAllContactsFoundTitle() {
70 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
72 $_REQUEST = $this->_params
;
73 $_REQUEST['title'] = 'p';
74 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
75 $this->assertEquals(2, $total);
76 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
77 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
81 * retrieve groups as 'view all contacts'
83 function testGroupListViewAllContactsNotFoundTitle() {
84 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
86 $_REQUEST = $this->_params
;
87 $_REQUEST['title'] = 'z';
88 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
89 $this->assertEquals(0, $total);
92 * retrieve groups as 'edit all contacts'
94 function testGroupListEditAllContacts() {
95 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
97 $_REQUEST = $this->_params
;
98 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
99 $this->assertEquals(2, $total);
100 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
101 $this->assertEquals('not-me-active', $groups[4]['group_name']);
105 * retrieve groups as 'view all contacts'
107 function testGroupListViewAllContactsEnabled() {
108 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
110 $_REQUEST = $this->_params
;
111 $_REQUEST['status'] = 1;
112 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
113 $this->assertEquals(2, $total);
114 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
115 $this->assertEquals('not-me-active', $groups[4]['group_name']);
119 * retrieve groups as 'view all contacts'
121 function testGroupListViewAllContactsDisabled() {
122 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
124 $_REQUEST = $this->_params
;
125 $_REQUEST['status'] = 2;
126 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
127 $this->assertEquals(2, $total);
128 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
129 $this->assertEquals('not-me-disabled', $groups[3]['group_name']);
133 * retrieve groups as 'view all contacts'
135 function testGroupListViewAllContactsDisabledNotFoundTitle() {
136 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
138 $_REQUEST = $this->_params
;
139 $_REQUEST['status'] = 2;
140 $_REQUEST['title'] = 'n';
141 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
142 $this->assertEquals(1, $total);
143 $this->assertEquals('not-me-disabled', $groups[3]['group_name']);
147 * retrieve groups as 'view all contacts'
149 function testGroupListViewAllContactsDisabledFoundTitle() {
150 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
152 $_REQUEST = $this->_params
;
153 $_REQUEST['status'] = 2;
154 $_REQUEST['title'] = 'p';
155 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
156 $this->assertEquals(1, $total);
157 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
161 * retrieve groups as 'view all contacts'
163 function testGroupListViewAllContactsAll() {
164 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('view all contacts');
166 $_REQUEST = $this->_params
;
167 $_REQUEST['status'] = 3;
168 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
169 $this->assertEquals(4, $total);
170 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
171 $this->assertEquals('not-me-disabled', $groups[3]['group_name']);
172 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
173 $this->assertEquals('not-me-active', $groups[4]['group_name']);
178 * retrieve groups as 'view all contacts'
180 function testGroupListAccessCiviCRM() {
181 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
183 $_REQUEST = $this->_params
;
184 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
185 $this->assertEquals(0, count($groups));
186 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
187 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
190 * retrieve groups as 'view all contacts'
192 function testGroupListAccessCiviCRMEnabled() {
193 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
195 $_REQUEST = $this->_params
;
196 $_REQUEST['status'] = 1;
197 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
198 $this->assertEquals(0, count($groups));
199 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
200 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
203 * retrieve groups as 'view all contacts'
205 function testGroupListAccessCiviCRMDisabled() {
206 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
208 $_REQUEST = $this->_params
;
209 $_REQUEST['status'] = 2;
210 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
211 $this->assertEquals(0, count($groups));
212 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
213 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
217 * retrieve groups as 'view all contacts'
219 function testGroupListAccessCiviCRMAll() {
220 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
222 $_REQUEST = $this->_params
;
223 $_REQUEST['status'] = 2;
224 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
225 $this->assertEquals(0, count($groups));
226 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
227 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
231 * retrieve groups as 'view all contacts'
233 function testGroupListAccessCiviCRMFound() {
234 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
236 $_REQUEST = $this->_params
;
237 $_REQUEST['title'] = 'p';
238 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
239 $this->assertEquals(0, count($groups));
240 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
244 * retrieve groups as 'view all contacts'
246 function testGroupListAccessCiviCRMNotFound() {
247 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
249 $_REQUEST = $this->_params
;
250 $_REQUEST['title'] = 'z';
251 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
252 $this->assertEquals(0, count($groups));
253 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
256 function testTraditionalACL () {
259 $_REQUEST = $this->_params
;
260 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
261 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
262 $this->assertEquals(1, $total, 'Total needs to be set correctly');
263 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
266 function testTraditionalACLNotFoundTitle () {
269 $_REQUEST = $this->_params
;
270 $_REQUEST['title'] = 'n';
271 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
272 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
273 $this->assertEquals(0, $total, 'Total needs to be set correctly');
276 function testTraditionalACLFoundTitle () {
279 $_REQUEST = $this->_params
;
280 $_REQUEST['title'] = 'p';
281 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
282 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
283 $this->assertEquals(2, $total, 'Total needs to be set correctly');
284 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
285 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
288 function testTraditionalACLDisabled () {
291 $_REQUEST = $this->_params
;
292 $_REQUEST['status'] = 2;
293 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
294 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
295 $this->assertEquals(1, $total, 'Total needs to be set correctly');
296 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
299 function testTraditionalACLDisabledFoundTitle () {
302 $_REQUEST = $this->_params
;
303 $_REQUEST['status'] = 2;
304 $_REQUEST['title'] = 'p';
305 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
306 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
307 $this->assertEquals(1, $total, 'Total needs to be set correctly');
308 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
311 function testTraditionalACLDisabledNotFoundTitle () {
314 $_REQUEST = $this->_params
;
315 $_REQUEST['status'] = 2;
316 $_REQUEST['title'] = 'n';
317 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
318 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
319 $this->assertEquals(0, $total, 'Total needs to be set correctly');
322 function testTraditionalACLEnabled () {
325 $_REQUEST = $this->_params
;
326 $_REQUEST['status'] = 1;
327 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
328 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
329 $this->assertEquals(1, $total, 'Total needs to be set correctly');
330 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
333 function testTraditionalACLAll () {
336 $_REQUEST = $this->_params
;
337 $_REQUEST['status'] = 3;
338 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
339 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
340 $this->assertEquals(2, $total, 'Total needs to be set correctly');
341 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
342 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
348 function testGroupListAclGroupHookDisabled() {
349 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
350 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
352 $_REQUEST = $this->_params
;
353 $_REQUEST['status'] = 2;
354 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
355 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
356 $this->assertEquals(1, $total, 'Total needs to be set correctly');
357 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
363 function testGroupListAclGroupHookDisabledFound() {
364 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
365 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
367 $_REQUEST = $this->_params
;
368 $_REQUEST['status'] = 2;
369 $_REQUEST['title'] = 'p';
370 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
371 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
372 $this->assertEquals(1, $total, 'Total needs to be set correctly');
373 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
379 function testGroupListAclGroupHookDisabledNotFound() {
380 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
381 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
383 $_REQUEST = $this->_params
;
384 $_REQUEST['status'] = 2;
385 $_REQUEST['title'] = 'n';
386 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
387 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
388 $this->assertEquals(0, $total, 'Total needs to be set correctly');
395 function testGroupListAclGroupHook() {
396 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
397 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
399 $_REQUEST = $this->_params
;
400 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
401 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
402 $this->assertEquals(1, $total, 'Total needs to be set correctly');
403 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
409 function testGroupListAclGroupHookTitleNotFound() {
410 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
411 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
413 $_REQUEST = $this->_params
;
414 $_REQUEST['title'] = 'n';
415 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
416 $this->assertEquals(0, $total, 'Total needs to be set correctly');
417 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
423 function testGroupListAclGroupHookTitleFound() {
424 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
425 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
427 $_REQUEST = $this->_params
;
428 $_REQUEST['title'] = 'p';
429 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
430 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
431 $this->assertEquals(2, $total, 'Total needs to be set correctly');
432 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
433 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
439 function testGroupListAclGroupHookAll() {
440 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
441 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
443 $_REQUEST = $this->_params
;
444 $_REQUEST['status'] = 3;
445 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
446 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
447 $this->assertEquals(2, $total, 'Total needs to be set correctly');
448 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
449 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
455 function testGroupListAclGroupHookEnabled() {
456 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
457 $this->hookClass
->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
459 $_REQUEST = $this->_params
;
460 $_REQUEST['status'] = 1;
461 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
462 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
463 $this->assertEquals(1, $total, 'Total needs to be set correctly');
464 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
468 * Implements ACLGroup hook
469 * aclGroup function returns a list of permitted groups
470 * @param string $type
471 * @param integer $contactID
472 * @param string $tableName
473 * @param array $allGroups
474 * @param array $currentGroups
476 function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
477 //dont use api - you will get a loop
478 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
480 $dao = CRM_Core_DAO
::executeQuery($sql);
481 while ($dao->fetch()) {
482 $groups[] = $dao->id
;
485 if(!empty($allGroups)) {
486 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
487 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
488 $currentGroups = array_intersect($groups, array_flip($allGroups));
491 $currentGroups = $groups;