From a67e41a6460b55fd3581e1440aa64ecee7b9ea58 Mon Sep 17 00:00:00 2001 From: Piotr Przybylski Date: Sun, 3 Nov 2013 12:54:20 +0100 Subject: [PATCH] Refactored ConfigFile class so that it is no longer a singleton Fixes bug #4123 Double config including --- ChangeLog | 1 + libraries/Config.class.php | 7 + libraries/config/ConfigFile.class.php | 108 +-- libraries/config/Form.class.php | 14 +- libraries/config/FormDisplay.class.php | 48 +- libraries/config/Validator.class.php | 13 +- libraries/core.lib.php | 22 + libraries/user_preferences.inc.php | 2 +- libraries/user_preferences.lib.php | 4 +- prefs_forms.php | 7 +- prefs_manage.php | 8 +- setup/config.php | 12 +- setup/frames/config.inc.php | 4 +- setup/frames/form.inc.php | 2 +- setup/frames/index.inc.php | 4 +- setup/frames/servers.inc.php | 4 +- setup/lib/ConfigGenerator.class.php | 5 +- setup/lib/common.inc.php | 3 +- setup/lib/form_processing.lib.php | 2 +- setup/lib/index.lib.php | 4 +- setup/validate.php | 2 +- test/classes/config/PMA_FormDisplay_test.php | 9 +- test/classes/config/PMA_Form_test.php | 5 +- .../auth/PMA_AuthenticationCookie_test.php | 8 +- .../auth/PMA_AuthenticationHttp_test.php | 3 + test/libraries/PMA_ConfigFile_test.php | 859 ++++++------------ test/libraries/PMA_ConfigGenerator_test.php | 9 +- ...Index_test.php => PMA_SetupIndex_test.php} | 12 +- test/libraries/PMA_user_preferences_test.php | 2 +- 29 files changed, 443 insertions(+), 740 deletions(-) rename test/libraries/{PMA_Index_test.php => PMA_SetupIndex_test.php} (97%) diff --git a/ChangeLog b/ChangeLog index 865dc17e38..072c945394 100644 --- a/ChangeLog +++ b/ChangeLog @@ -48,6 +48,7 @@ phpMyAdmin - ChangeLog + Added an Error Reporting Component + Javascript files are no longer uglified - bug #4145 Config screen fails to validate MemoryLimit = -1 (new default) +- bug #4123 Double config including 4.0.10.0 (not yet released) - bug #4150 Clicking database name in query window opens a new tab diff --git a/libraries/Config.class.php b/libraries/Config.class.php index c541c7fd1f..798b1defc8 100644 --- a/libraries/Config.class.php +++ b/libraries/Config.class.php @@ -32,6 +32,11 @@ class PMA_Config */ var $default = array(); + /** + * @var array configuration settings, without user preferences applied + */ + var $base_settings = array(); + /** * @var array configuration settings */ @@ -93,6 +98,8 @@ class PMA_Config $this->checkSystem(); $this->isHttps(); + + $this->base_settings = $this->settings; } /** diff --git a/libraries/config/ConfigFile.class.php b/libraries/config/ConfigFile.class.php index 3be0138527..f145fa0c76 100644 --- a/libraries/config/ConfigFile.class.php +++ b/libraries/config/ConfigFile.class.php @@ -18,13 +18,7 @@ class ConfigFile * Stores default PMA config from config.default.php * @var array */ - private $_cfg; - - /** - * Stores original PMA_Config object, not modified by user preferences - * @var PMA_Config - */ - private $_orgCfgObject; + private $_defaultCfg; /** * Stores allowed values for non-standard fields @@ -32,6 +26,18 @@ class ConfigFile */ private $_cfgDb; + /** + * Stores original PMA config, not modified by user preferences + * @var PMA_Config + */ + private $_baseCfg; + + /** + * Whether we are currently working in PMA Setup context + * @var bool + */ + private $_isInSetup; + /** * Keys which will be always written to config file * @var array @@ -65,25 +71,18 @@ class ConfigFile private $_flattenArrayResult; /** - * ConfigFile instance - * @var ConfigFile - */ - private static $_instance; - - /** - * Private constructor, use {@link getInstance()} + * Constructor * + * @param array $base_config base configuration read from {@link PMA_Config::$base_config}, + * use only when not in PMA Setup */ - private function __construct() + public function __construct(array $base_config = null) { // load default config values - $cfg = &$this->_cfg; + $cfg = &$this->_defaultCfg; include './libraries/config.default.php'; $cfg['fontsize'] = '82%'; - // create PMA_Config to read config.inc.php values - $this->_orgCfgObject = new PMA_Config(CONFIG_FILE); - // load additional config information $cfg_db = &$this->_cfgDb; include './libraries/config.values.php'; @@ -95,35 +94,14 @@ class ConfigFile } } + $this->_baseCfg = $base_config; + $this->_isInSetup = is_null($base_config); $this->_id = 'ConfigFile' . $GLOBALS['server']; if (!isset($_SESSION[$this->_id])) { $_SESSION[$this->_id] = array(); } } - /** - * Returns class instance - * - * @return ConfigFile - */ - public static function getInstance() - { - if (is_null(self::$_instance)) { - self::$_instance = new ConfigFile(); - } - return self::$_instance; - } - - /** - * Returns PMA_Config without user preferences applied - * - * @return PMA_Config - */ - public function getOrgConfigObj() - { - return $this->_orgCfgObject; - } - /** * Sets names of config options which will be placed in config file even if * they are set to their default values (use only full paths) @@ -132,7 +110,7 @@ class ConfigFile * * @return void */ - public function setPersistKeys($keys) + public function setPersistKeys(array $keys) { // checking key presence is much faster than searching so move values // to keys @@ -225,21 +203,25 @@ class ConfigFile ) { return; } - // remove if the path isn't protected and it's empty or has a default - // value + // if the path isn't protected it may be removed if (!isset($this->_persistKeys[$canonical_path])) { $default_value = $this->getDefault($canonical_path); - // we need original config values not overwritten by user - // preferences to allow for overwriting options set in - // config.inc.php with default values - $instance_default_value = PMA_arrayRead( - $canonical_path, - $this->_orgCfgObject->settings - ); - if (($value === $default_value && (defined('PMA_SETUP') - || $instance_default_value === $default_value)) - || (empty($value) && empty($default_value) && (defined('PMA_SETUP'))) - ) { + $remove_path = $value === $default_value; + if ($this->_isInSetup) { + // remove if it has a default value or is empty + $remove_path = $remove_path || (empty($value) && empty($default_value)); + } else { + // get original config values not overwritten by user + // preferences to allow for overwriting options set in + // config.inc.php with default values + $instance_default_value = PMA_arrayRead( + $canonical_path, + $this->_baseCfg + ); + // remove if it has a default value and base config (config.inc.php) uses default value + $remove_path = $remove_path && ($instance_default_value === $default_value); + } + if ($remove_path) { PMA_arrayRemove($path, $_SESSION[$this->_id]); return; } @@ -277,7 +259,7 @@ class ConfigFile public function getFlatDefaultConfig() { $this->_flattenArrayResult = array(); - array_walk($this->_cfg, array($this, '_flattenArray'), ''); + array_walk($this->_defaultCfg, array($this, '_flattenArray'), ''); $flat_cfg = $this->_flattenArrayResult; $this->_flattenArrayResult = null; return $flat_cfg; @@ -335,7 +317,7 @@ class ConfigFile */ public function getDefault($canonical_path, $default = null) { - return PMA_arrayRead($canonical_path, $this->_cfg, $default); + return PMA_arrayRead($canonical_path, $this->_defaultCfg, $default); } /** @@ -481,7 +463,7 @@ class ConfigFile unset($_SESSION[$this->_id]['Servers'][$last_server]); if (isset($_SESSION[$this->_id]['ServerDefault']) - && $_SESSION[$this->_id]['ServerDefault'] >= 0 + && $_SESSION[$this->_id]['ServerDefault'] == $last_server ) { unset($_SESSION[$this->_id]['ServerDefault']); } @@ -511,8 +493,10 @@ class ConfigFile { $c = $_SESSION[$this->_id]; foreach ($this->_cfgUpdateReadMapping as $map_to => $map_from) { - PMA_arrayWrite($map_to, $c, PMA_arrayRead($map_from, $c)); - PMA_arrayRemove($map_from, $c); + if (PMA_arrayKeyExists($map_to, $c)) { + PMA_arrayWrite($map_to, $c, PMA_arrayRead($map_from, $c)); + PMA_arrayRemove($map_from, $c); + } } return $c; } @@ -534,7 +518,7 @@ class ConfigFile array_keys($c) ); foreach ($persistKeys as $k) { - $c[$k] = $this->getDefault($k); + $c[$k] = $this->getDefault($this->getCanonicalPath($k)); } foreach ($this->_cfgUpdateReadMapping as $map_to => $map_from) { diff --git a/libraries/config/Form.class.php b/libraries/config/Form.class.php index efc7076377..f8d26ef0dd 100644 --- a/libraries/config/Form.class.php +++ b/libraries/config/Form.class.php @@ -44,16 +44,24 @@ class Form */ private $_fieldsTypes; + /** + * ConfigFile instance + * @var ConfigFile + */ + private $_configFile; + /** * Constructor, reads default config values * * @param string $form_name Form name * @param array $form Form data + * @param ConfigFile $cf Config file instance * @param int $index arbitrary index, stored in Form::$index */ - public function __construct($form_name, array $form, $index = null) + public function __construct($form_name, array $form, ConfigFile $cf, $index = null) { $this->index = $index; + $this->_configFile = $cf; $this->loadForm($form_name, $form); } @@ -81,7 +89,7 @@ class Form */ public function getOptionValueList($option_path) { - $value = ConfigFile::getInstance()->getDbEntry($option_path); + $value = $this->_configFile->getDbEntry($option_path); if ($value === null) { trigger_error("$option_path - select options not defined", E_USER_ERROR); return array(); @@ -175,7 +183,7 @@ class Form */ protected function readTypes() { - $cf = ConfigFile::getInstance(); + $cf = $this->_configFile; foreach ($this->fields as $name => $path) { if (strpos($name, ':group:') === 0) { $this->_fieldsTypes[$name] = 'group'; diff --git a/libraries/config/FormDisplay.class.php b/libraries/config/FormDisplay.class.php index 8ed054550f..e6486fa447 100644 --- a/libraries/config/FormDisplay.class.php +++ b/libraries/config/FormDisplay.class.php @@ -27,6 +27,12 @@ require_once './libraries/js_escape.lib.php'; */ class FormDisplay { + /** + * ConfigFile instance + * @var ConfigFile + */ + private $_configFile; + /** * Form list * @var Form[] @@ -81,8 +87,10 @@ class FormDisplay /** * Constructor + * + * @param ConfigFile $cf Config file instance */ - public function __construct() + public function __construct(ConfigFile $cf) { $this->_jsLangStrings = array( 'error_nan_p' => __('Not a positive number'), @@ -90,8 +98,19 @@ class FormDisplay 'error_incorrect_port' => __('Not a valid port number'), 'error_invalid_value' => __('Incorrect value'), 'error_value_lte' => __('Value must be equal or lower than %s')); + $this->_configFile = $cf; // initialize validators - PMA_Validator::getValidators(); + PMA_Validator::getValidators($this->_configFile); + } + + /** + * Returns {@link ConfigFile} associated with this instance + * + * @return ConfigFile + */ + public function getConfigFile() + { + return $this->_configFile; } /** @@ -105,7 +124,7 @@ class FormDisplay */ public function registerForm($form_name, array $form, $server_id = null) { - $this->_forms[$form_name] = new Form($form_name, $form, $server_id); + $this->_forms[$form_name] = new Form($form_name, $form, $this->_configFile, $server_id); $this->_isValidated = false; foreach ($this->_forms[$form_name]->fields as $path) { $work_path = $server_id === null @@ -149,7 +168,6 @@ class FormDisplay return; } - $cf = ConfigFile::getInstance(); $paths = array(); $values = array(); foreach ($this->_forms as $form) { @@ -158,13 +176,13 @@ class FormDisplay // collect values and paths foreach ($form->fields as $path) { $work_path = array_search($path, $this->_systemPaths); - $values[$path] = $cf->getValue($work_path); + $values[$path] = $this->_configFile->getValue($work_path); $paths[] = $path; } } // run validation - $errors = PMA_Validator::validate($paths, $values, false); + $errors = PMA_Validator::validate($this->_configFile, $paths, $values, false); // change error keys from canonical paths to work paths if (is_array($errors) && count($errors) > 0) { @@ -198,7 +216,7 @@ class FormDisplay $js = array(); $js_default = array(); $tabbed_form = $tabbed_form && (count($this->_forms) > 1); - $validators = PMA_Validator::getValidators(); + $validators = PMA_Validator::getValidators($this->_configFile); PMA_displayFormTop(); @@ -315,9 +333,8 @@ class FormDisplay $name = PMA_langName($system_path); $description = PMA_langName($system_path, 'desc', ''); - $cf = ConfigFile::getInstance(); - $value = $cf->get($work_path); - $value_default = $cf->getDefault($system_path); + $value = $this->_configFile->get($work_path); + $value_default = $this->_configFile->getDefault($system_path); $value_is_default = false; if ($value === null || $value === $value_default) { $value = $value_default; @@ -456,7 +473,7 @@ class FormDisplay return; } - $cf = ConfigFile::getInstance(); + $cf = $this->_configFile; foreach (array_keys($this->_errors) as $work_path) { if (!isset($this->_systemPaths[$work_path])) { continue; @@ -508,7 +525,6 @@ class FormDisplay public function save($forms, $allow_partial_save = true) { $result = true; - $cf = ConfigFile::getInstance(); $forms = (array) $forms; $values = array(); @@ -528,7 +544,7 @@ class FormDisplay } // get current server id $change_index = $form->index === 0 - ? $cf->getServerCount() + 1 + ? $this->_configFile->getServerCount() + 1 : false; // grab POST values foreach ($form->fields as $field => $system_path) { @@ -646,10 +662,10 @@ class FormDisplay } $values[$path] = $proxies; } - $cf->set($work_path, $values[$path], $path); + $this->_configFile->set($work_path, $values[$path], $path); } if ($is_setup_script) { - $cf->set( + $this->_configFile->set( 'UserprefsDisallow', array_keys($this->_userprefsDisallow) ); @@ -744,7 +760,7 @@ class FormDisplay $this->_userprefsKeys = array_flip(PMA_readUserprefsFieldNames()); // read real config for user preferences display $userprefs_disallow = defined('PMA_SETUP') - ? ConfigFile::getInstance()->get('UserprefsDisallow', array()) + ? $this->_configFile->get('UserprefsDisallow', array()) : $GLOBALS['cfg']['UserprefsDisallow']; $this->_userprefsDisallow = array_flip($userprefs_disallow); } diff --git a/libraries/config/Validator.class.php b/libraries/config/Validator.class.php index a9c24e1d31..0a885689bc 100644 --- a/libraries/config/Validator.class.php +++ b/libraries/config/Validator.class.php @@ -25,21 +25,20 @@ class PMA_Validator /** * Returns validator list * + * @param ConfigFile $cf Config file instance * @return array */ - public static function getValidators() + public static function getValidators(ConfigFile $cf) { static $validators = null; if ($validators === null) { - $cf = ConfigFile::getInstance(); $validators = $cf->getDbEntry('_validators', array()); if (!defined('PMA_SETUP')) { // not in setup script: load additional validators for user // preferences we need original config values not overwritten // by user preferences, creating a new PMA_Config instance is a // better idea than hacking into its code - $org_cfg = $cf->getOrgConfigObj(); $uvs = $cf->getDbEntry('_userValidators', array()); foreach ($uvs as $field => $uv_list) { $uv_list = (array)$uv_list; @@ -50,7 +49,7 @@ class PMA_Validator for ($i = 1; $i < count($uv); $i++) { if (substr($uv[$i], 0, 6) == 'value:') { $uv[$i] = PMA_arrayRead( - substr($uv[$i], 6), $org_cfg->settings + substr($uv[$i], 6), $GLOBALS['PMA_Config']->base_settings ); } } @@ -73,6 +72,7 @@ class PMA_Validator * cleanup in HTML documen * o false - when no validators match name(s) given by $validator_id * + * @param ConfigFile $cf Config file instance * @param string|array $validator_id ID of validator(s) to run * @param array &$values Values to validate * @param bool $isPostSource tells whether $values are directly from @@ -80,13 +80,12 @@ class PMA_Validator * * @return bool|array */ - public static function validate($validator_id, &$values, $isPostSource) + public static function validate(ConfigFile $cf, $validator_id, &$values, $isPostSource) { // find validators $validator_id = (array) $validator_id; - $validators = static::getValidators(); + $validators = static::getValidators($cf); $vids = array(); - $cf = ConfigFile::getInstance(); foreach ($validator_id as &$vid) { $vid = $cf->getCanonicalPath($vid); if (isset($validators[$vid])) { diff --git a/libraries/core.lib.php b/libraries/core.lib.php index bc283b74f3..c58ad31a4f 100644 --- a/libraries/core.lib.php +++ b/libraries/core.lib.php @@ -654,6 +654,28 @@ function PMA_downloadHeader($filename, $mimetype, $length = 0, $no_cache = true) } } +/** + * Checks whether element given by $path exists in $array. + * $path is a string describing position of an element in an associative array, + * eg. Servers/1/host refers to $array[Servers][1][host] + * + * @param string $path path in the arry + * @param array $array the array + * + * @return mixed array element or $default + */ +function PMA_arrayKeyExists($path, $array) +{ + $keys = explode('/', $path); + $value =& $array; + foreach ($keys as $key) { + if (! isset($value[$key])) { + return false; + } + $value =& $value[$key]; + } + return true; +} /** * Returns value of an element in $array given by $path. diff --git a/libraries/user_preferences.inc.php b/libraries/user_preferences.inc.php index b0ed4cc511..d338bcd224 100644 --- a/libraries/user_preferences.inc.php +++ b/libraries/user_preferences.inc.php @@ -47,7 +47,7 @@ if (!empty($_GET['saved'])) { } /* debug code -$arr = ConfigFile::getInstance()->getConfigArray(); +$arr = $cf->getConfigArray(); $arr2 = array(); foreach ($arr as $k => $v) { $arr2[] = "$k " . var_export($v, true); diff --git a/libraries/user_preferences.lib.php b/libraries/user_preferences.lib.php index 5e83251198..3974c628a0 100644 --- a/libraries/user_preferences.lib.php +++ b/libraries/user_preferences.lib.php @@ -12,12 +12,12 @@ if (! defined('PHPMYADMIN')) { /** * Common initialization for user preferences modification pages * + * @param ConfigFile $cf Config file instance * @return void */ -function PMA_userprefsPageInit() +function PMA_userprefsPageInit(ConfigFile $cf) { $forms_all_keys = PMA_readUserprefsFieldNames($GLOBALS['forms']); - $cf = ConfigFile::getInstance(); $cf->resetConfigData(); // start with a clean instance $cf->setAllowedKeys($forms_all_keys); $cf->setCfgUpdateReadMapping( diff --git a/prefs_forms.php b/prefs_forms.php index 5d8b787dee..c44e77519f 100644 --- a/prefs_forms.php +++ b/prefs_forms.php @@ -18,7 +18,8 @@ require_once 'libraries/config/Form.class.php'; require_once 'libraries/config/FormDisplay.class.php'; require 'libraries/config/user_preferences.forms.php'; -PMA_userprefsPageInit(); +$cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings); +PMA_userprefsPageInit($cf); // handle form processing @@ -28,7 +29,7 @@ if (! isset($forms[$form_param])) { $form_param = array_shift($forms_keys); } -$form_display = new FormDisplay(); +$form_display = new FormDisplay($cf); foreach ($forms[$form_param] as $form_name => $form) { // skip Developer form if no setting is available if ($form_name == 'Developer' && !$GLOBALS['cfg']['UserprefsDeveloperTab']) { @@ -52,7 +53,7 @@ if (isset($_POST['revert'])) { $error = null; if ($form_display->process(false) && !$form_display->hasErrors()) { // save settings - $result = PMA_saveUserprefs(ConfigFile::getInstance()->getConfigArray()); + $result = PMA_saveUserprefs($cf->getConfigArray()); if ($result === true) { // reload config $GLOBALS['PMA_Config']->loadUserPreferences(); diff --git a/prefs_manage.php b/prefs_manage.php index 161659903f..2227ffb17c 100644 --- a/prefs_manage.php +++ b/prefs_manage.php @@ -18,7 +18,8 @@ require_once 'libraries/config/Form.class.php'; require_once 'libraries/config/FormDisplay.class.php'; require 'libraries/config/user_preferences.forms.php'; -PMA_userprefsPageInit(); +$cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings); +PMA_userprefsPageInit($cf); $error = ''; if (isset($_POST['submit_export']) @@ -82,13 +83,12 @@ if (isset($_POST['submit_export']) } else { // sanitize input values: treat them as though // they came from HTTP POST request - $form_display = new FormDisplay(); + $form_display = new FormDisplay($cf); foreach ($forms as $formset_id => $formset) { foreach ($formset as $form_name => $form) { $form_display->registerForm($formset_id . ': ' . $form_name, $form); } } - $cf = ConfigFile::getInstance(); $new_config = $cf->getFlatDefaultConfig(); if (!empty($_POST['import_merge'])) { $new_config = array_merge($new_config, $cf->getConfigArray()); @@ -238,7 +238,7 @@ PMA_printJsValue("PMA_messages['strSavedOn']", __('Saved on: @DATE@'));

