Replace static methods with instance methods

Replaces PhpMyAdmin\UserPreferences static methods with instance methods.

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
This commit is contained in:
Maurício Meneghini Fauth 2018-02-23 15:26:02 -03:00
parent 69c7720336
commit 17bf8b7309
9 changed files with 100 additions and 61 deletions

View File

@ -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'])) {

View File

@ -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

View File

@ -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();

View File

@ -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)

View File

@ -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);
}
/**

View File

@ -1,7 +1,7 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Functions for displaying user preferences pages
* Holds the PhpMyAdmin\UserPreferences class
*
* @package PhpMyAdmin
*/
@ -17,7 +17,7 @@ use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
/**
* PhpMyAdmin\UserPreferences class
* Functions for displaying user preferences pages
*
* @package PhpMyAdmin
*/
@ -30,7 +30,7 @@ class UserPreferences
*
* @return void
*/
public static function pageInit(ConfigFile $cf)
public function pageInit(ConfigFile $cf)
{
$forms_all_keys = UserFormList::getFields();
$cf->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'

View File

@ -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

View File

@ -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;

View File

@ -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<br /><br />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(
'<form action="prefs_manage.php" method="post" class="disableAjax">',