}
// By default, we try to get a count of the contacts in each group
- // to display to the user on the Manage Group page.
- $skip_getcount = FALSE;
- // If it's a smart group AND the contacts aren't cached, don't try to
- // generate a count, it will take forever.
- if ($object->saved_search_id) {
- if (is_null($object->cache_date)) {
- $skip_getcount = TRUE;
- }
- else {
- // We have a cache_date - see if it is expired.
- $params['name'] = 'smartGroupCacheTimeout';
- $timeout = civicrm_api3('Setting', 'getvalue', $params);
- $cache_expires = date('Y-m-d H:i:s', time() - (intval($timeout) * 60));
- if ($cache_expires > $object->cache_date) {
- $skip_getcount = TRUE;
- }
- }
- }
- if ($object->children) {
- // If there are children, any of the children could be expired
- // smart groups.
- $skip_getcount = TRUE;
- }
-
- if ($skip_getcount) {
+ // to display to the user on the Manage Group page. However, if
+ // that will result in the cache being regenerated, then dipslay
+ // "unknown" instead to avoid a long wait for the user.
+ if (CRM_Contact_BAO_GroupContactCache::shouldGroupBeRefreshed($object->id)) {
$values[$object->id]['count'] = ts('unknown');
}
else {
$dao = CRM_Core_DAO::executeQuery($sql, $params);
$this->assertEquals($dao->N, 1, '1 record should be found in smart group');
+ // Load the Manage Group page code and we should get a count from our
+ // group because the cache is fresh.
+ $_GET = $this->_params;
+ $obj = new CRM_Group_Page_AJAX();
+ $groups = $obj->getGroupList();
+
+ // Make sure we returned our smart group and ensure the count is accurate.
+ $found = FALSE;
+ $right_count = FALSE;
+ foreach ($groups['data'] as $returned_group) {
+ if ($returned_group['group_id'] == $group->id) {
+ $found = TRUE;
+ if ($returned_group['count'] == 1) {
+ $right_count = TRUE;
+ }
+ }
+ }
+ $this->assertTrue($found, 'Smart group shows up on Manage Group page.');
+ $this->assertTrue($right_count, 'Smart group displays proper count when cache is loaded.');
+
// Purge the group contact cache.
CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($group->id);
$obj = new CRM_Group_Page_AJAX();
$groups = $obj->getGroupList();
- // Make sure we returned our smart group.
- $found = FALSE;
+ // Make sure the smart group reports unknown count.
+ $count_is_unknown = FALSE;
foreach ($groups['data'] as $returned_group) {
if ($returned_group['group_id'] == $group->id) {
- $found = TRUE;
+ if ($returned_group['count'] == ts('unknown')) {
+ $count_is_unknown = TRUE;
+ }
}
}
- $this->assertTrue($found, 'Smart group shows up on Manage Group page.');
+ $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.');
// Ensure we did not populate the cache.
$sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';