CRM-21229 - ensure parent group totals display when cache is warm
authorJamie McClelland <jm@mayfirst.org>
Thu, 28 Sep 2017 14:37:10 +0000 (10:37 -0400)
committerJamie McClelland <jm@mayfirst.org>
Thu, 28 Sep 2017 14:37:10 +0000 (10:37 -0400)
CRM/Contact/BAO/Group.php
tests/phpunit/CRM/Group/Page/AjaxTest.php

index b9e6b6326c21d904db9727a95ec512ab87594c62..a64e698da00a11d4cbe2643af088fe6c51bf4f91 100644 (file)
@@ -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 {
index 07377ebdc69afd9784ae05c97de96d6f6cb7c02e..0ac7fa69852b1c69ba9bb3dba4d3899bbe397303 100644 (file)
@@ -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';