From eb40b5a4fc34d1e9cc85c7129515e022ac1b183d Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 12 Apr 2013 11:13:23 +1200 Subject: [PATCH] fix failing settings test by allowing more cache clearing interim commit setting test passing but regression on api test --- CRM/Core/BAO/Domain.php | 1 + CRM/Core/BAO/Setting.php | 34 ++++++++++++++++++---- tests/phpunit/CRM/Core/BAO/SettingTest.php | 19 +++++++++--- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/CRM/Core/BAO/Domain.php b/CRM/Core/BAO/Domain.php index 0016252f83..a728386d70 100644 --- a/CRM/Core/BAO/Domain.php +++ b/CRM/Core/BAO/Domain.php @@ -101,6 +101,7 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { static function setDomain($domainID){ CRM_Core_Config::domainID($domainID); self::getDomain($domainID); + CRM_Core_Config::singleton(TRUE, TRUE); } /** diff --git a/CRM/Core/BAO/Setting.php b/CRM/Core/BAO/Setting.php index e9d9a819ab..0161abe92e 100644 --- a/CRM/Core/BAO/Setting.php +++ b/CRM/Core/BAO/Setting.php @@ -75,31 +75,45 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { * @static * @access public */ - static function inCache($group, + static function inCache( + $group, $name, $componentID = NULL, $contactID = NULL, $load = FALSE, - $domainID = NULL + $domainID = NULL, + $force = FALSE ) { if (!isset(self::$_cache)) { self::$_cache = array(); } $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}"; + if ($load && - !isset(self::$_cache[$cacheKey]) + ($force || !isset(self::$_cache[$cacheKey])) ) { + // check in civi cache if present (typically memcache) $globalCache = CRM_Utils_Cache::singleton(); $result = $globalCache->get($cacheKey); if ($result) { + self::$_cache[$cacheKey] = $result; } } return isset(self::$_cache[$cacheKey]) ? $cacheKey : NULL; } + /** + * Allow key o be cleared + * @param string $cacheKey + */ + static function flushCache($cacheKey){ + unset(self::$_cache[$cacheKey]); + $globalCache = CRM_Utils_Cache::singleton(); + $globalCache->delete($cacheKey); + } static function setCache($values, $group, @@ -200,7 +214,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $cacheKey = self::setCache($values, $group, $componentID, $contactID, $domainID); } - return $name ? CRM_Utils_Array::value($name, self::$_cache[$cacheKey], $defaultValue) : self::$_cache[$cacheKey]; } @@ -234,6 +247,12 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $result[$domainID] = array(); foreach ($fieldsToGet as $name => $value) { if(!empty($fields['values'][$name]['prefetch'])){ + if(isset($params['filters']) && isset($params['filters']['prefetch']) + && $params['filters']['prefetch'] == 0){ + // we are filtering out the prefetches from the return array + // so we will skip + continue; + } $configKey = CRM_Utils_Array::value('config_key', $fields['values'][$name], $name); if(isset($config->$configKey)){ $setting = $config->$configKey; @@ -245,7 +264,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $fields['values'][$name]['group_name'], $name, CRM_Utils_Array::value('component_id', $params), - CRM_Utils_Array::value('default_value', $params), + null, CRM_Utils_Array::value('contact_id', $params), $domainID ); @@ -283,7 +302,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $createdID = NULL, $domainID = NULL ) { - if (empty($domainID)) { $domainID = CRM_Core_Config::domainID(); } @@ -635,8 +653,10 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $apiParams = array( 'version' => 3, 'domain_id' => 'all', + 'filters' => array('prefetch' => 0), ); $existing = civicrm_api('setting', 'get', $apiParams); + if (!empty($existing['values'])) { $allSettings = civicrm_api('setting', 'getfields', array('version' => 3)); foreach ($existing['values'] as $domainID => $domainSettings) { @@ -657,6 +677,8 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { * Note that where the key name is being changed the 'legacy_key' will give us the old name */ static function convertConfigToSetting($name, $domainID = null) { + // we have to force this here in case more than one domain is in play. + // whenever there is a possibility of more than one domain we must force it $config = CRM_Core_Config::singleton(); if (empty($domainID)) { $domainID= CRM_Core_Config::domainID(); diff --git a/tests/phpunit/CRM/Core/BAO/SettingTest.php b/tests/phpunit/CRM/Core/BAO/SettingTest.php index 7964d8a997..b2b59698b5 100644 --- a/tests/phpunit/CRM/Core/BAO/SettingTest.php +++ b/tests/phpunit/CRM/Core/BAO/SettingTest.php @@ -121,19 +121,30 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { * **/ function testConvertAndFillSettings() { - $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'"; - CRM_Core_DAO::executeQuery($sql); - $settings = array('maxAttachments' => 6); CRM_Core_BAO_ConfigSetting::add($settings); $config = CRM_Core_Config::singleton(TRUE, TRUE); $this->assertEquals(6, $config->maxAttachments); - $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%Max%' AND id = 1 + $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%\"maxAttachments\";i:6%' AND id = 1 "; $checkresult = CRM_Core_DAO::singleValueQuery($checkSQL); $this->assertEquals(1, $checkresult, "Check that maxAttachments has been saved to database not just stored in config"); + $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'"; + $currentDomain = CRM_Core_Config::domainID(); +<<<<<<< Updated upstream + + CRM_Core_DAO::executeQuery($sql); + CRM_Core_BAO_Setting::inCache('CiviCRM Preferences', 'max_attachments', NULL, NULL, TRUE, $currentDomain, TRUE); CRM_Core_BAO_Setting::updateSettingsFromMetaData(); +======= +>>>>>>> Stashed changes + CRM_Core_DAO::executeQuery($sql); + // we are setting up an artificial situation here as we are trying to drive out + // previous memory of this setting so we need to flush it out + $cachekey = CRM_Core_BAO_Setting::inCache('CiviCRM Preferences', 'max_attachments', NULL, NULL, TRUE, $currentDomain); + CRM_Core_BAO_Setting::flushCache($cachekey); + CRM_Core_BAO_Setting::updateSettingsFromMetaData(); //check current domain $value = civicrm_api('setting', 'getvalue', array( 'version' => 3, -- 2.25.1