diff --git a/core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php b/core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php index 3d236f4869..869688b6fb 100644 --- a/core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php +++ b/core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php @@ -51,7 +51,6 @@ class TestSiteInstallCommand extends Command { */ protected $databasePrefix; - /** * The language to install the site in. * @@ -71,7 +70,10 @@ protected function configure() { ->addOption('base_url', NULL, InputOption::VALUE_OPTIONAL, 'Base URL for site under test or SIMPLETEST_BASE_URL', getenv('SIMPLETEST_BASE_URL')) ->addOption('install_profile', NULL, InputOption::VALUE_OPTIONAL, 'Install profile to install the site in. Defaults to testing', 'testing') ->addOption('langcode', NULL, InputOption::VALUE_OPTIONAL, 'The language to install the site in. Defaults to en', 'en') - ->addOption('json', NULL, InputOption::VALUE_NONE, 'Output test site connection details in JSON'); + ->addOption('json', NULL, InputOption::VALUE_NONE, 'Output test site connection details in JSON') + ->addUsage('--setup_class "\Drupal\TestSite\TestSiteInstallTestScript" --json') + ->addUsage('--install_profile demo_umami --langcode fr') + ->addUsage('--base_url "http://example.com" --db_url "mysql://username:password@localhost/databasename#table_prefix"'); } /** diff --git a/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php b/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php index 4a6191f7d6..5b8fbba094 100644 --- a/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php +++ b/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php @@ -11,6 +11,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\HttpFoundation\Request; /** @@ -51,31 +52,42 @@ protected function configure() { ->setHelp('All the database tables and files will be removed.') ->addArgument('db_prefix', InputArgument::REQUIRED, 'The database prefix for the test site') ->addOption('db_url', NULL, InputOption::VALUE_OPTIONAL, 'URL for database or SIMPLETEST_DB', getenv('SIMPLETEST_DB')) - ->addOption('base_url', NULL, InputOption::VALUE_OPTIONAL, 'Base URL for site under test or SIMPLETEST_BASE_URL', getenv('SIMPLETEST_BASE_URL')); + ->addUsage('test12345678') + ->addUsage('test12345678 --db_url "mysql://username:password@localhost/databasename#table_prefix"'); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { - $db_url = $input->getOption('db_url'); $db_prefix = $input->getArgument('db_prefix'); - $base_url = $input->getOption('base_url'); + // Validate the db_prefix argument. + try { + $test_database = new TestDatabase($db_prefix); + } + catch (\InvalidArgumentException $e) { + $io = new SymfonyStyle($input, $output); + $io->getErrorStyle()->error("Invalid database prefix: $db_prefix\n\nValid database prefixes match the regular expression '/test(\d+)$/'. For example, 'test12345678'."); + // Display the synopsis of the command like Composer does. + $output->writeln(sprintf('%s', sprintf($this->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET); + return 1; + } + + $db_url = $input->getOption('db_url'); putenv("SIMPLETEST_DB=$db_url"); - putenv("SIMPLETEST_BASE_URL=$base_url"); $kernel = $this->bootstrapDrupal(); // Handle the cleanup of the test site. - $this->tearDown($db_prefix, $db_url, $kernel->getAppRoot()); + $this->tearDown($test_database, $db_url, $kernel->getAppRoot()); $output->writeln("Successfully uninstalled $db_prefix test site"); } /** * Removes a given instance by deleting all the database tables and files. * - * @param string $db_prefix - * The used database prefix. + * @param \Drupal\Core\Test\TestDatabase $test_database + * The test database object. * @param string $db_url * The database URL. * @param string $app_root @@ -83,10 +95,10 @@ protected function execute(InputInterface $input, OutputInterface $output) { * * @see \Drupal\Tests\BrowserTestBase::cleanupEnvironment() */ - protected function tearDown($db_prefix, $db_url, $app_root) { + protected function tearDown(TestDatabase $test_database, $db_url, $app_root) { // Connect to the test database. $database = Database::convertDbUrlToConnectionInfo($db_url, $app_root); - $database['prefix'] = ['default' => $db_prefix]; + $database['prefix'] = ['default' => $test_database->getDatabasePrefix()]; Database::addConnectionInfo(__CLASS__, 'default', $database); // Remove all the tables. @@ -94,7 +106,6 @@ protected function tearDown($db_prefix, $db_url, $app_root) { array_walk($schema->findTables('%'), [$schema, 'dropTable']); // Delete test site directory. - $test_database = new TestDatabase($db_prefix); file_unmanaged_delete_recursive($test_database->getTestSitePath(), [BrowserTestBase::class, 'filePreDeleteCallback']); } diff --git a/core/tests/Drupal/Tests/Scripts/TestSiteApplicationTest.php b/core/tests/Drupal/Tests/Scripts/TestSiteApplicationTest.php index b93e9d5f32..4f764df60b 100644 --- a/core/tests/Drupal/Tests/Scripts/TestSiteApplicationTest.php +++ b/core/tests/Drupal/Tests/Scripts/TestSiteApplicationTest.php @@ -195,6 +195,21 @@ public function testInstallInDifferentLanguage() { $this->assertCount(0, Database::getConnection('default', $this->addTestDatabase($db_prefix))->schema()->findTables('%')); } + /** + * @coversNothing + */ + public function testTearDownDbPrefixValidation() { + $php_binary_finder = new PhpExecutableFinder(); + $php_binary_path = $php_binary_finder->find(); + + $command_line = $php_binary_path . ' core/scripts/test-site.php tear-down not-a-valid-prefix'; + $process = new Process($command_line, $this->root); + $process->setTimeout(500); + $process->run(); + $this->assertSame(1, $process->getExitCode()); + $this->assertContains('Invalid database prefix: not-a-valid-prefix', $process->getErrorOutput()); + } + /** * Adds the installed test site to the database connection info. *