Skip to content

Commit 4f17c22

Browse files
committed
Merge remote-tracking branch 'ogresce/MAGETWO-31904-table-prefix-validation' into develop
Conflicts: setup/view/magento/setup/add-database.phtml
2 parents ca389b0 + c1b30b7 commit 4f17c22

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +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+
5051
try {
5152
$installer = $this->installerFactory->create($this->webLogger);
5253
$password = isset($params['password']) ? $params['password'] : '';
5354
$installer->checkDatabaseConnection($params['name'], $params['host'], $params['user'], $password);
55+
$tablePrefix = isset($params['tablePrefix']) ? $params['tablePrefix'] : '';
56+
$installer->checkDatabaseTablePrefix($tablePrefix);
5457
return new JsonModel(['success' => true]);
5558
} catch (\Exception $e) {
5659
return new JsonModel(['success' => false, 'error' => $e->getMessage()]);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,24 @@ public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass = '')
875875
return true;
876876
}
877877

878+
/**
879+
* Check if database table prefix is valid
880+
*
881+
* @param string $prefix
882+
* @return boolean
883+
* @throws \InvalidArgumentException
884+
*/
885+
public function checkDatabaseTablePrefix($prefix)
886+
{
887+
//The table prefix should contain only letters (a-z), numbers (0-9) or underscores (_);
888+
// the first character should be a letter.
889+
if ($prefix !== '' && !preg_match('/^([a-zA-Z])([[:alnum:]_]+)$/', $prefix)) {
890+
throw new \InvalidArgumentException('Please correct the table prefix format.');
891+
}
892+
893+
return true;
894+
}
895+
878896
/**
879897
* Return messages
880898
*
@@ -994,6 +1012,9 @@ private function assertDbAccessible()
9941012
$config[DbConfig::KEY_USER],
9951013
$config[DbConfig::KEY_PASS]
9961014
);
1015+
if (isset($config[DbConfig::KEY_PREFIX])) {
1016+
$this->checkDatabaseTablePrefix($config[DbConfig::KEY_PREFIX]);
1017+
}
9971018
}
9981019

9991020
/**

setup/view/magento/setup/add-database.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@
337337
id="dbTablePrefix"
338338
class="form-el-input"
339339
tooltip-placement="right"
340-
tooltip="Enter a tracking prefix to be used for database tables created for this Magento installation. (ex: 'mg1_' )."
340+
tooltip="Enter a prefix for database tables created in this installation. Use letters, numbers or underscores, and begin with a letter (Ex: ‘mg1_’)"
341341
tooltip-trigger="focus"
342342
tooltip-append-to-body="true"
343343
type="text"

0 commit comments

Comments
 (0)