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;
21 * @var CRM_Utils_Hook_UnitTests
25 protected $_params = array();
27 public function setUp() {
29 $this->_params
= array(
36 'is_unit_test' => TRUE,
38 $this->hookClass
= CRM_Utils_Hook
::singleton();
39 $this->createLoggedInUser();
40 $this->_permissionedDisabledGroup
= $this->groupCreate(array(
41 'title' => 'pick-me-disabled',
43 'name' => 'pick-me-disabled',
45 $this->_permissionedGroup
= $this->groupCreate(array(
46 'title' => 'pick-me-active',
48 'name' => 'pick-me-active',
50 $this->groupCreate(array('title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled'));
51 $this->groupCreate(array('title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active'));
54 public function tearDown() {
55 CRM_Utils_Hook
::singleton()->reset();
56 $this->quickCleanup(array('civicrm_group'));
57 $config = CRM_Core_Config
::singleton();
58 unset($config->userPermissionClass
->permissions
);
65 public function setPermissionAndRequest($permission) {
66 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= (array) $permission;
67 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
69 $_REQUEST = $this->_params
;
76 public function setHookAndRequest($permission, $hook) {
77 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= (array) $permission;
78 $this->hookClass
->setHook('civicrm_aclGroup', array($this, $hook));
79 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
81 $_REQUEST = $this->_params
;
85 * Retrieve groups as 'view all contacts'
87 public function testGroupListViewAllContacts() {
88 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
89 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
90 $this->assertEquals(2, $total);
91 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
92 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
96 * Check Group Edit w/o 'edit groups' permission.
98 public function testGroupEditWithAndWithoutPermission() {
99 $this->setPermissionAndRequest('view all contacts');
100 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
101 $this->assertEquals(2, $total);
102 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
103 $this->assertEquals('not-me-active', $groups[4]['group_name']);
105 // as per changes made in PR-6822
106 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
107 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
108 $this->assertEquals(2, $total);
109 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
110 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
114 * Retrieve groups as 'view all contacts' permissioned user
115 * Without setting params the default is both enabled & disabled
116 * (if you do set default it is enabled only)
118 public function testGroupListViewAllContactsFoundTitle() {
119 $this->_params
['title'] = 'p';
120 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
121 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
122 $this->assertEquals(2, $total);
123 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
124 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
128 * Retrieve groups as 'view all contacts'
130 public function testGroupListViewAllContactsNotFoundTitle() {
131 $this->_params
['title'] = 'z';
132 $this->setPermissionAndRequest('view all contacts');
133 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
134 $this->assertEquals(0, $total);
138 * Retrieve groups as 'edit all contacts'
140 public function testGroupListEditAllContacts() {
141 $this->setPermissionAndRequest(array('edit all contacts', 'edit groups'));
142 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
143 $this->assertEquals(2, $total);
144 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
145 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
149 * Retrieve groups as 'view all contacts'
151 public function testGroupListViewAllContactsEnabled() {
152 $this->_params
['status'] = 1;
153 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
154 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
155 $this->assertEquals(2, $total);
156 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
157 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
161 * Retrieve groups as 'view all contacts'
163 public function testGroupListViewAllContactsDisabled() {
164 $this->_params
['status'] = 2;
165 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
166 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
167 $this->assertEquals(2, $total);
168 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
169 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
173 * Retrieve groups as 'view all contacts'
175 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
176 $this->_params
['status'] = 2;
177 $this->_params
['title'] = 'n';
178 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
179 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
180 $this->assertEquals(1, $total);
181 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
185 * Retrieve groups as 'view all contacts'
187 public function testGroupListViewAllContactsDisabledFoundTitle() {
188 $this->_params
['status'] = 2;
189 $this->_params
['title'] = 'p';
190 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
191 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
192 $this->assertEquals(1, $total);
193 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
197 * Retrieve groups as 'view all contacts'
199 public function testGroupListViewAllContactsAll() {
200 $this->_params
['status'] = 3;
201 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
202 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
203 $this->assertEquals(4, $total);
204 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
205 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
206 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
207 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
212 * Retrieve groups as 'view all contacts'
214 public function testGroupListAccessCiviCRM() {
215 $this->setPermissionAndRequest('access CiviCRM');
216 $permissionClause = CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
217 $this->assertEquals('1 = 0', $permissionClause);
218 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
219 $this->assertEquals(0, count($groups));
220 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
224 * Retrieve groups as 'view all contacts'
226 public function testGroupListAccessCiviCRMEnabled() {
227 $this->_params
['status'] = 1;
228 $this->setPermissionAndRequest('access CiviCRM');
229 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
230 $this->assertEquals(0, count($groups));
231 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
235 * Retrieve groups as 'view all contacts'
237 public function testGroupListAccessCiviCRMDisabled() {
238 $this->_params
['status'] = 2;
239 $this->setPermissionAndRequest('access CiviCRM');
240 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
241 $this->assertEquals(0, count($groups));
242 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
246 * Retrieve groups as 'view all contacts'
248 public function testGroupListAccessCiviCRMAll() {
249 $this->_params
['status'] = 2;
250 $this->setPermissionAndRequest('access CiviCRM');
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');
257 * Retrieve groups as 'view all contacts'
259 public function testGroupListAccessCiviCRMFound() {
260 $this->_params
['title'] = 'p';
261 $this->setPermissionAndRequest('access CiviCRM');
262 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
263 $this->assertEquals(0, count($groups));
264 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
268 * Retrieve groups as 'view all contacts'
270 public function testGroupListAccessCiviCRMNotFound() {
271 $this->_params
['title'] = 'z';
272 $this->setPermissionAndRequest('access CiviCRM');
273 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
274 $this->assertEquals(0, count($groups));
275 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
278 public function testTraditionalACL() {
280 $this->setPermissionAndRequest('edit groups');
281 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
282 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
283 $this->assertEquals(1, $total, 'Total needs to be set correctly');
284 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
287 public function testTraditionalACLNotFoundTitle() {
288 $this->_params
['title'] = 'n';
290 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
291 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
292 $this->assertEquals(0, $total, 'Total needs to be set correctly');
295 public function testTraditionalACLFoundTitle() {
296 $this->_params
['title'] = 'p';
298 $this->setPermissionAndRequest('edit groups');
299 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
300 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
301 $this->assertEquals(2, $total, 'Total needs to be set correctly');
302 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
303 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
306 public function testTraditionalACLDisabled() {
307 $this->_params
['status'] = 2;
309 $this->setPermissionAndRequest('edit groups');
310 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
311 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
312 $this->assertEquals(1, $total, 'Total needs to be set correctly');
313 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
316 public function testTraditionalACLDisabledFoundTitle() {
317 $this->_params
['status'] = 2;
318 $this->_params
['title'] = 'p';
320 $this->setPermissionAndRequest('edit groups');
321 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
322 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
323 $this->assertEquals(1, $total, 'Total needs to be set correctly');
324 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
327 public function testTraditionalACLDisabledNotFoundTitle() {
328 $this->_params
['status'] = 2;
329 $this->_params
['title'] = 'n';
331 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
332 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
333 $this->assertEquals(0, $total, 'Total needs to be set correctly');
336 public function testTraditionalACLEnabled() {
337 $this->_params
['status'] = 1;
339 $this->setPermissionAndRequest('edit groups');
340 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
341 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
342 $this->assertEquals(1, $total, 'Total needs to be set correctly');
343 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
346 public function testTraditionalACLAll() {
347 $this->_params
['status'] = 3;
349 $this->setPermissionAndRequest('edit groups');
350 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
351 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
352 $this->assertEquals(2, $total, 'Total needs to be set correctly');
353 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
354 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
360 public function testGroupListAclGroupHookDisabled() {
361 $this->_params
['status'] = 2;
362 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
363 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
364 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
365 $this->assertEquals(1, $total, 'Total needs to be set correctly');
366 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
372 public function testGroupListAclGroupHookDisabledFound() {
373 $this->_params
['status'] = 2;
374 $this->_params
['title'] = 'p';
375 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
376 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
377 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
378 $this->assertEquals(1, $total, 'Total needs to be set correctly');
379 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
385 public function testGroupListAclGroupHookDisabledNotFound() {
386 $this->_params
['status'] = 2;
387 $this->_params
['title'] = 'n';
388 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
389 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
390 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
391 $this->assertEquals(0, $total, 'Total needs to be set correctly');
398 public function testGroupListAclGroupHook() {
399 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
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('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
409 public function testGroupListAclGroupHookTitleNotFound() {
410 $this->_params
['title'] = 'n';
411 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
412 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
413 $this->assertEquals(0, $total, 'Total needs to be set correctly');
414 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
420 public function testGroupListAclGroupHookTitleFound() {
421 $this->_params
['title'] = 'p';
422 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
423 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
424 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
425 $this->assertEquals(2, $total, 'Total needs to be set correctly');
426 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
427 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
433 public function testGroupListAclGroupHookAll() {
434 $this->_params
['status'] = 3;
435 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
436 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
437 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
438 $this->assertEquals(2, $total, 'Total needs to be set correctly');
439 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
440 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
446 public function testGroupListAclGroupHookEnabled() {
447 $this->_params
['status'] = 1;
448 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
449 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
450 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
451 $this->assertEquals(1, $total, 'Total needs to be set correctly');
452 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
456 * Implements ACLGroup hook.
457 * aclGroup function returns a list of permitted groups
458 * @param string $type
459 * @param int $contactID
460 * @param string $tableName
461 * @param array $allGroups
462 * @param array $currentGroups
464 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
465 //don't use api - you will get a loop
466 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
468 $dao = CRM_Core_DAO
::executeQuery($sql);
469 while ($dao->fetch()) {
470 $groups[] = $dao->id
;
473 if (!empty($allGroups)) {
474 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
475 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
476 $currentGroups = array_intersect($groups, array_flip($allGroups));
479 $currentGroups = $groups;