From 01ef4db933b77d2f373ae6cf9591b1a5a07b194c Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Wed, 27 Sep 2017 21:18:11 -0400 Subject: [PATCH] CRM-21229 - handle two new reasons not to regenerate cache 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 | 27 +++++++++++++++-- tests/phpunit/CRM/Group/Page/AjaxTest.php | 36 ++++++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 3963f26b00..b9e6b6326c 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -1016,14 +1016,35 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { $values[$object->id]['created_by'] = "{$object->created_by}"; } + // 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)); } } diff --git a/tests/phpunit/CRM/Group/Page/AjaxTest.php b/tests/phpunit/CRM/Group/Page/AjaxTest.php index 09ef82f5d6..07377ebdc6 100644 --- a/tests/phpunit/CRM/Group/Page/AjaxTest.php +++ b/tests/phpunit/CRM/Group/Page/AjaxTest.php @@ -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); } /** -- 2.25.1