diff --git a/setup/config.php b/setup/config.php index e923211132..9074ebe5ac 100644 --- a/setup/config.php +++ b/setup/config.php @@ -16,10 +16,10 @@ require_once './setup/lib/ConfigGenerator.class.php'; require './libraries/config/setup.forms.php'; -$form_display = new FormDisplay(); +$form_display = new FormDisplay($GLOBALS['ConfigFile']); $form_display->registerForm('_config.php', $forms['_config.php']); $form_display->save('_config.php'); -$config_file_path = ConfigFile::getInstance()->getFilePath(); +$config_file_path = $GLOBALS['ConfigFile']->getFilePath(); if (isset($_POST['eol'])) { $_SESSION['eol'] = ($_POST['eol'] == 'unix') ? 'unix' : 'win'; @@ -29,7 +29,7 @@ if (PMA_ifSetOr($_POST['submit_clear'], '')) { // // Clear current config and return to main page // - ConfigFile::getInstance()->resetConfigData(); + $GLOBALS['ConfigFile']->resetConfigData(); // drop post data header('HTTP/1.1 303 See Other'); header('Location: index.php'); @@ -39,13 +39,13 @@ if (PMA_ifSetOr($_POST['submit_clear'], '')) { // Output generated config file // PMA_downloadHeader('config.inc.php', 'text/plain'); - echo ConfigGenerator::getConfigFile(); + echo ConfigGenerator::getConfigFile($GLOBALS['ConfigFile']); exit; } elseif (PMA_ifSetOr($_POST['submit_save'], '')) { // // Save generated config file on the server // - file_put_contents($config_file_path, ConfigGenerator::getConfigFile()); + file_put_contents($config_file_path, ConfigGenerator::getConfigFile($GLOBALS['ConfigFile'])); header('HTTP/1.1 303 See Other'); header('Location: index.php?action_done=config_saved'); exit; @@ -55,7 +55,7 @@ if (PMA_ifSetOr($_POST['submit_clear'], '')) { // $cfg = array(); include_once $config_file_path; - ConfigFile::getInstance()->setConfigData($cfg); + $GLOBALS['ConfigFile']->setConfigData($cfg); header('HTTP/1.1 303 See Other'); header('Location: index.php'); exit; diff --git a/setup/frames/config.inc.php b/setup/frames/config.inc.php index a0e7d96212..f265160989 100644 --- a/setup/frames/config.inc.php +++ b/setup/frames/config.inc.php @@ -26,12 +26,12 @@ echo '

' . __('Configuration file') . '

'; PMA_displayFormTop('config.php'); echo ''; -PMA_displayFieldsetTop('', '', null, array('class' => 'simple')); +PMA_displayFieldsetTop('config.inc.php', '', null, array('class' => 'simple')); echo ''; echo ''; echo ''; echo ''; echo ''; diff --git a/setup/frames/form.inc.php b/setup/frames/form.inc.php index cf188eb2b0..c747e06ef3 100644 --- a/setup/frames/form.inc.php +++ b/setup/frames/form.inc.php @@ -28,7 +28,7 @@ if (! isset($forms[$formset_id])) { if (isset($GLOBALS['strConfigFormset_' . $formset_id])) { echo '

' . $GLOBALS['strConfigFormset_' . $formset_id] . '

'; } -$form_display = new FormDisplay(); +$form_display = new FormDisplay($GLOBALS['ConfigFile']); foreach ($forms[$formset_id] as $form_name => $form) { $form_display->registerForm($form_name, $form); } diff --git a/setup/frames/index.inc.php b/setup/frames/index.inc.php index f34599b93f..62ac09822f 100644 --- a/setup/frames/index.inc.php +++ b/setup/frames/index.inc.php @@ -21,7 +21,7 @@ require_once './setup/lib/index.lib.php'; $all_languages = PMA_langList(); uasort($all_languages, 'PMA_languageCmp'); -$cf = ConfigFile::getInstance(); +$cf = $GLOBALS['ConfigFile']; $separator = PMA_URL_getArgSeparator('html'); // message handling @@ -211,7 +211,7 @@ echo '
' . __('Configuration file') . ' // // Display config file settings and load/save form // -$form_display = new FormDisplay(); +$form_display = new FormDisplay($cf); PMA_displayFormTop('config.php'); echo ''; diff --git a/setup/frames/servers.inc.php b/setup/frames/servers.inc.php index cbd612e3ef..10c02ac9cd 100644 --- a/setup/frames/servers.inc.php +++ b/setup/frames/servers.inc.php @@ -22,7 +22,7 @@ require './libraries/config/setup.forms.php'; $mode = filter_input(INPUT_GET, 'mode'); $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT); -$cf = ConfigFile::getInstance(); +$cf = $GLOBALS['ConfigFile']; $server_exists = !empty($id) && $cf->get("Servers/$id") !== null; if ($mode == 'edit' && $server_exists) { @@ -42,7 +42,7 @@ if ($mode == 'edit' && $server_exists) { if (isset($page_title)) { echo '

' . $page_title . '

'; } -$form_display = new FormDisplay(); +$form_display = new FormDisplay($cf); foreach ($forms['Servers'] as $form_name => $form) { $form_display->registerForm($form_name, $form, $id); } diff --git a/setup/lib/ConfigGenerator.class.php b/setup/lib/ConfigGenerator.class.php index 82bc940bbc..d734cbc755 100644 --- a/setup/lib/ConfigGenerator.class.php +++ b/setup/lib/ConfigGenerator.class.php @@ -16,12 +16,11 @@ class ConfigGenerator /** * Creates config file * + * @param ConfigFile $cf Config file instance * @return string */ - public static function getConfigFile() + public static function getConfigFile(ConfigFile $cf) { - $cf = ConfigFile::getInstance(); - $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n"; $c = $cf->getConfig(); diff --git a/setup/lib/common.inc.php b/setup/lib/common.inc.php index 47443217ea..b9fc2260cc 100644 --- a/setup/lib/common.inc.php +++ b/setup/lib/common.inc.php @@ -32,7 +32,8 @@ restore_error_handler(); // Save current language in a cookie, required since we use PMA_MINIMUM_COMMON $GLOBALS['PMA_Config']->setCookie('pma_lang', $GLOBALS['lang']); -ConfigFile::getInstance()->setPersistKeys( +$GLOBALS['ConfigFile'] = new ConfigFile(); +$GLOBALS['ConfigFile']->setPersistKeys( array( 'DefaultLang', 'ServerDefault', diff --git a/setup/lib/form_processing.lib.php b/setup/lib/form_processing.lib.php index 082c28675b..9ca3b5317a 100644 --- a/setup/lib/form_processing.lib.php +++ b/setup/lib/form_processing.lib.php @@ -40,7 +40,7 @@ function process_formset(FormDisplay $form_display) $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT); if ($id === null && $page == 'servers') { // we've just added a new server, get it's id - $id = ConfigFile::getInstance()->getServerCount(); + $id = $form_display->getConfigFile()->getServerCount(); } $id = $id ? "{$separator}id=$id" : ''; ?> diff --git a/setup/lib/index.lib.php b/setup/lib/index.lib.php index 98f633f95e..c7c6ae312e 100644 --- a/setup/lib/index.lib.php +++ b/setup/lib/index.lib.php @@ -186,7 +186,7 @@ function PMA_versionCheck() */ function PMA_checkConfigRw(&$is_readable, &$is_writable, &$file_exists) { - $file_path = ConfigFile::getInstance()->getFilePath(); + $file_path = $GLOBALS['ConfigFile']->getFilePath(); $file_dir = dirname($file_path); $is_readable = true; $is_writable = is_dir($file_dir); @@ -210,7 +210,7 @@ function PMA_checkConfigRw(&$is_readable, &$is_writable, &$file_exists) */ function PMA_performConfigChecks() { - $cf = ConfigFile::getInstance(); + $cf = $GLOBALS['ConfigFile']; $blowfish_secret = $cf->get('blowfish_secret'); $blowfish_secret_set = false; $cookie_auth_used = false; diff --git a/setup/validate.php b/setup/validate.php index 7c2308a3c6..fba2725dc3 100644 --- a/setup/validate.php +++ b/setup/validate.php @@ -22,7 +22,7 @@ if (!($values instanceof stdClass)) { PMA_fatalError(__('Wrong data')); } $values = (array)$values; -$result = PMA_Validator::validate($vids, $values, true); +$result = PMA_Validator::validate($GLOBALS['ConfigFile'], $vids, $values, true); if ($result === false) { $result = 'Wrong data or no validation for ' . $vids; } diff --git a/test/classes/config/PMA_FormDisplay_test.php b/test/classes/config/PMA_FormDisplay_test.php index 51549eb6f9..329b6e4016 100644 --- a/test/classes/config/PMA_FormDisplay_test.php +++ b/test/classes/config/PMA_FormDisplay_test.php @@ -7,7 +7,9 @@ */ require_once 'libraries/config/ConfigFile.class.php'; +require_once 'libraries/config/Form.class.php'; require_once 'libraries/config/FormDisplay.class.php'; +require_once 'libraries/config/config_functions.lib.php'; require_once 'libraries/Util.class.php'; require_once 'libraries/Theme.class.php'; require_once 'libraries/Config.class.php'; @@ -21,6 +23,9 @@ require_once 'libraries/user_preferences.lib.php'; */ class PMA_FormDisplay_Test extends PHPUnit_Framework_TestCase { + /** + * @var FormDisplay + */ protected $object; /** @@ -36,7 +41,7 @@ class PMA_FormDisplay_Test extends PHPUnit_Framework_TestCase $GLOBALS['PMA_Config'] = new PMA_Config(); $GLOBALS['PMA_Config']->enableBc(); $GLOBALS['server'] = 0; - $this->object = new FormDisplay(); + $this->object = new FormDisplay(new ConfigFile()); } /** @@ -512,7 +517,7 @@ class PMA_FormDisplay_Test extends PHPUnit_Framework_TestCase ); $comment = ''; - if (!function_exists("gzopen")) { + if (!function_exists("bzopen")) { $comment = 'Compressed import will not work due to missing function ' . 'bzopen.'; } diff --git a/test/classes/config/PMA_Form_test.php b/test/classes/config/PMA_Form_test.php index 095561abfd..dc8caed31e 100644 --- a/test/classes/config/PMA_Form_test.php +++ b/test/classes/config/PMA_Form_test.php @@ -20,6 +20,9 @@ require_once 'libraries/php-gettext/gettext.inc'; */ class PMA_Form_Test extends PHPUnit_Framework_TestCase { + /** + * @var Form + */ protected $object; /** @@ -35,7 +38,7 @@ class PMA_Form_Test extends PHPUnit_Framework_TestCase $GLOBALS['PMA_Config'] = new PMA_Config(); $GLOBALS['PMA_Config']->enableBc(); $GLOBALS['server'] = 0; - $this->object = new Form('pma_form_name', array('pma_form1','pma_form2'), 1); + $this->object = new Form('pma_form_name', array('pma_form1','pma_form2'), new ConfigFile(), 1); } /** diff --git a/test/classes/plugin/auth/PMA_AuthenticationCookie_test.php b/test/classes/plugin/auth/PMA_AuthenticationCookie_test.php index 8b506bccba..412da0769f 100644 --- a/test/classes/plugin/auth/PMA_AuthenticationCookie_test.php +++ b/test/classes/plugin/auth/PMA_AuthenticationCookie_test.php @@ -903,9 +903,13 @@ class PMA_AuthenticationCookie_Test extends PHPUnit_Framework_TestCase isset($_COOKIE['pmaServer-2']) ); + // target can be "phpunit" or "ide-phpunut.php", depending on testing environment + $this->assertStringStartsWith( + 'Location: http://phpmyadmin.net/index.php?target=', + $GLOBALS['header'][0] + ); $this->assertContains( - 'Location: http://phpmyadmin.net/index.php?target=phpunit&server=2' . - '&lang=en&collation_connection=utf-8&token=token&PHPSESSID=', + '&server=2&lang=en&collation_connection=utf-8&token=token&PHPSESSID=', $GLOBALS['header'][0] ); diff --git a/test/classes/plugin/auth/PMA_AuthenticationHttp_test.php b/test/classes/plugin/auth/PMA_AuthenticationHttp_test.php index e0bab34f40..55e07f139c 100644 --- a/test/classes/plugin/auth/PMA_AuthenticationHttp_test.php +++ b/test/classes/plugin/auth/PMA_AuthenticationHttp_test.php @@ -22,6 +22,9 @@ require_once 'libraries/Error_Handler.class.php'; */ class PMA_AuthenticationHttp_Test extends PHPUnit_Framework_TestCase { + /** + * @var AuthenticationHttp + */ protected $object; /** diff --git a/test/libraries/PMA_ConfigFile_test.php b/test/libraries/PMA_ConfigFile_test.php index 096c8f6fa1..b43f961a11 100644 --- a/test/libraries/PMA_ConfigFile_test.php +++ b/test/libraries/PMA_ConfigFile_test.php @@ -19,6 +19,16 @@ require_once 'libraries/php-gettext/gettext.inc'; */ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase { + /** + * Any valid key that exists in config.default.php and isn't empty + * @var string + */ + const SIMPLE_KEY_WITH_DEFAULT_VALUE = 'DefaultQueryTable'; + + /** + * Object under test + * @var ConfigFile + */ protected $object; /** @@ -29,9 +39,9 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ protected function setUp() { - $GLOBALS['cfg']['AvailableCharsets'] = array(); $GLOBALS['server'] = 1; - $this->object = ConfigFile::getInstance(); + $GLOBALS['cfg']['AvailableCharsets'] = array(); + $this->object = new ConfigFile(); } /** @@ -41,14 +51,8 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ protected function tearDown() { - unset($_SESSION[$this->readAttribute($this->object, "_id")]); unset($this->object); - - // reset the instance - $attr_instance = new ReflectionProperty("ConfigFile", "_instance"); - $attr_instance->setAccessible(true); - $attr_instance->setValue(null, null); } /** @@ -57,22 +61,12 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase * @return void * @test */ - public function testConfigFileConstructor() + public function testNewObjectState() { - $attr_instance = new ReflectionProperty("ConfigFile", "_instance"); - $attr_instance->setAccessible(true); - $attr_instance->setValue(null, null); - $this->object = ConfigFile::getInstance(); - $cfg = $this->readAttribute($this->object, '_cfg'); - + // Check default dynamic values $this->assertEquals( "82%", - $cfg['fontsize'] - ); - - $this->assertInstanceOf( - "PMA_Config", - $this->readAttribute($this->object, '_orgCfgObject') + $this->object->getDefault('fontsize') ); if (extension_loaded('mysqli')) { @@ -83,48 +77,22 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase $this->assertEquals( $expect, - $cfg['Servers'][1]['extension'] + $this->object->getDefault('Servers/1/extension') + ); + $this->assertEquals( + array(), + $this->object->getConfig() ); + // Check environment state $this->assertEquals( array(), $_SESSION["ConfigFile1"] ); - $this->assertAttributeEquals( - "ConfigFile1", - "_id", - $this->object - ); - - } - - /** - * Test for ConfigFile::getInstance() - * - * @return void - * @test - */ - public function testGetInstance() - { - $this->assertInstanceOf( - "ConfigFile", - $this->object - ); - } - - /** - * Test for ConfigFile::getOrgConfigObj() - * - * @return void - * @test - */ - public function testGetOrgConfigObj() - { - $this->assertEquals( - $this->readAttribute($this->object, '_orgCfgObject'), - $this->object->getOrgConfigObj() - ); + // Validate default value used in tests + $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE); + $this->assertNotNull($default_value); } /** @@ -133,26 +101,42 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase * @return void * @test */ - public function testSetPersistKeys() + public function testPersistentKeys() { - $this->object->setPersistKeys(array("a" => 1, "b" => 1, "c" => 2)); - $this->assertEquals( - array("1" => "b", "2" => "c"), - $this->readAttribute($this->object, '_persistKeys') - ); - } + $default_simple_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE); + $default_host = $this->object->getDefault('Servers/1/host'); + $default_config = array( + self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_simple_value, + 'Servers/1/host' => $default_host, + 'Servers/2/host' => $default_host); - /** - * Test for ConfigFile::getPersistKeysMap - * - * @return void - * @test - */ - public function testGetPersistKeysMap() - { + /** + * Case 1: set default value, key should not be persisted + */ + $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_simple_value); + $this->object->set('Servers/1/host', $default_host); + $this->object->set('Servers/2/host', $default_host); + $this->assertEmpty($this->object->getConfig()); + + /** + * Case 2: persistent keys should be always present in flat array, even if not explicitly set + * (unless they are Server entries) + */ + $this->object->setPersistKeys(array_keys($default_config)); + $this->object->resetConfigData(); + $this->assertEmpty($this->object->getConfig()); $this->assertEquals( - $this->readAttribute($this->object, '_persistKeys'), - $this->object->getPersistKeysMap() + $default_config, + $this->object->getConfigArray() + ); + + /** + * Case 3: persistent keys should be always saved, even if set to default values + */ + $this->object->set('Servers/2/host', $default_host); + $this->assertEquals( + array('Servers' => array(2 => array('host' => $default_host))), + $this->object->getConfig() ); } @@ -162,18 +146,31 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase * @return void * @test */ - public function testSetAllowedKeys() + public function testAllowedKeys() { - $this->object->setAllowedKeys(array("a" => 1, "c" => 2)); + /** + * Case 1: filter should not allow to set b + */ + $this->object->setAllowedKeys(array('a', 'c')); + $this->object->set('a', 1); + $this->object->set('b', 2); + $this->object->set('c', 3); + $this->assertEquals( - array("1" => "a", "2" => "c"), - $this->readAttribute($this->object, '_setFilter') + array('a' => 1, 'c' => 3), + $this->object->getConfig() ); + + /** + * Case 2: disabling filter should allow to set b + */ $this->object->setAllowedKeys(null); + $this->object->set('b', 2); + $this->assertEquals( - null, - $this->readAttribute($this->object, '_setFilter') + array('a' => 1, 'b' => 2, 'c' => 3), + $this->object->getConfig() ); } @@ -183,12 +180,26 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase * @return void * @test */ - public function testSetCfgUpdateReadMapping() + public function testConfigReadMapping() { - $this->object->setCfgUpdateReadMapping(array(1, 2, 3)); + $this->object->setCfgUpdateReadMapping(array( + 'Servers/value1' => 'Servers/1/value1', + 'Servers/value2' => 'Servers/1/value2')); + $this->object->set('Servers/1/passthrough1', 1); + $this->object->set('Servers/1/passthrough2', 2); + $this->object->updateWithGlobalConfig(array('Servers/value1' => 3)); + $this->assertEquals( - array(1, 2, 3), - $this->readAttribute($this->object, '_cfgUpdateReadMapping') + array('Servers' => array( + 1 => array( + 'passthrough1' => 1, + 'passthrough2' => 2, + 'value1' => 3))), + $this->object->getConfig() + ); + $this->assertEquals( + 3, + $this->object->get('Servers/1/value1') ); } @@ -200,15 +211,12 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testResetConfigData() { - $selfid = $this->readAttribute($this->object, '_id'); - $_SESSION[$selfid] = "foo"; + $this->object->set('key', 'value'); $this->object->resetConfigData(); - $this->assertEquals( - array(), - $_SESSION[$selfid] - ); + $this->assertEmpty($this->object->getConfig()); + $this->assertEmpty($this->object->getConfigArray()); } /** @@ -219,168 +227,99 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testSetConfigData() { - $selfid = $this->readAttribute($this->object, '_id'); - - $this->object->setConfigData(array("foo")); + $this->object->set('abc', 'should be deleted by setConfigData'); + $this->object->setConfigData(array('a' => 'b')); $this->assertEquals( - array("foo"), - $_SESSION[$selfid] + array('a' => 'b'), + $this->object->getConfig() + ); + $this->assertEquals( + array('a' => 'b'), + $this->object->getConfigArray() ); } /** - * Test for ConfigFile::set + * Test for ConfigFile::set and ConfigFile::get * * @return void * @test */ - public function testConfigFileSet() + public function testBasicSetUsage() { - if (! PMA_HAS_RUNKIT) { - $this->markTestSkipped("Cannot redefine constant"); - } + $default_host = $this->object->getDefault('Servers/1/host'); + $nondefault_host = $default_host . '.abc'; - $reflection = new \ReflectionClass("ConfigFile"); - - $attrSetFilter = $reflection->getProperty("_setFilter"); - $attrSetFilter->setAccessible(true); - - $attrCfg = $reflection->getProperty('_cfg'); - $attrCfg->setAccessible(true); - - $attrCfgObject = $reflection->getProperty('_orgCfgObject'); - $attrCfgObject->setAccessible(true); - - $attrConfigProperty = new \ReflectionProperty("PMA_Config", "settings"); - $attrConfigProperty->setAccessible(true); - - /** - * Case 1 - */ - $attrSetFilter->setValue($this->object, array()); + $this->object->set('Servers/4/host', $nondefault_host); + $this->object->set('Servers/5/host', $default_host); + $this->object->set('Servers/6/host', $default_host, 'Servers/6/host'); + $this->assertEquals( + $nondefault_host, + $this->object->get('Servers/4/host') + ); + $this->assertEquals( + null, + $this->object->get('Servers/5/host') + ); + $this->assertEquals( + $default_host, + $this->object->get('Servers/6/host') + ); + // return default value for nonexistent keys $this->assertNull( - $this->object->set("a", "b") + $this->object->get('key not excist') ); - - /** - * Case 2 - */ - $this->object->setPersistKeys(array("Servers/1/test" => 1)); - $attrSetFilter->setValue($this->object, array("Servers/1/test" => 1)); - $this->object->set("Servers/42/test", "val"); - - $expectedArr = array("Servers" => array("42" => array("test" => "val"))); - $this->assertEquals( - $expectedArr, - $_SESSION[$this->readAttribute($this->object, "_id")] + array(1), + $this->object->get('key not excist', array(1)) ); - - $expectedArr = array("Servers" => array("42" => "val")); - - /** - * Case 3 - */ - $attrSetFilter->setValue($this->object, array("Servers/42" => 1)); - $attrCfg->setValue($this->object, $expectedArr); - $attrConfigProperty->setValue( - $attrCfgObject->getValue($this->object), - array() + $default = new stdClass(); + $this->assertInstanceOf( + 'stdClass', + $this->object->get('key not excist', $default) ); - - $pma_setup = null; - - if (!defined('PMA_SETUP')) { - define('PMA_SETUP', true); - } else { - $pma_setup = PMA_SETUP; - runkit_constant_redefine('PMA_SETUP', true); - } - - $this->object->setPersistKeys(array("Servers/42" => 1)); - $this->object->set("Servers/42", "val"); - - $this->assertEquals( - array(), - $_SESSION[$this->readAttribute($this->object, "_id")] - ); - - /** - * Case 4 - */ - $attrConfigProperty->setValue( - $attrCfgObject->getValue($this->object), - $expectedArr - ); - - $this->object->set("Servers/42", "val"); - $this->assertEquals( - array(), - $_SESSION[$this->readAttribute($this->object, "_id")] - ); - - /** - * Case 5 - */ - $attrCfg->setValue($this->object, array()); - - $this->object->set("Servers/42", ""); - $this->assertEquals( - array(), - $_SESSION[$this->readAttribute($this->object, "_id")] - ); - - /** - * Case 6 - */ - $this->object->set("Servers/42", "foobar"); - runkit_constant_redefine('PMA_SETUP', false); - - $this->assertEquals( - array("Servers" => array("42" => "foobar")), - $_SESSION[$this->readAttribute($this->object, "_id")] - ); - - if ($pma_setup) { - runkit_constant_redefine("PMA_SETUP", $pma_setup); - } else { - runkit_constant_remove("PMA_SETUP"); - } } /** - * Test for ConfigFile::_flattenArray + * Test for ConfigFile::set - in PMA Setup * * @return void * @test */ - public function testFlattenArray() + public function testConfigFileSetInSetup() { - $method = new \ReflectionMethod("ConfigFile", "_flattenArray"); - $method->setAccessible(true); + $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE); - $method->invoke( - $this->object, - array( - "one" => array("foo" => "bar"), - "two" => array(1, 2, 3), - "three" => 3 - ), - "one", - "foobar" - ); + // default values are not written + $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_value); + $this->assertEmpty($this->object->getConfig()); + } - $expectArr = array( - "foobarone/one/foo" => "bar", - "foobarone/two" => array(1, 2, 3), - "foobarone/three" => 3 - ); + /** + * Test for ConfigFile::set - in user preferences + * + * @return void + * @test + */ + public function testConfigFileSetInUserPreferences() + { + $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE); + // values are not written when they are the same as in config.inc.php + $this->object = new ConfigFile(array(self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_value)); + $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_value); + $this->assertEmpty($this->object->getConfig()); + + // but if config.inc.php differs from config.default.php, allow to overwrite with + // value from config.default.php + $config_inc_php_value = $default_value . 'suffix'; + $this->object = new ConfigFile(array(self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $config_inc_php_value)); + $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_value); $this->assertEquals( - $expectArr, - $this->readAttribute($this->object, "_flattenArrayResult") + array(self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_value), + $this->object->getConfig() ); } @@ -392,27 +331,19 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testGetFlatDefaultConfig() { - $attrCfg = new \ReflectionProperty('ConfigFile', '_cfg'); - $attrCfg->setAccessible(true); - $attrCfg->setValue( - $this->object, - array( - "one" => array("foo" => "bar"), - "two" => array(1, 2, 3), - "three" => 3 - ) - ); + $flat_default_config = $this->object->getFlatDefaultConfig(); - $expectArr = array( - "one/foo" => "bar", - "two" => array(1, 2, 3), - "three" => 3 - ); + $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE); + $this->assertEquals($default_value, $flat_default_config[self::SIMPLE_KEY_WITH_DEFAULT_VALUE]); - $this->assertEquals( - $expectArr, - $this->object->getFlatDefaultConfig() - ); + $localhost_value = $this->object->getDefault('Servers/1/host'); + $this->assertEquals($localhost_value, $flat_default_config['Servers/1/host']); + + $cfg = array(); + include './libraries/config.default.php'; + // verify that $cfg read from config.default.php is valid + $this->assertGreaterThanOrEqual(100, count($cfg)); + $this->assertGreaterThanOrEqual(count($cfg), count($flat_default_config)); } /** @@ -423,133 +354,13 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testUpdateWithGlobalConfig() { - $this->object = $this->getMockBuilder('ConfigFile') - ->setMethods(array("set")) - ->disableOriginalConstructor() - ->getMock(); - - $reflection = new \ReflectionClass('ConfigFile'); - - $attrReadMapping = $reflection->getProperty('_cfgUpdateReadMapping'); - $attrReadMapping->setAccessible(true); - $attrReadMapping->setValue( - $this->object, - array("one/foo" => "one/foobar") - ); - - - $this->object - ->expects($this->at(0)) - ->method('set') - ->with("one/foobar", "bar", "one/foobar"); - - $this->object - ->expects($this->at(1)) - ->method('set') - ->with("two", array(1, 2, 3), "two"); - - $this->object->updateWithGlobalConfig( - array( - "one" => array("foo" => "bar"), - "two" => array(1, 2, 3) - ) - ); - } - - /** - * Test for ConfigFile::get - * - * @return void - * @test - */ - public function testConfigFileGet() - { - $_SESSION[$this->readAttribute($this->object, "_id")] = array( - "1" => array("2" => "val") - ); + $this->object->set('key', 'value'); + $this->object->set('key2', 'value'); + $this->object->updateWithGlobalConfig(array('key' => 'ABC')); $this->assertEquals( - "val", - $this->object->get("1/2") - ); - - $_SESSION[$this->readAttribute($this->object, "_id")] = array( - "1" => array() - ); - - $this->assertEquals( - "foobar", - $this->object->get("1/2", "foobar") - ); - } - - /** - * Test for ConfigFile::getValue - * - * @return void - * @test - */ - public function testGetValue() - { - $_SESSION[$this->readAttribute($this->object, "_id")] = array( - "Servers" => array("2" => "val") - ); - - $this->assertEquals( - "val", - $this->object->getValue("Servers/2") - ); - - $_SESSION[$this->readAttribute($this->object, "_id")] = array(); - - $reflection = new \ReflectionClass('ConfigFile'); - $attrCfg = $reflection->getProperty('_cfg'); - $attrCfg->setAccessible(true); - $attrCfg->setValue( - $this->object, - array( - "Servers" => array("1" => array("test" => "val")) - ) - ); - - $this->assertEquals( - "val", - $this->object->getValue("Servers/2/test") - ); - } - - /** - * Test for ConfigFile::getDefault - * - * @return void - * @test - */ - public function testGetDefault() - { - - $reflection = new \ReflectionClass('ConfigFile'); - $attrCfg = $reflection->getProperty('_cfg'); - $attrCfg->setAccessible(true); - $attrCfg->setValue( - $this->object, - array( - "Servers" => array("1" => array("test" => "val")) - ) - ); - $this->assertEquals( - "val", - $this->object->getDefault("Servers/1/test") - ); - - $attrCfg->setValue( - $this->object, - array( - "Servers" => array() - ) - ); - $this->assertEquals( - "val", - $this->object->getDefault("Servers/1/test", "val") + array('key' => 'ABC', 'key2' => 'value'), + $this->object->getConfig() ); } @@ -580,30 +391,18 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testGetDbEntry() { - $reflection = new \ReflectionClass('ConfigFile'); - $attrCfg = $reflection->getProperty('_cfgDb'); - $attrCfg->setAccessible(true); - $attrCfg->setValue( - $this->object, - array( - "Servers" => array("1" => array("test" => "val")) - ) - ); - $this->assertEquals( - "val", - $this->object->getDbEntry("Servers/1/test") - ); + $cfg_db = array(); + include './libraries/config.values.php'; + // verify that $cfg_db read from config.values.php is valid + $this->assertGreaterThanOrEqual(20, count($cfg_db)); - $attrCfg->setValue( - $this->object, - array( - "Servers" => array() - ) - ); $this->assertEquals( - "val", - $this->object->getDbEntry("Servers/1/test", "val") - ); + $cfg_db['Servers'][1]['port'], + $this->object->getDbEntry('Servers/1/port')); + $this->assertNull($this->object->getDbEntry('no such key')); + $this->assertEquals( + array(1), + $this->object->getDbEntry('no such key', array(1))); } /** @@ -614,21 +413,37 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testGetServerCount() { - $_SESSION[$this->readAttribute($this->object, '_id')]['Servers'] = array( - 1, 2, 3, 4 - ); + $this->object->set('Servers/1/x', 1); + $this->object->set('Servers/2/x', 2); + $this->object->set('Servers/3/x', 3); + $this->object->set('Servers/4/x', 4); + $this->object->set('ServerDefault', 3); $this->assertEquals( 4, $this->object->getServerCount() ); - unset($_SESSION[$this->readAttribute($this->object, '_id')]['Servers']); + $this->object->removeServer(2); + $this->object->removeServer(2); $this->assertEquals( - 0, + 2, $this->object->getServerCount() ); + + $this->assertLessThanOrEqual( + 2, + $this->object->get('ServerDefault') + ); + $this->assertEquals( + array('Servers' => array(1 => array('x' => 1), 2 => array('x' => 4))), + $this->object->getConfig() + ); + $this->assertEquals( + array('Servers/1/x' => 1, 'Servers/2/x' => 4), + $this->object->getConfigArray() + ); } /** @@ -639,21 +454,13 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testGetServers() { - $_SESSION[$this->readAttribute($this->object, '_id')]['Servers'] = array( - 1, 2, 3, 4 - ); + $this->object->set('Servers/1/x', 'a'); + $this->object->set('Servers/2/x', 'b'); $this->assertEquals( - array(1, 2, 3, 4), + array(1 => array('x' => 'a'), 2 => array('x' => 'b')), $this->object->getServers() ); - - unset($_SESSION[$this->readAttribute($this->object, '_id')]['Servers']); - - $this->assertEquals( - null, - $this->object->getServerCount() - ); } /** @@ -666,38 +473,43 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase { $this->assertEquals( '', - $this->object->getServerDSN('foobar123') - ); - - $objectID = $this->readAttribute($this->object, "_id"); - $_SESSION[$objectID]['Servers']['foobar123'] = array( - "extension" => "mysqli", - "auth_type" => "config", - "user" => "testUser", - "connect_type" => "tcp", - "host" => "example.com", - "port" => "21" + $this->object->getServerDSN(1) ); + $this->object->updateWithGlobalConfig(array( + 'Servers' => array( + 1 => array( + "extension" => "mysqli", + "auth_type" => "config", + "user" => "testUser", + "connect_type" => "tcp", + "host" => "example.com", + "port" => "21" + ) + ) + )); $this->assertEquals( "mysqli://testUser:***@example.com:21", - $this->object->getServerDSN("foobar123") - ); - - $_SESSION[$objectID]['Servers']['foobar123'] = array( - "extension" => "mysqli", - "auth_type" => "config", - "user" => "testUser", - "connect_type" => "ssh", - "host" => "example.com", - "port" => "21", - "nopassword" => "yes", - "socket" => "123" + $this->object->getServerDSN(1) ); + $this->object->updateWithGlobalConfig(array( + 'Servers' => array( + 1 => array( + "extension" => "mysql", + "auth_type" => "config", + "user" => "testUser", + "connect_type" => "socket", + "host" => "example.com", + "port" => "21", + "nopassword" => "yes", + "socket" => "123" + ) + ) + )); $this->assertEquals( - "mysqli://testUser@123", - $this->object->getServerDSN("foobar123") + "mysql://testUser@123", + $this->object->getServerDSN(1) ); } @@ -711,103 +523,19 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase { $this->assertEquals( '', - $this->object->getServerName('foobar123') - ); - - $objectID = $this->readAttribute($this->object, "_id"); - $_SESSION[$objectID]['Servers']['foobar123'] = array( - "verbose" => "testData" + $this->object->getServerName(1) ); + $this->object->set('Servers/1/host', 'example.com'); $this->assertEquals( - "testData", - $this->object->getServerName("foobar123") - ); - - $_SESSION[$objectID]['Servers']['foobar123'] = array( - "host" => "example.com" + 'example.com', + $this->object->getServerName(1) ); + $this->object->set('Servers/1/verbose', 'testData'); $this->assertEquals( - "example.com", - $this->object->getServerName("foobar123") - ); - - $_SESSION[$objectID]['Servers']['foobar123'] = array( - "host" => "" - ); - - $this->assertEquals( - "localhost", - $this->object->getServerName("foobar123") - ); - } - - /** - * Test for ConfigFile::removeServer - * - * @return void - * @test - */ - public function testRemoveServer() - { - $this->assertEquals( - null, - $this->object->removeServer(1) - ); - - $objectID = $this->readAttribute($this->object, "_id"); - $_SESSION[$objectID]['Servers'] = array( - "1" => array(), - "2" => array("test" => "val"), - "3" => array() - ); - - $this->object->removeServer(2); - - $this->assertEquals( - array( - "1" => array(), - "2" => array() - ), - $_SESSION[$objectID]['Servers'] - ); - - $_SESSION[$objectID]['Servers'] = array( - "1" => array() - ); - - $this->object->removeServer(1); - - $this->assertEmpty( - $_SESSION[$objectID]['Servers'] - ); - - $_SESSION[$objectID]['Servers'] = array( - "1" => array(), - "2" => array("test" => "val"), - "3" => array() - ); - - $_SESSION[$objectID]['ServerDefault'] = 5; - - $this->object->removeServer(3); - - $this->assertEquals( - array( - "1" => array(), - "2" => array("test" => "val") - ), - $_SESSION[$objectID]['Servers'] - ); - - if (isset($_SESSION[$objectID]['ServerDefault'])) { - $success = false; - } else { - $success = true; - } - $this->assertTrue( - $success + 'testData', + $this->object->getServerName(1) ); } @@ -819,51 +547,7 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testGetFilePath() { - $result = $this->object->getFilePath(); - - $this->assertEquals( - SETUP_CONFIG_FILE, - $result - ); - } - - /** - * Test for ConfigFile::getConfig - * - * @return void - * @test - */ - public function testGetConfig() - { - $objectID = $this->readAttribute($this->object, "_id"); - - $_SESSION[$objectID] = array( - "foo" => array( - "bar" => array(1, 2, 3) - ) - ); - - $attrReadMapping = new \ReflectionProperty( - "ConfigFile", - "_cfgUpdateReadMapping" - ); - - $attrReadMapping->setAccessible(true); - $attrReadMapping->setValue( - $this->object, - array( - "key" => "foo/bar" - ) - ); - - $expect = array( - "key" => array(1, 2, 3) - ); - - $this->assertEquals( - $expect, - $this->object->getConfig() - ); + $this->assertNotEmpty($this->object->getFilePath()); } /** @@ -874,54 +558,17 @@ class PMA_ConfigFile_Test extends PHPUnit_Framework_TestCase */ public function testGetConfigArray() { - $objectID = $this->readAttribute($this->object, "_id"); - $reflection = new \ReflectionClass('ConfigFile'); - - $_SESSION[$objectID] = array( - "two" => array(1, 2, 3) - ); - - $attrPersistKeys = $reflection->getProperty('_persistKeys'); - $attrPersistKeys->setAccessible(true); - $attrPersistKeys->setValue( - $this->object, - array( - "one/foo" => array(), - "two" => array(1, 2, 3), - "three" => 3 - ) - ); - - $attrCfg = $reflection->getProperty('_cfg'); - $attrCfg->setAccessible(true); - $attrCfg->setValue( - $this->object, - array( - "one" => array("foo" => "val"), - "three" => "val2" - ) - ); - - $attrReadMapping = $reflection->getProperty("_cfgUpdateReadMapping"); - $attrReadMapping->setAccessible(true); - $attrReadMapping->setValue( - $this->object, - array( - "2" => "two", - "3" => "foobar" - ) - ); + $this->object->setPersistKeys(array(self::SIMPLE_KEY_WITH_DEFAULT_VALUE)); + $this->object->set('Array/test', array('x', 'y')); + $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE); $this->assertEquals( array( - "one/foo" => "val", - "three" => "val2", - "2" => array(1, 2, 3) + self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_value, + 'Array/test' => array('x', 'y') ), $this->object->getConfigArray() ); - - } } ?> diff --git a/test/libraries/PMA_ConfigGenerator_test.php b/test/libraries/PMA_ConfigGenerator_test.php index 154b9adc08..c536d9cd38 100644 --- a/test/libraries/PMA_ConfigGenerator_test.php +++ b/test/libraries/PMA_ConfigGenerator_test.php @@ -30,15 +30,10 @@ class PMA_ConfigGenerator_Test extends PHPUnit_Framework_TestCase public function testGetConfigFile() { $GLOBALS['cfg']['AvailableCharsets'] = array(); - $GLOBALS['PMA_Config'] = new PMA_Config(); unset($_SESSION['eol']); - $attr_instance = new ReflectionProperty("ConfigFile", "_instance"); - $attr_instance->setAccessible(true); - $attr_instance->setValue(null, null); - $GLOBALS['server'] = 0; - $cf = ConfigFile::getInstance(); + $cf = new ConfigFile(); $_SESSION['ConfigFile0'] = array('a', 'b', 'c'); $_SESSION['ConfigFile0']['Servers'] = array( array(1, 2, 3) @@ -46,7 +41,7 @@ class PMA_ConfigGenerator_Test extends PHPUnit_Framework_TestCase $cf->setPersistKeys(array("1/", 2)); - $result = ConfigGenerator::getConfigFile(); + $result = ConfigGenerator::getConfigFile($cf); $this->assertContains( "setAccessible(true); $sessionID = $reflection->getValue($cf); diff --git a/test/libraries/PMA_user_preferences_test.php b/test/libraries/PMA_user_preferences_test.php index 103ef14859..0d21837c0f 100644 --- a/test/libraries/PMA_user_preferences_test.php +++ b/test/libraries/PMA_user_preferences_test.php @@ -53,7 +53,7 @@ class PMA_User_Preferences_Test extends PHPUnit_Framework_TestCase ) ); - PMA_userprefsPageInit(); + PMA_userprefsPageInit(new ConfigFile()); $this->assertEquals( array(