From 56daea7d86159ad6fadd37cbe41668060717c03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Thu, 8 Feb 2018 12:25:13 -0200 Subject: [PATCH 1/7] Replace static methods with instance methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- error_report.php | 13 ++++++++----- libraries/classes/ErrorReport.php | 24 ++++++++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/error_report.php b/error_report.php index 3ac3c9ed80..55feb8da34 100644 --- a/error_report.php +++ b/error_report.php @@ -6,6 +6,7 @@ * @package PhpMyAdmin */ use PhpMyAdmin\ErrorReport; +use PhpMyAdmin\Message; use PhpMyAdmin\Response; use PhpMyAdmin\UserPreferences; @@ -19,6 +20,8 @@ if (!isset($_REQUEST['exception_type']) $response = Response::getInstance(); +$errorReport = new ErrorReport(); + if (isset($_REQUEST['send_error_report']) && ($_REQUEST['send_error_report'] == true || $_REQUEST['send_error_report'] == '1') @@ -47,10 +50,10 @@ if (isset($_REQUEST['send_error_report']) ); } } - $reportData = ErrorReport::getReportData($_REQUEST['exception_type']); + $reportData = $errorReport->getReportData($_REQUEST['exception_type']); // report if and only if there were 'actual' errors. if (count($reportData) > 0) { - $server_response = ErrorReport::send($reportData); + $server_response = $errorReport->send($reportData); if ($server_response === false) { $success = false; } else { @@ -87,9 +90,9 @@ if (isset($_REQUEST['send_error_report']) /* Create message object */ if ($success) { - $msg = PhpMyAdmin\Message::notice($msg); + $msg = Message::notice($msg); } else { - $msg = PhpMyAdmin\Message::error($msg); + $msg = Message::error($msg); } /* Add message to response */ @@ -122,7 +125,7 @@ if (isset($_REQUEST['send_error_report']) $response->addJSON('report_setting', $GLOBALS['cfg']['SendErrorReports']); } else { if ($_REQUEST['exception_type'] == 'js') { - $response->addHTML(ErrorReport::getForm()); + $response->addHTML($errorReport->getForm()); } else { // clear previous errors & save new ones. $GLOBALS['error_handler']->savePreviousErrors(); diff --git a/libraries/classes/ErrorReport.php b/libraries/classes/ErrorReport.php index e914413c59..4f91f738f4 100644 --- a/libraries/classes/ErrorReport.php +++ b/libraries/classes/ErrorReport.php @@ -32,9 +32,9 @@ class ErrorReport * * @return String the report */ - public static function getPrettyReportData() + public function getPrettyReportData() { - $report = self::getReportData(); + $report = $this->getReportData(); return json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } @@ -47,7 +47,7 @@ class ErrorReport * * @return array error report if success, Empty Array otherwise */ - public static function getReportData($exception_type = 'js') + public function getReportData($exception_type = 'js') { $relParams = Relation::getRelationsParam(); // common params for both, php & js exceptions @@ -70,8 +70,8 @@ class ErrorReport return array(); } $exception = $_REQUEST['exception']; - $exception["stack"] = self::translateStacktrace($exception["stack"]); - List($uri, $script_name) = self::sanitizeUrl($exception["url"]); + $exception["stack"] = $this->translateStacktrace($exception["stack"]); + List($uri, $script_name) = $this->sanitizeUrl($exception["url"]); $exception["uri"] = $uri; unset($exception["url"]); @@ -135,7 +135,7 @@ class ErrorReport * * @return array the uri and script name */ - public static function sanitizeUrl($url) + public function sanitizeUrl($url) { $components = parse_url($url); if (isset($components["fragment"]) @@ -177,7 +177,7 @@ class ErrorReport * * @return String the reply of the server */ - public static function send(array $report) + public function send(array $report) { $httpRequest = new HttpRequest(); $response = $httpRequest->create( @@ -198,7 +198,7 @@ class ErrorReport * * @return array $stack the modified stack trace */ - public static function translateStacktrace(array $stack) + public function translateStacktrace(array $stack) { foreach ($stack as &$level) { foreach ($level["context"] as &$line) { @@ -207,7 +207,7 @@ class ErrorReport } } unset($level["context"]); - List($uri, $script_name) = self::sanitizeUrl($level["url"]); + List($uri, $script_name) = $this->sanitizeUrl($level["url"]); $level["uri"] = $uri; $level["scriptname"] = $script_name; unset($level["url"]); @@ -222,15 +222,15 @@ class ErrorReport * * @return String the form */ - public static function getForm() + public function getForm() { $datas = array( - 'report_data' => self::getPrettyReportData(), + 'report_data' => $this->getPrettyReportData(), 'hidden_inputs' => Url::getHiddenInputs(), 'hidden_fields' => null, ); - $reportData = self::getReportData(); + $reportData = $this->getReportData(); if (!empty($reportData)) { $datas['hidden_fields'] = Url::getHiddenFields($reportData); } From d60f28e441c18a54772088ce8b2185c42c6c687f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Thu, 8 Feb 2018 14:32:41 -0200 Subject: [PATCH 2/7] Fix visibility of the methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/ErrorReport.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/classes/ErrorReport.php b/libraries/classes/ErrorReport.php index 4f91f738f4..22a7742a70 100644 --- a/libraries/classes/ErrorReport.php +++ b/libraries/classes/ErrorReport.php @@ -32,7 +32,7 @@ class ErrorReport * * @return String the report */ - public function getPrettyReportData() + private function getPrettyReportData() { $report = $this->getReportData(); @@ -135,7 +135,7 @@ class ErrorReport * * @return array the uri and script name */ - public function sanitizeUrl($url) + private function sanitizeUrl($url) { $components = parse_url($url); if (isset($components["fragment"]) @@ -198,7 +198,7 @@ class ErrorReport * * @return array $stack the modified stack trace */ - public function translateStacktrace(array $stack) + private function translateStacktrace(array $stack) { foreach ($stack as &$level) { foreach ($level["context"] as &$line) { From eaae50a1bcafe6fc19f82ccb20cd5a67957ebc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 9 Feb 2018 21:10:17 -0200 Subject: [PATCH 3/7] Add a constructor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- error_report.php | 3 ++- libraries/classes/ErrorReport.php | 30 +++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/error_report.php b/error_report.php index 55feb8da34..6b6af843ff 100644 --- a/error_report.php +++ b/error_report.php @@ -9,6 +9,7 @@ use PhpMyAdmin\ErrorReport; use PhpMyAdmin\Message; use PhpMyAdmin\Response; use PhpMyAdmin\UserPreferences; +use PhpMyAdmin\Utils\HttpRequest; require_once 'libraries/common.inc.php'; @@ -20,7 +21,7 @@ if (!isset($_REQUEST['exception_type']) $response = Response::getInstance(); -$errorReport = new ErrorReport(); +$errorReport = new ErrorReport(new HttpRequest()); if (isset($_REQUEST['send_error_report']) && ($_REQUEST['send_error_report'] == true diff --git a/libraries/classes/ErrorReport.php b/libraries/classes/ErrorReport.php index 22a7742a70..79dcb62ce5 100644 --- a/libraries/classes/ErrorReport.php +++ b/libraries/classes/ErrorReport.php @@ -1,7 +1,7 @@ httpRequest = $httpRequest; + $this->submissionUrl = 'https://reports.phpmyadmin.net/incidents/create'; + } /** * returns the pretty printed error report data collected from the @@ -179,9 +196,8 @@ class ErrorReport */ public function send(array $report) { - $httpRequest = new HttpRequest(); - $response = $httpRequest->create( - self::SUBMISSION_URL, + $response = $this->httpRequest->create( + $this->submissionUrl, "POST", false, json_encode($report), From 67e65d91bf40de53c9fd33acdf8a96509c1ca396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 9 Feb 2018 21:54:02 -0200 Subject: [PATCH 4/7] Fix some coding style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- error_report.php | 2 +- libraries/classes/ErrorReport.php | 117 +++++++++++++++--------------- 2 files changed, 58 insertions(+), 61 deletions(-) diff --git a/error_report.php b/error_report.php index 6b6af843ff..a30998e777 100644 --- a/error_report.php +++ b/error_report.php @@ -51,7 +51,7 @@ if (isset($_REQUEST['send_error_report']) ); } } - $reportData = $errorReport->getReportData($_REQUEST['exception_type']); + $reportData = $errorReport->getData($_REQUEST['exception_type']); // report if and only if there were 'actual' errors. if (count($reportData) > 0) { $server_response = $errorReport->send($reportData); diff --git a/libraries/classes/ErrorReport.php b/libraries/classes/ErrorReport.php index 79dcb62ce5..d512123458 100644 --- a/libraries/classes/ErrorReport.php +++ b/libraries/classes/ErrorReport.php @@ -20,7 +20,7 @@ use PhpMyAdmin\Utils\HttpRequest; class ErrorReport { /** - * the url where to submit reports to + * The URL where to submit reports to * * @var string */ @@ -43,71 +43,70 @@ class ErrorReport } /** - * returns the pretty printed error report data collected from the + * Returns the pretty printed error report data collected from the * current configuration or from the request parameters sent by the * error reporting js code. * - * @return String the report + * @return string the report */ - private function getPrettyReportData() + private function getPrettyData() { - $report = $this->getReportData(); + $report = $this->getData(); return json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } /** - * returns the error report data collected from the current configuration or + * Returns the error report data collected from the current configuration or * from the request parameters sent by the error reporting js code. * - * @param string $exception_type whether exception is 'js' or 'php' + * @param string $exceptionType whether exception is 'js' or 'php' * * @return array error report if success, Empty Array otherwise */ - public function getReportData($exception_type = 'js') + public function getData($exceptionType = 'js') { $relParams = Relation::getRelationsParam(); // common params for both, php & js exceptions - $report = array( - "pma_version" => PMA_VERSION, - "browser_name" => PMA_USR_BROWSER_AGENT, - "browser_version" => PMA_USR_BROWSER_VER, - "user_os" => PMA_USR_OS, - "server_software" => $_SERVER['SERVER_SOFTWARE'], - "user_agent_string" => $_SERVER['HTTP_USER_AGENT'], - "locale" => $_COOKIE['pma_lang'], - "configuration_storage" => - is_null($relParams['db']) ? "disabled" : - "enabled", - "php_version" => phpversion() - ); + $report = [ + "pma_version" => PMA_VERSION, + "browser_name" => PMA_USR_BROWSER_AGENT, + "browser_version" => PMA_USR_BROWSER_VER, + "user_os" => PMA_USR_OS, + "server_software" => $_SERVER['SERVER_SOFTWARE'], + "user_agent_string" => $_SERVER['HTTP_USER_AGENT'], + "locale" => $_COOKIE['pma_lang'], + "configuration_storage" => + is_null($relParams['db']) ? "disabled" : "enabled", + "php_version" => phpversion() + ]; - if ($exception_type == 'js') { + if ($exceptionType == 'js') { if (empty($_REQUEST['exception'])) { - return array(); + return []; } $exception = $_REQUEST['exception']; $exception["stack"] = $this->translateStacktrace($exception["stack"]); - List($uri, $script_name) = $this->sanitizeUrl($exception["url"]); + list($uri, $scriptName) = $this->sanitizeUrl($exception["url"]); $exception["uri"] = $uri; unset($exception["url"]); - $report ["exception_type"] = 'js'; - $report ["exception"] = $exception; - $report ["script_name"] = $script_name; - $report ["microhistory"] = $_REQUEST['microhistory']; + $report["exception_type"] = 'js'; + $report["exception"] = $exception; + $report["script_name"] = $scriptName; + $report["microhistory"] = $_REQUEST['microhistory']; if (! empty($_REQUEST['description'])) { $report['steps'] = $_REQUEST['description']; } - } elseif ($exception_type == 'php') { - $errors = array(); + } elseif ($exceptionType == 'php') { + $errors = []; // create php error report $i = 0; if (!isset($_SESSION['prev_errors']) || $_SESSION['prev_errors'] == '' ) { - return array(); + return []; } foreach ($_SESSION['prev_errors'] as $errorObj) { /* @var $errorObj PhpMyAdmin\Error */ @@ -115,26 +114,25 @@ class ErrorReport && $errorObj->getType() && $errorObj->getNumber() != E_USER_WARNING ) { - $errors[$i++] = array( + $errors[$i++] = [ "lineNum" => $errorObj->getLine(), "file" => $errorObj->getFile(), "type" => $errorObj->getType(), "msg" => $errorObj->getOnlyMessage(), "stackTrace" => $errorObj->getBacktrace(5), "stackhash" => $errorObj->getHash() - ); - + ]; } } // if there were no 'actual' errors to be submitted. if ($i==0) { - return array(); // then return empty array + return []; // then return empty array } - $report ["exception_type"] = 'php'; + $report["exception_type"] = 'php'; $report["errors"] = $errors; } else { - return array(); + return []; } return $report; @@ -148,7 +146,7 @@ class ErrorReport * hostname and identifying query params. The second is the name of the * php script in the url * - * @param String $url the url to sanitize + * @param string $url the url to sanitize * * @return array the uri and script name */ @@ -166,25 +164,25 @@ class ErrorReport // get script name preg_match("<([a-zA-Z\-_\d]*\.php)$>", $components["path"], $matches); if (count($matches) < 2) { - $script_name = 'index.php'; + $scriptName = 'index.php'; } else { - $script_name = $matches[1]; + $scriptName = $matches[1]; } // remove deployment specific details to make uri more generic if (isset($components["query"])) { - parse_str($components["query"], $query_array); - unset($query_array["db"]); - unset($query_array["table"]); - unset($query_array["token"]); - unset($query_array["server"]); - $query = http_build_query($query_array); + parse_str($components["query"], $queryArray); + unset($queryArray["db"]); + unset($queryArray["table"]); + unset($queryArray["token"]); + unset($queryArray["server"]); + $query = http_build_query($queryArray); } else { $query = ''; } - $uri = $script_name . "?" . $query; - return array($uri, $script_name); + $uri = $scriptName . "?" . $query; + return [$uri, $scriptName]; } /** @@ -192,7 +190,7 @@ class ErrorReport * * @param array $report the report info to be sent * - * @return String the reply of the server + * @return string the reply of the server */ public function send(array $report) { @@ -207,7 +205,7 @@ class ErrorReport } /** - * translates the cumulative line numbers in the stack trace as well as sanitize + * Translates the cumulative line numbers in the stack trace as well as sanitize * urls and trim long lines in the context * * @param array $stack the stack trace @@ -223,9 +221,9 @@ class ErrorReport } } unset($level["context"]); - List($uri, $script_name) = $this->sanitizeUrl($level["url"]); + list($uri, $scriptName) = $this->sanitizeUrl($level["url"]); $level["uri"] = $uri; - $level["scriptname"] = $script_name; + $level["scriptname"] = $scriptName; unset($level["url"]); } unset($level); @@ -233,25 +231,24 @@ class ErrorReport } /** - * generates the error report form to collect user description and preview the + * Generates the error report form to collect user description and preview the * report before being sent * - * @return String the form + * @return string the form */ public function getForm() { - $datas = array( - 'report_data' => $this->getPrettyReportData(), + $datas = [ + 'report_data' => $this->getPrettyData(), 'hidden_inputs' => Url::getHiddenInputs(), 'hidden_fields' => null, - ); + ]; - $reportData = $this->getReportData(); + $reportData = $this->getData(); if (!empty($reportData)) { $datas['hidden_fields'] = Url::getHiddenFields($reportData); } - return Template::get('error/report_form') - ->render($datas); + return Template::get('error/report_form')->render($datas); } } From fba227c1e80895cb977ded6bf5a2525483304d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 10 Feb 2018 00:24:04 -0200 Subject: [PATCH 5/7] Replace static methods with instance methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Normalization.php | 52 ++++++++-------- normalization.php | 37 ++++++------ test/classes/NormalizationTest.php | 94 +++++++++++++++-------------- 3 files changed, 94 insertions(+), 89 deletions(-) diff --git a/libraries/classes/Normalization.php b/libraries/classes/Normalization.php index f591128596..9931a7a8f5 100644 --- a/libraries/classes/Normalization.php +++ b/libraries/classes/Normalization.php @@ -1,7 +1,7 @@ ' . __('Select one…') . "" . "" - . self::getHtmlForColumnsList( + . $this->getHtmlForColumnsList( $db, $table, _pgettext('string types', 'String') @@ -200,7 +200,7 @@ class Normalization * * @return string HTML contents for step 1.2 */ - public static function getHtmlContentsFor1NFStep2($db, $table) + public function getHtmlContentsFor1NFStep2($db, $table) { $step = 2; $stepTxt = __('Have a primary key'); @@ -250,7 +250,7 @@ class Normalization * * @return string HTML contents for step 1.4 */ - public static function getHtmlContentsFor1NFStep4($db, $table) + public function getHtmlContentsFor1NFStep4($db, $table) { $step = 4; $stepTxt = __('Remove redundant columns'); @@ -265,7 +265,7 @@ class Normalization "Check the columns which are redundant and click on remove. " . "If no redundant column, click on 'No redundant column'" ); - $extra = self::getHtmlForColumnsList($db, $table, 'all', "checkbox") . "
" + $extra = $this->getHtmlForColumnsList($db, $table, 'all', "checkbox") . "
" . '' . '" + $extra = $this->getHtmlForColumnsList($db, $table, 'all', "checkbox") . "
" . '' . 'selectDb($db); @@ -878,8 +878,8 @@ class Normalization foreach ($primarycols as $col) { $pk[] = Util::backquote($col->getName()); } - $partialKeys = self::getAllCombinationPartialKeys($pk); - $distinctValCount = self::findDistinctValuesCount( + $partialKeys = $this->getAllCombinationPartialKeys($pk); + $distinctValCount = $this->findDistinctValuesCount( array_unique( array_merge($columns, $partialKeys) ), $table @@ -888,7 +888,7 @@ class Normalization if (!in_array($column, $pk)) { foreach ($partialKeys as $partialKey) { if ($partialKey - && self::checkPartialDependency( + && $this->checkPartialDependency( $partialKey, $column, $table, $distinctValCount[$partialKey], $distinctValCount[$column], $totalRows @@ -936,7 +936,7 @@ class Normalization * * @return boolean TRUE if $column is dependent on $partialKey, False otherwise */ - public static function checkPartialDependency( + private function checkPartialDependency( $partialKey, $column, $table, $pkCnt, $colCnt, $totalRows ) { $query = 'SELECT ' @@ -963,7 +963,7 @@ class Normalization * * @return array associative array containing the count */ - public static function findDistinctValuesCount(array $columns, $table) + private function findDistinctValuesCount(array $columns, $table) { $result = array(); $query = 'SELECT '; @@ -992,7 +992,7 @@ class Normalization * * @return array containing all the possible partial keys(subset of primary key) */ - public static function getAllCombinationPartialKeys(array $primaryKey) + private function getAllCombinationPartialKeys(array $primaryKey) { $results = array(''); foreach ($primaryKey as $element) { diff --git a/normalization.php b/normalization.php index 2f38698a64..9d5251d65b 100644 --- a/normalization.php +++ b/normalization.php @@ -11,16 +11,15 @@ use PhpMyAdmin\Normalization; use PhpMyAdmin\Response; use PhpMyAdmin\Url; -/** - * - */ require_once 'libraries/common.inc.php'; +$normalization = new Normalization(); + if (isset($_REQUEST['getColumns'])) { $html = '' . ''; //get column whose datatype falls under string category - $html .= Normalization::getHtmlForColumnsList( + $html .= $normalization->getHtmlForColumnsList( $db, $table, _pgettext('string types', 'String') @@ -30,7 +29,7 @@ if (isset($_REQUEST['getColumns'])) { } if (isset($_REQUEST['splitColumn'])) { $num_fields = min(4096, intval($_REQUEST['numFields'])); - $html = Normalization::getHtmlForCreateNewColumn($num_fields, $db, $table); + $html = $normalization->getHtmlForCreateNewColumn($num_fields, $db, $table); $html .= Url::getHiddenInputs($db, $table); echo $html; exit; @@ -38,7 +37,7 @@ if (isset($_REQUEST['splitColumn'])) { if (isset($_REQUEST['addNewPrimary'])) { $num_fields = 1; $columnMeta = array('Field'=>$table . "_id", 'Extra'=>'auto_increment'); - $html = Normalization::getHtmlForCreateNewColumn( + $html = $normalization->getHtmlForCreateNewColumn( $num_fields, $db, $table, $columnMeta ); $html .= Url::getHiddenInputs($db, $table); @@ -46,14 +45,14 @@ if (isset($_REQUEST['addNewPrimary'])) { exit; } if (isset($_REQUEST['findPdl'])) { - $html = Normalization::findPartialDependencies($table, $db); + $html = $normalization->findPartialDependencies($table, $db); echo $html; exit; } if (isset($_REQUEST['getNewTables2NF'])) { $partialDependencies = json_decode($_REQUEST['pd']); - $html = Normalization::getHtmlForNewTables2NF($partialDependencies, $table); + $html = $normalization->getHtmlForNewTables2NF($partialDependencies, $table); echo $html; exit; } @@ -63,7 +62,7 @@ $response = Response::getInstance(); if (isset($_REQUEST['getNewTables3NF'])) { $dependencies = json_decode($_REQUEST['pd']); $tables = json_decode($_REQUEST['tables']); - $newTables = Normalization::getHtmlForNewTables3NF($dependencies, $tables, $db); + $newTables = $normalization->getHtmlForNewTables3NF($dependencies, $tables, $db); $response->disable(); Core::headerJSON(); echo json_encode($newTables); @@ -81,13 +80,13 @@ if (Core::isValid($_REQUEST['normalizeTo'], array('1nf', '2nf', '3nf'))) { if (isset($_REQUEST['createNewTables2NF'])) { $partialDependencies = json_decode($_REQUEST['pd']); $tablesName = json_decode($_REQUEST['newTablesName']); - $res = Normalization::createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db); + $res = $normalization->createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db); $response->addJSON($res); exit; } if (isset($_REQUEST['createNewTables3NF'])) { $newtables = json_decode($_REQUEST['newTables']); - $res = Normalization::createNewTablesFor3NF($newtables, $db); + $res = $normalization->createNewTablesFor3NF($newtables, $db); $response->addJSON($res); exit; } @@ -96,31 +95,31 @@ if (isset($_POST['repeatingColumns'])) { $newTable = $_POST['newTable']; $newColumn = $_POST['newColumn']; $primary_columns = $_POST['primary_columns']; - $res = Normalization::moveRepeatingGroup( + $res = $normalization->moveRepeatingGroup( $repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db ); $response->addJSON($res); exit; } if (isset($_REQUEST['step1'])) { - $html = Normalization::getHtmlFor1NFStep1($db, $table, $normalForm); + $html = $normalization->getHtmlFor1NFStep1($db, $table, $normalForm); $response->addHTML($html); } elseif (isset($_REQUEST['step2'])) { - $res = Normalization::getHtmlContentsFor1NFStep2($db, $table); + $res = $normalization->getHtmlContentsFor1NFStep2($db, $table); $response->addJSON($res); } elseif (isset($_REQUEST['step3'])) { - $res = Normalization::getHtmlContentsFor1NFStep3($db, $table); + $res = $normalization->getHtmlContentsFor1NFStep3($db, $table); $response->addJSON($res); } elseif (isset($_REQUEST['step4'])) { - $res = Normalization::getHtmlContentsFor1NFStep4($db, $table); + $res = $normalization->getHtmlContentsFor1NFStep4($db, $table); $response->addJSON($res); } elseif (isset($_REQUEST['step']) && $_REQUEST['step'] == '2.1') { - $res = Normalization::getHtmlFor2NFstep1($db, $table); + $res = $normalization->getHtmlFor2NFstep1($db, $table); $response->addJSON($res); } elseif (isset($_REQUEST['step']) && $_REQUEST['step'] == '3.1') { $tables = $_REQUEST['tables']; - $res = Normalization::getHtmlFor3NFstep1($db, $tables); + $res = $normalization->getHtmlFor3NFstep1($db, $tables); $response->addJSON($res); } else { - $response->addHTML(Normalization::getHtmlForNormalizetable()); + $response->addHTML($normalization->getHtmlForNormalizetable()); } diff --git a/test/classes/NormalizationTest.php b/test/classes/NormalizationTest.php index cc27e26b9d..533dfadbc4 100644 --- a/test/classes/NormalizationTest.php +++ b/test/classes/NormalizationTest.php @@ -13,10 +13,9 @@ use PhpMyAdmin\Theme; use PhpMyAdmin\Types; use PhpMyAdmin\Util; use PHPUnit\Framework\TestCase; +use ReflectionClass; use stdClass; -$GLOBALS['server'] = 1; - /** * tests for PhpMyAdmin\Normalization * @@ -24,6 +23,8 @@ $GLOBALS['server'] = 1; */ class NormalizationTest extends TestCase { + private $normalization; + /** * prepares environment for tests * @@ -92,10 +93,11 @@ class NormalizationTest extends TestCase ->method('fetchResult') ->will($this->returnValue(array(0))); + $this->normalization = new Normalization(); } /** - * Test for Normalization::getHtmlForColumnsList + * Test for getHtmlForColumnsList * * @return void */ @@ -105,16 +107,16 @@ class NormalizationTest extends TestCase $table= "PMA_table"; $this->assertContains( '', - Normalization::getHtmlForColumnsList($table, $db) + $this->normalization->getHtmlForColumnsList($table, $db) ); $this->assertEquals( 'col1 [ varchar(100) ]
', - Normalization::getHtmlForColumnsList($table, $db, 'String', 'checkbox') + $this->normalization->getHtmlForColumnsList($table, $db, 'String', 'checkbox') ); } /** - * Test for Normalization::getHtmlForCreateNewColumn + * Test for getHtmlForCreateNewColumn * * @return void */ @@ -126,7 +128,7 @@ class NormalizationTest extends TestCase $db = "PMA_db"; $table= "PMA_table"; $num_fields = 1; - $result = Normalization::getHtmlForCreateNewColumn($num_fields, $db, $table); + $result = $this->normalization->getHtmlForCreateNewColumn($num_fields, $db, $table); $this->assertContains( 'normalization->getHtmlFor1NFStep1($db, $table, $normalizedTo); $this->assertContains( "

" . __('First step of normalization (1NF)') . "

", @@ -171,7 +173,7 @@ class NormalizationTest extends TestCase ); $this->assertContains( - Normalization::getHtmlForColumnsList( + $this->normalization->getHtmlForColumnsList( $db, $table, _pgettext('string types', 'String') ), $result ); @@ -179,7 +181,7 @@ class NormalizationTest extends TestCase } /** - * Test for Normalization::getHtmlContentsFor1NFStep2 + * Test for getHtmlContentsFor1NFStep2 * * @return void */ @@ -187,7 +189,7 @@ class NormalizationTest extends TestCase { $db = "PMA_db"; $table= "PMA_table1"; - $result = Normalization::getHtmlContentsFor1NFStep2($db, $table); + $result = $this->normalization->getHtmlContentsFor1NFStep2($db, $table); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); $this->assertArrayHasKey('headText', $result); @@ -204,12 +206,12 @@ class NormalizationTest extends TestCase ); $this->assertEquals('0', $result['hasPrimaryKey']); $this->assertContains(__('Step 1.') . 2, $result['legendText']); - $result1 = Normalization::getHtmlContentsFor1NFStep2($db, 'PMA_table'); + $result1 = $this->normalization->getHtmlContentsFor1NFStep2($db, 'PMA_table'); $this->assertEquals('1', $result1['hasPrimaryKey']); } /** - * Test for Normalization::getHtmlContentsFor1NFStep4 + * Test for getHtmlContentsFor1NFStep4 * * @return void */ @@ -217,7 +219,7 @@ class NormalizationTest extends TestCase { $db = "PMA_db"; $table= "PMA_table"; - $result = Normalization::getHtmlContentsFor1NFStep4($db, $table); + $result = $this->normalization->getHtmlContentsFor1NFStep4($db, $table); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); $this->assertArrayHasKey('headText', $result); @@ -225,7 +227,7 @@ class NormalizationTest extends TestCase $this->assertArrayHasKey('extra', $result); $this->assertContains(__('Step 1.') . 4, $result['legendText']); $this->assertContains( - Normalization::getHtmlForColumnsList($db, $table, 'all', "checkbox"), + $this->normalization->getHtmlForColumnsList($db, $table, 'all', "checkbox"), $result['extra'] ); $this->assertContains( @@ -235,7 +237,7 @@ class NormalizationTest extends TestCase } /** - * Test for Normalization::getHtmlContentsFor1NFStep3 + * Test for getHtmlContentsFor1NFStep3 * * @return void */ @@ -243,7 +245,7 @@ class NormalizationTest extends TestCase { $db = "PMA_db"; $table= "PMA_table"; - $result = Normalization::getHtmlContentsFor1NFStep3($db, $table); + $result = $this->normalization->getHtmlContentsFor1NFStep3($db, $table); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); $this->assertArrayHasKey('headText', $result); @@ -252,7 +254,7 @@ class NormalizationTest extends TestCase $this->assertArrayHasKey('primary_key', $result); $this->assertContains(__('Step 1.') . 3, $result['legendText']); $this->assertContains( - Normalization::getHtmlForColumnsList($db, $table, 'all', "checkbox"), + $this->normalization->getHtmlForColumnsList($db, $table, 'all', "checkbox"), $result['extra'] ); $this->assertContains( @@ -263,7 +265,7 @@ class NormalizationTest extends TestCase } /** - * Test for Normalization::getHtmlFor2NFstep1 + * Test for getHtmlFor2NFstep1 * * @return void */ @@ -271,7 +273,7 @@ class NormalizationTest extends TestCase { $db = "PMA_db"; $table= "PMA_table"; - $result = Normalization::getHtmlFor2NFstep1($db, $table); + $result = $this->normalization->getHtmlFor2NFstep1($db, $table); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); $this->assertArrayHasKey('headText', $result); @@ -280,7 +282,7 @@ class NormalizationTest extends TestCase $this->assertArrayHasKey('primary_key', $result); $this->assertContains(__('Step 2.') . 1, $result['legendText']); $this->assertEquals('id', $result['primary_key']); - $result1 = Normalization::getHtmlFor2NFstep1($db, "PMA_table2"); + $result1 = $this->normalization->getHtmlFor2NFstep1($db, "PMA_table2"); $this->assertEquals('id, col1', $result1['primary_key']); $this->assertContains( 'array('col2')); - $result = Normalization::getHtmlForNewTables2NF($partialDependencies, $table); + $result = $this->normalization->getHtmlForNewTables2NF($partialDependencies, $table); $this->assertContains( 'id = 'PMA_table'; $tablesName->col1 = 'PMA_table1'; $partialDependencies = array('id'=>array('col2')); - $result = Normalization::createNewTablesFor2NF( + $result = $this->normalization->createNewTablesFor2NF( $partialDependencies, $tablesName, $table, $db ); $this->assertInternalType('array', $result); @@ -329,7 +331,7 @@ class NormalizationTest extends TestCase $this->assertArrayHasKey('headText', $result); $this->assertArrayHasKey('queryError', $result); $partialDependencies = array('id'=>array('col2'), 'col1'=>array('col2')); - $result1 = Normalization::createNewTablesFor2NF( + $result1 = $this->normalization->createNewTablesFor2NF( $partialDependencies, $tablesName, $table, $db ); $this->assertArrayHasKey('extra', $result1); @@ -338,7 +340,7 @@ class NormalizationTest extends TestCase } /** - * Test for Normalization::getHtmlForNewTables3NF + * Test for getHtmlForNewTables3NF * * @return void */ @@ -348,7 +350,7 @@ class NormalizationTest extends TestCase $db = 'PMA_db'; $dependencies = new stdClass(); $dependencies->col1 = array('col2'); - $result = Normalization::getHtmlForNewTables3NF($dependencies, $tables, $db); + $result = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $db); $this->assertEquals( array( 'html' => '', @@ -358,7 +360,7 @@ class NormalizationTest extends TestCase ); $tables= array("PMA_table"=>array('col1', 'PMA_table')); $dependencies->PMA_table = array('col4', 'col5'); - $result1 = Normalization::getHtmlForNewTables3NF($dependencies, $tables, $db); + $result1 = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $db); $this->assertInternalType('array', $result1); $this->assertContains( 'pk = 'col2'; $cols1->nonpk = 'col3, col4'; $newTables = array('PMA_table'=>array('PMA_table'=>$cols, 'table1'=>$cols1)); - $result = Normalization::createNewTablesFor3NF( + $result = $this->normalization->createNewTablesFor3NF( $newTables, $db ); $this->assertInternalType('array', $result); @@ -403,7 +405,7 @@ class NormalizationTest extends TestCase $this->assertArrayHasKey('headText', $result); $this->assertArrayHasKey('queryError', $result); $newTables1 = array(); - $result1 = Normalization::createNewTablesFor3NF( + $result1 = $this->normalization->createNewTablesFor3NF( $newTables1, $db ); $this->assertArrayHasKey('queryError', $result1); @@ -412,7 +414,7 @@ class NormalizationTest extends TestCase } /** - * Test for Normalization::moveRepeatingGroup + * Test for moveRepeatingGroup * * @return void */ @@ -424,7 +426,7 @@ class NormalizationTest extends TestCase $newColumn = 'PMA_newCol'; $table= "PMA_table"; $db = 'PMA_db'; - $result = Normalization::moveRepeatingGroup( + $result = $this->normalization->moveRepeatingGroup( $repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db ); $this->assertInternalType('array', $result); @@ -436,7 +438,7 @@ class NormalizationTest extends TestCase } /** - * Test for Normalization::getHtmlFor3NFstep1 + * Test for getHtmlFor3NFstep1 * * @return void */ @@ -444,7 +446,7 @@ class NormalizationTest extends TestCase { $db = "PMA_db"; $tables= array("PMA_table"); - $result = Normalization::getHtmlFor3NFstep1($db, $tables); + $result = $this->normalization->getHtmlFor3NFstep1($db, $tables); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); $this->assertArrayHasKey('headText', $result); @@ -459,20 +461,20 @@ class NormalizationTest extends TestCase 'normalization->getHtmlFor3NFstep1($db, array("PMA_table2")); $this->assertEquals( '', $result1['subText'] ); } /** - * Test for Normalization::getHtmlForNormalizetable + * Test for getHtmlForNormalizetable * * @return void */ public function testPMAGetHtmlForNormalizetable() { - $result = Normalization::getHtmlForNormalizetable(); + $result = $this->normalization->getHtmlForNormalizetable(); $this->assertContains( '
normalization->findPartialDependencies($table, $db); $this->assertContains( '
getMethod('getAllCombinationPartialKeys'); + $method->setAccessible(true); + $primaryKey = array('id', 'col1', 'col2'); - $result = Normalization::getAllCombinationPartialKeys($primaryKey); + $result = $method->invokeArgs($this->normalization, [$primaryKey]); $this->assertEquals( array('', 'id', 'col1', 'col1,id', 'col2', 'col2,id', 'col2,col1'), $result From e2fa9664fb2b29d80bce7f3560955892a65b6393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 10 Feb 2018 00:37:13 -0200 Subject: [PATCH 6/7] Use DI for DatabaseInterface instance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Normalization.php | 67 ++++++++++++++++++----------- normalization.php | 2 +- test/classes/NormalizationTest.php | 2 +- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/libraries/classes/Normalization.php b/libraries/classes/Normalization.php index 9931a7a8f5..36bfcd43c4 100644 --- a/libraries/classes/Normalization.php +++ b/libraries/classes/Normalization.php @@ -22,6 +22,23 @@ use PhpMyAdmin\Util; */ class Normalization { + /** + * DatabaseInterface instance + * + * @var DatabaseInterface + */ + private $dbi; + + /** + * Constructor + * + * @param DatabaseInterface $dbi DatabaseInterface instance + */ + public function __construct(DatabaseInterface $dbi) + { + $this->dbi = $dbi; + } + /** * build the html for columns of $colTypeCategory category * in form of given $listType in a table @@ -39,11 +56,11 @@ class Normalization ) { $columnTypeList = array(); if ($colTypeCategory != 'all') { - $types = $GLOBALS['dbi']->types->getColumns(); + $types = $this->dbi->types->getColumns(); $columnTypeList = $types[$colTypeCategory]; } - $GLOBALS['dbi']->selectDb($db); - $columns = $GLOBALS['dbi']->getColumns( + $this->dbi->selectDb($db); + $columns = $this->dbi->getColumns( $db, $table, null, true ); @@ -129,10 +146,10 @@ class Normalization 'server_type' => Util::getServerType(), 'max_rows' => intval($GLOBALS['cfg']['MaxRows']), 'char_editing' => $GLOBALS['cfg']['CharEditing'], - 'attribute_types' => $GLOBALS['dbi']->types->getAttributes(), + 'attribute_types' => $this->dbi->types->getAttributes(), 'privs_available' => $GLOBALS['col_priv'] && $GLOBALS['is_reload_priv'], - 'max_length' => $GLOBALS['dbi']->getVersion() >= 50503 ? 1024 : 255, - 'dbi' => $GLOBALS['dbi'], + 'max_length' => $this->dbi->getVersion() >= 50503 ? 1024 : 255, + 'dbi' => $this->dbi, 'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'], ) ); @@ -352,8 +369,8 @@ class Normalization } $key = implode(', ', $pk); if (count($primarycols) > 1) { - $GLOBALS['dbi']->selectDb($db); - $columns = (array) $GLOBALS['dbi']->getColumnNames( + $this->dbi->selectDb($db); + $columns = (array) $this->dbi->getColumnNames( $db, $table ); if (count($pk) == count($columns)) { @@ -476,7 +493,7 @@ class Normalization ); } $message = ''; - $GLOBALS['dbi']->selectDb($db); + $this->dbi->selectDb($db); foreach ($partialDependencies as $key=>$dependents) { if ($tablesName->$key != $table) { $backquotedKey = implode(', ', Util::backquote(explode(', ', $key))); @@ -505,11 +522,11 @@ class Normalization $queries[] = 'DROP TABLE ' . Util::backquote($table); } foreach ($queries as $query) { - if (!$GLOBALS['dbi']->tryQuery($query)) { + if (!$this->dbi->tryQuery($query)) { $message = Message::error(__('Error in processing!')); $message->addMessage( Message::rawError( - $GLOBALS['dbi']->getError() + $this->dbi->getError() ), '

' ); @@ -608,7 +625,7 @@ class Normalization ); } $message = ''; - $GLOBALS['dbi']->selectDb($db); + $this->dbi->selectDb($db); foreach ($newTables as $originalTable=>$tablesList) { foreach ($tablesList as $table=>$cols) { if ($table != $originalTable) { @@ -629,7 +646,7 @@ class Normalization } } if ($dropCols) { - $columns = (array) $GLOBALS['dbi']->getColumnNames( + $columns = (array) $this->dbi->getColumnNames( $db, $originalTable ); $colPresent = array_merge( @@ -650,11 +667,11 @@ class Normalization $dropCols = false; } foreach ($queries as $query) { - if (!$GLOBALS['dbi']->tryQuery($query)) { + if (!$this->dbi->tryQuery($query)) { $message = Message::error(__('Error in processing!')); $message->addMessage( Message::rawError( - $GLOBALS['dbi']->getError() + $this->dbi->getError() ), '

' ); @@ -714,13 +731,13 @@ class Normalization } $query2 = trim($query2, ','); $queries = array($query1, $query2); - $GLOBALS['dbi']->selectDb($db); + $this->dbi->selectDb($db); foreach ($queries as $query) { - if (!$GLOBALS['dbi']->tryQuery($query)) { + if (!$this->dbi->tryQuery($query)) { $message = Message::error(__('Error in processing!')); $message->addMessage( Message::rawError( - $GLOBALS['dbi']->getError() + $this->dbi->getError() ), '

' ); @@ -766,8 +783,8 @@ class Normalization foreach ($primarycols as $col) { $pk[] = $col->getName(); } - $GLOBALS['dbi']->selectDb($db); - $columns = (array) $GLOBALS['dbi']->getColumnNames( + $this->dbi->selectDb($db); + $columns = (array) $this->dbi->getColumnNames( $db, $table ); if (count($columns) - count($pk) <= 1) { @@ -862,12 +879,12 @@ class Normalization public function findPartialDependencies($table, $db) { $dependencyList = array(); - $GLOBALS['dbi']->selectDb($db); - $columns = (array) $GLOBALS['dbi']->getColumnNames( + $this->dbi->selectDb($db); + $columns = (array) $this->dbi->getColumnNames( $db, $table ); $columns = (array)Util::backquote($columns); - $totalRowsRes = $GLOBALS['dbi']->fetchResult( + $totalRowsRes = $this->dbi->fetchResult( 'SELECT COUNT(*) FROM (SELECT * FROM ' . Util::backquote($table) . ' LIMIT 500) as dt;' ); @@ -943,7 +960,7 @@ class Normalization . 'COUNT(DISTINCT ' . $partialKey . ',' . $column . ') as pkColCnt ' . 'FROM (SELECT * FROM ' . Util::backquote($table) . ' LIMIT 500) as dt' . ';'; - $res = $GLOBALS['dbi']->fetchResult($query, null, null); + $res = $this->dbi->fetchResult($query, null, null); $pkColCnt = $res[0]; if ($pkCnt && $pkCnt == $colCnt && $colCnt == $pkColCnt) { return true; @@ -976,7 +993,7 @@ class Normalization $query = trim($query, ', '); $query .= ' FROM (SELECT * FROM ' . Util::backquote($table) . ' LIMIT 500) as dt' . ';'; - $res = $GLOBALS['dbi']->fetchResult($query, null, null); + $res = $this->dbi->fetchResult($query, null, null); foreach ($columns as $column) { if ($column) { $result[$column] = $res[0][$column . '_cnt']; diff --git a/normalization.php b/normalization.php index 9d5251d65b..c68a15c5b8 100644 --- a/normalization.php +++ b/normalization.php @@ -13,7 +13,7 @@ use PhpMyAdmin\Url; require_once 'libraries/common.inc.php'; -$normalization = new Normalization(); +$normalization = new Normalization($GLOBALS['dbi']); if (isset($_REQUEST['getColumns'])) { $html = '' diff --git a/test/classes/NormalizationTest.php b/test/classes/NormalizationTest.php index 533dfadbc4..677b3830b8 100644 --- a/test/classes/NormalizationTest.php +++ b/test/classes/NormalizationTest.php @@ -93,7 +93,7 @@ class NormalizationTest extends TestCase ->method('fetchResult') ->will($this->returnValue(array(0))); - $this->normalization = new Normalization(); + $this->normalization = new Normalization($dbi); } /** From 8f0dbe453b29b3369d3417fe9c17435622edd537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 10 Feb 2018 01:08:31 -0200 Subject: [PATCH 7/7] Fix some coding style issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Normalization.php | 296 ++++++++++++++++------------ normalization.php | 2 +- test/classes/NormalizationTest.php | 187 ++++++++++-------- 3 files changed, 274 insertions(+), 211 deletions(-) diff --git a/libraries/classes/Normalization.php b/libraries/classes/Normalization.php index 36bfcd43c4..0fe950b739 100644 --- a/libraries/classes/Normalization.php +++ b/libraries/classes/Normalization.php @@ -52,24 +52,29 @@ class Normalization * @return string HTML for list of columns in form of given list types */ public function getHtmlForColumnsList( - $db, $table, $colTypeCategory='all', $listType='dropdown' + $db, + $table, + $colTypeCategory = 'all', + $listType = 'dropdown' ) { - $columnTypeList = array(); + $columnTypeList = []; if ($colTypeCategory != 'all') { $types = $this->dbi->types->getColumns(); $columnTypeList = $types[$colTypeCategory]; } $this->dbi->selectDb($db); $columns = $this->dbi->getColumns( - $db, $table, null, + $db, + $table, + null, true ); $type = ""; $selectColHtml = ""; foreach ($columns as $column => $def) { if (isset($def['Type'])) { - $extracted_columnspec = Util::extractColumnSpec($def['Type']); - $type = $extracted_columnspec['type']; + $extractedColumnSpec = Util::extractColumnSpec($def['Type']); + $type = $extractedColumnSpec['type']; } if (empty($columnTypeList) || in_array(mb_strtoupper($type), $columnTypeList) @@ -93,7 +98,7 @@ class Normalization /** * get the html of the form to add the new column to given table * - * @param integer $num_fields number of columns to add + * @param integer $numFields number of columns to add * @param string $db current database * @param string $table current table * @param array $columnMeta array containing default values for the fields @@ -101,66 +106,67 @@ class Normalization * @return string HTML */ public function getHtmlForCreateNewColumn( - $num_fields, $db, $table, array $columnMeta = array() + $numFields, + $db, + $table, + array $columnMeta = [] ) { $cfgRelation = Relation::getRelationsParam(); - $content_cells = array(); - $available_mime = array(); - $mime_map = array(); + $contentCells = []; + $availableMime = []; + $mimeMap = []; if ($cfgRelation['mimework'] && $GLOBALS['cfg']['BrowseMIME']) { - $mime_map = Transformations::getMIME($db, $table); - $available_mime = Transformations::getAvailableMIMEtypes(); + $mimeMap = Transformations::getMIME($db, $table); + $availableMime = Transformations::getAvailableMIMEtypes(); } - $comments_map = Relation::getComments($db, $table); - for ($columnNumber = 0; $columnNumber < $num_fields; $columnNumber++) { - $content_cells[$columnNumber] = array( + $commentsMap = Relation::getComments($db, $table); + for ($columnNumber = 0; $columnNumber < $numFields; $columnNumber++) { + $contentCells[$columnNumber] = [ 'column_number' => $columnNumber, 'column_meta' => $columnMeta, 'type_upper' => '', 'length_values_input_size' => 8, 'length' => '', - 'extracted_columnspec' => array(), + 'extracted_columnspec' => [], 'submit_attribute' => null, - 'comments_map' => $comments_map, + 'comments_map' => $commentsMap, 'fields_meta' => null, 'is_backup' => true, - 'move_columns' => array(), + 'move_columns' => [], 'cfg_relation' => $cfgRelation, - 'available_mime' => isset($available_mime)?$available_mime:array(), - 'mime_map' => $mime_map - ); + 'available_mime' => isset($availableMime) ? $availableMime : [], + 'mime_map' => $mimeMap + ]; } return Template::get( 'columns_definitions/table_fields_definitions' - ) - ->render( - array( - 'is_backup' => true, - 'fields_meta' => null, - 'mimework' => $cfgRelation['mimework'], - 'content_cells' => $content_cells, - 'change_column' => $_REQUEST['change_column'], - 'is_virtual_columns_supported' => Util::isVirtualColumnsSupported(), - 'browse_mime' => $GLOBALS['cfg']['BrowseMIME'], - 'server_type' => Util::getServerType(), - 'max_rows' => intval($GLOBALS['cfg']['MaxRows']), - 'char_editing' => $GLOBALS['cfg']['CharEditing'], - 'attribute_types' => $this->dbi->types->getAttributes(), - 'privs_available' => $GLOBALS['col_priv'] && $GLOBALS['is_reload_priv'], - 'max_length' => $this->dbi->getVersion() >= 50503 ? 1024 : 255, - 'dbi' => $this->dbi, - 'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'], - ) - ); + )->render([ + 'is_backup' => true, + 'fields_meta' => null, + 'mimework' => $cfgRelation['mimework'], + 'content_cells' => $contentCells, + 'change_column' => $_REQUEST['change_column'], + 'is_virtual_columns_supported' => Util::isVirtualColumnsSupported(), + 'browse_mime' => $GLOBALS['cfg']['BrowseMIME'], + 'server_type' => Util::getServerType(), + 'max_rows' => intval($GLOBALS['cfg']['MaxRows']), + 'char_editing' => $GLOBALS['cfg']['CharEditing'], + 'attribute_types' => $this->dbi->types->getAttributes(), + 'privs_available' => $GLOBALS['col_priv'] && $GLOBALS['is_reload_priv'], + 'max_length' => $this->dbi->getVersion() >= 50503 ? 1024 : 255, + 'dbi' => $this->dbi, + 'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'], + ]); } + /** * build the html for step 1.1 of normalization * * @param string $db current database * @param string $table current table * @param string $normalizedTo up to which step normalization will go, - * possible values 1nf|2nf|3nf + * possible values 1nf|2nf|3nf * * @return string HTML for step 1.1 */ @@ -237,7 +243,8 @@ class Normalization ); $subText = '
' . Util::getIcon( - 'b_index_add', __( + 'b_index_add', + __( 'Add a primary key on existing column(s)' ) ) @@ -249,13 +256,13 @@ class Normalization . '' . __('+ Add a new primary key column') . ''; } - $res = array( + $res = [ 'legendText' => $legendText, 'headText' => $headText, 'subText' => $subText, 'hasPrimaryKey' => $hasPrimaryKey, 'extra' => $extra - ); + ]; return $res; } @@ -288,12 +295,12 @@ class Normalization . ''; - $res = array( + $res = [ 'legendText' => $legendText, 'headText' => $headText, 'subText' => $subText, 'extra' => $extra - ); + ]; return $res; } @@ -330,17 +337,17 @@ class Normalization . '/>'; $primary = Index::getPrimary($table, $db); $primarycols = $primary->getColumns(); - $pk = array(); + $pk = []; foreach ($primarycols as $col) { $pk[] = $col->getName(); } - $res = array( + $res = [ 'legendText' => $legendText, 'headText' => $headText, 'subText' => $subText, 'extra' => $extra, 'primary_key' => json_encode($pk) - ); + ]; return $res; } @@ -357,7 +364,7 @@ class Normalization $legendText = __('Step 2.') . "1 " . __('Find partial dependencies'); $primary = Index::getPrimary($table, $db); $primarycols = $primary->getColumns(); - $pk = array(); + $pk = []; $subText = ''; $selectPkForm = ""; $extra = ""; @@ -371,7 +378,8 @@ class Normalization if (count($primarycols) > 1) { $this->dbi->selectDb($db); $columns = (array) $this->dbi->getColumnNames( - $db, $table + $db, + $table ); if (count($pk) == count($columns)) { $headText = sprintf( @@ -379,7 +387,8 @@ class Normalization 'No partial dependencies possible as ' . 'no non-primary column exists since primary key ( %1$s ) ' . 'is composed of all the columns in the table.' - ), htmlspecialchars($key) + ), + htmlspecialchars($key) ) . '
'; $extra = '

' . __('Table is already in second normal form.') . '

'; @@ -388,7 +397,8 @@ class Normalization __( 'The primary key ( %1$s ) consists of more than one column ' . 'so we need to find the partial dependencies.' - ), htmlspecialchars($key) + ), + htmlspecialchars($key) ) . '
' . __( 'Please answer the following question(s) ' . 'carefully to obtain a correct normalization.' @@ -408,7 +418,8 @@ class Normalization if (!in_array($column, $pk)) { $cnt++; $extra .= "" . sprintf( - __('\'%1$s\' depends on:'), htmlspecialchars($column) + __('\'%1$s\' depends on:'), + htmlspecialchars($column) ) . "
"; $extra .= '' @@ -421,17 +432,18 @@ class Normalization __( 'No partial dependencies possible as the primary key' . ' ( %1$s ) has just one column.' - ), htmlspecialchars($key) + ), + htmlspecialchars($key) ) . '
'; $extra = '

' . __('Table is already in second normal form.') . '

'; } - $res = array( + $res = [ 'legendText' => $legendText, 'headText' => $headText, 'subText' => $subText, 'extra' => $extra, 'primary_key' => $key - ); + ]; return $res; } @@ -450,15 +462,16 @@ class Normalization 'In order to put the ' . 'original table \'%1$s\' into Second normal form we need ' . 'to create the following tables:' - ), htmlspecialchars($table) + ), + htmlspecialchars($table) ) . '

