2 require_once 'CiviTest/CiviUnitTestCase.php';
5 * Class CRM_Group_Page_AjaxTest
7 class CRM_Group_Page_AjaxTest
extends CiviUnitTestCase
{
9 * Permissioned group is used both as an active group the contact can see and as a group that allows
10 * logged in user to see contacts
13 protected $_permissionedGroup;
15 * AS disabled group the contact has permission to.
18 protected $_permissionedDisabledGroup;
22 protected $_params = array();
24 public function setUp() {
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(
38 'title' => 'pick-me-disabled',
40 'name' => 'pick-me-disabled',
42 $this->_permissionedGroup
= $this->groupCreate(array(
43 'title' => 'pick-me-active',
45 'name' => 'pick-me-active',
47 $this->groupCreate(array('title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled'));
48 $this->groupCreate(array('title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active'));
51 public function tearDown() {
52 CRM_Utils_Hook
::singleton()->reset();
53 $this->quickCleanup(array('civicrm_group'));
54 $config = CRM_Core_Config
::singleton();
55 unset($config->userPermissionClass
->permissions
);
62 public function setPermissionAndRequest($permission) {
63 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= (array) $permission;
64 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
66 $_REQUEST = $this->_params
;
73 public function setHookAndRequest($permission, $hook) {
74 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= (array) $permission;
75 $this->hookClass
->setHook('civicrm_aclGroup', array($this, $hook));
76 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
78 $_REQUEST = $this->_params
;
82 * Retrieve groups as 'view all contacts'
84 public function testGroupListViewAllContacts() {
85 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
86 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
87 $this->assertEquals(2, $total);
88 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
89 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
93 * Check Group Edit w/o 'edit groups' permission.
95 public function testGroupEditWithAndWithoutPermission() {
96 $this->setPermissionAndRequest('view all contacts');
97 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
98 $this->assertEquals(2, $total);
99 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
100 $this->assertEquals('not-me-active', $groups[4]['group_name']);
102 // as per changes made in PR-6822
103 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
104 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
105 $this->assertEquals(2, $total);
106 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
107 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
111 * Retrieve groups as 'view all contacts' permissioned user
112 * Without setting params the default is both enabled & disabled
113 * (if you do set default it is enabled only)
115 public function testGroupListViewAllContactsFoundTitle() {
116 $this->_params
['title'] = 'p';
117 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
118 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
119 $this->assertEquals(2, $total);
120 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
121 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
125 * Retrieve groups as 'view all contacts'
127 public function testGroupListViewAllContactsNotFoundTitle() {
128 $this->_params
['title'] = 'z';
129 $this->setPermissionAndRequest('view all contacts');
130 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
131 $this->assertEquals(0, $total);
135 * Retrieve groups as 'edit all contacts'
137 public function testGroupListEditAllContacts() {
138 $this->setPermissionAndRequest(array('edit all contacts', 'edit groups'));
139 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
140 $this->assertEquals(2, $total);
141 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
142 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
146 * Retrieve groups as 'view all contacts'
148 public function testGroupListViewAllContactsEnabled() {
149 $this->_params
['status'] = 1;
150 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
151 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
152 $this->assertEquals(2, $total);
153 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
154 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
158 * Retrieve groups as 'view all contacts'
160 public function testGroupListViewAllContactsDisabled() {
161 $this->_params
['status'] = 2;
162 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
163 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
164 $this->assertEquals(2, $total);
165 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
166 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
170 * Retrieve groups as 'view all contacts'
172 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
173 $this->_params
['status'] = 2;
174 $this->_params
['title'] = 'n';
175 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
176 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
177 $this->assertEquals(1, $total);
178 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
182 * Retrieve groups as 'view all contacts'
184 public function testGroupListViewAllContactsDisabledFoundTitle() {
185 $this->_params
['status'] = 2;
186 $this->_params
['title'] = 'p';
187 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
188 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
189 $this->assertEquals(1, $total);
190 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
194 * Retrieve groups as 'view all contacts'
196 public function testGroupListViewAllContactsAll() {
197 $this->_params
['status'] = 3;
198 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
199 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
200 $this->assertEquals(4, $total);
201 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
202 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
203 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
204 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
209 * Retrieve groups as 'view all contacts'
211 public function testGroupListAccessCiviCRM() {
212 $this->setPermissionAndRequest('access CiviCRM');
213 $permissionClause = CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
214 $this->assertEquals('1 = 0', $permissionClause);
215 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
216 $this->assertEquals(0, count($groups));
217 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
221 * Retrieve groups as 'view all contacts'
223 public function testGroupListAccessCiviCRMEnabled() {
224 $this->_params
['status'] = 1;
225 $this->setPermissionAndRequest('access CiviCRM');
226 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
227 $this->assertEquals(0, count($groups));
228 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
232 * Retrieve groups as 'view all contacts'
234 public function testGroupListAccessCiviCRMDisabled() {
235 $this->_params
['status'] = 2;
236 $this->setPermissionAndRequest('access CiviCRM');
237 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
238 $this->assertEquals(0, count($groups));
239 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
243 * Retrieve groups as 'view all contacts'
245 public function testGroupListAccessCiviCRMAll() {
246 $this->_params
['status'] = 2;
247 $this->setPermissionAndRequest('access CiviCRM');
248 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
249 $this->assertEquals(0, count($groups));
250 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
254 * Retrieve groups as 'view all contacts'
256 public function testGroupListAccessCiviCRMFound() {
257 $this->_params
['title'] = 'p';
258 $this->setPermissionAndRequest('access CiviCRM');
259 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
260 $this->assertEquals(0, count($groups));
261 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
265 * Retrieve groups as 'view all contacts'
267 public function testGroupListAccessCiviCRMNotFound() {
268 $this->_params
['title'] = 'z';
269 $this->setPermissionAndRequest('access CiviCRM');
270 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
271 $this->assertEquals(0, count($groups));
272 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
275 public function testTraditionalACL() {
277 $this->setPermissionAndRequest('edit groups');
278 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
279 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
280 $this->assertEquals(1, $total, 'Total needs to be set correctly');
281 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
284 public function testTraditionalACLNotFoundTitle() {
285 $this->_params
['title'] = 'n';
287 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
288 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
289 $this->assertEquals(0, $total, 'Total needs to be set correctly');
292 public function testTraditionalACLFoundTitle() {
293 $this->_params
['title'] = 'p';
295 $this->setPermissionAndRequest('edit groups');
296 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
297 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
298 $this->assertEquals(2, $total, 'Total needs to be set correctly');
299 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
300 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
303 public function testTraditionalACLDisabled() {
304 $this->_params
['status'] = 2;
306 $this->setPermissionAndRequest('edit groups');
307 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
308 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
309 $this->assertEquals(1, $total, 'Total needs to be set correctly');
310 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
313 public function testTraditionalACLDisabledFoundTitle() {
314 $this->_params
['status'] = 2;
315 $this->_params
['title'] = 'p';
317 $this->setPermissionAndRequest('edit groups');
318 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
319 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
320 $this->assertEquals(1, $total, 'Total needs to be set correctly');
321 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
324 public function testTraditionalACLDisabledNotFoundTitle() {
325 $this->_params
['status'] = 2;
326 $this->_params
['title'] = 'n';
328 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
329 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
330 $this->assertEquals(0, $total, 'Total needs to be set correctly');
333 public function testTraditionalACLEnabled() {
334 $this->_params
['status'] = 1;
336 $this->setPermissionAndRequest('edit groups');
337 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
338 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
339 $this->assertEquals(1, $total, 'Total needs to be set correctly');
340 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
343 public function testTraditionalACLAll() {
344 $this->_params
['status'] = 3;
346 $this->setPermissionAndRequest('edit groups');
347 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
348 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
349 $this->assertEquals(2, $total, 'Total needs to be set correctly');
350 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
351 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
357 public function testGroupListAclGroupHookDisabled() {
358 $this->_params
['status'] = 2;
359 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
360 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
361 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
362 $this->assertEquals(1, $total, 'Total needs to be set correctly');
363 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
369 public function testGroupListAclGroupHookDisabledFound() {
370 $this->_params
['status'] = 2;
371 $this->_params
['title'] = 'p';
372 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
373 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
374 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
375 $this->assertEquals(1, $total, 'Total needs to be set correctly');
376 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
382 public function testGroupListAclGroupHookDisabledNotFound() {
383 $this->_params
['status'] = 2;
384 $this->_params
['title'] = 'n';
385 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
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 public function testGroupListAclGroupHook() {
396 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
397 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
398 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
399 $this->assertEquals(1, $total, 'Total needs to be set correctly');
400 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
406 public function testGroupListAclGroupHookTitleNotFound() {
407 $this->_params
['title'] = 'n';
408 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
409 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
410 $this->assertEquals(0, $total, 'Total needs to be set correctly');
411 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
417 public function testGroupListAclGroupHookTitleFound() {
418 $this->_params
['title'] = 'p';
419 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
420 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
421 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
422 $this->assertEquals(2, $total, 'Total needs to be set correctly');
423 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
424 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
430 public function testGroupListAclGroupHookAll() {
431 $this->_params
['status'] = 3;
432 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
433 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
434 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
435 $this->assertEquals(2, $total, 'Total needs to be set correctly');
436 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
437 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
443 public function testGroupListAclGroupHookEnabled() {
444 $this->_params
['status'] = 1;
445 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
446 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
447 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
448 $this->assertEquals(1, $total, 'Total needs to be set correctly');
449 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
453 * Implements ACLGroup hook.
454 * aclGroup function returns a list of permitted groups
455 * @param string $type
456 * @param int $contactID
457 * @param string $tableName
458 * @param array $allGroups
459 * @param array $currentGroups
461 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
462 //don't use api - you will get a loop
463 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
465 $dao = CRM_Core_DAO
::executeQuery($sql);
466 while ($dao->fetch()) {
467 $groups[] = $dao->id
;
470 if (!empty($allGroups)) {
471 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
472 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
473 $currentGroups = array_intersect($groups, array_flip($allGroups));
476 $currentGroups = $groups;