diff --git a/core/tests/Drupal/Setup/Commands/TestInstallationSetupApplication.php b/core/tests/Drupal/Setup/Commands/TestInstallationSetupApplication.php index 1ec945958c..868933f6e6 100644 --- a/core/tests/Drupal/Setup/Commands/TestInstallationSetupApplication.php +++ b/core/tests/Drupal/Setup/Commands/TestInstallationSetupApplication.php @@ -7,6 +7,10 @@ /** * Application wrapper for TestInstallationSetupCommand. * + * In order to see what commands are available and how to use them run + * "php core/scripts/setup-drupal-test.php" from command line and use the help + * system. + * * @internal */ class TestInstallationSetupApplication extends Application { @@ -36,7 +40,7 @@ protected function getDefaultCommands() { // Even though this is a single command, keep the HelpCommand (--help). $default_commands = parent::getDefaultCommands(); $default_commands[] = new TestInstallationSetupCommand(); - $default_commands[] = new TestTeardownCommand($this->autoloader); + $default_commands[] = new TestTearDownCommand($this->autoloader); return $default_commands; } diff --git a/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php b/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php index 933c88ef3a..47bc6ce465 100644 --- a/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php +++ b/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php @@ -14,7 +14,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * Symfony console command to setup Drupal. + * Command to create a test Drupal site. * * @internal */ @@ -63,7 +63,9 @@ class TestInstallationSetupCommand extends Command { */ protected function configure() { $this->setName('setup-drupal-test') - ->addOption('setup_class', NULL, InputOption::VALUE_OPTIONAL) + ->setDescription('Creates a test Drupal site') + ->setHelp('The details to connect to the test site created will be returned in JSON. It will contain the database prefix and the user agent.') + ->addOption('setup_class', NULL, InputOption::VALUE_OPTIONAL, 'A PHP class to setup configuration used by the test, for example, \Drupal\Setup\SetupDrupalTestScript') ->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')) ->addOption('install_profile', NULL, InputOption::VALUE_OPTIONAL, 'Install profile to install the site in. Defaults to testing', 'testing') @@ -106,11 +108,11 @@ protected function validateSetupClass($class) { return; } if (!class_exists($class)) { - throw new \InvalidArgumentException("There was a problem loading {$class}"); + throw new \InvalidArgumentException("There was a problem loading $class"); } if (!is_subclass_of($class, TestSetupInterface::class)) { - throw new \InvalidArgumentException(sprintf('You need to define a class implementing \Drupal\Setup\TestSetupInterface')); + throw new \InvalidArgumentException('You need to define a class implementing \Drupal\Setup\TestSetupInterface'); } } diff --git a/core/tests/Drupal/Setup/Commands/TestTeardownCommand.php b/core/tests/Drupal/Setup/Commands/TestTearDownCommand.php similarity index 75% rename from core/tests/Drupal/Setup/Commands/TestTeardownCommand.php rename to core/tests/Drupal/Setup/Commands/TestTearDownCommand.php index 102feaca3b..d6ff68b410 100644 --- a/core/tests/Drupal/Setup/Commands/TestTeardownCommand.php +++ b/core/tests/Drupal/Setup/Commands/TestTearDownCommand.php @@ -5,6 +5,7 @@ use Drupal\Core\Database\Database; use Drupal\Core\DrupalKernel; use Drupal\Core\Test\TestDatabase; +use Drupal\Tests\BrowserTestBase; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -13,11 +14,11 @@ use Symfony\Component\HttpFoundation\Request; /** - * Symfony console command to tear down Drupal. + * Command to tear down a test Drupal site. * * @internal */ -class TestTeardownCommand extends Command { +class TestTearDownCommand extends Command { /** * The used PHP autoloader. @@ -27,7 +28,7 @@ class TestTeardownCommand extends Command { protected $autoloader; /** - * Constructs a new TestTeardownCommand. + * Constructs a new TestTearDownCommand. * * @param string $autoloader * The used PHP autoloader. @@ -45,7 +46,9 @@ public function __construct($autoloader, $name = NULL) { * {@inheritdoc} */ protected function configure() { - $this->setName('teardown-drupal-test') + $this->setName('tear-down-drupal-test') + ->setDescription('Removes a test site added by the setup-drupal-test command') + ->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')); @@ -64,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $kernel = $this->bootstrapDrupal(); // Handle the cleanup of the test site. - $this->teardown($db_prefix, $db_url, $kernel->getAppRoot()); + $this->tearDown($db_prefix, $db_url, $kernel->getAppRoot()); } /** @@ -79,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { * * @see \Drupal\Tests\BrowserTestBase::cleanupEnvironment() */ - public function teardown($db_prefix, $db_url, $app_root) { + public function tearDown($db_prefix, $db_url, $app_root) { // Connect to the test database. $database = Database::convertDbUrlToConnectionInfo($db_url, $app_root); $database['prefix'] = ['default' => $db_prefix]; @@ -96,25 +99,7 @@ public function teardown($db_prefix, $db_url, $app_root) { // Delete test site directory. $test_database = new TestDatabase($db_prefix); - file_unmanaged_delete_recursive($test_database->getTestSitePath(), [$this, 'filePreDeleteCallback']); - } - - /** - * Ensures test files are deletable within file_unmanaged_delete_recursive(). - * - * Some tests chmod generated files to be read only. During - * BrowserTestBase::cleanupEnvironment() and other cleanup operations, - * these files need to get deleted too. - * - * @param string $path - * The file path. - */ - public static function filePreDeleteCallback($path) { - // When the webserver runs with the same system user as phpunit, we can - // make read-only files writable again. If not, chmod will fail while the - // file deletion still works if file permissions have been configured - // correctly. Thus, we ignore any problems while running chmod. - @chmod($path, 0700); + file_unmanaged_delete_recursive($test_database->getTestSitePath(), [BrowserTestBase::class, 'filePreDeleteCallback']); } /** diff --git a/core/tests/Drupal/Setup/SetupDrupalTestScript.php b/core/tests/Drupal/Setup/SetupDrupalTestScript.php index d8f7e1fbc9..47dd6fb9ed 100644 --- a/core/tests/Drupal/Setup/SetupDrupalTestScript.php +++ b/core/tests/Drupal/Setup/SetupDrupalTestScript.php @@ -3,7 +3,9 @@ namespace Drupal\Setup; /** - * Setup file used by \Drupal\KernelTests\Setup\Commands\SetupDrupalTestScriptTest + * Setup file used by SetupDrupalTestScriptTest. + * + * @see \Drupal\Tests\Setup\Commands\SetupDrupalTestScriptTest */ class SetupDrupalTestScript implements TestSetupInterface { diff --git a/core/tests/Drupal/Setup/TestSetupInterface.php b/core/tests/Drupal/Setup/TestSetupInterface.php index 5cd60c0df2..3d092839ca 100644 --- a/core/tests/Drupal/Setup/TestSetupInterface.php +++ b/core/tests/Drupal/Setup/TestSetupInterface.php @@ -10,14 +10,15 @@ /** * Run the code to setup the test environment. * - * You have access to any API provided by any installed module. To install - * modules use + * You have access to any API provided by any installed module. For example, + * to install modules use: * @code * \Drupal::service('module_installer')->install(['my_module']) * @endcode * - * Check out 'core/tests/Drupal/Setup/SetupDrupalTestScript.php' for an - * example. + * Check out SetupDrupalTestScript for an example. + * + * @see \Drupal\Setup\SetupDrupalTestScript */ public function setup(); diff --git a/core/tests/Drupal/Tests/Setup/Commands/SetupDrupalTestScriptTest.php b/core/tests/Drupal/Tests/Setup/Commands/SetupDrupalTestScriptTest.php index 40fd16da64..39ed32aef4 100644 --- a/core/tests/Drupal/Tests/Setup/Commands/SetupDrupalTestScriptTest.php +++ b/core/tests/Drupal/Tests/Setup/Commands/SetupDrupalTestScriptTest.php @@ -24,7 +24,7 @@ * * @see \Drupal\Setup\Commands\TestInstallationSetupApplication * @see \Drupal\Setup\Commands\TestInstallationSetupCommand - * @see \Drupal\Setup\Commands\TestTeardownCommand + * @see \Drupal\Setup\Commands\TestTearDownCommand */ class SetupDrupalTestScriptTest extends UnitTestCase { @@ -101,7 +101,8 @@ public function testInstallScript() { // Ensure the test_page_test module got installed. $this->assertContains('Test page | Drupal', (string) $response->getBody()); - // Ensure that there are files and database tables for teardown to clean up. + // Ensure that there are files and database tables for tear down command to + // clean up. $key = $this->addTestDatabase($db_prefix); $this->assertGreaterThan(0, count(Database::getConnection('default', $key)->schema()->findTables('%'))); $test_database = new TestDatabase($db_prefix); @@ -117,7 +118,7 @@ public function testInstallScript() { $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%'))); // Now test the tear down process as well. - $command_line = $phpBinaryPath . ' core/scripts/setup-drupal-test.php teardown-drupal-test ' . $db_prefix . ' --db_url "' . getenv('SIMPLETEST_DB') . '"'; + $command_line = $phpBinaryPath . ' core/scripts/setup-drupal-test.php tear-down-drupal-test ' . $db_prefix . ' --db_url "' . getenv('SIMPLETEST_DB') . '"'; $process = new Process($command_line, $this->root); $process->setTimeout(500); $process->run(); @@ -134,7 +135,7 @@ public function testInstallScript() { $this->assertFileExists($test_file); // Now test the tear down process as well. - $command_line = $phpBinaryPath . ' core/scripts/setup-drupal-test.php teardown-drupal-test ' . $other_db_prefix . ' --db_url "' . getenv('SIMPLETEST_DB') . '"'; + $command_line = $phpBinaryPath . ' core/scripts/setup-drupal-test.php tear-down-drupal-test ' . $other_db_prefix . ' --db_url "' . getenv('SIMPLETEST_DB') . '"'; $process = new Process($command_line, $this->root); $process->setTimeout(500); $process->run(); @@ -169,7 +170,7 @@ public function testInstallInDifferentLanguage() { $this->assertContains('lang="fr"', (string) $response->getBody()); // Now test the tear down process as well. - $command_line = $phpBinaryPath . ' core/scripts/setup-drupal-test.php teardown-drupal-test ' . $db_prefix . ' --db_url "' . getenv('SIMPLETEST_DB') . '"'; + $command_line = $phpBinaryPath . ' core/scripts/setup-drupal-test.php tear-down-drupal-test ' . $db_prefix . ' --db_url "' . getenv('SIMPLETEST_DB') . '"'; $process = new Process($command_line, $this->root); $process->setTimeout(500); $process->run();