Chris@4: setName('install') Chris@4: ->setDescription('Creates a test Drupal site') Chris@4: ->setHelp('The details to connect to the test site created will be displayed upon success. It will contain the database prefix and the user agent.') Chris@4: ->addOption('setup-file', NULL, InputOption::VALUE_OPTIONAL, 'The path to a PHP file containing a class to setup configuration used by the test, for example, core/tests/Drupal/TestSite/TestSiteInstallTestScript.php.') Chris@4: ->addOption('db-url', NULL, InputOption::VALUE_OPTIONAL, 'URL for database. Defaults to the environment variable SIMPLETEST_DB.', getenv('SIMPLETEST_DB')) Chris@4: ->addOption('base-url', NULL, InputOption::VALUE_OPTIONAL, 'Base URL for site under test. Defaults to the environment variable SIMPLETEST_BASE_URL.', getenv('SIMPLETEST_BASE_URL')) Chris@4: ->addOption('install-profile', NULL, InputOption::VALUE_OPTIONAL, 'Install profile to install the site in. Defaults to testing.', 'testing') Chris@4: ->addOption('langcode', NULL, InputOption::VALUE_OPTIONAL, 'The language to install the site in. Defaults to en.', 'en') Chris@4: ->addOption('json', NULL, InputOption::VALUE_NONE, 'Output test site connection details in JSON.') Chris@4: ->addUsage('--setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --json') Chris@4: ->addUsage('--install-profile demo_umami --langcode fr') Chris@4: ->addUsage('--base-url "http://example.com" --db-url "mysql://username:password@localhost/databasename#table_prefix"'); Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: protected function execute(InputInterface $input, OutputInterface $output) { Chris@4: // Determines and validates the setup class prior to installing a database Chris@4: // to avoid creating unnecessary sites. Chris@4: $root = dirname(dirname(dirname(dirname(dirname(__DIR__))))); Chris@4: chdir($root); Chris@4: $class_name = $this->getSetupClass($input->getOption('setup-file')); Chris@4: // Ensure we can install a site in the sites/simpletest directory. Chris@4: $this->ensureDirectory($root); Chris@4: Chris@4: $db_url = $input->getOption('db-url'); Chris@4: $base_url = $input->getOption('base-url'); Chris@4: putenv("SIMPLETEST_DB=$db_url"); Chris@4: putenv("SIMPLETEST_BASE_URL=$base_url"); Chris@4: Chris@4: // Manage site fixture. Chris@4: $this->setup($input->getOption('install-profile'), $class_name, $input->getOption('langcode')); Chris@4: Chris@4: $user_agent = drupal_generate_test_ua($this->databasePrefix); Chris@4: if ($input->getOption('json')) { Chris@4: $output->writeln(json_encode([ Chris@4: 'db_prefix' => $this->databasePrefix, Chris@4: 'user_agent' => $user_agent, Chris@4: 'site_path' => $this->siteDirectory, Chris@4: ])); Chris@4: } Chris@4: else { Chris@4: $output->writeln('Successfully installed a test site'); Chris@4: $io = new SymfonyStyle($input, $output); Chris@4: $io->table([], [ Chris@4: ['Database prefix', $this->databasePrefix], Chris@4: ['User agent', $user_agent], Chris@4: ['Site path', $this->siteDirectory], Chris@4: ]); Chris@4: } Chris@4: } Chris@4: Chris@4: /** Chris@4: * Gets the setup class. Chris@4: * Chris@4: * @param string|null $file Chris@4: * The file to get the setup class from. Chris@4: * Chris@4: * @return string|null Chris@4: * The setup class contained in the provided $file. Chris@4: * Chris@4: * @throws \InvalidArgumentException Chris@4: * Thrown if the file does not exist, does not contain a class or the class Chris@4: * does not implement \Drupal\TestSite\TestSetupInterface. Chris@4: */ Chris@4: protected function getSetupClass($file) { Chris@4: if ($file === NULL) { Chris@4: return; Chris@4: } Chris@4: if (!file_exists($file)) { Chris@4: throw new \InvalidArgumentException("The file $file does not exist."); Chris@4: } Chris@4: Chris@4: $classes = get_declared_classes(); Chris@4: include_once $file; Chris@4: $new_classes = array_values(array_diff(get_declared_classes(), $classes)); Chris@4: if (empty($new_classes)) { Chris@4: throw new \InvalidArgumentException("The file $file does not contain a class."); Chris@4: } Chris@4: $class = array_pop($new_classes); Chris@4: Chris@4: if (!is_subclass_of($class, TestSetupInterface::class)) { Chris@4: throw new \InvalidArgumentException("The class $class contained in $file needs to implement \Drupal\TestSite\TestSetupInterface"); Chris@4: } Chris@4: return $class; Chris@4: } Chris@4: Chris@4: /** Chris@4: * Ensures that the sites/simpletest directory exists and is writable. Chris@4: * Chris@4: * @param string $root Chris@4: * The Drupal root. Chris@4: */ Chris@4: protected function ensureDirectory($root) { Chris@4: if (!is_writable($root . '/sites/simpletest')) { Chris@4: if (!@mkdir($root . '/sites/simpletest')) { Chris@4: throw new \RuntimeException($root . '/sites/simpletest must exist and be writable to install a test site'); Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: /** Chris@4: * Creates a test drupal installation. Chris@4: * Chris@4: * @param string $profile Chris@4: * (optional) The installation profile to use. Chris@4: * @param string $setup_class Chris@4: * (optional) Setup class. A PHP class to setup configuration used by the Chris@4: * test. Chris@4: * @param string $langcode Chris@4: * (optional) The language to install the site in. Chris@4: */ Chris@4: public function setup($profile = 'testing', $setup_class = NULL, $langcode = 'en') { Chris@4: $this->profile = $profile; Chris@4: $this->langcode = $langcode; Chris@4: $this->setupBaseUrl(); Chris@4: $this->prepareEnvironment(); Chris@4: $this->installDrupal(); Chris@4: Chris@4: if ($setup_class) { Chris@4: $this->executeSetupClass($setup_class); Chris@4: } Chris@4: } Chris@4: Chris@4: /** Chris@4: * Installs Drupal into the test site. Chris@4: */ Chris@4: protected function installDrupal() { Chris@4: $this->initUserSession(); Chris@4: $this->prepareSettings(); Chris@4: $this->doInstall(); Chris@4: $this->initSettings(); Chris@4: $container = $this->initKernel(\Drupal::request()); Chris@4: $this->initConfig($container); Chris@4: $this->installModulesFromClassProperty($container); Chris@4: $this->rebuildAll(); Chris@4: } Chris@4: Chris@4: /** Chris@4: * Uses the setup file to configure Drupal. Chris@4: * Chris@4: * @param string $class Chris@4: * The fully qualified class name, which should set up Drupal for tests. For Chris@4: * example this class could create content types and fields or install Chris@4: * modules. The class needs to implement TestSetupInterface. Chris@4: * Chris@4: * @see \Drupal\TestSite\TestSetupInterface Chris@4: */ Chris@4: protected function executeSetupClass($class) { Chris@4: /** @var \Drupal\TestSite\TestSetupInterface $instance */ Chris@4: $instance = new $class(); Chris@4: $instance->setup(); Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: protected function installParameters() { Chris@4: $parameters = $this->installParametersTrait(); Chris@4: $parameters['parameters']['langcode'] = $this->langcode; Chris@4: return $parameters; Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: protected function changeDatabasePrefix() { Chris@4: // Ensure that we use the database from SIMPLETEST_DB environment variable. Chris@4: Database::removeConnection('default'); Chris@4: $this->changeDatabasePrefixTrait(); Chris@4: } Chris@4: Chris@4: /** Chris@4: * {@inheritdoc} Chris@4: */ Chris@4: protected function prepareDatabasePrefix() { Chris@4: // Override this method so that we can force a lock to be created. Chris@4: $test_db = new TestDatabase(NULL, TRUE); Chris@4: $this->siteDirectory = $test_db->getTestSitePath(); Chris@4: $this->databasePrefix = $test_db->getDatabasePrefix(); Chris@4: } Chris@4: Chris@4: }