'; $tableName = $table; $i = 1; - foreach ($partialDependencies as $key=>$dependents) { + foreach ($partialDependencies as $key => $dependents) { $html .= '

' . '( ' . htmlspecialchars($key) . '' - . (count($dependents)>0?', ':'') + . (count($dependents)>0?', ':'') . htmlspecialchars(implode(', ', $dependents)) . ' )'; $i++; $tableName = 'table' . $i; @@ -479,22 +492,22 @@ class Normalization public function createNewTablesFor2NF(array $partialDependencies, $tablesName, $table, $db) { $dropCols = false; - $nonPKCols = array(); - $queries = array(); + $nonPKCols = []; + $queries = []; $error = false; $headText = '

' . sprintf( __('The second step of normalization is complete for table \'%1$s\'.'), htmlspecialchars($table) ) . '

'; if (count((array)$partialDependencies) == 1) { - return array( + return [ 'legendText'=>__('End of step'), 'headText'=>$headText, 'queryError'=>$error - ); + ]; } $message = ''; $this->dbi->selectDb($db); - foreach ($partialDependencies as $key=>$dependents) { + foreach ($partialDependencies as $key => $dependents) { if ($tablesName->$key != $table) { $backquotedKey = implode(', ', Util::backquote(explode(', ', $key))); $queries[] = 'CREATE TABLE ' . Util::backquote($tablesName->$key) @@ -534,12 +547,12 @@ class Normalization break; } } - return array( + return [ 'legendText' => __('End of step'), 'headText' => $headText, 'queryError' => $error, 'extra' => $message - ); + ]; } /** @@ -556,14 +569,14 @@ class Normalization { $html = ""; $i = 1; - $newTables = array(); - foreach ($tables as $table=>$arrDependson) { + $newTables = []; + foreach ($tables as $table => $arrDependson) { if (count(array_unique($arrDependson)) == 1) { continue; } $primary = Index::getPrimary($table, $db); $primarycols = $primary->getColumns(); - $pk = array(); + $pk = []; foreach ($primarycols as $col) { $pk[] = $col->getName(); } @@ -572,10 +585,11 @@ class Normalization 'In order to put the ' . 'original table \'%1$s\' into Third normal form we need ' . 'to create the following tables:' - ), htmlspecialchars($table) + ), + htmlspecialchars($table) ) . '

