Skip to content

Commit c436ce7

Browse files
author
Ivan Gavryshko
committed
MAGETWO-31904: There is no validation for 'Table prefix' on Step 2: Add a Database during instalation
- Fixed.
1 parent 49a494c commit c436ce7

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

dev/tests/unit/testsuite/Magento/Setup/Controller/DatabaseCheckTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ public function testIndexActionWithError()
5353
$this->assertArrayHasKey('error', $variables);
5454
$this->assertFalse($variables['success']);
5555
}
56+
57+
public function testIndexActionCheckPrefix()
58+
{
59+
$this->installer->expects($this->once())->method('checkDatabaseTablePrefix');
60+
$this->controller->indexAction();
61+
}
5662
}

setup/src/Magento/Setup/Controller/DatabaseCheck.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,13 @@ public function __construct(InstallerFactory $installerFactory, WebLogger $webLo
4747
public function indexAction()
4848
{
4949
$params = Json::decode($this->getRequest()->getContent(), Json::TYPE_ARRAY);
50-
var_dump($params);
50+
5151
try {
52-
if (isset($params['tablePrefix'])
53-
&& $params['tablePrefix'] !==''
54-
&& !preg_match('/^([[:alnum:]]+)([[:alnum:]_]+)$/', $params['tablePrefix'])
55-
) {
56-
die("dfdfd");
57-
throw new \InvalidArgumentException('Table prefix is in wrong format.');
58-
}
5952
$installer = $this->installerFactory->create($this->webLogger);
6053
$password = isset($params['password']) ? $params['password'] : '';
6154
$installer->checkDatabaseConnection($params['name'], $params['host'], $params['user'], $password);
55+
$tablePrefix = isset($params['tablePrefix']) ? $params['tablePrefix'] : '';
56+
$installer->checkDatabaseTablePrefix($tablePrefix);
6257
return new JsonModel(['success' => true]);
6358
} catch (\Exception $e) {
6459
return new JsonModel(['success' => false, 'error' => $e->getMessage()]);

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,23 @@ public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass = '')
757757
return true;
758758
}
759759

760+
/**
761+
* Check if database table is valid
762+
*
763+
* @param string $prefix
764+
* @return boolean
765+
* @throws \InvalidArgumentException
766+
*/
767+
public function checkDatabaseTablePrefix($prefix)
768+
{
769+
if ($prefix !=='' && !preg_match('/^([[:alnum:]]+)([[:alnum:]_]+)$/', $prefix)
770+
) {
771+
throw new \InvalidArgumentException('Table prefix is in wrong format.');
772+
}
773+
774+
return true;
775+
}
776+
760777
/**
761778
* Return messages
762779
*
@@ -876,6 +893,9 @@ private function assertDbAccessible()
876893
$config[DbConfig::KEY_USER],
877894
$config[DbConfig::KEY_PASS]
878895
);
896+
if (isset($config[DbConfig::KEY_PREFIX])) {
897+
$this->checkDatabaseTablePrefix($config[DbConfig::KEY_PREFIX]);
898+
}
879899
}
880900

881901
/**

0 commit comments

Comments
 (0)