4 * 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(
35 'parentsOnly' => FALSE,
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 $this->setPermissions((array) $permission);
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));
79 $_REQUEST = $this->_params
;
83 * CRM-18528 - Retrieve groups with filter
85 public function testGroupListWithFilter() {
86 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
88 $_GET = $this->_params
;
89 $obj = new CRM_Group_Page_AJAX();
92 $_GET['title'] = "not-me-active";
93 $groups = $obj->getGroupList();
94 $this->assertEquals(1, $groups['recordsTotal']);
95 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
96 unset($_GET['title']);
100 $groups = $obj->getGroupList();
101 foreach ($groups['data'] as $key => $val) {
102 $this->assertEquals('crm-entity disabled', $val['DT_RowClass']);
107 * Retrieve groups as 'view all contacts'
109 public function testGroupListViewAllContacts() {
110 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
111 $params = $this->_params
;
112 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
113 $this->assertEquals(2, $groups['recordsTotal']);
114 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
115 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
119 * Check Group Edit w/o 'edit groups' permission.
121 * FIXME permissions to edit groups can only be determined by the links, which is ridiculously long
123 public function testGroupEditWithAndWithoutPermission() {
124 $this->setPermissionAndRequest('view all contacts');
125 $params = $this->_params
;
126 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
127 $this->assertEquals(2, $groups['recordsTotal']);
128 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=4&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a></span>', $groups['data'][0]['links']);
129 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=2&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a></span>', $groups['data'][1]['links']);
131 // as per changes made in PR-6822
132 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
133 $params = $this->_params
;
134 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
135 $this->assertEquals(2, $groups['recordsTotal']);
136 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=4&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a><a href="/index.php?q=civicrm/group&reset=1&action=update&id=4" class="action-item crm-hover-button" title=\'Edit Group\' >Settings</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="#" class="action-item crm-hover-button crm-enable-disable" title=\'Disable Group\' >Disable</a></li><li><a href="/index.php?q=civicrm/group&reset=1&action=delete&id=4" class="action-item crm-hover-button small-popup" title=\'Delete Group\' >Delete</a></li></ul></span>', $groups['data'][0]['links']);
137 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=2&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a><a href="/index.php?q=civicrm/group&reset=1&action=update&id=2" class="action-item crm-hover-button" title=\'Edit Group\' >Settings</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="#" class="action-item crm-hover-button crm-enable-disable" title=\'Disable Group\' >Disable</a></li><li><a href="/index.php?q=civicrm/group&reset=1&action=delete&id=2" class="action-item crm-hover-button small-popup" title=\'Delete Group\' >Delete</a></li></ul></span>', $groups['data'][1]['links']);
141 * Retrieve groups as 'view all contacts' permissioned user
142 * Without setting params the default is both enabled & disabled
143 * (if you do set default it is enabled only)
145 public function testGroupListViewAllContactsFoundTitle() {
146 $this->_params
['title'] = 'p';
147 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
148 $params = $this->_params
;
149 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
150 $this->assertEquals(2, $groups['recordsTotal']);
151 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
152 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
156 * Retrieve groups as 'view all contacts'
158 public function testGroupListViewAllContactsNotFoundTitle() {
159 $this->_params
['title'] = 'z';
160 $this->setPermissionAndRequest('view all contacts');
161 $params = $this->_params
;
162 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
163 $this->assertEquals(0, $groups['recordsTotal']);
167 * Retrieve groups as 'edit all contacts'
169 public function testGroupListEditAllContacts() {
170 $this->setPermissionAndRequest(array('edit all contacts', 'edit groups'));
171 $params = $this->_params
;
172 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
173 $this->assertEquals(2, $groups['recordsTotal']);
174 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
175 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
179 * Retrieve groups as 'view all contacts'
181 public function testGroupListViewAllContactsEnabled() {
182 $this->_params
['status'] = 1;
183 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
184 $params = $this->_params
;
185 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
186 $this->assertEquals(2, $groups['recordsTotal']);
187 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
188 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
192 * Retrieve groups as 'view all contacts'
194 public function testGroupListViewAllContactsDisabled() {
195 $this->_params
['status'] = 2;
196 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
197 $params = $this->_params
;
198 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
199 $this->assertEquals(2, $groups['recordsTotal']);
200 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
201 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
205 * Retrieve groups as 'view all contacts'
207 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
208 $this->_params
['status'] = 2;
209 $this->_params
['title'] = 'n';
210 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
211 $params = $this->_params
;
212 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
213 $this->assertEquals(1, $groups['recordsTotal']);
214 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
218 * Retrieve groups as 'view all contacts'
220 public function testGroupListViewAllContactsDisabledFoundTitle() {
221 $this->_params
['status'] = 2;
222 $this->_params
['title'] = 'p';
223 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
224 $params = $this->_params
;
225 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
226 $this->assertEquals(1, $groups['recordsTotal']);
227 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
231 * Retrieve groups as 'view all contacts'
233 public function testGroupListViewAllContactsAll() {
234 $this->_params
['status'] = 3;
235 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
236 $params = $this->_params
;
237 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
238 $this->assertEquals(4, $groups['recordsTotal']);
239 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
240 $this->assertEquals('not-me-disabled', $groups['data'][1]['title']);
241 $this->assertEquals('pick-me-active', $groups['data'][2]['title']);
242 $this->assertEquals('pick-me-disabled', $groups['data'][3]['title']);
246 * Retrieve groups as 'view all contacts'
248 public function testGroupListAccessCiviCRM() {
249 $this->setPermissionAndRequest('access CiviCRM');
250 $permissionClause = CRM_Contact_BAO_Group
::getPermissionClause();
251 $this->assertEquals('1 = 0', $permissionClause);
252 $params = $this->_params
;
253 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
254 $this->assertEquals(0, count($groups['data']));
255 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
259 * Retrieve groups as 'view all contacts'
261 public function testGroupListAccessCiviCRMEnabled() {
262 $this->_params
['status'] = 1;
263 $this->setPermissionAndRequest('access CiviCRM');
264 $params = $this->_params
;
265 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
266 $this->assertEquals(0, count($groups['data']));
267 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
271 * Retrieve groups as 'view all contacts'
273 public function testGroupListAccessCiviCRMDisabled() {
274 $this->_params
['status'] = 2;
275 $this->setPermissionAndRequest('access CiviCRM');
276 $params = $this->_params
;
277 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
278 $this->assertEquals(0, count($groups['data']));
279 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
283 * Retrieve groups as 'view all contacts'
285 public function testGroupListAccessCiviCRMAll() {
286 $this->_params
['status'] = 2;
287 $this->setPermissionAndRequest('access CiviCRM');
288 $params = $this->_params
;
289 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
290 $this->assertEquals(0, count($groups['data']));
291 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
295 * Retrieve groups as 'view all contacts'
297 public function testGroupListAccessCiviCRMFound() {
298 $this->_params
['title'] = 'p';
299 $this->setPermissionAndRequest('access CiviCRM');
300 $params = $this->_params
;
301 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
302 $this->assertEquals(0, count($groups['data']));
303 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
307 * Retrieve groups as 'view all contacts'
309 public function testGroupListAccessCiviCRMNotFound() {
310 $this->_params
['title'] = 'z';
311 $this->setPermissionAndRequest('access CiviCRM');
312 $params = $this->_params
;
313 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
314 $this->assertEquals(0, count($groups['data']));
315 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
318 public function testTraditionalACL() {
320 $this->setPermissionAndRequest('edit groups');
321 $params = $this->_params
;
322 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
323 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
324 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
325 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
328 public function testTraditionalACLNotFoundTitle() {
329 $this->_params
['title'] = 'n';
331 $params = $this->_params
;
332 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
333 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
334 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
337 public function testTraditionalACLFoundTitle() {
338 $this->_params
['title'] = 'p';
340 $this->setPermissionAndRequest('edit groups');
341 $params = $this->_params
;
342 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
343 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
344 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
345 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
346 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
349 public function testTraditionalACLDisabled() {
350 $this->_params
['status'] = 2;
352 $this->setPermissionAndRequest('edit groups');
353 $params = $this->_params
;
354 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
355 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
356 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
357 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
360 public function testTraditionalACLDisabledFoundTitle() {
361 $this->_params
['status'] = 2;
362 $this->_params
['title'] = 'p';
364 $this->setPermissionAndRequest('edit groups');
365 $params = $this->_params
;
366 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
367 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
368 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
369 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
372 public function testTraditionalACLDisabledNotFoundTitle() {
373 $this->_params
['status'] = 2;
374 $this->_params
['title'] = 'n';
376 $params = $this->_params
;
377 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
378 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
379 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
382 public function testTraditionalACLEnabled() {
383 $this->_params
['status'] = 1;
385 $this->setPermissionAndRequest('edit groups');
386 $params = $this->_params
;
387 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
388 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
389 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
390 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
393 public function testTraditionalACLAll() {
394 $this->_params
['status'] = 3;
396 $this->setPermissionAndRequest('edit groups');
397 $params = $this->_params
;
398 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
399 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
400 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
401 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
402 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
408 public function testGroupListAclGroupHookDisabled() {
409 $this->_params
['status'] = 2;
410 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
411 $params = $this->_params
;
412 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
413 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
414 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
415 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
421 public function testGroupListAclGroupHookDisabledFound() {
422 $this->_params
['status'] = 2;
423 $this->_params
['title'] = 'p';
424 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
425 $params = $this->_params
;
426 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
427 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
428 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
429 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
435 public function testGroupListAclGroupHookDisabledNotFound() {
436 $this->_params
['status'] = 2;
437 $this->_params
['title'] = 'n';
438 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
439 $params = $this->_params
;
440 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
441 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
442 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
448 public function testGroupListAclGroupHook() {
449 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
450 $params = $this->_params
;
451 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
452 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
453 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
454 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
460 public function testGroupListAclGroupHookTitleNotFound() {
461 $this->_params
['title'] = 'n';
462 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
463 $params = $this->_params
;
464 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
465 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
466 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
472 public function testGroupListAclGroupHookTitleFound() {
473 $this->_params
['title'] = 'p';
474 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
475 $params = $this->_params
;
476 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
477 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
478 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
479 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
480 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
486 public function testGroupListAclGroupHookAll() {
487 $this->_params
['status'] = 3;
488 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
489 $params = $this->_params
;
490 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
491 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
492 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
493 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
494 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
500 public function testGroupListAclGroupHookEnabled() {
501 $this->_params
['status'] = 1;
502 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
503 $params = $this->_params
;
504 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
505 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
506 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
507 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
511 * Don't populate smart group cache when building Group list.
513 * It takes forever, especially if you have lots of smart groups.
515 public function testGroupDontRegenerateSmartGroups() {
517 $firstName = 'Tweak';
518 $lastName = 'Octonaut';
520 'first_name' => $firstName,
521 'last_name' => $lastName,
522 'contact_type' => 'Individual',
524 $contact = CRM_Contact_BAO_Contact
::add($params);
526 // Create a smart group.
527 $searchParams = array(
528 'last_name' => $lastName,
530 $groupParams = array('title' => 'Find all Octonauts', 'formValues' => $searchParams, 'is_active' => 1);
531 $group = CRM_Contact_BAO_Group
::createSmartGroup($groupParams);
533 // Ensure the smart group is created.
534 $this->assertTrue(is_int($group->id
), "Smart group created successfully.");
535 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
537 // Ensure it is populating the cache when loaded.
538 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
539 $params = array(1 => array($group->id
, 'Integer'));
540 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
541 $this->assertEquals($dao->N
, 1, '1 record should be found in smart group');
543 // Load the Manage Group page code and we should get a count from our
544 // group because the cache is fresh.
545 $_GET = $this->_params
;
546 $obj = new CRM_Group_Page_AJAX();
547 $groups = $obj->getGroupList();
549 // Make sure we returned our smart group and ensure the count is accurate.
551 $right_count = FALSE;
552 foreach ($groups['data'] as $returned_group) {
553 if ($returned_group['group_id'] == $group->id
) {
555 if ($returned_group['count'] == 1) {
560 $this->assertTrue($found, 'Smart group shows up on Manage Group page.');
561 $this->assertTrue($right_count, 'Smart group displays proper count when cache is loaded.');
563 // Purge the group contact cache.
564 CRM_Contact_BAO_GroupContactCache
::clearGroupContactCache($group->id
);
566 // Load the Manage Group page code.
567 $_GET = $this->_params
;
568 $obj = new CRM_Group_Page_AJAX();
569 $groups = $obj->getGroupList();
571 // Make sure the smart group reports unknown count.
572 $count_is_unknown = FALSE;
573 foreach ($groups['data'] as $returned_group) {
574 if ($returned_group['group_id'] == $group->id
) {
575 if ($returned_group['count'] == ts('unknown')) {
576 $count_is_unknown = TRUE;
580 $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.');
582 // Ensure we did not populate the cache.
583 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
584 $params = array(1 => array($group->id
, 'Integer'));
585 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
586 $test = 'Group contact cache should not be populated on Manage Groups ' .
587 'when cache_date is null';
588 $this->assertEquals($dao->N
, 0, $test);
590 // Do it again, but this time don't clear group contact cache. Instead,
592 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
593 $params['name'] = 'smartGroupCacheTimeout';
594 $timeout = civicrm_api3('Setting', 'getvalue', $params);
595 $timeout = intval($timeout) * 60;
596 // Reset the cache_date to $timeout seconds ago minus another 60
597 // seconds for good measure.
598 $cache_date = date('YmdHis', time() - $timeout - 60);
600 $sql = "UPDATE civicrm_group SET cache_date = %1 WHERE id = %2";
601 $update_params = array(
602 1 => array($cache_date, 'Timestamp'),
603 2 => array($group->id
, 'Integer'),
605 CRM_Core_DAO
::executeQuery($sql, $update_params);
607 // Load the Manage Group page code.
608 $_GET = $this->_params
;
609 $obj = new CRM_Group_Page_AJAX();
610 $groups = $obj->getGroupList();
612 // Ensure we did not regenerate the cache.
613 $sql = 'SELECT DATE_FORMAT(cache_date, "%Y%m%d%H%i%s") AS cache_date ' .
614 'FROM civicrm_group WHERE id = %1';
615 $params = array(1 => array($group->id
, 'Integer'));
616 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
618 $test = 'Group contact cache should not be re-populated on Manage Groups ' .
619 'when cache_date has expired';
620 $this->assertEquals($dao->cache_date
, $cache_date, $test);
624 * Implements ACLGroup hook.
625 * aclGroup function returns a list of permitted groups
626 * @param string $type
627 * @param int $contactID
628 * @param string $tableName
629 * @param array $allGroups
630 * @param array $currentGroups
632 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
633 //don't use api - you will get a loop
634 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
636 $dao = CRM_Core_DAO
::executeQuery($sql);
637 while ($dao->fetch()) {
638 $groups[] = $dao->id
;
641 if (!empty($allGroups)) {
642 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
643 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
644 $currentGroups = array_intersect($groups, array_flip($allGroups));
647 $currentGroups = $groups;
651 public function testEditAllGroupsACL() {
652 $this->setupEditAllGroupsACL();
653 $params = $this->_params
;
654 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
655 $this->assertNotEmpty($groups, 'If Edit All Groups is granted, at least one group should be visible');
659 * Set up an acl allowing Authenticated contacts to Edit All Groups
661 * You need to have pre-created these groups & created the user e.g
662 * $this->createLoggedInUser();
665 public function setupEditAllGroupsACL() {
667 $_REQUEST = $this->_params
;
669 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
670 $optionGroupID = $this->callAPISuccessGetValue('option_group', array('return' => 'id', 'name' => 'acl_role'));
671 $ov = new CRM_Core_DAO_OptionValue();
672 $ov->option_group_id
= $optionGroupID;
674 if ($ov->find(TRUE)) {
675 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}");
677 $optionValue = $this->callAPISuccess('option_value', 'create', array(
678 'option_group_id' => $optionGroupID,
679 'label' => 'groupmaster',
682 $groupId = $this->groupCreate(['name' => 'groupmaster group']);
683 // Assign groupmaster to groupmaster group in civicrm_acl_entity_role
684 CRM_Core_DAO
::executeQuery("
685 INSERT INTO civicrm_acl_entity_role (
686 `acl_role_id`, `entity_table`, `entity_id`, `is_active`
687 ) VALUES (55, 'civicrm_group', $groupId, 1);
689 // Put the user into this group
690 $this->_loggedInUser
= CRM_Core_Session
::singleton()->get('userID');
691 $this->callAPISuccess('group_contact', 'create', array(
692 'group_id' => $groupId,
693 'contact_id' => $this->_loggedInUser
,
696 CRM_Core_DAO
::executeQuery("
697 INSERT INTO civicrm_acl (
698 `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
701 'core-580', 'civicrm_acl_role', 55, 'Edit', 'civicrm_saved_search', 0, 1