From 02fe7f42c82aa31c16e958ca4a65d1786ea65690 Mon Sep 17 00:00:00 2001 From: irice Date: Mon, 27 Dec 2021 22:26:28 +0800 Subject: [PATCH] Fix #18660: Check name if backslash appears --- framework/CHANGELOG.md | 1 + .../console/controllers/MigrateController.php | 2 +- .../controllers/MigrateControllerTest.php | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index a0fbafc4df..722a7e90e2 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.44 under development ------------------------ +- Bug #18660: Check name if backslash appears (iridance) - Enh #13105: Add yiiActiveForm validate_only property for skipping form auto-submission (ptolomaues) - Enh #18967: Use proper attribute names for tabular data in `yii\widgets\ActiveField::addAriaAttributes()` (AnkIF) - Bug #18798: Fix `StringHelper::dirname()` when passing string with a trailing slash (perlexed) diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 8bf011fe42..9b82cc800c 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -406,7 +406,7 @@ class MigrateController extends BaseMigrateController $name = $params['name']; if ($params['namespace']) { - $name = substr($name, strrpos($name, '\\') + 1); + $name = substr($name, (strrpos($name, '\\') ?: -1) + 1); } $templateFile = $this->templateFile; diff --git a/tests/framework/console/controllers/MigrateControllerTest.php b/tests/framework/console/controllers/MigrateControllerTest.php index 31e235adae..1a406b40b6 100644 --- a/tests/framework/console/controllers/MigrateControllerTest.php +++ b/tests/framework/console/controllers/MigrateControllerTest.php @@ -78,6 +78,22 @@ class MigrateControllerTest extends TestCase $this->assertFileContent($expectedFile, $class, $table, $namespace); } + /** + * Check config namespace but without input namespace + * @param mixed $expectedFile + * @param mixed $migrationName + * @param mixed $table + * @param array $params + */ + protected function assertCommandCreatedFileWithoutNamespaceInput($expectedFile, $migrationName, $table, $params = []) + { + $params[0] = $migrationName; + list($config, $namespace, $class) = $this->prepareMigrationNameData($this->migrationNamespace . '\\' . $migrationName); + + $this->runMigrateControllerAction('create', $params, $config); + $this->assertFileContent($expectedFile, $class, $table, $namespace); + } + public function assertFileContentJunction($expectedFile, $class, $junctionTable, $firstTable, $secondTable, $namespace = null) { if ($namespace) { @@ -101,6 +117,23 @@ class MigrateControllerTest extends TestCase $this->assertFileContentJunction($expectedFile, $class, $junctionTable, $firstTable, $secondTable, $namespace); } + /** + * Check config namespace but without input namespace + * @param mixed $expectedFile + * @param mixed $migrationName + * @param mixed $junctionTable + * @param mixed $firstTable + * @param mixed $secondTable + */ + protected function assertCommandCreatedJunctionFileWithoutNamespaceInput($expectedFile, $migrationName, $junctionTable, $firstTable, $secondTable) + { + list($config, $namespace, $class) = $this->prepareMigrationNameData($this->migrationNamespace . '\\' . $migrationName); + + $this->runMigrateControllerAction('create', [$migrationName], $config); + $this->assertSame(ExitCode::OK, $this->getExitCode()); + $this->assertFileContentJunction($expectedFile, $class, $junctionTable, $firstTable, $secondTable, $namespace); + } + protected function prepareMigrationNameData($migrationName) { $config = []; @@ -318,6 +351,7 @@ class MigrateControllerTest extends TestCase $table, $params ); + $this->assertCommandCreatedFileWithoutNamespaceInput($expectedFile, $migrationName, $table, $params); } /** @@ -369,6 +403,13 @@ class MigrateControllerTest extends TestCase $firstTable, $secondTable ); + $this->assertCommandCreatedJunctionFileWithoutNamespaceInput( + 'junction_test', + $migrationName, + $junctionTable, + $firstTable, + $secondTable + ); } public function testUpdatingLongNamedMigration()