CRM-21229 - handle two new reasons not to regenerate cache
authorJamie McClelland <jm@mayfirst.org>
Thu, 28 Sep 2017 01:18:11 +0000 (21:18 -0400)
committerJamie McClelland <jm@mayfirst.org>
Thu, 28 Sep 2017 01:18:11 +0000 (21:18 -0400)
If group has children - they could have expired smart groups
and if the group's cache_date is a date that has passed.

CRM/Contact/BAO/Group.php
tests/phpunit/CRM/Group/Page/AjaxTest.php

index 3963f26b0044a0ae4c2060ca7b40a316ef091a37..b9e6b6326c21d904db9727a95ec512ab87594c62 100644 (file)
@@ -1016,14 +1016,35 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
         $values[$object->id]['created_by'] = "<a href='{$contactUrl}'>{$object->created_by}</a>";
       }
 
+      // 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 && is_null($object->cache_date)) {
+      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) {
         $values[$object->id]['count'] = ts('unknown');
       }
       else {
-        // If it's not a smart group or we have them cached, then
-        // populate the count.
         $values[$object->id]['count'] = civicrm_api3('Contact', 'getcount', array('group' => $object->id));
       }
     }
index 09ef82f5d6541e8a436c4dafef758a454cdbdc9e..07377ebdc69afd9784ae05c97de96d6f6cb7c02e 100644 (file)
@@ -564,7 +564,41 @@ class CRM_Group_Page_AjaxTest extends CiviUnitTestCase {
     $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
     $params = array(1 => array($group->id, 'Integer'));
     $dao = CRM_Core_DAO::executeQuery($sql, $params);
-    $this->assertEquals($dao->N, 0, 'Group contact cache should not be populated on Manage Groups');
+    $test = 'Group contact cache should not be populated on Manage Groups ' .
+      'when cache_date is null';
+    $this->assertEquals($dao->N, 0, $test);
+
+    // Do it again, but this time don't clear group contact cache. Instead,
+    // set it to expire.
+    CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
+    $params['name'] = 'smartGroupCacheTimeout';
+    $timeout = civicrm_api3('Setting', 'getvalue', $params);
+    $timeout = intval($timeout) * 60;
+    // Reset the cache_date to $timeout seconds ago minus another 60
+    // seconds for good measure.
+    $cache_date = date('YmdHis', time() - $timeout - 60);
+
+    $sql = "UPDATE civicrm_group SET cache_date = %1 WHERE id = %2";
+    $update_params = array(
+      1 => array($cache_date, 'Timestamp'),
+      2 => array($group->id, 'Integer'),
+    );
+    CRM_Core_DAO::executeQuery($sql, $update_params);
+
+    // Load the Manage Group page code.
+    $_GET = $this->_params;
+    $obj = new CRM_Group_Page_AJAX();
+    $groups = $obj->getGroupList();
+
+    // Ensure we did not regenerate the cache.
+    $sql = 'SELECT DATE_FORMAT(cache_date, "%Y%m%d%H%i%s") AS cache_date ' .
+      'FROM civicrm_group WHERE id = %1';
+    $params = array(1 => array($group->id, 'Integer'));
+    $dao = CRM_Core_DAO::executeQuery($sql, $params);
+    $dao->fetch();
+    $test = 'Group contact cache should not be re-populated on Manage Groups ' .
+     'when cache_date has expired';
+    $this->assertEquals($dao->cache_date, $cache_date, $test);
   }
 
   /**