From b92e95d062b126c99ebee0369d4a827dc3e0ff1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 11:22:56 -0300 Subject: [PATCH 1/9] Extract normalization 3NF new tables action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 4 +- .../ThirdNormalForm/NewTablesController.php | 33 ++++++++ .../Controllers/NormalizationController.php | 13 --- libraries/routes.php | 1 + libraries/services_controllers.php | 8 ++ phpstan-baseline.neon | 12 +-- psalm-baseline.xml | 24 ++++-- .../NewTablesControllerTest.php | 82 +++++++++++++++++++ .../NormalizationControllerTest.php | 71 ---------------- 9 files changed, 147 insertions(+), 101 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php create mode 100644 test/classes/Controllers/Normalization/ThirdNormalForm/NewTablesControllerTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index c800ff634d..6306e95f09 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -366,10 +366,10 @@ function goTo3NFStep2 (pd, tablesTds) { 'tables': JSON.stringify(tablesTds), 'server': window.CommonParams.get('server'), 'pd': JSON.stringify(pd), - 'getNewTables3NF':1 }; + }; $.ajax({ type: 'POST', - url: 'index.php?route=/normalization', + url: 'index.php?route=/normalization/3nf/new-tables', data: datastring, async:false, success: function (data) { diff --git a/libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php b/libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php new file mode 100644 index 0000000000..1717a29f39 --- /dev/null +++ b/libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php @@ -0,0 +1,33 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $dependencies = json_decode($_POST['pd']); + $tables = json_decode($_POST['tables'], true); + $newTables = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $GLOBALS['db']); + $this->response->addJSON($newTables); + } +} diff --git a/libraries/classes/Controllers/NormalizationController.php b/libraries/classes/Controllers/NormalizationController.php index 1dbf6586bd..eb1ad441ce 100644 --- a/libraries/classes/Controllers/NormalizationController.php +++ b/libraries/classes/Controllers/NormalizationController.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace PhpMyAdmin\Controllers; -use PhpMyAdmin\Core; use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Normalization; use PhpMyAdmin\ResponseRenderer; @@ -15,7 +14,6 @@ use function __; use function _pgettext; use function intval; use function json_decode; -use function json_encode; use function min; /** @@ -82,17 +80,6 @@ class NormalizationController extends AbstractController return; } - if (isset($_POST['getNewTables3NF'])) { - $dependencies = json_decode($_POST['pd']); - $tables = json_decode($_POST['tables'], true); - $newTables = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $GLOBALS['db']); - $this->response->disable(); - Core::headerJSON(); - echo json_encode($newTables); - - return; - } - $this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']); if (isset($_POST['createNewTables2NF'])) { diff --git a/libraries/routes.php b/libraries/routes.php index 7f17bbf603..d79d8e831d 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -138,6 +138,7 @@ return static function (RouteCollector $routes): void { $routes->post('/1nf/step4', Normalization\FirstNormalForm\FourthStepController::class); $routes->post('/2nf/new-tables', Normalization\SecondNormalForm\NewTablesController::class); $routes->post('/2nf/step1', Normalization\SecondNormalForm\FirstStepController::class); + $routes->post('/3nf/new-tables', Normalization\ThirdNormalForm\NewTablesController::class); $routes->post('/3nf/step1', Normalization\ThirdNormalForm\FirstStepController::class); }); $routes->get('/phpinfo', PhpInfoController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index 3d9eb312ea..8187c52721 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -641,6 +641,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\ThirdNormalForm\NewTablesController::class => [ + 'class' => Normalization\ThirdNormalForm\NewTablesController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], NormalizationController::class => [ 'class' => NormalizationController::class, 'arguments' => [ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index b223fbc395..69fd359214 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1228,7 +1228,12 @@ parameters: - message: "#^Parameter \\#1 \\$dependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects object, mixed given\\.$#" count: 1 - path: libraries/classes/Controllers/NormalizationController.php + path: libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php + + - + message: "#^Parameter \\#2 \\$tables of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects array, mixed given\\.$#" + count: 1 + path: libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php - message: "#^Parameter \\#1 \\$newTables of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor3NF\\(\\) expects array, mixed given\\.$#" @@ -1240,11 +1245,6 @@ parameters: count: 1 path: libraries/classes/Controllers/NormalizationController.php - - - message: "#^Parameter \\#2 \\$tables of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects array, mixed given\\.$#" - count: 1 - path: libraries/classes/Controllers/NormalizationController.php - - message: "#^Parameter \\#2 \\$tablesName of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor2NF\\(\\) expects object, mixed given\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 98672f2a3c..eba1b98b29 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2424,32 +2424,38 @@ $tables - - - $_POST['newTables'] - $_POST['newTablesName'] - $_POST['pd'] + + $_POST['pd'] $_POST['tables'] $dependencies + $tables + + + $dependencies + $tables + + + + + $_POST['newTables'] + $_POST['newTablesName'] + $_POST['pd'] $newColumn $newTable $newtables $partialDependencies $primary_columns $repeatingColumns - $tables $tablesName - - $dependencies + $newColumn $newTable $newtables $partialDependencies $primary_columns $repeatingColumns - $tables $tablesName diff --git a/test/classes/Controllers/Normalization/ThirdNormalForm/NewTablesControllerTest.php b/test/classes/Controllers/Normalization/ThirdNormalForm/NewTablesControllerTest.php new file mode 100644 index 0000000000..88d14fc7c0 --- /dev/null +++ b/test/classes/Controllers/Normalization/ThirdNormalForm/NewTablesControllerTest.php @@ -0,0 +1,82 @@ + [ + 'event', + 'event', + 'event', + 'event', + 'NameOfVenue', + 'event', + 'period', + 'event', + 'event', + ], + ]); + $_POST['pd'] = json_encode([ + '' => [], + 'event' => [ + 'TypeOfEvent', + 'period', + 'Start_time', + 'NameOfVenue', + 'LocationOfVenue', + ], + 'NameOfVenue' => ['DateOfEvent'], + 'period' => ['NumberOfGuests'], + ]); + + $dbi = $this->createDatabaseInterface(); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new NewTablesController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + // phpcs:disable Generic.Files.LineLength.TooLong + $this->assertSame([ + 'html' => '

In order to put the original table \'test_table\' into Third normal form we need to create the following tables:

( event, TypeOfEvent, period, Start_time, NameOfVenue, LocationOfVenue )

( NameOfVenue, DateOfEvent )

( period, NumberOfGuests )', + 'newTables' => [ + 'test_table' => [ + 'test_table' => [ + 'pk' => 'event', + 'nonpk' => 'TypeOfEvent, period, Start_time, NameOfVenue, LocationOfVenue', + ], + 'table2' => ['pk' => 'NameOfVenue', 'nonpk' => 'DateOfEvent'], + 'table3' => ['pk' => 'period', 'nonpk' => 'NumberOfGuests'], + ], + ], + 'success' => true, + ], $response->getJSONResult()); + // phpcs:enable + } +} diff --git a/test/classes/Controllers/NormalizationControllerTest.php b/test/classes/Controllers/NormalizationControllerTest.php index 6e8ca6e76d..173243b2e3 100644 --- a/test/classes/Controllers/NormalizationControllerTest.php +++ b/test/classes/Controllers/NormalizationControllerTest.php @@ -46,77 +46,6 @@ class NormalizationControllerTest extends AbstractTestCase $GLOBALS['table'] = 'test_tbl'; } - public function testGetNewTables3NF(): void - { - $_POST['getNewTables3NF'] = 1; - $_POST['tables'] = json_encode([ - 'test_tbl' => [ - 'event', - 'event', - 'event', - 'event', - 'NameOfVenue', - 'event', - 'period', - 'event', - 'event', - ], - ]); - $_POST['pd'] = json_encode([ - '' => [], - 'event' => [ - 'TypeOfEvent', - 'period', - 'Start_time', - 'NameOfVenue', - 'LocationOfVenue', - ], - 'NameOfVenue' => ['DateOfEvent'], - 'period' => ['NumberOfGuests'], - ]); - - $GLOBALS['goto'] = 'index.php?route=/sql'; - $GLOBALS['containerBuilder']->setParameter('db', $GLOBALS['db']); - $GLOBALS['containerBuilder']->setParameter('table', $GLOBALS['table']); - /** @var NormalizationController $normalizationController */ - $normalizationController = $GLOBALS['containerBuilder']->get(NormalizationController::class); - $normalizationController($this->createStub(ServerRequest::class)); - - $this->assertResponseWasSuccessfull(); - - $this->getResponseJsonResult();// Will echo the contents - - $data = (string) json_encode( - [ - 'html' => '

