3 namespace Drupal\TestSite\Commands;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Test\FunctionalTestSetupTrait;
7 use Drupal\Core\Test\TestDatabase;
8 use Drupal\Core\Test\TestSetupTrait;
9 use Drupal\TestSite\TestSetupInterface;
10 use Drupal\Tests\RandomGeneratorTrait;
11 use Symfony\Component\Console\Command\Command;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Style\SymfonyStyle;
18 * Command to create a test Drupal site.
22 class TestSiteInstallCommand extends Command {
24 use FunctionalTestSetupTrait {
25 installParameters as protected installParametersTrait;
27 use RandomGeneratorTrait;
29 changeDatabasePrefix as protected changeDatabasePrefixTrait;
33 * The install profile to use.
37 protected $profile = 'testing';
40 * Time limit in seconds for the test.
42 * Used by \Drupal\Core\Test\FunctionalTestSetupTrait::prepareEnvironment().
46 protected $timeLimit = 500;
49 * The database prefix of this test run.
53 protected $databasePrefix;
56 * The language to install the site in.
60 protected $langcode = 'en';
65 protected function configure() {
66 $this->setName('install')
67 ->setDescription('Creates a test Drupal site')
68 ->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.')
69 ->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.')
70 ->addOption('db-url', NULL, InputOption::VALUE_OPTIONAL, 'URL for database. Defaults to the environment variable SIMPLETEST_DB.', getenv('SIMPLETEST_DB'))
71 ->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'))
72 ->addOption('install-profile', NULL, InputOption::VALUE_OPTIONAL, 'Install profile to install the site in. Defaults to testing.', 'testing')
73 ->addOption('langcode', NULL, InputOption::VALUE_OPTIONAL, 'The language to install the site in. Defaults to en.', 'en')
74 ->addOption('json', NULL, InputOption::VALUE_NONE, 'Output test site connection details in JSON.')
75 ->addUsage('--setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --json')
76 ->addUsage('--install-profile demo_umami --langcode fr')
77 ->addUsage('--base-url "http://example.com" --db-url "mysql://username:password@localhost/databasename#table_prefix"');
83 protected function execute(InputInterface $input, OutputInterface $output) {
84 // Determines and validates the setup class prior to installing a database
85 // to avoid creating unnecessary sites.
86 $root = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
88 $class_name = $this->getSetupClass($input->getOption('setup-file'));
89 // Ensure we can install a site in the sites/simpletest directory.
90 $this->ensureDirectory($root);
92 $db_url = $input->getOption('db-url');
93 $base_url = $input->getOption('base-url');
94 putenv("SIMPLETEST_DB=$db_url");
95 putenv("SIMPLETEST_BASE_URL=$base_url");
97 // Manage site fixture.
98 $this->setup($input->getOption('install-profile'), $class_name, $input->getOption('langcode'));
100 $user_agent = drupal_generate_test_ua($this->databasePrefix);
101 if ($input->getOption('json')) {
102 $output->writeln(json_encode([
103 'db_prefix' => $this->databasePrefix,
104 'user_agent' => $user_agent,
105 'site_path' => $this->siteDirectory,
109 $output->writeln('<info>Successfully installed a test site</info>');
110 $io = new SymfonyStyle($input, $output);
112 ['Database prefix', $this->databasePrefix],
113 ['User agent', $user_agent],
114 ['Site path', $this->siteDirectory],
120 * Gets the setup class.
122 * @param string|null $file
123 * The file to get the setup class from.
125 * @return string|null
126 * The setup class contained in the provided $file.
128 * @throws \InvalidArgumentException
129 * Thrown if the file does not exist, does not contain a class or the class
130 * does not implement \Drupal\TestSite\TestSetupInterface.
132 protected function getSetupClass($file) {
133 if ($file === NULL) {
136 if (!file_exists($file)) {
137 throw new \InvalidArgumentException("The file $file does not exist.");
140 $classes = get_declared_classes();
142 $new_classes = array_values(array_diff(get_declared_classes(), $classes));
143 if (empty($new_classes)) {
144 throw new \InvalidArgumentException("The file $file does not contain a class.");
146 $class = array_pop($new_classes);
148 if (!is_subclass_of($class, TestSetupInterface::class)) {
149 throw new \InvalidArgumentException("The class $class contained in $file needs to implement \Drupal\TestSite\TestSetupInterface");
155 * Ensures that the sites/simpletest directory exists and is writable.
157 * @param string $root
160 protected function ensureDirectory($root) {
161 if (!is_writable($root . '/sites/simpletest')) {
162 if (!@mkdir($root . '/sites/simpletest')) {
163 throw new \RuntimeException($root . '/sites/simpletest must exist and be writable to install a test site');
169 * Creates a test drupal installation.
171 * @param string $profile
172 * (optional) The installation profile to use.
173 * @param string $setup_class
174 * (optional) Setup class. A PHP class to setup configuration used by the
176 * @param string $langcode
177 * (optional) The language to install the site in.
179 public function setup($profile = 'testing', $setup_class = NULL, $langcode = 'en') {
180 $this->profile = $profile;
181 $this->langcode = $langcode;
182 $this->setupBaseUrl();
183 $this->prepareEnvironment();
184 $this->installDrupal();
187 $this->executeSetupClass($setup_class);
192 * Installs Drupal into the test site.
194 protected function installDrupal() {
195 $this->initUserSession();
196 $this->prepareSettings();
198 $this->initSettings();
199 $container = $this->initKernel(\Drupal::request());
200 $this->initConfig($container);
201 $this->installModulesFromClassProperty($container);
206 * Uses the setup file to configure Drupal.
208 * @param string $class
209 * The fully qualified class name, which should set up Drupal for tests. For
210 * example this class could create content types and fields or install
211 * modules. The class needs to implement TestSetupInterface.
213 * @see \Drupal\TestSite\TestSetupInterface
215 protected function executeSetupClass($class) {
216 /** @var \Drupal\TestSite\TestSetupInterface $instance */
217 $instance = new $class();
224 protected function installParameters() {
225 $parameters = $this->installParametersTrait();
226 $parameters['parameters']['langcode'] = $this->langcode;
233 protected function changeDatabasePrefix() {
234 // Ensure that we use the database from SIMPLETEST_DB environment variable.
235 Database::removeConnection('default');
236 $this->changeDatabasePrefixTrait();
242 protected function prepareDatabasePrefix() {
243 // Override this method so that we can force a lock to be created.
244 $test_db = new TestDatabase(NULL, TRUE);
245 $this->siteDirectory = $test_db->getTestSitePath();
246 $this->databasePrefix = $test_db->getDatabasePrefix();