'; $tableName = $table; - $columnList = array(); + $columnList = []; foreach ($arrDependson as $key) { $dependents = $dependencies->$key; if ($key == $table) { @@ -589,17 +603,17 @@ class Normalization . htmlspecialchars($tableName) . '" value="' . htmlspecialchars($tableName) . '"/>' . '( ' . htmlspecialchars($key) . '' - . (count($dependents)>0?', ':'') + . (count($dependents)>0?', ':'') . htmlspecialchars(implode(', ', $dependents)) . ' )'; - $newTables[$table][$tableName] = array( + $newTables[$table][$tableName] = [ "pk"=>$key, "nonpk"=>implode(', ', $dependents) - ); + ]; $i++; $tableName = 'table' . $i; } } } - return array('html' => $html, 'newTables' => $newTables, 'success' => true); + return ['html' => $html, 'newTables' => $newTables, 'success' => true]; } /** @@ -612,28 +626,30 @@ class Normalization */ public function createNewTablesFor3NF(array $newTables, $db) { - $queries = array(); + $queries = []; $dropCols = false; $error = false; $headText = '

' . __('The third step of normalization is complete.') . '

'; if (count((array)$newTables) == 0) { - return array( + return [ 'legendText'=>__('End of step'), 'headText'=>$headText, 'queryError'=>$error - ); + ]; } $message = ''; $this->dbi->selectDb($db); - foreach ($newTables as $originalTable=>$tablesList) { - foreach ($tablesList as $table=>$cols) { + foreach ($newTables as $originalTable => $tablesList) { + foreach ($tablesList as $table => $cols) { if ($table != $originalTable) { $quotedPk = implode( - ', ', Util::backquote(explode(', ', $cols->pk)) + ', ', + Util::backquote(explode(', ', $cols->pk)) ); $quotedNonpk = implode( - ', ', Util::backquote(explode(', ', $cols->nonpk)) + ', ', + Util::backquote(explode(', ', $cols->nonpk)) ); $queries[] = 'CREATE TABLE ' . Util::backquote($table) . ' SELECT DISTINCT ' . $quotedPk @@ -647,10 +663,12 @@ class Normalization } if ($dropCols) { $columns = (array) $this->dbi->getColumnNames( - $db, $originalTable + $db, + $originalTable ); $colPresent = array_merge( - explode(', ', $dropCols->pk), explode(', ', $dropCols->nonpk) + explode(', ', $dropCols->pk), + explode(', ', $dropCols->nonpk) ); $query = 'ALTER TABLE ' . Util::backquote($originalTable); foreach ($columns as $col) { @@ -679,20 +697,20 @@ class Normalization break; } } - return array( + return [ 'legendText' => __('End of step'), 'headText' => $headText, 'queryError' => $error, 'extra' => $message - ); + ]; } /** * move the repeating group of columns to a new table * * @param string $repeatingColumns comma separated list of repeating group columns - * @param string $primary_columns comma separated list of column in primary key - * of $table + * @param string $primaryColumns comma separated list of column in primary key + * of $table * @param string $newTable name of the new table to be created * @param string $newColumn name of the new column in the new table * @param string $table current table @@ -701,13 +719,19 @@ class Normalization * @return array */ public function moveRepeatingGroup( - $repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db + $repeatingColumns, + $primaryColumns, + $newTable, + $newColumn, + $table, + $db ) { $repeatingColumnsArr = (array)Util::backquote( explode(', ', $repeatingColumns) ); - $primary_columns = implode( - ',', Util::backquote(explode(',', $primary_columns)) + $primaryColumns = implode( + ',', + Util::backquote(explode(',', $primaryColumns)) ); $query1 = 'CREATE TABLE ' . Util::backquote($newTable); $query2 = 'ALTER TABLE ' . Util::backquote($table); @@ -724,13 +748,13 @@ class Normalization $query1 .= ' UNION '; } $first = false; - $query1 .= ' SELECT ' . $primary_columns . ',' . $repeatingColumn + $query1 .= ' SELECT ' . $primaryColumns . ',' . $repeatingColumn . ' as ' . Util::backquote($newColumn) . ' FROM ' . Util::backquote($table); $query2 .= ' DROP ' . $repeatingColumn . ','; } $query2 = trim($query2, ','); - $queries = array($query1, $query2); + $queries = [$query1, $query2]; $this->dbi->selectDb($db); foreach ($queries as $query) { if (!$this->dbi->tryQuery($query)) { @@ -745,9 +769,9 @@ class Normalization break; } } - return array( + return [ 'queryError' => $error, 'message' => $message - ); + ]; } /** @@ -779,13 +803,14 @@ class Normalization $primary = Index::getPrimary($table, $db); $primarycols = $primary->getColumns(); $selectTdForm = ""; - $pk = array(); + $pk = []; foreach ($primarycols as $col) { $pk[] = $col->getName(); } $this->dbi->selectDb($db); $columns = (array) $this->dbi->getColumnNames( - $db, $table + $db, + $table ); if (count($columns) - count($pk) <= 1) { continue; @@ -801,7 +826,8 @@ class Normalization if (!in_array($column, $pk)) { $cnt++; $extra .= "" . sprintf( - __('\'%1$s\' depends on:'), htmlspecialchars($column) + __('\'%1$s\' depends on:'), + htmlspecialchars($column) ) . "
"; $extra .= '"; } - $res = array( + $res = [ 'legendText' => $legendText, 'headText' => $headText, 'subText' => $subText, 'extra' => $extra - ); + ]; return $res; } @@ -834,28 +860,31 @@ class Normalization * * @return string HTML */ - public function getHtmlForNormalizetable() + public function getHtmlForNormalizeTable() { - $html_output = '' . Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']) . ''; - $html_output .= '
'; - $html_output .= '' + $htmlOutput .= '
'; + $htmlOutput .= '' . __('Improve table structure (Normalization):') . ''; - $html_output .= '

' . __('Select up to what step you want to normalize') + $htmlOutput .= '

' . __('Select up to what step you want to normalize') . '

'; - $choices = array( + $choices = [ '1nf' => __('First step of normalization (1NF)'), '2nf' => __('Second step of normalization (1NF+2NF)'), - '3nf' => __('Third step of normalization (1NF+2NF+3NF)')); + '3nf' => __('Third step of normalization (1NF+2NF+3NF)')]; - $html_output .= Util::getRadioFields( - 'normalizeTo', $choices, '1nf', true + $htmlOutput .= Util::getRadioFields( + 'normalizeTo', + $choices, + '1nf', + true ); - $html_output .= '
' + $htmlOutput .= '
' . "" . __( 'Hint: Please follow the procedure carefully in order ' . 'to obtain correct normalization' @@ -865,7 +894,7 @@ class Normalization . '' . '
'; - return $html_output; + return $htmlOutput; } /** @@ -878,10 +907,11 @@ class Normalization */ public function findPartialDependencies($table, $db) { - $dependencyList = array(); + $dependencyList = []; $this->dbi->selectDb($db); $columns = (array) $this->dbi->getColumnNames( - $db, $table + $db, + $table ); $columns = (array)Util::backquote($columns); $totalRowsRes = $this->dbi->fetchResult( @@ -891,7 +921,7 @@ class Normalization $totalRows = $totalRowsRes[0]; $primary = Index::getPrimary($table, $db); $primarycols = $primary->getColumns(); - $pk = array(); + $pk = []; foreach ($primarycols as $col) { $pk[] = Util::backquote($col->getName()); } @@ -899,16 +929,20 @@ class Normalization $distinctValCount = $this->findDistinctValuesCount( array_unique( array_merge($columns, $partialKeys) - ), $table + ), + $table ); foreach ($columns as $column) { if (!in_array($column, $pk)) { foreach ($partialKeys as $partialKey) { if ($partialKey && $this->checkPartialDependency( - $partialKey, $column, $table, + $partialKey, + $column, + $table, $distinctValCount[$partialKey], - $distinctValCount[$column], $totalRows + $distinctValCount[$column], + $totalRows ) ) { $dependencyList[$partialKey][] = $column; @@ -922,7 +956,7 @@ class Normalization . 'and is not necessarily accurate. ' ) . '
'; - foreach ($dependencyList as $dependon=>$colList) { + foreach ($dependencyList as $dependon => $colList) { $html .= '' . '' . '' @@ -944,7 +978,7 @@ class Normalization * check whether a particular column is dependent on given subset of primary key * * @param string $partialKey the partial key, subset of primary key, - * each column in key supposed to be backquoted + * each column in key supposed to be backquoted * @param string $column backquoted column on whose dependency being checked * @param string $table current table * @param integer $pkCnt distinct value count for given partial key @@ -954,12 +988,17 @@ class Normalization * @return boolean TRUE if $column is dependent on $partialKey, False otherwise */ private function checkPartialDependency( - $partialKey, $column, $table, $pkCnt, $colCnt, $totalRows + $partialKey, + $column, + $table, + $pkCnt, + $colCnt, + $totalRows ) { $query = 'SELECT ' . 'COUNT(DISTINCT ' . $partialKey . ',' . $column . ') as pkColCnt ' . 'FROM (SELECT * FROM ' . Util::backquote($table) - . ' LIMIT 500) as dt' . ';'; + . ' LIMIT 500) as dt' . ';'; $res = $this->dbi->fetchResult($query, null, null); $pkColCnt = $res[0]; if ($pkCnt && $pkCnt == $colCnt && $colCnt == $pkColCnt) { @@ -975,18 +1014,18 @@ class Normalization * function to get distinct values count of all the column in the array $columns * * @param array $columns array of backquoted columns whose distinct values - * need to be counted. + * need to be counted. * @param string $table table to which these columns belong * * @return array associative array containing the count */ private function findDistinctValuesCount(array $columns, $table) { - $result = array(); + $result = []; $query = 'SELECT '; foreach ($columns as $column) { if ($column) { //each column is already backquoted - $query .= 'COUNT(DISTINCT ' . $column . ') as \'' + $query .= 'COUNT(DISTINCT ' . $column . ') as \'' . $column . '_cnt\', '; } } @@ -1011,11 +1050,12 @@ class Normalization */ private function getAllCombinationPartialKeys(array $primaryKey) { - $results = array(''); + $results = ['']; foreach ($primaryKey as $element) { foreach ($results as $combination) { array_push( - $results, trim($element . ',' . $combination, ',') + $results, + trim($element . ',' . $combination, ',') ); } } diff --git a/normalization.php b/normalization.php index c68a15c5b8..8f3c55c2a4 100644 --- a/normalization.php +++ b/normalization.php @@ -121,5 +121,5 @@ if (isset($_REQUEST['step1'])) { $res = $normalization->getHtmlFor3NFstep1($db, $tables); $response->addJSON($res); } else { - $response->addHTML($normalization->getHtmlForNormalizetable()); + $response->addHTML($normalization->getHtmlForNormalizeTable()); } diff --git a/test/classes/NormalizationTest.php b/test/classes/NormalizationTest.php index 677b3830b8..f9bf1996c3 100644 --- a/test/classes/NormalizationTest.php +++ b/test/classes/NormalizationTest.php @@ -59,30 +59,30 @@ class NormalizationTest extends TestCase ->method('getColumns') ->will( $this->returnValue( - array( - "id"=>array("Type"=>"integer"), - "col1"=>array("Type"=>'varchar(100)'), - "col2"=>array("Type"=>'DATETIME') - ) + [ + "id"=>["Type"=>"integer"], + "col1"=>["Type"=>'varchar(100)'], + "col2"=>["Type"=>'DATETIME'] + ] ) ); $dbi->expects($this->any()) ->method('getColumnNames') - ->will($this->returnValue(array("id", "col1", "col2"))); - $map = array( - array('PMA_db', 'PMA_table1', DatabaseInterface::CONNECT_USER, array()), - array( + ->will($this->returnValue(["id", "col1", "col2"])); + $map = [ + ['PMA_db', 'PMA_table1', DatabaseInterface::CONNECT_USER, []], + [ 'PMA_db', 'PMA_table', DatabaseInterface::CONNECT_USER, - array(array('Key_name'=>'PRIMARY', 'Column_name'=>'id')) - ), - array( + [['Key_name'=>'PRIMARY', 'Column_name'=>'id']] + ], + [ 'PMA_db', 'PMA_table2', DatabaseInterface::CONNECT_USER, - array( - array('Key_name'=>'PRIMARY', 'Column_name'=>'id'), - array('Key_name'=>'PRIMARY', 'Column_name'=>'col1') - ) - ), - ); + [ + ['Key_name'=>'PRIMARY', 'Column_name'=>'id'], + ['Key_name'=>'PRIMARY', 'Column_name'=>'col1'] + ] + ], + ]; $dbi->expects($this->any()) ->method('getTableIndexes') ->will($this->returnValueMap($map)); @@ -91,7 +91,7 @@ class NormalizationTest extends TestCase ->will($this->returnValue(true)); $dbi->expects($this->any()) ->method('fetchResult') - ->will($this->returnValue(array(0))); + ->will($this->returnValue([0])); $this->normalization = new Normalization($dbi); } @@ -101,7 +101,7 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlForColumnsList() + public function testGetHtmlForColumnsList() { $db = "PMA_db"; $table= "PMA_table"; @@ -120,15 +120,15 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlForCreateNewColumn() + public function testGetHtmlForCreateNewColumn() { $GLOBALS['cfg']['BrowseMIME'] = true; $GLOBALS['cfg']['MaxRows'] = 25; $GLOBALS['col_priv'] = false; $db = "PMA_db"; - $table= "PMA_table"; - $num_fields = 1; - $result = $this->normalization->getHtmlForCreateNewColumn($num_fields, $db, $table); + $table = "PMA_table"; + $numFields = 1; + $result = $this->normalization->getHtmlForCreateNewColumn($numFields, $db, $table); $this->assertContains( '
assertContains( $this->normalization->getHtmlForColumnsList( - $db, $table, _pgettext('string types', 'String') - ), $result + $db, + $table, + _pgettext('string types', 'String') + ), + $result ); - } /** @@ -185,7 +187,7 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlContentsFor1NFStep2() + public function testGetHtmlContentsFor1NFStep2() { $db = "PMA_db"; $table= "PMA_table1"; @@ -215,7 +217,7 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlContentsFor1NFStep4() + public function testGetHtmlContentsFor1NFStep4() { $db = "PMA_db"; $table= "PMA_table"; @@ -241,7 +243,7 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlContentsFor1NFStep3() + public function testGetHtmlContentsFor1NFStep3() { $db = "PMA_db"; $table= "PMA_table"; @@ -261,7 +263,7 @@ class NormalizationTest extends TestCase 'assertEquals(json_encode(array('id')), $result['primary_key']); + $this->assertEquals(json_encode(['id']), $result['primary_key']); } /** @@ -269,7 +271,7 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlFor2NFstep1() + public function testGetHtmlFor2NFstep1() { $db = "PMA_db"; $table= "PMA_table"; @@ -299,10 +301,10 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlForNewTables2NF() + public function testGetHtmlForNewTables2NF() { $table= "PMA_table"; - $partialDependencies = array('col1'=>array('col2')); + $partialDependencies = ['col1'=>['col2']]; $result = $this->normalization->getHtmlForNewTables2NF($partialDependencies, $table); $this->assertContains( 'id = 'PMA_table'; $tablesName->col1 = 'PMA_table1'; - $partialDependencies = array('id'=>array('col2')); + $partialDependencies = ['id'=>['col2']]; $result = $this->normalization->createNewTablesFor2NF( - $partialDependencies, $tablesName, $table, $db + $partialDependencies, + $tablesName, + $table, + $db ); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); $this->assertArrayHasKey('headText', $result); $this->assertArrayHasKey('queryError', $result); - $partialDependencies = array('id'=>array('col2'), 'col1'=>array('col2')); + $partialDependencies = ['id'=>['col2'], 'col1'=>['col2']]; $result1 = $this->normalization->createNewTablesFor2NF( - $partialDependencies, $tablesName, $table, $db + $partialDependencies, + $tablesName, + $table, + $db ); $this->assertArrayHasKey('extra', $result1); $this->assertEquals(__('End of step'), $result1['legendText']); @@ -344,22 +352,23 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlForNewTables3NF() + public function testGetHtmlForNewTables3NF() { - $tables= array("PMA_table"=>array('col1')); + $tables= ["PMA_table"=>['col1']]; $db = 'PMA_db'; $dependencies = new stdClass(); - $dependencies->col1 = array('col2'); + $dependencies->col1 = ['col2']; $result = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $db); $this->assertEquals( - array( + [ 'html' => '', 'success' => true, - 'newTables' => array() - ), $result + 'newTables' => [] + ], + $result ); - $tables= array("PMA_table"=>array('col1', 'PMA_table')); - $dependencies->PMA_table = array('col4', 'col5'); + $tables= ["PMA_table"=>['col1', 'PMA_table']]; + $dependencies->PMA_table = ['col4', 'col5']; $result1 = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $db); $this->assertInternalType('array', $result1); $this->assertContains( @@ -367,18 +376,19 @@ class NormalizationTest extends TestCase $result1['html'] ); $this->assertEquals( - array( - 'PMA_table' => array ( - 'PMA_table' => array ( + [ + 'PMA_table' => [ + 'PMA_table' => [ 'pk' => 'col1', 'nonpk' => 'col2' - ), - 'table2' => array ( + ], + 'table2' => [ 'pk' => 'id', 'nonpk' => 'col4, col5' - ) - ) - ), $result1['newTables'] + ] + ] + ], + $result1['newTables'] ); } @@ -387,7 +397,7 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMACreateNewTablesFor3NF() + public function testCreateNewTablesFor3NF() { $db = 'PMA_db'; $cols = new stdClass(); @@ -396,17 +406,19 @@ class NormalizationTest extends TestCase $cols1 = new stdClass(); $cols1->pk = 'col2'; $cols1->nonpk = 'col3, col4'; - $newTables = array('PMA_table'=>array('PMA_table'=>$cols, 'table1'=>$cols1)); + $newTables = ['PMA_table'=>['PMA_table'=>$cols, 'table1'=>$cols1]]; $result = $this->normalization->createNewTablesFor3NF( - $newTables, $db + $newTables, + $db ); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); $this->assertArrayHasKey('headText', $result); $this->assertArrayHasKey('queryError', $result); - $newTables1 = array(); + $newTables1 = []; $result1 = $this->normalization->createNewTablesFor3NF( - $newTables1, $db + $newTables1, + $db ); $this->assertArrayHasKey('queryError', $result1); $this->assertEquals(__('End of step'), $result1['legendText']); @@ -418,22 +430,28 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAMoveRepeatingGroup() + public function testMoveRepeatingGroup() { $repeatingColumns = 'col1, col2'; - $primary_columns = 'id,col1'; + $primaryColumns = 'id,col1'; $newTable = 'PMA_newTable'; $newColumn = 'PMA_newCol'; $table= "PMA_table"; $db = 'PMA_db'; $result = $this->normalization->moveRepeatingGroup( - $repeatingColumns, $primary_columns, $newTable, $newColumn, $table, $db + $repeatingColumns, + $primaryColumns, + $newTable, + $newColumn, + $table, + $db ); $this->assertInternalType('array', $result); $this->assertArrayHasKey('queryError', $result); $this->assertArrayHasKey('message', $result); $this->assertInstanceOf( - 'PhpMyAdmin\Message', $result['message'] + 'PhpMyAdmin\Message', + $result['message'] ); } @@ -442,10 +460,10 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetHtmlFor3NFstep1() + public function testGetHtmlFor3NFstep1() { $db = "PMA_db"; - $tables= array("PMA_table"); + $tables= ["PMA_table"]; $result = $this->normalization->getHtmlFor3NFstep1($db, $tables); $this->assertInternalType('array', $result); $this->assertArrayHasKey('legendText', $result); @@ -461,37 +479,42 @@ class NormalizationTest extends TestCase 'normalization->getHtmlFor3NFstep1($db, array("PMA_table2")); + $result1 = $this->normalization->getHtmlFor3NFstep1($db, ["PMA_table2"]); $this->assertEquals( - '', $result1['subText'] + '', + $result1['subText'] ); } /** - * Test for getHtmlForNormalizetable + * Test for getHtmlForNormalizeTable * * @return void */ - public function testPMAGetHtmlForNormalizetable() + public function testgetHtmlForNormalizeTable() { - $result = $this->normalization->getHtmlForNormalizetable(); + $result = $this->normalization->getHtmlForNormalizeTable(); $this->assertContains( 'assertContains( - '', $result + '', + $result ); - $choices = array( + $choices = [ '1nf' => __('First step of normalization (1NF)'), '2nf' => __('Second step of normalization (1NF+2NF)'), - '3nf' => __('Third step of normalization (1NF+2NF+3NF)')); + '3nf' => __('Third step of normalization (1NF+2NF+3NF)')]; - $html_tmp = Util::getRadioFields( - 'normalizeTo', $choices, '1nf', true + $htmlTmp = Util::getRadioFields( + 'normalizeTo', + $choices, + '1nf', + true ); - $this->assertContains($html_tmp, $result); + $this->assertContains($htmlTmp, $result); } /** @@ -499,7 +522,7 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAFindPartialDependencies() + public function testFindPartialDependencies() { $table= "PMA_table2"; $db = 'PMA_db'; @@ -516,16 +539,16 @@ class NormalizationTest extends TestCase * * @return void */ - public function testPMAGetAllCombinationPartialKeys() + public function testGetAllCombinationPartialKeys() { $class = new ReflectionClass(Normalization::class); $method = $class->getMethod('getAllCombinationPartialKeys'); $method->setAccessible(true); - $primaryKey = array('id', 'col1', 'col2'); + $primaryKey = ['id', 'col1', 'col2']; $result = $method->invokeArgs($this->normalization, [$primaryKey]); $this->assertEquals( - array('', 'id', 'col1', 'col1,id', 'col2', 'col2,id', 'col2,col1'), + ['', 'id', 'col1', 'col1,id', 'col2', 'col2,id', 'col2,col1'], $result ); }