Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract normalization 1NF step 1 action
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
- Loading branch information
1 parent
9c9ae50
commit 398bdae
Showing
8 changed files
with
147 additions
and
22 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
libraries/classes/Controllers/Normalization/FirstNormalForm/FirstStepController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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; | ||
|
||
use function in_array; | ||
|
||
final class FirstStepController 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 | ||
{ | ||
$this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']); | ||
|
||
$normalForm = '1nf'; | ||
if (isset($_POST['normalizeTo']) && in_array($_POST['normalizeTo'], ['1nf', '2nf', '3nf'])) { | ||
$normalForm = $_POST['normalizeTo']; | ||
} | ||
|
||
$html = $this->normalization->getHtmlFor1NFStep1($GLOBALS['db'], $GLOBALS['table'], $normalForm); | ||
$this->response->addHTML($html); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
test/classes/Controllers/Normalization/FirstNormalForm/FirstStepControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Tests\Controllers\Normalization\FirstNormalForm; | ||
|
||
use PhpMyAdmin\ConfigStorage\Relation; | ||
use PhpMyAdmin\Controllers\Normalization\FirstNormalForm\FirstStepController; | ||
use PhpMyAdmin\Http\ServerRequest; | ||
use PhpMyAdmin\Normalization; | ||
use PhpMyAdmin\Template; | ||
use PhpMyAdmin\Tests\AbstractTestCase; | ||
use PhpMyAdmin\Tests\Stubs\ResponseRenderer; | ||
use PhpMyAdmin\Transformations; | ||
|
||
use function in_array; | ||
|
||
/** | ||
* @covers \PhpMyAdmin\Controllers\Normalization\FirstNormalForm\FirstStepController | ||
*/ | ||
class FirstStepControllerTest extends AbstractTestCase | ||
{ | ||
/** | ||
* @psalm-param '1nf'|'2nf'|'3nf' $expectedNormalizeTo | ||
* | ||
* @dataProvider providerForTestDefault | ||
*/ | ||
public function testDefault(?string $normalizeTo, string $expectedNormalizeTo): void | ||
{ | ||
$GLOBALS['db'] = 'test_db'; | ||
$GLOBALS['table'] = 'test_table'; | ||
$_POST['normalizeTo'] = $normalizeTo; | ||
|
||
$dbiDummy = $this->createDbiDummy(); | ||
$dbiDummy->addSelectDb('test_db'); | ||
|
||
$dbi = $this->createDatabaseInterface($dbiDummy); | ||
$GLOBALS['dbi'] = $dbi; | ||
$response = new ResponseRenderer(); | ||
$template = new Template(); | ||
|
||
$controller = new FirstStepController( | ||
$response, | ||
$template, | ||
new Normalization($dbi, new Relation($dbi), new Transformations(), $template) | ||
); | ||
$controller($this->createStub(ServerRequest::class)); | ||
|
||
$files = $response->getHeader()->getScripts()->getFiles(); | ||
$this->assertTrue( | ||
in_array(['name' => 'normalization.js', 'fire' => 1], $files, true), | ||
'normalization.js script was not included in the response.' | ||
); | ||
$this->assertTrue( | ||
in_array(['name' => 'vendor/jquery/jquery.uitablefilter.js', 'fire' => 0], $files, true), | ||
'vendor/jquery/jquery.uitablefilter.js script was not included in the response.' | ||
); | ||
|
||
$output = $response->getHTMLResult(); | ||
$this->assertStringContainsString('First step of normalization (1NF)', $output); | ||
$this->assertStringContainsString( | ||
'<div id=\'mainContent\' data-normalizeto=\'' . $expectedNormalizeTo . '\'>', | ||
$output | ||
); | ||
$this->assertStringContainsString('<option value=\'no_such_col\'>No such column</option>', $output); | ||
} | ||
|
||
/** | ||
* @return array<int, array{string|null, '1nf'|'2nf'|'3nf'}> | ||
*/ | ||
public function providerForTestDefault(): iterable | ||
{ | ||
return [ | ||
[null, '1nf'], | ||
['', '1nf'], | ||
['invalid', '1nf'], | ||
['1nf', '1nf'], | ||
['2nf', '2nf'], | ||
['3nf', '3nf'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters