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']);
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(array('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(array('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');
450 public function testGroupListAclGroupHook() {
451 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
452 $params = $this->_params
;
453 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
454 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
455 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
456 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
462 public function testGroupListAclGroupHookTitleNotFound() {
463 $this->_params
['title'] = 'n';
464 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
465 $params = $this->_params
;
466 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
467 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
468 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
474 public function testGroupListAclGroupHookTitleFound() {
475 $this->_params
['title'] = 'p';
476 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
477 $params = $this->_params
;
478 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
479 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
480 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
481 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
482 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
488 public function testGroupListAclGroupHookAll() {
489 $this->_params
['status'] = 3;
490 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
491 $params = $this->_params
;
492 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
493 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
494 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
495 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
496 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
502 public function testGroupListAclGroupHookEnabled() {
503 $this->_params
['status'] = 1;
504 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
505 $params = $this->_params
;
506 $groups = CRM_Contact_BAO_Group
::getGroupListSelector($params);
507 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
508 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
509 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
513 * Don't populate smart group cache when building Group list.
515 * It takes forever, especially if you have lots of smart groups.
517 public function testGroupDontRegenerateSmartGroups() {
519 $firstName = 'Tweak';
520 $lastName = 'Octonaut';
522 'first_name' => $firstName,
523 'last_name' => $lastName,
524 'contact_type' => 'Individual',
526 $contact = CRM_Contact_BAO_Contact
::add($params);
528 // Create a smart group.
529 $searchParams = array(
530 'last_name' => $lastName,
532 $groupParams = array('title' => 'Find all Octonauts', 'formValues' => $searchParams, 'is_active' => 1);
533 $group = CRM_Contact_BAO_Group
::createSmartGroup($groupParams);
535 // Ensure the smart group is created.
536 $this->assertTrue(is_int($group->id
), "Smart group created successfully.");
537 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
539 // Ensure it is populating the cache when loaded.
540 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
541 $params = array(1 => array($group->id
, 'Integer'));
542 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
543 $this->assertEquals($dao->N
, 1, '1 record should be found in smart group');
545 // Load the Manage Group page code and we should get a count from our
546 // group because the cache is fresh.
547 $_GET = $this->_params
;
548 $obj = new CRM_Group_Page_AJAX();
549 $groups = $obj->getGroupList();
551 // Make sure we returned our smart group and ensure the count is accurate.
553 $right_count = FALSE;
554 foreach ($groups['data'] as $returned_group) {
555 if ($returned_group['group_id'] == $group->id
) {
557 if ($returned_group['count'] == 1) {
562 $this->assertTrue($found, 'Smart group shows up on Manage Group page.');
563 $this->assertTrue($right_count, 'Smart group displays proper count when cache is loaded.');
565 // Purge the group contact cache.
566 CRM_Contact_BAO_GroupContactCache
::clearGroupContactCache($group->id
);
568 // Load the Manage Group page code.
569 $_GET = $this->_params
;
570 $obj = new CRM_Group_Page_AJAX();
571 $groups = $obj->getGroupList();
573 // Make sure the smart group reports unknown count.
574 $count_is_unknown = FALSE;
575 foreach ($groups['data'] as $returned_group) {
576 if ($returned_group['group_id'] == $group->id
) {
577 if ($returned_group['count'] == ts('unknown')) {
578 $count_is_unknown = TRUE;
582 $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.');
584 // Ensure we did not populate the cache.
585 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
586 $params = array(1 => array($group->id
, 'Integer'));
587 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
588 $test = 'Group contact cache should not be populated on Manage Groups ' .
589 'when cache_date is null';
590 $this->assertEquals($dao->N
, 0, $test);
592 // Do it again, but this time don't clear group contact cache. Instead,
594 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
595 $params['name'] = 'smartGroupCacheTimeout';
596 $timeout = civicrm_api3('Setting', 'getvalue', $params);
597 $timeout = intval($timeout) * 60;
598 // Reset the cache_date to $timeout seconds ago minus another 60
599 // seconds for good measure.
600 $cache_date = date('YmdHis', time() - $timeout - 60);
602 $sql = "UPDATE civicrm_group SET cache_date = %1 WHERE id = %2";
603 $update_params = array(
604 1 => array($cache_date, 'Timestamp'),
605 2 => array($group->id
, 'Integer'),
607 CRM_Core_DAO
::executeQuery($sql, $update_params);
609 // Load the Manage Group page code.
610 $_GET = $this->_params
;
611 $obj = new CRM_Group_Page_AJAX();
612 $groups = $obj->getGroupList();
614 // Ensure we did not regenerate the cache.
615 $sql = 'SELECT DATE_FORMAT(cache_date, "%Y%m%d%H%i%s") AS cache_date ' .
616 'FROM civicrm_group WHERE id = %1';
617 $params = array(1 => array($group->id
, 'Integer'));
618 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
620 $test = 'Group contact cache should not be re-populated on Manage Groups ' .
621 'when cache_date has expired';
622 $this->assertEquals($dao->cache_date
, $cache_date, $test);
626 * Implements ACLGroup hook.
627 * aclGroup function returns a list of permitted groups
628 * @param string $type
629 * @param int $contactID
630 * @param string $tableName
631 * @param array $allGroups
632 * @param array $currentGroups
634 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
635 //don't use api - you will get a loop
636 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
638 $dao = CRM_Core_DAO
::executeQuery($sql);
639 while ($dao->fetch()) {
640 $groups[] = $dao->id
;
643 if (!empty($allGroups)) {
644 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
645 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
646 $currentGroups = array_intersect($groups, array_flip($allGroups));
649 $currentGroups = $groups;