From b804448de6b873e7351664673b3b8aa6315d2a03 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Thu, 28 Sep 2017 10:37:10 -0400 Subject: [PATCH] CRM-21229 - ensure parent group totals display when cache is warm --- CRM/Contact/BAO/Group.php | 29 +++------------------- tests/phpunit/CRM/Group/Page/AjaxTest.php | 30 ++++++++++++++++++++--- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index b9e6b6326c..a64e698da0 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -1017,31 +1017,10 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { } // 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 { diff --git a/tests/phpunit/CRM/Group/Page/AjaxTest.php b/tests/phpunit/CRM/Group/Page/AjaxTest.php index 07377ebdc6..0ac7fa6985 100644 --- a/tests/phpunit/CRM/Group/Page/AjaxTest.php +++ b/tests/phpunit/CRM/Group/Page/AjaxTest.php @@ -543,6 +543,26 @@ class CRM_Group_Page_AjaxTest extends CiviUnitTestCase { $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); @@ -551,14 +571,16 @@ class CRM_Group_Page_AjaxTest extends CiviUnitTestCase { $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'; -- 2.25.1