From 17bf8b7309919f8ac593d7c563b31472780ee83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 23 Feb 2018 15:26:02 -0300 Subject: [PATCH] Replace static methods with instance methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces PhpMyAdmin\UserPreferences static methods with instance methods. Signed-off-by: MaurĂ­cio Meneghini Fauth --- error_report.php | 3 +- libraries/classes/Config.php | 13 ++++- libraries/classes/Config/PageSettings.php | 11 +++- libraries/classes/Header.php | 9 ++- libraries/classes/TwoFactor.php | 10 +++- libraries/classes/UserPreferences.php | 22 ++++---- prefs_forms.php | 8 ++- prefs_manage.php | 18 +++--- test/classes/UserPreferencesTest.php | 67 +++++++++++++---------- 9 files changed, 100 insertions(+), 61 deletions(-) diff --git a/error_report.php b/error_report.php index a30998e777..408a4eb201 100644 --- a/error_report.php +++ b/error_report.php @@ -119,7 +119,8 @@ if (isset($_REQUEST['send_error_report']) if (isset($_REQUEST['always_send']) && $_REQUEST['always_send'] === "true" ) { - UserPreferences::persistOption("SendErrorReports", "always", "ask"); + $userPreferences = new UserPreferences(); + $userPreferences->persistOption("SendErrorReports", "always", "ask"); } } } elseif (! empty($_REQUEST['get_settings'])) { diff --git a/libraries/classes/Config.php b/libraries/classes/Config.php index 5b772c649e..e20b2690bd 100644 --- a/libraries/classes/Config.php +++ b/libraries/classes/Config.php @@ -83,6 +83,11 @@ class Config */ var $done = false; + /** + * @var UserPreferences + */ + private $userPreferences; + /** * constructor * @@ -100,6 +105,8 @@ class Config $this->checkSystem(); $this->base_settings = $this->settings; + + $this->userPreferences = new UserPreferences(); } /** @@ -873,9 +880,9 @@ class Config if (! isset($_SESSION['cache'][$cache_key]['userprefs']) || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime ) { - $prefs = UserPreferences::load(); + $prefs = $this->userPreferences->load(); $_SESSION['cache'][$cache_key]['userprefs'] - = UserPreferences::apply($prefs['config_data']); + = $this->userPreferences->apply($prefs['config_data']); $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime']; $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type']; $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime; @@ -986,7 +993,7 @@ class Config if ($default_value === null) { $default_value = Core::arrayRead($cfg_path, $this->default); } - $result = UserPreferences::persistOption($cfg_path, $new_cfg_value, $default_value); + $result = $this->userPreferences->persistOption($cfg_path, $new_cfg_value, $default_value); } if ($prefs_type != 'db' && $cookie_name) { // fall back to cookies diff --git a/libraries/classes/Config/PageSettings.php b/libraries/classes/Config/PageSettings.php index 5caf758166..dee1eeabbc 100644 --- a/libraries/classes/Config/PageSettings.php +++ b/libraries/classes/Config/PageSettings.php @@ -47,6 +47,11 @@ class PageSettings */ private $_HTML = ''; + /** + * @var UserPreferences + */ + private $userPreferences; + /** * Constructor * @@ -55,6 +60,8 @@ class PageSettings */ public function __construct($formGroupName, $elemId = null) { + $this->userPreferences = new UserPreferences(); + $form_class = PageFormList::get($formGroupName); if (is_null($form_class)) { return; @@ -70,7 +77,7 @@ class PageSettings $this->_groupName = $formGroupName; $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings); - UserPreferences::pageInit($cf); + $this->userPreferences->pageInit($cf); $form_display = new $form_class($cf); @@ -99,7 +106,7 @@ class PageSettings { if ($form_display->process(false) && !$form_display->hasErrors()) { // save settings - $result = UserPreferences::save($cf->getConfigArray()); + $result = $this->userPreferences->save($cf->getConfigArray()); if ($result === true) { // reload page $response = Response::getInstance(); diff --git a/libraries/classes/Header.php b/libraries/classes/Header.php index b28622e14d..084bee727e 100644 --- a/libraries/classes/Header.php +++ b/libraries/classes/Header.php @@ -113,6 +113,11 @@ class Header */ private $_headerIsSent; + /** + * @var UserPreferences + */ + private $userPreferences; + /** * Creates a new class instance */ @@ -145,6 +150,8 @@ class Header ) { $this->_userprefsOfferImport = true; } + + $this->userPreferences = new UserPreferences(); } /** @@ -435,7 +442,7 @@ class Header $retval .= Config::renderHeader(); // offer to load user preferences from localStorage if ($this->_userprefsOfferImport) { - $retval .= UserPreferences::autoloadGetHeader(); + $retval .= $this->userPreferences->autoloadGetHeader(); } // pass configuration for hint tooltip display // (to be used by PMA_tooltip() in js/functions.js) diff --git a/libraries/classes/TwoFactor.php b/libraries/classes/TwoFactor.php index c55fb811a3..9ac4ee20b5 100644 --- a/libraries/classes/TwoFactor.php +++ b/libraries/classes/TwoFactor.php @@ -39,6 +39,11 @@ class TwoFactor */ protected $_available; + /** + * @var UserPreferences + */ + private $userPreferences; + /** * Creates new TwoFactor object * @@ -46,6 +51,7 @@ class TwoFactor */ public function __construct($user) { + $this->userPreferences = new UserPreferences(); $this->user = $user; $this->_available = $this->getAvailable(); $this->config = $this->readConfig(); @@ -61,7 +67,7 @@ class TwoFactor public function readConfig() { $result = []; - $config = UserPreferences::load(); + $config = $this->userPreferences->load(); if (isset($config['config_data']['2fa'])) { $result = $config['config_data']['2fa']; } @@ -220,7 +226,7 @@ class TwoFactor */ public function save() { - return UserPreferences::persistOption('2fa', $this->config, null); + return $this->userPreferences->persistOption('2fa', $this->config, null); } /** diff --git a/libraries/classes/UserPreferences.php b/libraries/classes/UserPreferences.php index 3680b31607..65fabe138d 100644 --- a/libraries/classes/UserPreferences.php +++ b/libraries/classes/UserPreferences.php @@ -1,7 +1,7 @@ resetConfigData(); // start with a clean instance @@ -54,7 +54,7 @@ class UserPreferences * * @return array */ - public static function load() + public function load() { $cfgRelation = Relation::getRelationsParam(); if (! $cfgRelation['userconfigwork']) { @@ -92,7 +92,7 @@ class UserPreferences * * @return true|PhpMyAdmin\Message */ - public static function save(array $config_array) + public function save(array $config_array) { $cfgRelation = Relation::getRelationsParam(); $server = isset($GLOBALS['server']) @@ -161,7 +161,7 @@ class UserPreferences * * @return array */ - public static function apply(array $config_data) + public function apply(array $config_data) { $cfg = array(); $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']); @@ -192,9 +192,9 @@ class UserPreferences * * @return true|PhpMyAdmin\Message */ - public static function persistOption($path, $value, $default_value) + public function persistOption($path, $value, $default_value) { - $prefs = self::load(); + $prefs = $this->load(); if ($value === $default_value) { if (isset($prefs['config_data'][$path])) { unset($prefs['config_data'][$path]); @@ -204,7 +204,7 @@ class UserPreferences } else { $prefs['config_data'][$path] = $value; } - return self::save($prefs['config_data']); + return $this->save($prefs['config_data']); } /** @@ -216,7 +216,7 @@ class UserPreferences * * @return void */ - public static function redirect($file_name, + public function redirect($file_name, $params = null, $hash = null ) { // redirect @@ -238,7 +238,7 @@ class UserPreferences * * @return string */ - public static function autoloadGetHeader() + public function autoloadGetHeader() { if (isset($_REQUEST['prefs_autoload']) && $_REQUEST['prefs_autoload'] == 'hide' diff --git a/prefs_forms.php b/prefs_forms.php index d596c999e1..70c90c18d4 100644 --- a/prefs_forms.php +++ b/prefs_forms.php @@ -17,8 +17,10 @@ use PhpMyAdmin\UserPreferences; */ require_once 'libraries/common.inc.php'; +$userPreferences = new UserPreferences(); + $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings); -UserPreferences::pageInit($cf); +$userPreferences->pageInit($cf); // handle form processing @@ -45,13 +47,13 @@ if (isset($_POST['revert'])) { $error = null; if ($form_display->process(false) && !$form_display->hasErrors()) { // save settings - $result = UserPreferences::save($cf->getConfigArray()); + $result = $userPreferences->save($cf->getConfigArray()); if ($result === true) { // reload config $GLOBALS['PMA_Config']->loadUserPreferences(); $tabHash = isset($_POST['tab_hash']) ? $_POST['tab_hash'] : null; $hash = ltrim($tabHash, '#'); - UserPreferences::redirect( + $userPreferences->redirect( 'prefs_forms.php', array('form' => $form_param), $hash diff --git a/prefs_manage.php b/prefs_manage.php index a961720110..94d0870f7c 100644 --- a/prefs_manage.php +++ b/prefs_manage.php @@ -22,8 +22,10 @@ use PhpMyAdmin\Util; */ require_once 'libraries/common.inc.php'; +$userPreferences = new UserPreferences(); + $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings); -UserPreferences::pageInit($cf); +$userPreferences->pageInit($cf); $response = Response::getInstance(); $error = ''; @@ -35,7 +37,7 @@ if (isset($_POST['submit_export']) $response->disable(); $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.json'; Core::downloadHeader($filename, 'application/json'); - $settings = UserPreferences::load(); + $settings = $userPreferences->load(); echo json_encode($settings['config_data'], JSON_PRETTY_PRINT); exit; } elseif (isset($_POST['submit_export']) @@ -46,7 +48,7 @@ if (isset($_POST['submit_export']) $response->disable(); $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.php'; Core::downloadHeader($filename, 'application/php'); - $settings = UserPreferences::load(); + $settings = $userPreferences->load(); echo '/* ' . __('phpMyAdmin configuration snippet') . " */\n\n"; echo '/* ' . __('Paste it to your config.inc.php') . " */\n\n"; foreach ($settings['config_data'] as $key => $val) { @@ -55,7 +57,7 @@ if (isset($_POST['submit_export']) } exit; } elseif (isset($_POST['submit_get_json'])) { - $settings = UserPreferences::load(); + $settings = $userPreferences->load(); $response->addJSON('prefs', json_encode($settings['config_data'])); $response->addJSON('mtime', $settings['mtime']); exit; @@ -162,7 +164,7 @@ if (isset($_POST['submit_export']) } // save settings - $result = UserPreferences::save($cf->getConfigArray()); + $result = $userPreferences->save($cf->getConfigArray()); if ($result === true) { if ($return_url) { $query = PhpMyAdmin\Util::splitURLQuery($return_url); @@ -181,19 +183,19 @@ if (isset($_POST['submit_export']) } // reload config $GLOBALS['PMA_Config']->loadUserPreferences(); - UserPreferences::redirect($return_url, $params); + $userPreferences->redirect($return_url, $params); exit; } else { $error = $result; } } } elseif (isset($_POST['submit_clear'])) { - $result = UserPreferences::save(array()); + $result = $userPreferences->save(array()); if ($result === true) { $params = array(); $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection'); $GLOBALS['PMA_Config']->removeCookie('pma_lang'); - UserPreferences::redirect('prefs_manage.php', $params); + $userPreferences->redirect('prefs_manage.php', $params); exit; } else { $error = $result; diff --git a/test/classes/UserPreferencesTest.php b/test/classes/UserPreferencesTest.php index accca518d5..af0379af93 100644 --- a/test/classes/UserPreferencesTest.php +++ b/test/classes/UserPreferencesTest.php @@ -19,25 +19,32 @@ use PhpMyAdmin\UserPreferences; */ class UserPreferencesTest extends PmaTestCase { + /** + * @var UserPreferences + */ + private $userPreferences; + /** * Setup various pre conditions * * @return void */ - function setUp() + protected function setUp() { global $cfg; include 'libraries/config.default.php'; $GLOBALS['server'] = 0; $GLOBALS['PMA_PHP_SELF'] = '/phpmyadmin/'; + + $this->userPreferences = new UserPreferences(); } /** - * Test for UserPreferences::pageInit + * Test for pageInit * * @return void */ - public function testUserPrefPageInit() + public function testPageInit() { $GLOBALS['cfg'] = array( 'Server/hide_db' => 'testval123', @@ -46,7 +53,7 @@ class UserPreferencesTest extends PmaTestCase $GLOBALS['cfg']['AvailableCharsets'] = array(); $GLOBALS['cfg']['UserprefsDeveloperTab'] = null; - UserPreferences::pageInit(new ConfigFile()); + $this->userPreferences->pageInit(new ConfigFile()); $this->assertEquals( array( @@ -61,18 +68,18 @@ class UserPreferencesTest extends PmaTestCase } /** - * Test for UserPreferences::load + * Test for load * * @return void */ - public function testLoadUserprefs() + public function testLoad() { $_SESSION['relation'][$GLOBALS['server']]['PMA_VERSION'] = PMA_VERSION; $_SESSION['relation'][$GLOBALS['server']]['userconfigwork'] = null; unset($_SESSION['userconfig']); - $result = UserPreferences::load(); + $result = $this->userPreferences->load(); $this->assertCount( 3, @@ -126,7 +133,7 @@ class UserPreferencesTest extends PmaTestCase $GLOBALS['dbi'] = $dbi; - $result = UserPreferences::load(); + $result = $this->userPreferences->load(); $this->assertEquals( array( @@ -139,18 +146,18 @@ class UserPreferencesTest extends PmaTestCase } /** - * Test for UserPreferences::save + * Test for save * * @return void */ - public function testSaveUserprefs() + public function testSave() { $GLOBALS['server'] = 2; $_SESSION['relation'][2]['PMA_VERSION'] = PMA_VERSION; $_SESSION['relation'][2]['userconfigwork'] = null; unset($_SESSION['userconfig']); - $result = UserPreferences::save(array(1)); + $result = $this->userPreferences->save(array(1)); $this->assertTrue( $result @@ -216,7 +223,7 @@ class UserPreferencesTest extends PmaTestCase $GLOBALS['dbi'] = $dbi; $this->assertTrue( - UserPreferences::save(array(1)) + $this->userPreferences->save(array(1)) ); // case 3 @@ -251,7 +258,7 @@ class UserPreferencesTest extends PmaTestCase $GLOBALS['dbi'] = $dbi; - $result = UserPreferences::save(array(1)); + $result = $this->userPreferences->save(array(1)); $this->assertEquals( 'Could not save configuration

err1', @@ -260,18 +267,18 @@ class UserPreferencesTest extends PmaTestCase } /** - * Test for UserPreferences::apply + * Test for apply * * @return void */ - public function testApplyUserprefs() + public function testApply() { $GLOBALS['cfg']['UserprefsDisallow'] = array( 'test' => 'val', 'foo' => 'bar' ); $GLOBALS['cfg']['UserprefsDeveloperTab'] = null; - $result = UserPreferences::apply( + $result = $this->userPreferences->apply( array( 'DBG/sql' => true, 'ErrorHandler/display' => true, @@ -292,14 +299,14 @@ class UserPreferencesTest extends PmaTestCase } /** - * Test for UserPreferences::apply + * Test for apply * * @return void */ - public function testApplyDevelUserprefs() + public function testApplyDevel() { $GLOBALS['cfg']['UserprefsDeveloperTab'] = true; - $result = UserPreferences::apply( + $result = $this->userPreferences->apply( array( 'DBG/sql' => true, ) @@ -314,7 +321,7 @@ class UserPreferencesTest extends PmaTestCase } /** - * Test for UserPreferences::persistOption + * Test for persistOption * * @return void */ @@ -333,24 +340,24 @@ class UserPreferencesTest extends PmaTestCase $_SESSION['relation'][2]['userconfigwork'] = null; $this->assertTrue( - UserPreferences::persistOption('Server/hide_db', 'val', 'val') + $this->userPreferences->persistOption('Server/hide_db', 'val', 'val') ); $this->assertTrue( - UserPreferences::persistOption('Server/hide_db', 'val2', 'val') + $this->userPreferences->persistOption('Server/hide_db', 'val2', 'val') ); $this->assertTrue( - UserPreferences::persistOption('Server/hide_db2', 'val', 'val') + $this->userPreferences->persistOption('Server/hide_db2', 'val', 'val') ); } /** - * Test for UserPreferences::redirect + * Test for redirect * * @return void */ - public function testUserprefsRedirect() + public function testRedirect() { $GLOBALS['lang'] = ''; $GLOBALS['db'] = 'db'; @@ -361,7 +368,7 @@ class UserPreferencesTest extends PmaTestCase $GLOBALS['PMA_Config']->set('PmaAbsoluteUri', ''); $GLOBALS['PMA_Config']->set('PMA_IS_IIS', false); - UserPreferences::redirect( + $this->userPreferences->redirect( 'file.html', array('a' => 'b'), 'h ash' @@ -369,18 +376,18 @@ class UserPreferencesTest extends PmaTestCase } /** - * Test for UserPreferences::autoloadGetHeader + * Test for autoloadGetHeader * * @return void */ - public function testUserprefsAutoloadGetHeader() + public function testAutoloadGetHeader() { $_SESSION['userprefs_autoload'] = false; $_REQUEST['prefs_autoload'] = 'hide'; $this->assertEquals( '', - UserPreferences::autoloadGetHeader() + $this->userPreferences->autoloadGetHeader() ); $this->assertTrue( @@ -390,7 +397,7 @@ class UserPreferencesTest extends PmaTestCase $_REQUEST['prefs_autoload'] = 'nohide'; $GLOBALS['cfg']['ServerDefault'] = 1; $GLOBALS['PMA_PHP_SELF'] = 'phpunit'; - $result = UserPreferences::autoloadGetHeader(); + $result = $this->userPreferences->autoloadGetHeader(); $this->assertContains( '
',