CRM_Core_BAO_Setting - Don't prefill settings
authorTim Otten <totten@civicrm.org>
Thu, 17 Sep 2015 04:49:57 +0000 (21:49 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 17 Sep 2015 22:49:33 +0000 (15:49 -0700)
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
CRM/Core/Invoke.php
CRM/Upgrade/Incremental/php/FourFour.php
tests/phpunit/CRM/Core/BAO/SettingTest.php

index 438df4884b6d41cf4f21860a7ab6a98580dd1611..b26de01920d9c5ea2a9ab0389d348a1d3673f12c 100644 (file)
@@ -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
index 4832004ed2cb1976461f58c8f37182386957f83b..24c0ecf77e7da2275941c674309b09c6af95ae3f 100644 (file)
@@ -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);
index e0e80e7958ea75bf9c7d65bcaa4bc0bb867f5d3c..20a9df238f2f2a98d52c662692f839f466b0e51a 100644 (file)
@@ -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
index 58985fe21c988435c1520b9d3eddfe05c1a79189..48f140d5b8f7061e9c8a06adab337291f20c01bf 100644 (file)
@@ -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.