In order to put the original table \'test_tbl\' into ' - . 'Third normal form we need to create the following tables:' - . '

' - . '( event, TypeOfEvent, period, Start_time, NameOfVenue, LocationOfVenue )' - . '

' - . '( NameOfVenue, DateOfEvent )

' - . '( period, NumberOfGuests )', - 'newTables' => [ - 'test_tbl' => [ - 'test_tbl' => [ - 'pk' => 'event', - 'nonpk' => 'TypeOfEvent, period, Start_time, NameOfVenue, LocationOfVenue', - ], - 'table2' => [ - 'pk' => 'NameOfVenue', - 'nonpk' => 'DateOfEvent', - ], - 'table3' => [ - 'pk' => 'period', - 'nonpk' => 'NumberOfGuests', - ], - ], - ], - 'success' => true, - ] - ); - $this->expectOutputString($data); - } - public function testCreateNewTables2NF(): void { $_POST['createNewTables2NF'] = 1; From c9bccf3b3d4ea402e0accd5ae2625fe2b107f81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 13:47:03 -0300 Subject: [PATCH 2/9] Extract normalization 2NF create new tables action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 6 +- .../CreateNewTablesController.php | 38 +++++++++++++ .../Controllers/NormalizationController.php | 14 ----- libraries/routes.php | 1 + libraries/services_controllers.php | 8 +++ phpstan-baseline.neon | 20 +++---- psalm-baseline.xml | 22 +++++--- .../CreateNewTablesControllerTest.php | 55 +++++++++++++++++++ .../NormalizationControllerTest.php | 34 ------------ 9 files changed, 129 insertions(+), 69 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesController.php create mode 100644 test/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesControllerTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index 6306e95f09..27d7639f5a 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -220,11 +220,11 @@ function goTo2NFFinish (pd) { 'table': window.CommonParams.get('table'), 'server': window.CommonParams.get('server'), 'pd': JSON.stringify(pd), - 'newTablesName':JSON.stringify(tables), - 'createNewTables2NF':1 }; + 'newTablesName': JSON.stringify(tables), + }; $.ajax({ type: 'POST', - url: 'index.php?route=/normalization', + url: 'index.php?route=/normalization/2nf/create-new-tables', data: datastring, async:false, success: function (data) { diff --git a/libraries/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesController.php b/libraries/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesController.php new file mode 100644 index 0000000000..2be037c58d --- /dev/null +++ b/libraries/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesController.php @@ -0,0 +1,38 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $partialDependencies = json_decode($_POST['pd'], true); + $tablesName = json_decode($_POST['newTablesName']); + $res = $this->normalization->createNewTablesFor2NF( + $partialDependencies, + $tablesName, + $GLOBALS['table'], + $GLOBALS['db'] + ); + $this->response->addJSON($res); + } +} diff --git a/libraries/classes/Controllers/NormalizationController.php b/libraries/classes/Controllers/NormalizationController.php index eb1ad441ce..f32c11a1cc 100644 --- a/libraries/classes/Controllers/NormalizationController.php +++ b/libraries/classes/Controllers/NormalizationController.php @@ -82,20 +82,6 @@ class NormalizationController extends AbstractController $this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']); - if (isset($_POST['createNewTables2NF'])) { - $partialDependencies = json_decode($_POST['pd'], true); - $tablesName = json_decode($_POST['newTablesName']); - $res = $this->normalization->createNewTablesFor2NF( - $partialDependencies, - $tablesName, - $GLOBALS['table'], - $GLOBALS['db'] - ); - $this->response->addJSON($res); - - return; - } - if (isset($_POST['createNewTables3NF'])) { $newtables = json_decode($_POST['newTables'], true); $res = $this->normalization->createNewTablesFor3NF($newtables, $GLOBALS['db']); diff --git a/libraries/routes.php b/libraries/routes.php index d79d8e831d..7fcdfdc00d 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -136,6 +136,7 @@ return static function (RouteCollector $routes): void { $routes->post('/1nf/step2', Normalization\FirstNormalForm\SecondStepController::class); $routes->post('/1nf/step3', Normalization\FirstNormalForm\ThirdStepController::class); $routes->post('/1nf/step4', Normalization\FirstNormalForm\FourthStepController::class); + $routes->post('/2nf/create-new-tables', Normalization\SecondNormalForm\CreateNewTablesController::class); $routes->post('/2nf/new-tables', Normalization\SecondNormalForm\NewTablesController::class); $routes->post('/2nf/step1', Normalization\SecondNormalForm\FirstStepController::class); $routes->post('/3nf/new-tables', Normalization\ThirdNormalForm\NewTablesController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index 8187c52721..b87ac7fbad 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -617,6 +617,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\SecondNormalForm\CreateNewTablesController::class => [ + 'class' => Normalization\SecondNormalForm\CreateNewTablesController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], Normalization\SecondNormalForm\FirstStepController::class => [ 'class' => Normalization\SecondNormalForm\FirstStepController::class, 'arguments' => [ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 69fd359214..34fb2434e7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1220,6 +1220,16 @@ parameters: count: 1 path: libraries/classes/Controllers/HomeController.php + - + message: "#^Parameter \\#1 \\$partialDependencies of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor2NF\\(\\) expects array, mixed given\\.$#" + count: 1 + path: libraries/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesController.php + + - + message: "#^Parameter \\#2 \\$tablesName of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor2NF\\(\\) expects object, mixed given\\.$#" + count: 1 + path: libraries/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesController.php + - message: "#^Parameter \\#1 \\$partialDependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables2NF\\(\\) expects array, mixed given\\.$#" count: 1 @@ -1240,16 +1250,6 @@ parameters: count: 1 path: libraries/classes/Controllers/NormalizationController.php - - - message: "#^Parameter \\#1 \\$partialDependencies of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor2NF\\(\\) expects array, mixed given\\.$#" - count: 1 - path: libraries/classes/Controllers/NormalizationController.php - - - - message: "#^Parameter \\#2 \\$tablesName of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor2NF\\(\\) expects object, mixed given\\.$#" - count: 1 - path: libraries/classes/Controllers/NormalizationController.php - - message: "#^Property PhpMyAdmin\\\\Controllers\\\\Server\\\\BinlogController\\:\\:\\$binaryLogs type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index eba1b98b29..03849e79ea 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2407,6 +2407,18 @@ $normalForm + + + $_POST['newTablesName'] + $_POST['pd'] + $partialDependencies + $tablesName + + + $partialDependencies + $tablesName + + $_POST['pd'] @@ -2437,26 +2449,20 @@ - + $_POST['newTables'] - $_POST['newTablesName'] - $_POST['pd'] $newColumn $newTable $newtables - $partialDependencies $primary_columns $repeatingColumns - $tablesName - + $newColumn $newTable $newtables - $partialDependencies $primary_columns $repeatingColumns - $tablesName diff --git a/test/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesControllerTest.php b/test/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesControllerTest.php new file mode 100644 index 0000000000..e9feec843c --- /dev/null +++ b/test/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesControllerTest.php @@ -0,0 +1,55 @@ + [], 'task' => ['timestamp']]); + $_POST['newTablesName'] = json_encode(['ID, task' => 'batch_log2', 'task' => 'table2']); + + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('CREATE TABLE `batch_log2` SELECT DISTINCT `ID`, `task` FROM `test_table`;', []); + $dbiDummy->addResult('CREATE TABLE `table2` SELECT DISTINCT `task`, `timestamp` FROM `test_table`;', []); + $dbiDummy->addResult('DROP TABLE `test_table`', []); + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new CreateNewTablesController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + $this->assertSame([ + 'legendText' => 'End of step', + 'headText' => '

The second step of normalization is complete for table \'test_table\'.

', + 'queryError' => false, + 'extra' => '', + ], $response->getJSONResult()); + } +} diff --git a/test/classes/Controllers/NormalizationControllerTest.php b/test/classes/Controllers/NormalizationControllerTest.php index 173243b2e3..e608c822dc 100644 --- a/test/classes/Controllers/NormalizationControllerTest.php +++ b/test/classes/Controllers/NormalizationControllerTest.php @@ -46,40 +46,6 @@ class NormalizationControllerTest extends AbstractTestCase $GLOBALS['table'] = 'test_tbl'; } - public function testCreateNewTables2NF(): void - { - $_POST['createNewTables2NF'] = 1; - $_POST['pd'] = json_encode([ - 'ID, task' => [], - 'task' => ['timestamp'], - ]); - $_POST['newTablesName'] = json_encode([ - 'ID, task' => 'batch_log2', - 'task' => 'table2', - ]); - - $GLOBALS['goto'] = 'index.php?route=/sql'; - $GLOBALS['containerBuilder']->setParameter('db', $GLOBALS['db']); - $GLOBALS['containerBuilder']->setParameter('table', $GLOBALS['table']); - /** @var NormalizationController $normalizationController */ - $normalizationController = $GLOBALS['containerBuilder']->get(NormalizationController::class); - $this->dummyDbi->addSelectDb('my_db'); - $normalizationController($this->createStub(ServerRequest::class)); - $this->dummyDbi->assertAllSelectsConsumed(); - - $this->assertResponseWasSuccessfull(); - - $this->assertSame( - [ - 'legendText' => 'End of step', - 'headText' => '

The second step of normalization is complete for table \'test_tbl\'.

', - 'queryError' => false, - 'extra' => '', - ], - $this->getResponseJsonResult() - ); - } - public function testCreateNewTables3NF(): void { $_POST['createNewTables3NF'] = 1; From 97c5c01a8764eafa2b4016bf3fee63090b084e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 14:43:13 -0300 Subject: [PATCH 3/9] Extract normalization 3NF create new tables action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 6 +- .../CreateNewTablesController.php | 32 ++++++++++ .../Controllers/NormalizationController.php | 9 --- libraries/routes.php | 1 + libraries/services_controllers.php | 8 +++ phpstan-baseline.neon | 10 +-- psalm-baseline.xml | 16 +++-- .../CreateNewTablesControllerTest.php | 64 +++++++++++++++++++ .../NormalizationControllerTest.php | 39 ----------- 9 files changed, 124 insertions(+), 61 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php create mode 100644 test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index 27d7639f5a..c5334e2d08 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -265,11 +265,11 @@ function goTo3NFFinish (newTables) { 'ajax_request': true, 'db': window.CommonParams.get('db'), 'server': window.CommonParams.get('server'), - 'newTables':JSON.stringify(newTables), - 'createNewTables3NF':1 }; + 'newTables': JSON.stringify(newTables), + }; $.ajax({ type: 'POST', - url: 'index.php?route=/normalization', + url: 'index.php?route=/normalization/3nf/create-new-tables', data: datastring, async:false, success: function (data) { diff --git a/libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php b/libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php new file mode 100644 index 0000000000..bd3e0b2c4a --- /dev/null +++ b/libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php @@ -0,0 +1,32 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $newtables = json_decode($_POST['newTables'], true); + $res = $this->normalization->createNewTablesFor3NF($newtables, $GLOBALS['db']); + $this->response->addJSON($res); + } +} diff --git a/libraries/classes/Controllers/NormalizationController.php b/libraries/classes/Controllers/NormalizationController.php index f32c11a1cc..2f2a981816 100644 --- a/libraries/classes/Controllers/NormalizationController.php +++ b/libraries/classes/Controllers/NormalizationController.php @@ -13,7 +13,6 @@ use PhpMyAdmin\Url; use function __; use function _pgettext; use function intval; -use function json_decode; use function min; /** @@ -82,14 +81,6 @@ class NormalizationController extends AbstractController $this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']); - if (isset($_POST['createNewTables3NF'])) { - $newtables = json_decode($_POST['newTables'], true); - $res = $this->normalization->createNewTablesFor3NF($newtables, $GLOBALS['db']); - $this->response->addJSON($res); - - return; - } - if (isset($_POST['repeatingColumns'])) { $repeatingColumns = $_POST['repeatingColumns']; $newTable = $_POST['newTable']; diff --git a/libraries/routes.php b/libraries/routes.php index 7fcdfdc00d..fe0cfb7fae 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -139,6 +139,7 @@ return static function (RouteCollector $routes): void { $routes->post('/2nf/create-new-tables', Normalization\SecondNormalForm\CreateNewTablesController::class); $routes->post('/2nf/new-tables', Normalization\SecondNormalForm\NewTablesController::class); $routes->post('/2nf/step1', Normalization\SecondNormalForm\FirstStepController::class); + $routes->post('/3nf/create-new-tables', Normalization\ThirdNormalForm\CreateNewTablesController::class); $routes->post('/3nf/new-tables', Normalization\ThirdNormalForm\NewTablesController::class); $routes->post('/3nf/step1', Normalization\ThirdNormalForm\FirstStepController::class); }); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index b87ac7fbad..d38f99f167 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -641,6 +641,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\ThirdNormalForm\CreateNewTablesController::class => [ + 'class' => Normalization\ThirdNormalForm\CreateNewTablesController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], Normalization\ThirdNormalForm\FirstStepController::class => [ 'class' => Normalization\ThirdNormalForm\FirstStepController::class, 'arguments' => [ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 34fb2434e7..371bfa983e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1235,6 +1235,11 @@ parameters: count: 1 path: libraries/classes/Controllers/Normalization/SecondNormalForm/NewTablesController.php + - + message: "#^Parameter \\#1 \\$newTables of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor3NF\\(\\) expects array, mixed given\\.$#" + count: 1 + path: libraries/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesController.php + - message: "#^Parameter \\#1 \\$dependencies of method PhpMyAdmin\\\\Normalization\\:\\:getHtmlForNewTables3NF\\(\\) expects object, mixed given\\.$#" count: 1 @@ -1245,11 +1250,6 @@ parameters: count: 1 path: libraries/classes/Controllers/Normalization/ThirdNormalForm/NewTablesController.php - - - message: "#^Parameter \\#1 \\$newTables of method PhpMyAdmin\\\\Normalization\\:\\:createNewTablesFor3NF\\(\\) expects array, mixed given\\.$#" - count: 1 - path: libraries/classes/Controllers/NormalizationController.php - - message: "#^Property PhpMyAdmin\\\\Controllers\\\\Server\\\\BinlogController\\:\\:\\$binaryLogs type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 03849e79ea..2e48ec18a3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2428,6 +2428,15 @@ $partialDependencies
+ + + $_POST['newTables'] + $newtables + + + $newtables + + $tables @@ -2449,18 +2458,15 @@ - - $_POST['newTables'] + $newColumn $newTable - $newtables $primary_columns $repeatingColumns - + $newColumn $newTable - $newtables $primary_columns $repeatingColumns diff --git a/test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php b/test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php new file mode 100644 index 0000000000..a6980ce7ee --- /dev/null +++ b/test/classes/Controllers/Normalization/ThirdNormalForm/CreateNewTablesControllerTest.php @@ -0,0 +1,64 @@ + [ + 'event' => [ + 'pk' => 'eventID', + 'nonpk' => 'Start_time, DateOfEvent, NumberOfGuests, NameOfVenue, LocationOfVenue', + ], + 'table2' => ['pk' => 'Start_time', 'nonpk' => 'TypeOfEvent, period'], + ], + ]); + + // phpcs:disable Generic.Files.LineLength.TooLong + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('CREATE TABLE `event` SELECT DISTINCT `eventID`, `Start_time`, `DateOfEvent`, `NumberOfGuests`, `NameOfVenue`, `LocationOfVenue` FROM `test_table`;', []); + $dbiDummy->addResult('CREATE TABLE `table2` SELECT DISTINCT `Start_time`, `TypeOfEvent`, `period` FROM `test_table`;', []); + $dbiDummy->addResult('DROP TABLE `test_table`', []); + // phpcs:enable + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new CreateNewTablesController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + $this->assertSame([ + 'legendText' => 'End of step', + 'headText' => '

The third step of normalization is complete.

', + 'queryError' => false, + 'extra' => '', + ], $response->getJSONResult()); + } +} diff --git a/test/classes/Controllers/NormalizationControllerTest.php b/test/classes/Controllers/NormalizationControllerTest.php index e608c822dc..e884b255f7 100644 --- a/test/classes/Controllers/NormalizationControllerTest.php +++ b/test/classes/Controllers/NormalizationControllerTest.php @@ -16,7 +16,6 @@ use PhpMyAdmin\Tests\Stubs\ResponseRenderer; use PhpMyAdmin\Transformations; use function in_array; -use function json_encode; /** * @covers \PhpMyAdmin\Controllers\NormalizationController @@ -46,44 +45,6 @@ class NormalizationControllerTest extends AbstractTestCase $GLOBALS['table'] = 'test_tbl'; } - public function testCreateNewTables3NF(): void - { - $_POST['createNewTables3NF'] = 1; - $_POST['newTables'] = json_encode([ - 'test_tbl' => [ - 'event' => [ - 'pk' => 'eventID', - 'nonpk' => 'Start_time, DateOfEvent, NumberOfGuests, NameOfVenue, LocationOfVenue', - ], - 'table2' => [ - 'pk' => 'Start_time', - 'nonpk' => 'TypeOfEvent, period', - ], - ], - ]); - - $GLOBALS['goto'] = 'index.php?route=/sql'; - $GLOBALS['containerBuilder']->setParameter('db', $GLOBALS['db']); - $GLOBALS['containerBuilder']->setParameter('table', $GLOBALS['table']); - /** @var NormalizationController $normalizationController */ - $normalizationController = $GLOBALS['containerBuilder']->get(NormalizationController::class); - $this->dummyDbi->addSelectDb('my_db'); - $normalizationController($this->createStub(ServerRequest::class)); - $this->dummyDbi->assertAllSelectsConsumed(); - - $this->assertResponseWasSuccessfull(); - - $this->assertSame( - [ - 'legendText' => 'End of step', - 'headText' => '

The third step of normalization is complete.

', - 'queryError' => false, - 'extra' => '', - ], - $this->getResponseJsonResult() - ); - } - public function testNormalization(): void { $GLOBALS['db'] = 'test_db'; From a5024edcc5c259fd6d48920e19e0fa4cd7097998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 15:31:41 -0300 Subject: [PATCH 4/9] Extract normalization's move repeating group action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 2 +- .../Normalization/MoveRepeatingGroup.php | 40 ++++++++++++++ .../Controllers/NormalizationController.php | 18 ------- libraries/routes.php | 1 + libraries/services_controllers.php | 8 +++ psalm-baseline.xml | 28 +++++----- .../Normalization/MoveRepeatingGroupTest.php | 53 +++++++++++++++++++ 7 files changed, 117 insertions(+), 33 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/MoveRepeatingGroup.php create mode 100644 test/classes/Controllers/Normalization/MoveRepeatingGroupTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index c5334e2d08..d119cd9d19 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -465,7 +465,7 @@ function moveRepeatingGroup (repeatingCols) { }; $.ajax({ type: 'POST', - url: 'index.php?route=/normalization', + url: 'index.php?route=/normalization/move-repeating-group', data: datastring, async:false, success: function (data) { diff --git a/libraries/classes/Controllers/Normalization/MoveRepeatingGroup.php b/libraries/classes/Controllers/Normalization/MoveRepeatingGroup.php new file mode 100644 index 0000000000..f308d9a1e0 --- /dev/null +++ b/libraries/classes/Controllers/Normalization/MoveRepeatingGroup.php @@ -0,0 +1,40 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $repeatingColumns = $_POST['repeatingColumns']; + $newTable = $_POST['newTable']; + $newColumn = $_POST['newColumn']; + $primary_columns = $_POST['primary_columns']; + $res = $this->normalization->moveRepeatingGroup( + $repeatingColumns, + $primary_columns, + $newTable, + $newColumn, + $GLOBALS['table'], + $GLOBALS['db'] + ); + $this->response->addJSON($res); + } +} diff --git a/libraries/classes/Controllers/NormalizationController.php b/libraries/classes/Controllers/NormalizationController.php index 2f2a981816..9765d7ec8e 100644 --- a/libraries/classes/Controllers/NormalizationController.php +++ b/libraries/classes/Controllers/NormalizationController.php @@ -81,24 +81,6 @@ class NormalizationController extends AbstractController $this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']); - if (isset($_POST['repeatingColumns'])) { - $repeatingColumns = $_POST['repeatingColumns']; - $newTable = $_POST['newTable']; - $newColumn = $_POST['newColumn']; - $primary_columns = $_POST['primary_columns']; - $res = $this->normalization->moveRepeatingGroup( - $repeatingColumns, - $primary_columns, - $newTable, - $newColumn, - $GLOBALS['table'], - $GLOBALS['db'] - ); - $this->response->addJSON($res); - - return; - } - $this->render('table/normalization/normalization', [ 'db' => $GLOBALS['db'], 'table' => $GLOBALS['table'], diff --git a/libraries/routes.php b/libraries/routes.php index fe0cfb7fae..be116075c2 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -142,6 +142,7 @@ return static function (RouteCollector $routes): void { $routes->post('/3nf/create-new-tables', Normalization\ThirdNormalForm\CreateNewTablesController::class); $routes->post('/3nf/new-tables', Normalization\ThirdNormalForm\NewTablesController::class); $routes->post('/3nf/step1', Normalization\ThirdNormalForm\FirstStepController::class); + $routes->post('/move-repeating-group', Normalization\MoveRepeatingGroup::class); }); $routes->get('/phpinfo', PhpInfoController::class); $routes->addGroup('/preferences', static function (RouteCollector $routes): void { diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index d38f99f167..65e5b48f47 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -665,6 +665,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\MoveRepeatingGroup::class => [ + 'class' => Normalization\MoveRepeatingGroup::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], NormalizationController::class => [ 'class' => NormalizationController::class, 'arguments' => [ diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 2e48ec18a3..c6df54e6e3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2407,6 +2407,20 @@ $normalForm
+ + + $newColumn + $newTable + $primary_columns + $repeatingColumns + + + $newColumn + $newTable + $primary_columns + $repeatingColumns + + $_POST['newTablesName'] @@ -2457,20 +2471,6 @@ $tables - - - $newColumn - $newTable - $primary_columns - $repeatingColumns - - - $newColumn - $newTable - $primary_columns - $repeatingColumns - - $GLOBALS['cfg']['ShowPhpInfo'] diff --git a/test/classes/Controllers/Normalization/MoveRepeatingGroupTest.php b/test/classes/Controllers/Normalization/MoveRepeatingGroupTest.php new file mode 100644 index 0000000000..b7ff21a73a --- /dev/null +++ b/test/classes/Controllers/Normalization/MoveRepeatingGroupTest.php @@ -0,0 +1,53 @@ +createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('CREATE TABLE `new_table` SELECT `id`,`col1`,`col1` as `new_column` FROM `test_table` UNION SELECT `id`,`col1`,`col2` as `new_column` FROM `test_table`', []); + $dbiDummy->addResult('ALTER TABLE `test_table` DROP `col1`, DROP `col2`', []); + // phpcs:enable + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new MoveRepeatingGroup( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + $message = Message::success('Selected repeating group has been moved to the table \'test_table\''); + $this->assertSame(['queryError' => false, 'message' => $message->getDisplay()], $response->getJSONResult()); + } +} From 66970049cce943d4b63d1c03496f405235bd3b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 15:57:05 -0300 Subject: [PATCH 5/9] Rename NormalizationController to Normalization\MainController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- .../MainController.php} | 5 +++-- libraries/routes.php | 3 +-- libraries/services_controllers.php | 5 ++--- .../MainControllerTest.php} | 10 +++++----- 4 files changed, 11 insertions(+), 12 deletions(-) rename libraries/classes/Controllers/{NormalizationController.php => Normalization/MainController.php} (94%) rename test/classes/Controllers/{NormalizationControllerTest.php => Normalization/MainControllerTest.php} (91%) diff --git a/libraries/classes/Controllers/NormalizationController.php b/libraries/classes/Controllers/Normalization/MainController.php similarity index 94% rename from libraries/classes/Controllers/NormalizationController.php rename to libraries/classes/Controllers/Normalization/MainController.php index 9765d7ec8e..e24f61cc27 100644 --- a/libraries/classes/Controllers/NormalizationController.php +++ b/libraries/classes/Controllers/Normalization/MainController.php @@ -2,8 +2,9 @@ declare(strict_types=1); -namespace PhpMyAdmin\Controllers; +namespace PhpMyAdmin\Controllers\Normalization; +use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Normalization; use PhpMyAdmin\ResponseRenderer; @@ -18,7 +19,7 @@ use function min; /** * Normalization process (temporarily specific to 1NF). */ -class NormalizationController extends AbstractController +class MainController extends AbstractController { /** @var Normalization */ private $normalization; diff --git a/libraries/routes.php b/libraries/routes.php index be116075c2..5ee9182923 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -22,7 +22,6 @@ use PhpMyAdmin\Controllers\LintController; use PhpMyAdmin\Controllers\LogoutController; use PhpMyAdmin\Controllers\NavigationController; use PhpMyAdmin\Controllers\Normalization; -use PhpMyAdmin\Controllers\NormalizationController; use PhpMyAdmin\Controllers\PhpInfoController; use PhpMyAdmin\Controllers\Preferences; use PhpMyAdmin\Controllers\RecentTablesListController; @@ -131,7 +130,7 @@ return static function (RouteCollector $routes): void { $routes->addRoute(['GET', 'POST'], '/logout', LogoutController::class); $routes->addRoute(['GET', 'POST'], '/navigation', NavigationController::class); $routes->addGroup('/normalization', static function (RouteCollector $routes): void { - $routes->addRoute(['GET', 'POST'], '', NormalizationController::class); + $routes->addRoute(['GET', 'POST'], '', Normalization\MainController::class); $routes->post('/1nf/step1', Normalization\FirstNormalForm\FirstStepController::class); $routes->post('/1nf/step2', Normalization\FirstNormalForm\SecondStepController::class); $routes->post('/1nf/step3', Normalization\FirstNormalForm\ThirdStepController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index 65e5b48f47..4bd1d28ed3 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -22,7 +22,6 @@ use PhpMyAdmin\Controllers\LintController; use PhpMyAdmin\Controllers\LogoutController; use PhpMyAdmin\Controllers\NavigationController; use PhpMyAdmin\Controllers\Normalization; -use PhpMyAdmin\Controllers\NormalizationController; use PhpMyAdmin\Controllers\PhpInfoController; use PhpMyAdmin\Controllers\Preferences; use PhpMyAdmin\Controllers\RecentTablesListController; @@ -673,8 +672,8 @@ return [ '$normalization' => '@normalization', ], ], - NormalizationController::class => [ - 'class' => NormalizationController::class, + Normalization\MainController::class => [ + 'class' => Normalization\MainController::class, 'arguments' => [ '$response' => '@response', '$template' => '@template', diff --git a/test/classes/Controllers/NormalizationControllerTest.php b/test/classes/Controllers/Normalization/MainControllerTest.php similarity index 91% rename from test/classes/Controllers/NormalizationControllerTest.php rename to test/classes/Controllers/Normalization/MainControllerTest.php index e884b255f7..dd4ad90ad0 100644 --- a/test/classes/Controllers/NormalizationControllerTest.php +++ b/test/classes/Controllers/Normalization/MainControllerTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace PhpMyAdmin\Tests\Controllers; +namespace PhpMyAdmin\Tests\Controllers\Normalization; use PhpMyAdmin\ConfigStorage\Relation; -use PhpMyAdmin\Controllers\NormalizationController; +use PhpMyAdmin\Controllers\Normalization\MainController; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Normalization; @@ -18,9 +18,9 @@ use PhpMyAdmin\Transformations; use function in_array; /** - * @covers \PhpMyAdmin\Controllers\NormalizationController + * @covers \PhpMyAdmin\Controllers\Normalization\MainController */ -class NormalizationControllerTest extends AbstractTestCase +class MainControllerTest extends AbstractTestCase { /** @var DatabaseInterface */ protected $dbi; @@ -53,7 +53,7 @@ class NormalizationControllerTest extends AbstractTestCase $response = new ResponseRenderer(); $template = new Template(); - $controller = new NormalizationController( + $controller = new MainController( $response, $template, new Normalization($dbi, new Relation($dbi), new Transformations(), $template) From c550d41d1ddece5c85dbd00cdc984ed7e953aaef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 16:45:30 -0300 Subject: [PATCH 6/9] Extract normalization's add new primary action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 3 +- .../Normalization/AddNewPrimaryController.php | 41 +++++++++++++++++ .../Normalization/MainController.php | 18 -------- libraries/routes.php | 1 + libraries/services_controllers.php | 8 ++++ .../AddNewPrimaryControllerTest.php | 44 +++++++++++++++++++ 6 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/AddNewPrimaryController.php create mode 100644 test/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index d119cd9d19..ac6c52e373 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -594,13 +594,12 @@ window.AJAX.registerOnload('normalization.js', function () { $('#extra').on('click', '#addNewPrimary', function () { $.post( - 'index.php?route=/normalization', + 'index.php?route=/normalization/add-new-primary', { 'ajax_request': true, 'db': window.CommonParams.get('db'), 'table': window.CommonParams.get('table'), 'server': window.CommonParams.get('server'), - 'addNewPrimary': true }, function (data) { if (data.success === true) { diff --git a/libraries/classes/Controllers/Normalization/AddNewPrimaryController.php b/libraries/classes/Controllers/Normalization/AddNewPrimaryController.php new file mode 100644 index 0000000000..bbbdd9976c --- /dev/null +++ b/libraries/classes/Controllers/Normalization/AddNewPrimaryController.php @@ -0,0 +1,41 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $num_fields = 1; + $columnMeta = [ + 'Field' => $GLOBALS['table'] . '_id', + 'Extra' => 'auto_increment', + ]; + $html = $this->normalization->getHtmlForCreateNewColumn( + $num_fields, + $GLOBALS['db'], + $GLOBALS['table'], + $columnMeta + ); + $html .= Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']); + $this->response->addHTML($html); + } +} diff --git a/libraries/classes/Controllers/Normalization/MainController.php b/libraries/classes/Controllers/Normalization/MainController.php index e24f61cc27..1d36f0cfbb 100644 --- a/libraries/classes/Controllers/Normalization/MainController.php +++ b/libraries/classes/Controllers/Normalization/MainController.php @@ -55,24 +55,6 @@ class MainController extends AbstractController return; } - if (isset($_POST['addNewPrimary'])) { - $num_fields = 1; - $columnMeta = [ - 'Field' => $GLOBALS['table'] . '_id', - 'Extra' => 'auto_increment', - ]; - $html = $this->normalization->getHtmlForCreateNewColumn( - $num_fields, - $GLOBALS['db'], - $GLOBALS['table'], - $columnMeta - ); - $html .= Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']); - echo $html; - - return; - } - if (isset($_POST['findPdl'])) { $html = $this->normalization->findPartialDependencies($GLOBALS['table'], $GLOBALS['db']); echo $html; diff --git a/libraries/routes.php b/libraries/routes.php index 5ee9182923..236204281d 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -141,6 +141,7 @@ return static function (RouteCollector $routes): void { $routes->post('/3nf/create-new-tables', Normalization\ThirdNormalForm\CreateNewTablesController::class); $routes->post('/3nf/new-tables', Normalization\ThirdNormalForm\NewTablesController::class); $routes->post('/3nf/step1', Normalization\ThirdNormalForm\FirstStepController::class); + $routes->post('/add-new-primary', Normalization\AddNewPrimaryController::class); $routes->post('/move-repeating-group', Normalization\MoveRepeatingGroup::class); }); $routes->get('/phpinfo', PhpInfoController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index 4bd1d28ed3..df20d35ff4 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -664,6 +664,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\AddNewPrimaryController::class => [ + 'class' => Normalization\AddNewPrimaryController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], Normalization\MoveRepeatingGroup::class => [ 'class' => Normalization\MoveRepeatingGroup::class, 'arguments' => [ diff --git a/test/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php b/test/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php new file mode 100644 index 0000000000..d55100fc08 --- /dev/null +++ b/test/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php @@ -0,0 +1,44 @@ +createDbiDummy(); + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new AddNewPrimaryController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + $this->assertStringContainsString('getHTMLResult()); + } +} From a37e5681c8b1000661f710346281caf52b53c3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 20:48:48 -0300 Subject: [PATCH 7/9] Extract normalization's get columns action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 3 +- .../Normalization/GetColumnsController.php | 39 +++++++++++++++ .../Normalization/MainController.php | 16 ------- libraries/routes.php | 1 + libraries/services_controllers.php | 8 ++++ .../GetColumnsControllerTest.php | 48 +++++++++++++++++++ 6 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/GetColumnsController.php create mode 100644 test/classes/Controllers/Normalization/GetColumnsControllerTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index ac6c52e373..a64ac02acd 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -18,13 +18,12 @@ var dataParsed = null; function appendHtmlColumnsList () { $.post( - 'index.php?route=/normalization', + 'index.php?route=/normalization/get-columns', { 'ajax_request': true, 'db': window.CommonParams.get('db'), 'table': window.CommonParams.get('table'), 'server': window.CommonParams.get('server'), - 'getColumns': true }, function (data) { if (data.success === true) { diff --git a/libraries/classes/Controllers/Normalization/GetColumnsController.php b/libraries/classes/Controllers/Normalization/GetColumnsController.php new file mode 100644 index 0000000000..f9d6e333b6 --- /dev/null +++ b/libraries/classes/Controllers/Normalization/GetColumnsController.php @@ -0,0 +1,39 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $html = '' + . ''; + //get column whose datatype falls under string category + $html .= $this->normalization->getHtmlForColumnsList( + $GLOBALS['db'], + $GLOBALS['table'], + _pgettext('string types', 'String') + ); + $this->response->addHTML($html); + } +} diff --git a/libraries/classes/Controllers/Normalization/MainController.php b/libraries/classes/Controllers/Normalization/MainController.php index 1d36f0cfbb..cfc59e0759 100644 --- a/libraries/classes/Controllers/Normalization/MainController.php +++ b/libraries/classes/Controllers/Normalization/MainController.php @@ -11,8 +11,6 @@ use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; use PhpMyAdmin\Url; -use function __; -use function _pgettext; use function intval; use function min; @@ -32,20 +30,6 @@ class MainController extends AbstractController public function __invoke(ServerRequest $request): void { - if (isset($_POST['getColumns'])) { - $html = '' - . ''; - //get column whose datatype falls under string category - $html .= $this->normalization->getHtmlForColumnsList( - $GLOBALS['db'], - $GLOBALS['table'], - _pgettext('string types', 'String') - ); - echo $html; - - return; - } - if (isset($_POST['splitColumn'])) { $num_fields = min(4096, intval($_POST['numFields'])); $html = $this->normalization->getHtmlForCreateNewColumn($num_fields, $GLOBALS['db'], $GLOBALS['table']); diff --git a/libraries/routes.php b/libraries/routes.php index 236204281d..e8d730e776 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -142,6 +142,7 @@ return static function (RouteCollector $routes): void { $routes->post('/3nf/new-tables', Normalization\ThirdNormalForm\NewTablesController::class); $routes->post('/3nf/step1', Normalization\ThirdNormalForm\FirstStepController::class); $routes->post('/add-new-primary', Normalization\AddNewPrimaryController::class); + $routes->post('/get-columns', Normalization\GetColumnsController::class); $routes->post('/move-repeating-group', Normalization\MoveRepeatingGroup::class); }); $routes->get('/phpinfo', PhpInfoController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index df20d35ff4..79f6ee8d9f 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -672,6 +672,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\GetColumnsController::class => [ + 'class' => Normalization\GetColumnsController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], Normalization\MoveRepeatingGroup::class => [ 'class' => Normalization\MoveRepeatingGroup::class, 'arguments' => [ diff --git a/test/classes/Controllers/Normalization/GetColumnsControllerTest.php b/test/classes/Controllers/Normalization/GetColumnsControllerTest.php new file mode 100644 index 0000000000..d9bc364973 --- /dev/null +++ b/test/classes/Controllers/Normalization/GetColumnsControllerTest.php @@ -0,0 +1,48 @@ +createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new GetColumnsController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + // phpcs:disable Generic.Files.LineLength.TooLong + $this->assertSame( + '', + $response->getHTMLResult() + ); + // phpcs:enable + } +} From 28d53fe50bb5e87affd14d7b2bb241e9cf23ca66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 21:38:55 -0300 Subject: [PATCH 8/9] Extract normalization's create new column action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 3 +- .../CreateNewColumnController.php | 35 +++++++++++++++ .../Normalization/MainController.php | 13 ------ libraries/routes.php | 1 + libraries/services_controllers.php | 8 ++++ .../CreateNewColumnControllerTest.php | 45 +++++++++++++++++++ 6 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/CreateNewColumnController.php create mode 100644 test/classes/Controllers/Normalization/CreateNewColumnControllerTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index a64ac02acd..fd48a502a1 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -510,13 +510,12 @@ window.AJAX.registerOnload('normalization.js', function () { } var numField = $('#numField').val(); $.post( - 'index.php?route=/normalization', + 'index.php?route=/normalization/create-new-column', { 'ajax_request': true, 'db': window.CommonParams.get('db'), 'table': window.CommonParams.get('table'), 'server': window.CommonParams.get('server'), - 'splitColumn': true, 'numFields': numField }, function (data) { diff --git a/libraries/classes/Controllers/Normalization/CreateNewColumnController.php b/libraries/classes/Controllers/Normalization/CreateNewColumnController.php new file mode 100644 index 0000000000..ae03dc1d00 --- /dev/null +++ b/libraries/classes/Controllers/Normalization/CreateNewColumnController.php @@ -0,0 +1,35 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $num_fields = min(4096, intval($_POST['numFields'])); + $html = $this->normalization->getHtmlForCreateNewColumn($num_fields, $GLOBALS['db'], $GLOBALS['table']); + $html .= Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']); + $this->response->addHTML($html); + } +} diff --git a/libraries/classes/Controllers/Normalization/MainController.php b/libraries/classes/Controllers/Normalization/MainController.php index cfc59e0759..571dde45d2 100644 --- a/libraries/classes/Controllers/Normalization/MainController.php +++ b/libraries/classes/Controllers/Normalization/MainController.php @@ -9,10 +9,6 @@ use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Normalization; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; -use PhpMyAdmin\Url; - -use function intval; -use function min; /** * Normalization process (temporarily specific to 1NF). @@ -30,15 +26,6 @@ class MainController extends AbstractController public function __invoke(ServerRequest $request): void { - if (isset($_POST['splitColumn'])) { - $num_fields = min(4096, intval($_POST['numFields'])); - $html = $this->normalization->getHtmlForCreateNewColumn($num_fields, $GLOBALS['db'], $GLOBALS['table']); - $html .= Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']); - echo $html; - - return; - } - if (isset($_POST['findPdl'])) { $html = $this->normalization->findPartialDependencies($GLOBALS['table'], $GLOBALS['db']); echo $html; diff --git a/libraries/routes.php b/libraries/routes.php index e8d730e776..a71f637d20 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -143,6 +143,7 @@ return static function (RouteCollector $routes): void { $routes->post('/3nf/step1', Normalization\ThirdNormalForm\FirstStepController::class); $routes->post('/add-new-primary', Normalization\AddNewPrimaryController::class); $routes->post('/get-columns', Normalization\GetColumnsController::class); + $routes->post('/create-new-column', Normalization\CreateNewColumnController::class); $routes->post('/move-repeating-group', Normalization\MoveRepeatingGroup::class); }); $routes->get('/phpinfo', PhpInfoController::class); diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index 79f6ee8d9f..ddc00d922f 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -672,6 +672,14 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\CreateNewColumnController::class => [ + 'class' => Normalization\CreateNewColumnController::class, + 'arguments' => [ + '$response' => '@response', + '$template' => '@template', + '$normalization' => '@normalization', + ], + ], Normalization\GetColumnsController::class => [ 'class' => Normalization\GetColumnsController::class, 'arguments' => [ diff --git a/test/classes/Controllers/Normalization/CreateNewColumnControllerTest.php b/test/classes/Controllers/Normalization/CreateNewColumnControllerTest.php new file mode 100644 index 0000000000..2a18d763bb --- /dev/null +++ b/test/classes/Controllers/Normalization/CreateNewColumnControllerTest.php @@ -0,0 +1,45 @@ +createDbiDummy(); + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new CreateNewColumnController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + $this->assertStringContainsString('
getHTMLResult()); + } +} From 8645920f0db6f110ef17b5de45380cb3379be5ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Aug 2022 22:49:07 -0300 Subject: [PATCH 9/9] Extract normalization's partial dependencies action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- js/src/normalization.js | 3 +- .../Normalization/MainController.php | 20 ------- .../PartialDependenciesController.php | 29 ++++++++++ libraries/routes.php | 1 + libraries/services_controllers.php | 8 ++- .../Normalization/MainControllerTest.php | 11 +--- .../PartialDependenciesControllerTest.php | 56 +++++++++++++++++++ 7 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 libraries/classes/Controllers/Normalization/PartialDependenciesController.php create mode 100644 test/classes/Controllers/Normalization/PartialDependenciesControllerTest.php diff --git a/js/src/normalization.js b/js/src/normalization.js index fd48a502a1..3fc98a9f93 100644 --- a/js/src/normalization.js +++ b/js/src/normalization.js @@ -755,13 +755,12 @@ window.AJAX.registerOnload('normalization.js', function () { $('#newCols').insertAfter('#mainContent h4'); $('#newCols').html('
' + window.Messages.strLoading + '
' + window.Messages.strWaitForPd + '
'); $.post( - 'index.php?route=/normalization', + 'index.php?route=/normalization/partial-dependencies', { 'ajax_request': true, 'db': window.CommonParams.get('db'), 'table': window.CommonParams.get('table'), 'server': window.CommonParams.get('server'), - 'findPdl': true }, function (data) { $('#showPossiblePd').html('- ' + window.Messages.strHidePd); $('#showPossiblePd').addClass('hideList'); diff --git a/libraries/classes/Controllers/Normalization/MainController.php b/libraries/classes/Controllers/Normalization/MainController.php index 571dde45d2..91bde1300b 100644 --- a/libraries/classes/Controllers/Normalization/MainController.php +++ b/libraries/classes/Controllers/Normalization/MainController.php @@ -6,35 +6,15 @@ namespace PhpMyAdmin\Controllers\Normalization; use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\Http\ServerRequest; -use PhpMyAdmin\Normalization; -use PhpMyAdmin\ResponseRenderer; -use PhpMyAdmin\Template; /** * Normalization process (temporarily specific to 1NF). */ class MainController extends AbstractController { - /** @var Normalization */ - private $normalization; - - public function __construct(ResponseRenderer $response, Template $template, Normalization $normalization) - { - parent::__construct($response, $template); - $this->normalization = $normalization; - } - public function __invoke(ServerRequest $request): void { - if (isset($_POST['findPdl'])) { - $html = $this->normalization->findPartialDependencies($GLOBALS['table'], $GLOBALS['db']); - echo $html; - - return; - } - $this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']); - $this->render('table/normalization/normalization', [ 'db' => $GLOBALS['db'], 'table' => $GLOBALS['table'], diff --git a/libraries/classes/Controllers/Normalization/PartialDependenciesController.php b/libraries/classes/Controllers/Normalization/PartialDependenciesController.php new file mode 100644 index 0000000000..a847ef6832 --- /dev/null +++ b/libraries/classes/Controllers/Normalization/PartialDependenciesController.php @@ -0,0 +1,29 @@ +normalization = $normalization; + } + + public function __invoke(ServerRequest $request): void + { + $html = $this->normalization->findPartialDependencies($GLOBALS['table'], $GLOBALS['db']); + $this->response->addHTML($html); + } +} diff --git a/libraries/routes.php b/libraries/routes.php index a71f637d20..44f1062780 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -145,6 +145,7 @@ return static function (RouteCollector $routes): void { $routes->post('/get-columns', Normalization\GetColumnsController::class); $routes->post('/create-new-column', Normalization\CreateNewColumnController::class); $routes->post('/move-repeating-group', Normalization\MoveRepeatingGroup::class); + $routes->post('/partial-dependencies', Normalization\PartialDependenciesController::class); }); $routes->get('/phpinfo', PhpInfoController::class); $routes->addGroup('/preferences', static function (RouteCollector $routes): void { diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index ddc00d922f..02725969a4 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -688,6 +688,10 @@ return [ '$normalization' => '@normalization', ], ], + Normalization\MainController::class => [ + 'class' => Normalization\MainController::class, + 'arguments' => ['$response' => '@response', '$template' => '@template'], + ], Normalization\MoveRepeatingGroup::class => [ 'class' => Normalization\MoveRepeatingGroup::class, 'arguments' => [ @@ -696,8 +700,8 @@ return [ '$normalization' => '@normalization', ], ], - Normalization\MainController::class => [ - 'class' => Normalization\MainController::class, + Normalization\PartialDependenciesController::class => [ + 'class' => Normalization\PartialDependenciesController::class, 'arguments' => [ '$response' => '@response', '$template' => '@template', diff --git a/test/classes/Controllers/Normalization/MainControllerTest.php b/test/classes/Controllers/Normalization/MainControllerTest.php index dd4ad90ad0..803967e84c 100644 --- a/test/classes/Controllers/Normalization/MainControllerTest.php +++ b/test/classes/Controllers/Normalization/MainControllerTest.php @@ -4,16 +4,13 @@ declare(strict_types=1); namespace PhpMyAdmin\Tests\Controllers\Normalization; -use PhpMyAdmin\ConfigStorage\Relation; use PhpMyAdmin\Controllers\Normalization\MainController; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Http\ServerRequest; -use PhpMyAdmin\Normalization; use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\DbiDummy; use PhpMyAdmin\Tests\Stubs\ResponseRenderer; -use PhpMyAdmin\Transformations; use function in_array; @@ -49,15 +46,9 @@ class MainControllerTest extends AbstractTestCase { $GLOBALS['db'] = 'test_db'; $GLOBALS['table'] = 'test_table'; - $dbi = $this->createDatabaseInterface(); $response = new ResponseRenderer(); - $template = new Template(); - $controller = new MainController( - $response, - $template, - new Normalization($dbi, new Relation($dbi), new Transformations(), $template) - ); + $controller = new MainController($response, new Template()); $controller($this->createStub(ServerRequest::class)); $files = $response->getHeader()->getScripts()->getFiles(); diff --git a/test/classes/Controllers/Normalization/PartialDependenciesControllerTest.php b/test/classes/Controllers/Normalization/PartialDependenciesControllerTest.php new file mode 100644 index 0000000000..2d01962001 --- /dev/null +++ b/test/classes/Controllers/Normalization/PartialDependenciesControllerTest.php @@ -0,0 +1,56 @@ +createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('SELECT COUNT(*) FROM (SELECT * FROM `test_table` LIMIT 500) as dt;', [['0']], ['dt']); + $dbiDummy->addResult( + 'SELECT COUNT(DISTINCT `id`) as \'`id`_cnt\', COUNT(DISTINCT `name`) as \'`name`_cnt\', COUNT(DISTINCT `datetimefield`) as \'`datetimefield`_cnt\' FROM (SELECT * FROM `test_table` LIMIT 500) as dt;', + [], + ['`id`_cnt', '`name`_cnt', '`datetimefield`_cnt', '`datetimefield`_cnt', 'dt'] + ); + // phpcs:enable + + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $template = new Template(); + + $controller = new PartialDependenciesController( + $response, + $template, + new Normalization($dbi, new Relation($dbi), new Transformations(), $template) + ); + $controller($this->createStub(ServerRequest::class)); + + // phpcs:disable Generic.Files.LineLength.TooLong + $this->assertSame( + 'This list is based on a subset of the table\'s data and is not necessarily accurate.

No partial dependencies found!

', + $response->getHTMLResult() + ); + // phpcs:enable + } +}