3 namespace Drupal\simpletest;
5 use Drupal\Core\DrupalKernel;
6 use Drupal\Core\Language\Language;
7 use Drupal\Core\Session\UserSession;
8 use Drupal\Core\Site\Settings;
9 use Symfony\Component\DependencyInjection\ContainerBuilder;
10 use Symfony\Component\DependencyInjection\Reference;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpFoundation\RequestStack;
15 * Base class for testing the interactive installer.
17 abstract class InstallerTestBase extends WebTestBase {
20 * Custom settings.php values to write for a test run.
23 * An array of settings to write out, in the format expected by
24 * drupal_rewrite_settings().
26 protected $settings = [];
29 * The language code in which to install Drupal.
33 protected $langcode = 'en';
36 * The installation profile to install.
40 protected $profile = 'testing';
43 * Additional parameters to use for installer screens.
45 * @see WebTestBase::installParameters()
49 protected $parameters = [];
52 * A string translation map used for translated installer screens.
54 * Keys are English strings, values are translated strings.
58 protected $translations = [
59 'Save and continue' => 'Save and continue',
63 * Whether the installer has completed.
67 protected $isInstalled = FALSE;
72 protected function setUp() {
73 $this->isInstalled = FALSE;
75 // Define information about the user 1 account.
76 $this->rootUser = new UserSession([
79 'mail' => 'admin@example.com',
80 'pass_raw' => $this->randomMachineName(),
83 // If any $settings are defined for this test, copy and prepare an actual
84 // settings.php, so as to resemble a regular installation.
85 if (!empty($this->settings)) {
86 // Not using File API; a potential error must trigger a PHP warning.
87 copy(DRUPAL_ROOT . '/sites/default/default.settings.php', DRUPAL_ROOT . '/' . $this->siteDirectory . '/settings.php');
88 $this->writeSettings($this->settings);
91 // Note that WebTestBase::installParameters() returns form input values
92 // suitable for a programmed \Drupal::formBuilder()->submitForm().
93 // @see WebTestBase::translatePostValues()
94 $this->parameters = $this->installParameters();
96 // Set up a minimal container (required by WebTestBase). Set cookie and
97 // server information so that XDebug works.
98 // @see install_begin_request()
99 $request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER);
100 $this->container = new ContainerBuilder();
101 $request_stack = new RequestStack();
102 $request_stack->push($request);
104 ->set('request_stack', $request_stack);
106 ->setParameter('language.default_values', Language::$defaultValues);
108 ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
109 ->addArgument('%language.default_values%');
111 ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
112 ->addArgument(new Reference('language.default'));
114 ->set('app.root', DRUPAL_ROOT);
115 \Drupal::setContainer($this->container);
117 $this->visitInstaller();
120 $this->setUpLanguage();
123 $this->setUpProfile();
125 // Configure settings.
126 $this->setUpSettings();
128 // @todo Allow test classes based on this class to act on further installer
134 if ($this->isInstalled) {
135 // Import new settings.php written by the installer.
136 $request = Request::createFromGlobals();
137 $class_loader = require $this->container->get('app.root') . '/autoload.php';
138 Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader);
139 foreach ($GLOBALS['config_directories'] as $type => $path) {
140 $this->configDirectories[$type] = $path;
143 // After writing settings.php, the installer removes write permissions
144 // from the site directory. To allow drupal_generate_test_ua() to write
145 // a file containing the private key for drupal_valid_test_ua(), the site
146 // directory has to be writable.
147 // WebTestBase::tearDown() will delete the entire test site directory.
148 // Not using File API; a potential error must trigger a PHP warning.
149 chmod($this->container->get('app.root') . '/' . $this->siteDirectory, 0777);
150 $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE);
151 $this->kernel->prepareLegacyRequest($request);
152 $this->container = $this->kernel->getContainer();
154 // Manually configure the test mail collector implementation to prevent
155 // tests from sending out emails and collect them in state instead.
156 $this->container->get('config.factory')
157 ->getEditable('system.mail')
158 ->set('interface.default', 'test_mail_collector')
164 * Visits the interactive installer.
166 protected function visitInstaller() {
167 $this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
171 * Installer step: Select language.
173 protected function setUpLanguage() {
175 'langcode' => $this->langcode,
177 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
181 * Installer step: Select installation profile.
183 protected function setUpProfile() {
185 'profile' => $this->profile,
187 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
191 * Installer step: Configure settings.
193 protected function setUpSettings() {
194 $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']);
195 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
199 * Final installer step: Configure site.
201 protected function setUpSite() {
202 $edit = $this->translatePostValues($this->parameters['forms']['install_configure_form']);
203 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
204 // If we've got to this point the site is installed using the regular
205 // installation workflow.
206 $this->isInstalled = TRUE;
212 * WebTestBase::refreshVariables() tries to operate on persistent storage,
213 * which is only available after the installer completed.
215 protected function refreshVariables() {
216 if ($this->isInstalled) {
217 parent::refreshVariables();