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 if (!is_array($permission)) {
64 $permission = array($permission);
66 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= $permission;
67 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
69 $_REQUEST = $this->_params
;
76 public function setHookAndRequest($permission, $hook) {
77 if (!is_array($permission)) {
78 $permission = array($permission);
80 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= $permission;
81 $this->hookClass
->setHook('civicrm_aclGroup', array($this, $hook));
82 CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
84 $_REQUEST = $this->_params
;
88 * Retrieve groups as 'view all contacts'
90 public function testGroupListViewAllContacts() {
91 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
92 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
93 $this->assertEquals(2, $total);
94 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
95 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
99 * Check Group Edit w/o 'edit groups' permission.
101 public function testGroupEditWithAndWithoutPermission() {
102 $this->setPermissionAndRequest('view all contacts');
103 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
104 $this->assertEquals(2, $total);
105 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
106 $this->assertEquals('not-me-active', $groups[4]['group_name']);
108 // as per changes made in PR-6822
109 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
110 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
111 $this->assertEquals(2, $total);
112 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
113 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
117 * Retrieve groups as 'view all contacts' permissioned user
118 * Without setting params the default is both enabled & disabled
119 * (if you do set default it is enabled only)
121 public function testGroupListViewAllContactsFoundTitle() {
122 $this->_params
['title'] = 'p';
123 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
124 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
125 $this->assertEquals(2, $total);
126 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
127 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
131 * Retrieve groups as 'view all contacts'
133 public function testGroupListViewAllContactsNotFoundTitle() {
134 $this->_params
['title'] = 'z';
135 $this->setPermissionAndRequest('view all contacts');
136 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
137 $this->assertEquals(0, $total);
141 * Retrieve groups as 'edit all contacts'
143 public function testGroupListEditAllContacts() {
144 $this->setPermissionAndRequest(array('edit all contacts', 'edit groups'));
145 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
146 $this->assertEquals(2, $total);
147 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
148 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
152 * Retrieve groups as 'view all contacts'
154 public function testGroupListViewAllContactsEnabled() {
155 $this->_params
['status'] = 1;
156 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
157 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
158 $this->assertEquals(2, $total);
159 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
160 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
164 * Retrieve groups as 'view all contacts'
166 public function testGroupListViewAllContactsDisabled() {
167 $this->_params
['status'] = 2;
168 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
169 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
170 $this->assertEquals(2, $total);
171 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
172 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
176 * Retrieve groups as 'view all contacts'
178 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
179 $this->_params
['status'] = 2;
180 $this->_params
['title'] = 'n';
181 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
182 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
183 $this->assertEquals(1, $total);
184 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
188 * Retrieve groups as 'view all contacts'
190 public function testGroupListViewAllContactsDisabledFoundTitle() {
191 $this->_params
['status'] = 2;
192 $this->_params
['title'] = 'p';
193 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
194 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
195 $this->assertEquals(1, $total);
196 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
200 * Retrieve groups as 'view all contacts'
202 public function testGroupListViewAllContactsAll() {
203 $this->_params
['status'] = 3;
204 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
205 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
206 $this->assertEquals(4, $total);
207 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
208 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
209 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
210 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
215 * Retrieve groups as 'view all contacts'
217 public function testGroupListAccessCiviCRM() {
218 $this->setPermissionAndRequest('access CiviCRM');
219 $permissionClause = CRM_Contact_BAO_Group
::getPermissionClause(TRUE);
220 $this->assertEquals('1 = 0', $permissionClause);
221 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
222 $this->assertEquals(0, count($groups));
223 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
227 * Retrieve groups as 'view all contacts'
229 public function testGroupListAccessCiviCRMEnabled() {
230 $this->_params
['status'] = 1;
231 $this->setPermissionAndRequest('access CiviCRM');
232 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
233 $this->assertEquals(0, count($groups));
234 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
238 * Retrieve groups as 'view all contacts'
240 public function testGroupListAccessCiviCRMDisabled() {
241 $this->_params
['status'] = 2;
242 $this->setPermissionAndRequest('access CiviCRM');
243 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
244 $this->assertEquals(0, count($groups));
245 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
249 * Retrieve groups as 'view all contacts'
251 public function testGroupListAccessCiviCRMAll() {
252 $this->_params
['status'] = 2;
253 $this->setPermissionAndRequest('access CiviCRM');
254 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
255 $this->assertEquals(0, count($groups));
256 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
260 * Retrieve groups as 'view all contacts'
262 public function testGroupListAccessCiviCRMFound() {
263 $this->_params
['title'] = 'p';
264 $this->setPermissionAndRequest('access CiviCRM');
265 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
266 $this->assertEquals(0, count($groups));
267 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
271 * Retrieve groups as 'view all contacts'
273 public function testGroupListAccessCiviCRMNotFound() {
274 $this->_params
['title'] = 'z';
275 $this->setPermissionAndRequest('access CiviCRM');
276 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
277 $this->assertEquals(0, count($groups));
278 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
281 public function testTraditionalACL() {
283 $this->setPermissionAndRequest('edit groups');
284 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
285 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
286 $this->assertEquals(1, $total, 'Total needs to be set correctly');
287 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
290 public function testTraditionalACLNotFoundTitle() {
291 $this->_params
['title'] = 'n';
293 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
294 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
295 $this->assertEquals(0, $total, 'Total needs to be set correctly');
298 public function testTraditionalACLFoundTitle() {
299 $this->_params
['title'] = 'p';
301 $this->setPermissionAndRequest('edit groups');
302 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
303 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
304 $this->assertEquals(2, $total, 'Total needs to be set correctly');
305 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
306 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
309 public function testTraditionalACLDisabled() {
310 $this->_params
['status'] = 2;
312 $this->setPermissionAndRequest('edit groups');
313 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
314 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
315 $this->assertEquals(1, $total, 'Total needs to be set correctly');
316 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
319 public function testTraditionalACLDisabledFoundTitle() {
320 $this->_params
['status'] = 2;
321 $this->_params
['title'] = 'p';
323 $this->setPermissionAndRequest('edit groups');
324 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
325 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
326 $this->assertEquals(1, $total, 'Total needs to be set correctly');
327 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
330 public function testTraditionalACLDisabledNotFoundTitle() {
331 $this->_params
['status'] = 2;
332 $this->_params
['title'] = 'n';
334 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
335 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
336 $this->assertEquals(0, $total, 'Total needs to be set correctly');
339 public function testTraditionalACLEnabled() {
340 $this->_params
['status'] = 1;
342 $this->setPermissionAndRequest('edit groups');
343 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
344 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
345 $this->assertEquals(1, $total, 'Total needs to be set correctly');
346 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
349 public function testTraditionalACLAll() {
350 $this->_params
['status'] = 3;
352 $this->setPermissionAndRequest('edit groups');
353 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
354 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
355 $this->assertEquals(2, $total, 'Total needs to be set correctly');
356 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
357 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
363 public function testGroupListAclGroupHookDisabled() {
364 $this->_params
['status'] = 2;
365 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
366 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
367 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
368 $this->assertEquals(1, $total, 'Total needs to be set correctly');
369 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
375 public function testGroupListAclGroupHookDisabledFound() {
376 $this->_params
['status'] = 2;
377 $this->_params
['title'] = 'p';
378 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
379 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
380 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
381 $this->assertEquals(1, $total, 'Total needs to be set correctly');
382 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
388 public function testGroupListAclGroupHookDisabledNotFound() {
389 $this->_params
['status'] = 2;
390 $this->_params
['title'] = 'n';
391 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
392 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
393 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
394 $this->assertEquals(0, $total, 'Total needs to be set correctly');
401 public function testGroupListAclGroupHook() {
402 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
403 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
404 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
405 $this->assertEquals(1, $total, 'Total needs to be set correctly');
406 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
412 public function testGroupListAclGroupHookTitleNotFound() {
413 $this->_params
['title'] = 'n';
414 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
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 public function testGroupListAclGroupHookTitleFound() {
424 $this->_params
['title'] = 'p';
425 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
426 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
427 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
428 $this->assertEquals(2, $total, 'Total needs to be set correctly');
429 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
430 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
436 public function testGroupListAclGroupHookAll() {
437 $this->_params
['status'] = 3;
438 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
439 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
440 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
441 $this->assertEquals(2, $total, 'Total needs to be set correctly');
442 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
443 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
449 public function testGroupListAclGroupHookEnabled() {
450 $this->_params
['status'] = 1;
451 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
452 list($groups, $total) = CRM_Group_Page_AJAX
::getGroupList();
453 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
454 $this->assertEquals(1, $total, 'Total needs to be set correctly');
455 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
459 * Implements ACLGroup hook.
460 * aclGroup function returns a list of permitted groups
461 * @param string $type
462 * @param int $contactID
463 * @param string $tableName
464 * @param array $allGroups
465 * @param array $currentGroups
467 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
468 //don't use api - you will get a loop
469 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
471 $dao = CRM_Core_DAO
::executeQuery($sql);
472 while ($dao->fetch()) {
473 $groups[] = $dao->id
;
476 if (!empty($allGroups)) {
477 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
478 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
479 $currentGroups = array_intersect($groups, array_flip($allGroups));
482 $currentGroups = $groups;