From 76bd16abbf52d395985c32de124731523cf251bf Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 16 Sep 2015 21:49:57 -0700 Subject: [PATCH] CRM_Core_BAO_Setting - Don't prefill settings It's no longer necessary to prefill settings because defaults are loaded in real(ish) time. Removing this because it depends on ConfigSetting functions which I'd like to also remove. --- CRM/Core/BAO/Setting.php | 76 +----------------- CRM/Core/Invoke.php | 2 +- CRM/Upgrade/Incremental/php/FourFour.php | 4 +- tests/phpunit/CRM/Core/BAO/SettingTest.php | 90 +--------------------- 4 files changed, 9 insertions(+), 163 deletions(-) diff --git a/CRM/Core/BAO/Setting.php b/CRM/Core/BAO/Setting.php index 438df4884b..b26de01920 100644 --- a/CRM/Core/BAO/Setting.php +++ b/CRM/Core/BAO/Setting.php @@ -134,14 +134,8 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $config = CRM_Core_Config::singleton($reloadConfig, $reloadConfig); $result[$domainID] = array(); foreach ($fieldsToGet as $name => $value) { - $setting = CRM_Core_BAO_Setting::getItem( - $fields['values'][$name]['group_name'], - $name, - CRM_Utils_Array::value('component_id', $params), - NULL, - CRM_Utils_Array::value('contact_id', $params), - $domainID - ); + $contactID = CRM_Utils_Array::value('contact_id', $params); + $setting = CRM_Core_BAO_Setting::getItem(NULL, $name, NULL, NULL, $contactID, $domainID); if (!is_null($setting)) { // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones // e.g for revert of fill actions @@ -364,72 +358,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { return \Civi\Core\SettingsMetadata::getMetadata($filters, $domainID); } - /** - * Look for any missing settings and convert them from config or load default as appropriate. - * This should be run from GenCode & also from upgrades to add any new defaults. - * - * Multisites have often been overlooked in upgrade scripts so can be expected to be missing - * a number of settings - */ - public static function updateSettingsFromMetaData() { - $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) { - CRM_Core_BAO_Domain::setDomain($domainID); - $missing = array_diff_key($allSettings['values'], $domainSettings); - foreach ($missing as $name => $settings) { - self::convertConfigToSetting($name, $domainID); - } - CRM_Core_BAO_Domain::resetDomain(); - } - } - } - - /** - * Move an item from being in the config array to being stored as a setting - * remove from config - as appropriate based on metadata - * - * Note that where the key name is being changed the 'legacy_key' will give us the old name - */ - public 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(); - } - $domain = new CRM_Core_DAO_Domain(); - $domain->id = $domainID; - $domain->find(TRUE); - if ($domain->config_backend) { - $values = unserialize($domain->config_backend); - } - else { - $values = array(); - } - $spec = self::getSettingSpecification(NULL, array('name' => $name), $domainID); - $configKey = CRM_Utils_Array::value('config_key', $spec[$name], CRM_Utils_Array::value('legacy_key', $spec[$name], $name)); - if (!empty($values[$configKey])) { - civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID)); - } - else { - civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID)); - } - - if (!empty($values[$configKey])) { - unset($values[$configKey]); - $domain->config_backend = serialize($values); - $domain->save(); - } - } - /** * @param $group * @param string $name diff --git a/CRM/Core/Invoke.php b/CRM/Core/Invoke.php index 4832004ed2..24c0ecf77e 100644 --- a/CRM/Core/Invoke.php +++ b/CRM/Core/Invoke.php @@ -383,7 +383,7 @@ class CRM_Core_Invoke { // rebuild word replacement cache - pass false to prevent operations redundant with this fn CRM_Core_BAO_WordReplacement::rebuild(FALSE); - CRM_Core_BAO_Setting::updateSettingsFromMetaData(); + Civi::service('settings_manager')->flush(); // Clear js caches CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); CRM_Case_XMLRepository::singleton(TRUE); diff --git a/CRM/Upgrade/Incremental/php/FourFour.php b/CRM/Upgrade/Incremental/php/FourFour.php index e0e80e7958..20a9df238f 100644 --- a/CRM/Upgrade/Incremental/php/FourFour.php +++ b/CRM/Upgrade/Incremental/php/FourFour.php @@ -187,8 +187,8 @@ WHERE ceft.entity_table = 'civicrm_contribution' AND cft.payment_instrument_id I // CRM-12578 - Prior to this version a CSS file under drupal would disable core css if (!empty($config->customCSSURL) && strpos($config->userFramework, 'Drupal') === 0) { // The new setting doesn't exist yet - need to create it first - CRM_Core_BAO_Setting::updateSettingsFromMetaData(); - CRM_Core_BAO_Setting::setItem('1', CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'disable_core_css'); + Civi::service('settings_manager')->flush(); + Civi::settings()->set('disable_core_css', 1); } // CRM-13701 - Fix $config->timeInputFormat diff --git a/tests/phpunit/CRM/Core/BAO/SettingTest.php b/tests/phpunit/CRM/Core/BAO/SettingTest.php index 58985fe21c..48f140d5b8 100644 --- a/tests/phpunit/CRM/Core/BAO/SettingTest.php +++ b/tests/phpunit/CRM/Core/BAO/SettingTest.php @@ -114,94 +114,12 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { $this->assertEquals('/test/override', $values['imageUploadDir']); } - /** - * This test checks that CRM_Core_BAO_Setting::updateSettingsFromMetaData(); - * 1) Removes 'maxAttachments' from config (because 'prefetch' is not set in the metadata it should - * be removed - * 2) for current domain setting max_attachments is set to the value that $config->maxAttachments - * had (6) - * 3) for other domain (2) max_attachments is set to the configured default (3) - */ - public function testConvertAndFillSettings() { - $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 '%\"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'"; - CRM_Core_DAO::executeQuery($sql); - - $currentDomain = CRM_Core_Config::domainID(); - // 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); + public function testDefaults() { + CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"'); Civi::service('settings_manager')->flush(); - CRM_Core_BAO_Setting::updateSettingsFromMetaData(); - //check current domain - $value = civicrm_api('setting', 'getvalue', array( - 'version' => 3, - 'name' => 'max_attachments', - 'group' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, - )); - - $this->assertEquals(6, $value); - // check alternate domain - $value = civicrm_api('setting', 'getvalue', array( - 'version' => 3, - 'name' => 'max_attachments', - 'group' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, - 'domain_id' => 2, - )); - - $this->assertEquals(3, $value); - - //some caching inconsistency here - $config = CRM_Core_Config::singleton(TRUE, TRUE); - $maxAttachments = empty($config->maxAttachments) ? NULL : $config->maxAttachments; - $this->assertEmpty($maxAttachments, "Config item still Set to $maxAttachments - . This works fine when test run alone"); - } - - /** - * Ensure that overrides in $civicrm_setting apply when - * when using getItem(). - */ - public function testConvertConfigToSettingNoPrefetch() { - $settings = array('maxAttachments' => 6); - CRM_Core_BAO_ConfigSetting::add($settings); - $config = CRM_Core_Config::singleton(TRUE, TRUE); - $this->assertEquals(6, $config->maxAttachments); - - CRM_Core_BAO_Setting::convertConfigToSetting('max_attachments'); - $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); - $this->assertEquals(6, $value); - - $this->callAPISuccess('system', 'flush', array()); - $config = CRM_Core_Config::singleton(TRUE, TRUE); - $maxAttachments = empty($config->maxAttachments) ? NULL : $config->maxAttachments; - $this->assertEmpty($maxAttachments); - } - - /* @codingStandardsIgnoreStart - * Check that setting is converted without config value being removed - * - public function testConvertConfigToSettingPrefetch() { - $settings = array('debug' => 1); - CRM_Core_BAO_ConfigSetting::add($settings); - $config = CRM_Core_Config::singleton(true, true); - $this->assertEquals(1, $config->debug); - CRM_Core_BAO_Setting::convertConfigToSetting('debug_is_enabled'); - $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DEBUG_PREFERENCES_NAME, 'debug_is_enabled'); - $this->assertEquals(1, $value); - civicrm_api('system', 'flush', array('version' => 3)); - $config = CRM_Core_Config::singleton(true, true); - $this->assertEmpty($config->debug); + $this->assertEquals(3, Civi::settings()->get('max_attachments')); + $this->assertEquals(3, CRM_Core_Config::singleton()->maxAttachments); } - @codingStandardsIgnoreEnd */ /** * Ensure that on_change callbacks fire. -- 2.25.1