diff options
-rw-r--r-- | js/src/normalization.js | 2 | ||||
-rw-r--r-- | libraries/classes/Controllers/Normalization/MoveRepeatingGroup.php | 40 | ||||
-rw-r--r-- | libraries/classes/Controllers/NormalizationController.php | 18 | ||||
-rw-r--r-- | libraries/routes.php | 1 | ||||
-rw-r--r-- | libraries/services_controllers.php | 8 | ||||
-rw-r--r-- | psalm-baseline.xml | 28 | ||||
-rw-r--r-- | test/classes/Controllers/Normalization/MoveRepeatingGroupTest.php | 53 |
7 files changed, 117 insertions, 33 deletions
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 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Controllers\Normalization; + +use PhpMyAdmin\Controllers\AbstractController; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Normalization; +use PhpMyAdmin\ResponseRenderer; +use PhpMyAdmin\Template; + +final class MoveRepeatingGroup 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 + { + $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 @@ <code>$normalForm</code> </MixedAssignment> </file> + <file src="libraries/classes/Controllers/Normalization/MoveRepeatingGroup.php"> + <MixedArgument occurrences="4"> + <code>$newColumn</code> + <code>$newTable</code> + <code>$primary_columns</code> + <code>$repeatingColumns</code> + </MixedArgument> + <MixedAssignment occurrences="4"> + <code>$newColumn</code> + <code>$newTable</code> + <code>$primary_columns</code> + <code>$repeatingColumns</code> + </MixedAssignment> + </file> <file src="libraries/classes/Controllers/Normalization/SecondNormalForm/CreateNewTablesController.php"> <MixedArgument occurrences="4"> <code>$_POST['newTablesName']</code> @@ -2457,20 +2471,6 @@ <code>$tables</code> </MixedAssignment> </file> - <file src="libraries/classes/Controllers/NormalizationController.php"> - <MixedArgument occurrences="4"> - <code>$newColumn</code> - <code>$newTable</code> - <code>$primary_columns</code> - <code>$repeatingColumns</code> - </MixedArgument> - <MixedAssignment occurrences="4"> - <code>$newColumn</code> - <code>$newTable</code> - <code>$primary_columns</code> - <code>$repeatingColumns</code> - </MixedAssignment> - </file> <file src="libraries/classes/Controllers/PhpInfoController.php"> <InvalidArrayOffset occurrences="1"> <code>$GLOBALS['cfg']['ShowPhpInfo']</code> 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 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Tests\Controllers\Normalization; + +use PhpMyAdmin\ConfigStorage\Relation; +use PhpMyAdmin\Controllers\Normalization\MoveRepeatingGroup; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Message; +use PhpMyAdmin\Normalization; +use PhpMyAdmin\Template; +use PhpMyAdmin\Tests\AbstractTestCase; +use PhpMyAdmin\Tests\Stubs\ResponseRenderer; +use PhpMyAdmin\Transformations; + +/** + * @covers \PhpMyAdmin\Controllers\Normalization\MoveRepeatingGroup + */ +class MoveRepeatingGroupTest extends AbstractTestCase +{ + public function testDefault(): void + { + $GLOBALS['db'] = 'test_db'; + $GLOBALS['table'] = 'test_table'; + $_POST['repeatingColumns'] = 'col1, col2'; + $_POST['newTable'] = 'new_table'; + $_POST['newColumn'] = 'new_column'; + $_POST['primary_columns'] = 'id,col1'; + + // phpcs:disable Generic.Files.LineLength.TooLong + $dbiDummy = $this->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()); + } +} |