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.
14 protected $_permissionedGroup;
16 * AS disabled group the contact has permission to.
19 protected $_permissionedDisabledGroup;
22 * @var CRM_Utils_Hook_UnitTests
26 protected $_params = [];
28 public function setUp() {
36 'parentsOnly' => FALSE,
37 'is_unit_test' => TRUE,
39 $this->hookClass
= CRM_Utils_Hook
::singleton();
40 $this->createLoggedInUser();
41 $this->_permissionedDisabledGroup
= $this->groupCreate([
42 'title' => 'pick-me-disabled',
44 'name' => 'pick-me-disabled',
46 $this->_permissionedGroup
= $this->groupCreate([
47 'title' => 'pick-me-active',
49 'name' => 'pick-me-active',
51 $this->groupCreate(['title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled']);
52 $this->groupCreate(['title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active']);
55 public function tearDown() {
56 CRM_Utils_Hook
::singleton()->reset();
57 $this->quickCleanup(['civicrm_group']);
58 $config = CRM_Core_Config
::singleton();
59 unset($config->userPermissionClass
->permissions
);
66 public function setPermissionAndRequest($permission) {
67 $this->setPermissions((array) $permission);
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', [$this, $hook]);
80 $_REQUEST = $this->_params
;
84 * CRM-18528 - Retrieve groups with filter
86 public function testGroupListWithFilter() {
87 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
89 $_GET = $this->_params
;
90 $obj = new CRM_Group_Page_AJAX();
93 $_GET['title'] = "not-me-active";
94 $groups = $obj->getGroupList();
95 $this->assertEquals(1, $groups['recordsTotal']);
96 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
97 unset($_GET['title']);
101 $groups = $obj->getGroupList();
102 foreach ($groups['data'] as $key => $val) {
103 $this->assertEquals('crm-entity disabled', $val['DT_RowClass']);
108 * Retrieve groups as 'view all contacts'
110 public function testGroupListViewAllContacts() {
111 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
112 $params = $this->_params
;
113 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
114 $this->assertEquals(2, $groups['recordsTotal']);
115 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
116 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
120 * Check Group Edit w/o 'edit groups' permission.
122 * FIXME permissions to edit groups can only be determined by the links, which is ridiculously long
124 public function testGroupEditWithAndWithoutPermission() {
125 $this->setPermissionAndRequest('view all contacts');
126 $params = $this->_params
;
127 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
128 $this->assertEquals(2, $groups['recordsTotal']);
129 $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']);
130 $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']);
132 // as per changes made in PR-6822
133 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
134 $params = $this->_params
;
135 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
136 $this->assertEquals(2, $groups['recordsTotal']);
137 $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']);
138 $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']);
142 * Retrieve groups as 'view all contacts' permissioned user
143 * Without setting params the default is both enabled & disabled
144 * (if you do set default it is enabled only)
146 public function testGroupListViewAllContactsFoundTitle() {
147 $this->_params
['title'] = 'p';
148 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
149 $params = $this->_params
;
150 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
151 $this->assertEquals(2, $groups['recordsTotal']);
152 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
153 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
157 * Retrieve groups as 'view all contacts'
159 public function testGroupListViewAllContactsNotFoundTitle() {
160 $this->_params
['title'] = 'z';
161 $this->setPermissionAndRequest('view all contacts');
162 $params = $this->_params
;
163 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
164 $this->assertEquals(0, $groups['recordsTotal']);
168 * Retrieve groups as 'edit all contacts'
170 public function testGroupListEditAllContacts() {
171 $this->setPermissionAndRequest(['edit all contacts', 'edit groups']);
172 $params = $this->_params
;
173 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
174 $this->assertEquals(2, $groups['recordsTotal']);
175 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
176 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
180 * Retrieve groups as 'view all contacts'
182 public function testGroupListViewAllContactsEnabled() {
183 $this->_params
['status'] = 1;
184 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
185 $params = $this->_params
;
186 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
187 $this->assertEquals(2, $groups['recordsTotal']);
188 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
189 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
193 * Retrieve groups as 'view all contacts'
195 public function testGroupListViewAllContactsDisabled() {
196 $this->_params
['status'] = 2;
197 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
198 $params = $this->_params
;
199 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
200 $this->assertEquals(2, $groups['recordsTotal']);
201 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
202 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
206 * Retrieve groups as 'view all contacts'
208 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
209 $this->_params
['status'] = 2;
210 $this->_params
['title'] = 'n';
211 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
212 $params = $this->_params
;
213 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
214 $this->assertEquals(1, $groups['recordsTotal']);
215 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
219 * Retrieve groups as 'view all contacts'
221 public function testGroupListViewAllContactsDisabledFoundTitle() {
222 $this->_params
['status'] = 2;
223 $this->_params
['title'] = 'p';
224 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
225 $params = $this->_params
;
226 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
227 $this->assertEquals(1, $groups['recordsTotal']);
228 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
232 * Retrieve groups as 'view all contacts'
234 public function testGroupListViewAllContactsAll() {
235 $this->_params
['status'] = 3;
236 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
237 $params = $this->_params
;
238 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
239 $this->assertEquals(4, $groups['recordsTotal']);
240 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
241 $this->assertEquals('not-me-disabled', $groups['data'][1]['title']);
242 $this->assertEquals('pick-me-active', $groups['data'][2]['title']);
243 $this->assertEquals('pick-me-disabled', $groups['data'][3]['title']);
247 * Retrieve groups as 'view all contacts'
249 public function testGroupListAccessCiviCRM() {
250 $this->setPermissionAndRequest('access CiviCRM');
251 $permissionClause = CRM_Contact_BAO_Group
::getPermissionClause();
252 $this->assertEquals('1 = 0', $permissionClause);
253 $params = $this->_params
;
254 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
255 $this->assertEquals(0, count($groups['data']));
256 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
260 * Retrieve groups as 'view all contacts'
262 public function testGroupListAccessCiviCRMEnabled() {
263 $this->_params
['status'] = 1;
264 $this->setPermissionAndRequest('access CiviCRM');
265 $params = $this->_params
;
266 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
267 $this->assertEquals(0, count($groups['data']));
268 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
272 * Retrieve groups as 'view all contacts'
274 public function testGroupListAccessCiviCRMDisabled() {
275 $this->_params
['status'] = 2;
276 $this->setPermissionAndRequest('access CiviCRM');
277 $params = $this->_params
;
278 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
279 $this->assertEquals(0, count($groups['data']));
280 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
284 * Retrieve groups as 'view all contacts'
286 public function testGroupListAccessCiviCRMAll() {
287 $this->_params
['status'] = 2;
288 $this->setPermissionAndRequest('access CiviCRM');
289 $params = $this->_params
;
290 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
291 $this->assertEquals(0, count($groups['data']));
292 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
296 * Retrieve groups as 'view all contacts'
298 public function testGroupListAccessCiviCRMFound() {
299 $this->_params
['title'] = 'p';
300 $this->setPermissionAndRequest('access CiviCRM');
301 $params = $this->_params
;
302 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
303 $this->assertEquals(0, count($groups['data']));
304 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
308 * Retrieve groups as 'view all contacts'
310 public function testGroupListAccessCiviCRMNotFound() {
311 $this->_params
['title'] = 'z';
312 $this->setPermissionAndRequest('access CiviCRM');
313 $params = $this->_params
;
314 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
315 $this->assertEquals(0, count($groups['data']));
316 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
319 public function testTraditionalACL() {
321 $this->setPermissionAndRequest('edit groups');
322 $params = $this->_params
;
323 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
324 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
325 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
326 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
329 public function testTraditionalACLNotFoundTitle() {
330 $this->_params
['title'] = 'n';
332 $params = $this->_params
;
333 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
334 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
335 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
338 public function testTraditionalACLFoundTitle() {
339 $this->_params
['title'] = 'p';
341 $this->setPermissionAndRequest('edit groups');
342 $params = $this->_params
;
343 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
344 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
345 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
346 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
347 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
350 public function testTraditionalACLDisabled() {
351 $this->_params
['status'] = 2;
353 $this->setPermissionAndRequest('edit groups');
354 $params = $this->_params
;
355 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
356 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
357 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
358 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
361 public function testTraditionalACLDisabledFoundTitle() {
362 $this->_params
['status'] = 2;
363 $this->_params
['title'] = 'p';
365 $this->setPermissionAndRequest('edit groups');
366 $params = $this->_params
;
367 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
368 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
369 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
370 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
373 public function testTraditionalACLDisabledNotFoundTitle() {
374 $this->_params
['status'] = 2;
375 $this->_params
['title'] = 'n';
377 $params = $this->_params
;
378 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
379 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
380 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
383 public function testTraditionalACLEnabled() {
384 $this->_params
['status'] = 1;
386 $this->setPermissionAndRequest('edit groups');
387 $params = $this->_params
;
388 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
389 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
390 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
391 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
394 public function testTraditionalACLAll() {
395 $this->_params
['status'] = 3;
397 $this->setPermissionAndRequest('edit groups');
398 $params = $this->_params
;
399 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
400 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
401 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
402 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
403 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
409 public function testGroupListAclGroupHookDisabled() {
410 $this->_params
['status'] = 2;
411 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
412 $params = $this->_params
;
413 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
414 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
415 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
416 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
422 public function testGroupListAclGroupHookDisabledFound() {
423 $this->_params
['status'] = 2;
424 $this->_params
['title'] = 'p';
425 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
426 $params = $this->_params
;
427 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
428 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
429 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
430 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
436 public function testGroupListAclGroupHookDisabledNotFound() {
437 $this->_params
['status'] = 2;
438 $this->_params
['title'] = 'n';
439 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
440 $params = $this->_params
;
441 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
442 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
443 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
449 public function testGroupListAclGroupHook() {
450 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
451 $params = $this->_params
;
452 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
453 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
454 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
455 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
461 public function testGroupListAclGroupHookTitleNotFound() {
462 $this->_params
['title'] = 'n';
463 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
464 $params = $this->_params
;
465 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
466 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
467 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
473 public function testGroupListAclGroupHookTitleFound() {
474 $this->_params
['title'] = 'p';
475 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
476 $params = $this->_params
;
477 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
478 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
479 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
480 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
481 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
487 public function testGroupListAclGroupHookAll() {
488 $this->_params
['status'] = 3;
489 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
490 $params = $this->_params
;
491 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
492 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
493 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
494 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
495 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
501 public function testGroupListAclGroupHookEnabled() {
502 $this->_params
['status'] = 1;
503 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
504 $params = $this->_params
;
505 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
506 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
507 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
508 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
512 * Don't populate smart group cache when building Group list.
514 * It takes forever, especially if you have lots of smart groups.
516 public function testGroupDontRegenerateSmartGroups() {
518 $firstName = 'Tweak';
519 $lastName = 'Octonaut';
521 'first_name' => $firstName,
522 'last_name' => $lastName,
523 'contact_type' => 'Individual',
525 $contact = CRM_Contact_BAO_Contact
::add($params);
527 // Create a smart group.
529 'last_name' => $lastName,
531 $groupParams = ['title' => 'Find all Octonauts', 'formValues' => $searchParams, 'is_active' => 1];
532 $group = CRM_Contact_BAO_Group
::createSmartGroup($groupParams);
534 // Ensure the smart group is created.
535 $this->assertTrue(is_int($group->id
), "Smart group created successfully.");
536 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
538 // Ensure it is populating the cache when loaded.
539 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
540 $params = [1 => [$group->id
, 'Integer']];
541 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
542 $this->assertEquals($dao->N
, 1, '1 record should be found in smart group');
544 // Load the Manage Group page code and we should get a count from our
545 // group because the cache is fresh.
546 $_GET = $this->_params
;
547 $obj = new CRM_Group_Page_AJAX();
548 $groups = $obj->getGroupList();
550 // Make sure we returned our smart group and ensure the count is accurate.
552 $right_count = FALSE;
553 foreach ($groups['data'] as $returned_group) {
554 if ($returned_group['group_id'] == $group->id
) {
556 if ($returned_group['count'] == 1) {
561 $this->assertTrue($found, 'Smart group shows up on Manage Group page.');
562 $this->assertTrue($right_count, 'Smart group displays proper count when cache is loaded.');
564 // Purge the group contact cache.
565 CRM_Contact_BAO_GroupContactCache
::clearGroupContactCache($group->id
);
567 // Load the Manage Group page code.
568 $_GET = $this->_params
;
569 $obj = new CRM_Group_Page_AJAX();
570 $groups = $obj->getGroupList();
572 // Make sure the smart group reports unknown count.
573 $count_is_unknown = FALSE;
574 foreach ($groups['data'] as $returned_group) {
575 if ($returned_group['group_id'] == $group->id
) {
576 if ($returned_group['count'] == ts('unknown')) {
577 $count_is_unknown = TRUE;
581 $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.');
583 // Ensure we did not populate the cache.
584 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
585 $params = [1 => [$group->id
, 'Integer']];
586 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
587 $test = 'Group contact cache should not be populated on Manage Groups ' .
588 'when cache_date is null';
589 $this->assertEquals($dao->N
, 0, $test);
591 // Do it again, but this time don't clear group contact cache. Instead,
593 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
594 $params['name'] = 'smartGroupCacheTimeout';
595 $timeout = civicrm_api3('Setting', 'getvalue', $params);
596 $timeout = intval($timeout) * 60;
597 // Reset the cache_date to $timeout seconds ago minus another 60
598 // seconds for good measure.
599 $cache_date = date('YmdHis', time() - $timeout - 60);
601 $sql = "UPDATE civicrm_group SET cache_date = %1 WHERE id = %2";
603 1 => [$cache_date, 'Timestamp'],
604 2 => [$group->id
, 'Integer'],
606 CRM_Core_DAO
::executeQuery($sql, $update_params);
608 // Load the Manage Group page code.
609 $_GET = $this->_params
;
610 $obj = new CRM_Group_Page_AJAX();
611 $groups = $obj->getGroupList();
613 // Ensure we did not regenerate the cache.
614 $sql = 'SELECT DATE_FORMAT(cache_date, "%Y%m%d%H%i%s") AS cache_date ' .
615 'FROM civicrm_group WHERE id = %1';
616 $params = [1 => [$group->id
, 'Integer']];
617 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
619 $test = 'Group contact cache should not be re-populated on Manage Groups ' .
620 'when cache_date has expired';
621 $this->assertEquals($dao->cache_date
, $cache_date, $test);
625 * Implements ACLGroup hook.
626 * aclGroup function returns a list of permitted groups
627 * @param string $type
628 * @param int $contactID
629 * @param string $tableName
630 * @param array $allGroups
631 * @param array $currentGroups
633 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
634 //don't use api - you will get a loop
635 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
637 $dao = CRM_Core_DAO
::executeQuery($sql);
638 while ($dao->fetch()) {
639 $groups[] = $dao->id
;
642 if (!empty($allGroups)) {
643 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
644 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
645 $currentGroups = array_intersect($groups, array_flip($allGroups));
648 $currentGroups = $groups;
652 public function testEditAllGroupsACL() {
653 $this->setupEditAllGroupsACL();
654 $params = $this->_params
;
655 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
656 $this->assertNotEmpty($groups, 'If Edit All Groups is granted, at least one group should be visible');
660 * Set up an acl allowing Authenticated contacts to Edit All Groups
662 * You need to have pre-created these groups & created the user e.g
663 * $this->createLoggedInUser();
666 public function setupEditAllGroupsACL() {
668 $_REQUEST = $this->_params
;
670 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM'];
671 $optionGroupID = $this->callAPISuccessGetValue('option_group', ['return' => 'id', 'name' => 'acl_role']);
672 $ov = new CRM_Core_DAO_OptionValue();
673 $ov->option_group_id
= $optionGroupID;
675 if ($ov->find(TRUE)) {
676 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}");
678 $optionValue = $this->callAPISuccess('option_value', 'create', [
679 'option_group_id' => $optionGroupID,
680 'label' => 'groupmaster',
683 $groupId = $this->groupCreate(['name' => 'groupmaster group']);
684 // Assign groupmaster to groupmaster group in civicrm_acl_entity_role
685 CRM_Core_DAO
::executeQuery("
686 INSERT INTO civicrm_acl_entity_role (
687 `acl_role_id`, `entity_table`, `entity_id`, `is_active`
688 ) VALUES (55, 'civicrm_group', $groupId, 1);
690 // Put the user into this group
691 $this->_loggedInUser
= CRM_Core_Session
::singleton()->get('userID');
692 $this->callAPISuccess('group_contact', 'create', [
693 'group_id' => $groupId,
694 'contact_id' => $this->_loggedInUser
,
697 CRM_Core_DAO
::executeQuery("
698 INSERT INTO civicrm_acl (
699 `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
702 'core-580', 'civicrm_acl_role', 55, 'Edit', 'civicrm_saved_search', 0, 1