Skip to content

Commit

Permalink
Extract normalization 1NF step 3 action
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jul 29, 2022
1 parent 6e8600f commit 08297d9
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
3 changes: 1 addition & 2 deletions js/src/normalization.js
Expand Up @@ -157,13 +157,12 @@ window.goToStep4 = goToStep4;

function goToStep3 () {
$.post(
'index.php?route=/normalization',
'index.php?route=/normalization/1nf/step3',
{
'ajax_request': true,
'db': window.CommonParams.get('db'),
'table': window.CommonParams.get('table'),
'server': window.CommonParams.get('server'),
'step3': true
}, function (data) {
$('#mainContent legend').html(data.legendText);
$('#mainContent h4').html(data.headText);
Expand Down
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers\Normalization\FirstNormalForm;

use PhpMyAdmin\Controllers\AbstractController;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Normalization;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;

final class ThirdStepController 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
{
$res = $this->normalization->getHtmlContentsFor1NFStep3($GLOBALS['db'], $GLOBALS['table']);
$this->response->addJSON($res);
}
}
7 changes: 0 additions & 7 deletions libraries/classes/Controllers/NormalizationController.php
Expand Up @@ -143,13 +143,6 @@ public function __invoke(ServerRequest $request): void
return;
}

if (isset($_POST['step3'])) {
$res = $this->normalization->getHtmlContentsFor1NFStep3($GLOBALS['db'], $GLOBALS['table']);
$this->response->addJSON($res);

return;
}

if (isset($_POST['step4'])) {
$res = $this->normalization->getHtmlContentsFor1NFStep4($GLOBALS['db'], $GLOBALS['table']);
$this->response->addJSON($res);
Expand Down
1 change: 1 addition & 0 deletions libraries/routes.php
Expand Up @@ -134,6 +134,7 @@
$routes->addRoute(['GET', 'POST'], '', NormalizationController::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);
});
$routes->get('/phpinfo', PhpInfoController::class);
$routes->addGroup('/preferences', static function (RouteCollector $routes): void {
Expand Down
8 changes: 8 additions & 0 deletions libraries/services_controllers.php
Expand Up @@ -601,6 +601,14 @@
'$normalization' => '@normalization',
],
],
Normalization\FirstNormalForm\ThirdStepController::class => [
'class' => Normalization\FirstNormalForm\ThirdStepController::class,
'arguments' => [
'$response' => '@response',
'$template' => '@template',
'$normalization' => '@normalization',
],
],
NormalizationController::class => [
'class' => NormalizationController::class,
'arguments' => [
Expand Down
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Controllers\Normalization\FirstNormalForm;

use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Controllers\Normalization\FirstNormalForm\ThirdStepController;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Normalization;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
use PhpMyAdmin\Transformations;

/**
* @covers \PhpMyAdmin\Controllers\Normalization\FirstNormalForm\ThirdStepController
*/
class ThirdStepControllerTest extends AbstractTestCase
{
public function testDefault(): void
{
$GLOBALS['db'] = 'test_db';
$GLOBALS['table'] = 'test_table';

$dbiDummy = $this->createDbiDummy();
$dbiDummy->addSelectDb('test_db');

$dbi = $this->createDatabaseInterface($dbiDummy);
$GLOBALS['dbi'] = $dbi;
$response = new ResponseRenderer();
$template = new Template();

$controller = new ThirdStepController(
$response,
$template,
new Normalization($dbi, new Relation($dbi), new Transformations(), $template)
);
$controller($this->createStub(ServerRequest::class));

// phpcs:disable Generic.Files.LineLength.TooLong
$this->assertSame([
'legendText' => 'Step 1.3 Move repeating groups',
'headText' => 'Do you have a group of two or more columns that are closely related and are all repeating the same attribute? For example, a table that holds data on books might have columns such as book_id, author1, author2, author3 and so on which form a repeating group. In this case a new table (book_id, author) should be created.',
'subText' => 'Check the columns which form a repeating group. If no such group, click on \'No repeating group\'',
'extra' => '<input type="checkbox" value="id">id [ int(11) ]<br><input type="checkbox" value="name">name [ varchar(20) ]<br><input type="checkbox" value="datetimefield">datetimefield [ datetime ]<br><br><input class="btn btn-secondary" type="submit" id="moveRepeatingGroup" value="Done"><input class="btn btn-secondary" type="submit" value="No repeating group" onclick="goToStep4();">',
'primary_key' => '["id"]',
], $response->getJSONResult());
// phpcs:enable
}
}

0 comments on commit 08297d9

Please sign in to comment.