4 * Class CRM_Group_Page_AjaxTest
6 class CRM_Group_Page_AjaxTest
extends CiviUnitTestCase
{
8 * Permissioned group is used both as an active group the contact can see and as a group that allows
9 * logged in user to see contacts
12 protected $_permissionedGroup;
14 * AS disabled group the contact has permission to.
17 protected $_permissionedDisabledGroup;
20 * @var CRM_Utils_Hook_UnitTests
24 protected $_params = array();
26 public function setUp() {
28 $this->_params
= array(
35 'is_unit_test' => TRUE,
37 $this->hookClass
= CRM_Utils_Hook
::singleton();
38 $this->createLoggedInUser();
39 $this->_permissionedDisabledGroup
= $this->groupCreate(array(
40 'title' => 'pick-me-disabled',
42 'name' => 'pick-me-disabled',
44 $this->_permissionedGroup
= $this->groupCreate(array(
45 'title' => 'pick-me-active',
47 'name' => 'pick-me-active',
49 $this->groupCreate(array('title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled'));
50 $this->groupCreate(array('title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active'));
53 public function tearDown() {
54 CRM_Utils_Hook
::singleton()->reset();
55 $this->quickCleanup(array('civicrm_group'));
56 $config = CRM_Core_Config
::singleton();
57 unset($config->userPermissionClass
->permissions
);
64 public function setPermissionAndRequest($permission) {
65 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= (array) $permission;
66 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
68 $_REQUEST = $this->_params
;
75 public function setHookAndRequest($permission, $hook) {
76 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= (array) $permission;
77 $this->hookClass
->setHook('civicrm_aclGroup', array($this, $hook));
78 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
80 $_REQUEST = $this->_params
;
84 * Retrieve groups as 'view all contacts'
86 public function testGroupListViewAllContacts() {
87 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
88 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
89 $this->assertEquals(2, $total);
90 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
91 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
95 * Check Group Edit w/o 'edit groups' permission.
97 public function testGroupEditWithAndWithoutPermission() {
98 $this->setPermissionAndRequest('view all contacts');
99 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
100 $this->assertEquals(2, $total);
101 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
102 $this->assertEquals('not-me-active', $groups[4]['group_name']);
104 // as per changes made in PR-6822
105 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
106 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
107 $this->assertEquals(2, $total);
108 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
109 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
113 * Retrieve groups as 'view all contacts' permissioned user
114 * Without setting params the default is both enabled & disabled
115 * (if you do set default it is enabled only)
117 public function testGroupListViewAllContactsFoundTitle() {
118 $this->_params
['title'] = 'p';
119 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
120 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
121 $this->assertEquals(2, $total);
122 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
123 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
127 * Retrieve groups as 'view all contacts'
129 public function testGroupListViewAllContactsNotFoundTitle() {
130 $this->_params
['title'] = 'z';
131 $this->setPermissionAndRequest('view all contacts');
132 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
133 $this->assertEquals(0, $total);
137 * Retrieve groups as 'edit all contacts'
139 public function testGroupListEditAllContacts() {
140 $this->setPermissionAndRequest(array('edit all contacts', 'edit groups'));
141 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
142 $this->assertEquals(2, $total);
143 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
144 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
148 * Retrieve groups as 'view all contacts'
150 public function testGroupListViewAllContactsEnabled() {
151 $this->_params
['status'] = 1;
152 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
153 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
154 $this->assertEquals(2, $total);
155 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
156 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
160 * Retrieve groups as 'view all contacts'
162 public function testGroupListViewAllContactsDisabled() {
163 $this->_params
['status'] = 2;
164 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
165 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
166 $this->assertEquals(2, $total);
167 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
168 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
172 * Retrieve groups as 'view all contacts'
174 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
175 $this->_params
['status'] = 2;
176 $this->_params
['title'] = 'n';
177 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
178 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
179 $this->assertEquals(1, $total);
180 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
184 * Retrieve groups as 'view all contacts'
186 public function testGroupListViewAllContactsDisabledFoundTitle() {
187 $this->_params
['status'] = 2;
188 $this->_params
['title'] = 'p';
189 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
190 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
191 $this->assertEquals(1, $total);
192 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
196 * Retrieve groups as 'view all contacts'
198 public function testGroupListViewAllContactsAll() {
199 $this->_params
['status'] = 3;
200 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
201 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
202 $this->assertEquals(4, $total);
203 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
204 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
205 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
206 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
211 * Retrieve groups as 'view all contacts'
213 public function testGroupListAccessCiviCRM() {
214 $this->setPermissionAndRequest('access CiviCRM');
215 $permissionClause = CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
216 $this->assertEquals('1 = 0', $permissionClause);
217 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
218 $this->assertEquals(0, count($groups));
219 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
223 * Retrieve groups as 'view all contacts'
225 public function testGroupListAccessCiviCRMEnabled() {
226 $this->_params
['status'] = 1;
227 $this->setPermissionAndRequest('access CiviCRM');
228 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
229 $this->assertEquals(0, count($groups));
230 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
234 * Retrieve groups as 'view all contacts'
236 public function testGroupListAccessCiviCRMDisabled() {
237 $this->_params
['status'] = 2;
238 $this->setPermissionAndRequest('access CiviCRM');
239 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
240 $this->assertEquals(0, count($groups));
241 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
245 * Retrieve groups as 'view all contacts'
247 public function testGroupListAccessCiviCRMAll() {
248 $this->_params
['status'] = 2;
249 $this->setPermissionAndRequest('access CiviCRM');
250 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
251 $this->assertEquals(0, count($groups));
252 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
256 * Retrieve groups as 'view all contacts'
258 public function testGroupListAccessCiviCRMFound() {
259 $this->_params
['title'] = 'p';
260 $this->setPermissionAndRequest('access CiviCRM');
261 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
262 $this->assertEquals(0, count($groups));
263 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
267 * Retrieve groups as 'view all contacts'
269 public function testGroupListAccessCiviCRMNotFound() {
270 $this->_params
['title'] = 'z';
271 $this->setPermissionAndRequest('access CiviCRM');
272 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
273 $this->assertEquals(0, count($groups));
274 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
277 public function testTraditionalACL() {
279 $this->setPermissionAndRequest('edit groups');
280 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
281 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
282 $this->assertEquals(1, $total, 'Total needs to be set correctly');
283 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
286 public function testTraditionalACLNotFoundTitle() {
287 $this->_params
['title'] = 'n';
289 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
290 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
291 $this->assertEquals(0, $total, 'Total needs to be set correctly');
294 public function testTraditionalACLFoundTitle() {
295 $this->_params
['title'] = 'p';
297 $this->setPermissionAndRequest('edit groups');
298 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
299 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
300 $this->assertEquals(2, $total, 'Total needs to be set correctly');
301 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
302 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
305 public function testTraditionalACLDisabled() {
306 $this->_params
['status'] = 2;
308 $this->setPermissionAndRequest('edit groups');
309 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
310 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
311 $this->assertEquals(1, $total, 'Total needs to be set correctly');
312 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
315 public function testTraditionalACLDisabledFoundTitle() {
316 $this->_params
['status'] = 2;
317 $this->_params
['title'] = 'p';
319 $this->setPermissionAndRequest('edit groups');
320 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
321 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
322 $this->assertEquals(1, $total, 'Total needs to be set correctly');
323 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
326 public function testTraditionalACLDisabledNotFoundTitle() {
327 $this->_params
['status'] = 2;
328 $this->_params
['title'] = 'n';
330 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
331 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
332 $this->assertEquals(0, $total, 'Total needs to be set correctly');
335 public function testTraditionalACLEnabled() {
336 $this->_params
['status'] = 1;
338 $this->setPermissionAndRequest('edit groups');
339 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
340 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
341 $this->assertEquals(1, $total, 'Total needs to be set correctly');
342 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
345 public function testTraditionalACLAll() {
346 $this->_params
['status'] = 3;
348 $this->setPermissionAndRequest('edit groups');
349 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
350 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
351 $this->assertEquals(2, $total, 'Total needs to be set correctly');
352 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
353 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
359 public function testGroupListAclGroupHookDisabled() {
360 $this->_params
['status'] = 2;
361 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
362 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
363 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
364 $this->assertEquals(1, $total, 'Total needs to be set correctly');
365 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
371 public function testGroupListAclGroupHookDisabledFound() {
372 $this->_params
['status'] = 2;
373 $this->_params
['title'] = 'p';
374 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
375 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
376 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
377 $this->assertEquals(1, $total, 'Total needs to be set correctly');
378 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
384 public function testGroupListAclGroupHookDisabledNotFound() {
385 $this->_params
['status'] = 2;
386 $this->_params
['title'] = 'n';
387 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
388 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
389 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
390 $this->assertEquals(0, $total, 'Total needs to be set correctly');
397 public function testGroupListAclGroupHook() {
398 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
399 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
400 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
401 $this->assertEquals(1, $total, 'Total needs to be set correctly');
402 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
408 public function testGroupListAclGroupHookTitleNotFound() {
409 $this->_params
['title'] = 'n';
410 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
411 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
412 $this->assertEquals(0, $total, 'Total needs to be set correctly');
413 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
419 public function testGroupListAclGroupHookTitleFound() {
420 $this->_params
['title'] = 'p';
421 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
422 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
423 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
424 $this->assertEquals(2, $total, 'Total needs to be set correctly');
425 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
426 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
432 public function testGroupListAclGroupHookAll() {
433 $this->_params
['status'] = 3;
434 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
435 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
436 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
437 $this->assertEquals(2, $total, 'Total needs to be set correctly');
438 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
439 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
445 public function testGroupListAclGroupHookEnabled() {
446 $this->_params
['status'] = 1;
447 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
448 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
449 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
450 $this->assertEquals(1, $total, 'Total needs to be set correctly');
451 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
455 * Implements ACLGroup hook.
456 * aclGroup function returns a list of permitted groups
457 * @param string $type
458 * @param int $contactID
459 * @param string $tableName
460 * @param array $allGroups
461 * @param array $currentGroups
463 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
464 //don't use api - you will get a loop
465 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
467 $dao = CRM_Core_DAO
::executeQuery($sql);
468 while ($dao->fetch()) {
469 $groups[] = $dao->id
;
472 if (!empty($allGroups)) {
473 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
474 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
475 $currentGroups = array_intersect($groups, array_flip($allGroups));
478 $currentGroups = $groups;