3 namespace Drupal\FunctionalTests\Installer;
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 Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware;
10 use Drupal\Tests\BrowserTestBase;
11 use GuzzleHttp\HandlerStack;
12 use Symfony\Component\DependencyInjection\ContainerBuilder;
13 use Symfony\Component\DependencyInjection\Reference;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\RequestStack;
18 * Base class for testing the interactive installer.
20 abstract class InstallerTestBase extends BrowserTestBase {
23 * Custom settings.php values to write for a test run.
26 * An array of settings to write out, in the format expected by
27 * drupal_rewrite_settings().
29 protected $settings = [];
32 * The language code in which to install Drupal.
36 protected $langcode = 'en';
39 * The installation profile to install.
43 protected $profile = 'testing';
46 * Additional parameters to use for installer screens.
48 * @see FunctionalTestSetupTrait::installParameters()
52 protected $parameters = [];
55 * A string translation map used for translated installer screens.
57 * Keys are English strings, values are translated strings.
61 protected $translations = [
62 'Save and continue' => 'Save and continue',
66 * Whether the installer has completed.
70 protected $isInstalled = FALSE;
75 protected function setUp() {
76 $this->isInstalled = FALSE;
78 $this->setupBaseUrl();
80 $this->prepareDatabasePrefix();
82 // Install Drupal test site.
83 $this->prepareEnvironment();
85 // Define information about the user 1 account.
86 $this->rootUser = new UserSession([
89 'mail' => 'admin@example.com',
90 'pass_raw' => $this->randomMachineName(),
93 // If any $settings are defined for this test, copy and prepare an actual
94 // settings.php, so as to resemble a regular installation.
95 if (!empty($this->settings)) {
96 // Not using File API; a potential error must trigger a PHP warning.
97 copy(DRUPAL_ROOT . '/sites/default/default.settings.php', DRUPAL_ROOT . '/' . $this->siteDirectory . '/settings.php');
98 $this->writeSettings($this->settings);
101 // Note that FunctionalTestSetupTrait::installParameters() returns form
102 // input values suitable for a programmed
103 // \Drupal::formBuilder()->submitForm().
104 // @see InstallerTestBase::translatePostValues()
105 $this->parameters = $this->installParameters();
107 // Set up a minimal container (required by BrowserTestBase). Set cookie and
108 // server information so that XDebug works.
109 // @see install_begin_request()
110 $request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER);
111 $this->container = new ContainerBuilder();
112 $request_stack = new RequestStack();
113 $request_stack->push($request);
115 ->set('request_stack', $request_stack);
117 ->setParameter('language.default_values', Language::$defaultValues);
119 ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
120 ->addArgument('%language.default_values%');
122 ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
123 ->addArgument(new Reference('language.default'));
125 ->register('http_client', 'GuzzleHttp\Client')
126 ->setFactory('http_client_factory:fromOptions');
128 ->register('http_client_factory', 'Drupal\Core\Http\ClientFactory')
129 ->setArguments([new Reference('http_handler_stack')]);
130 $handler_stack = HandlerStack::create();
131 $test_http_client_middleware = new TestHttpClientMiddleware();
132 $handler_stack->push($test_http_client_middleware(), 'test.http_client.middleware');
134 ->set('http_handler_stack', $handler_stack);
137 ->set('app.root', DRUPAL_ROOT);
138 \Drupal::setContainer($this->container);
143 // Set up the browser test output file.
144 $this->initBrowserOutputFile();
146 $this->visitInstaller();
149 $this->setUpLanguage();
152 $this->setUpProfile();
154 // Address the requirements problem screen, if any.
155 $this->setUpRequirementsProblem();
157 // Configure settings.
158 $this->setUpSettings();
160 // @todo Allow test classes based on this class to act on further installer
166 if ($this->isInstalled) {
167 // Import new settings.php written by the installer.
168 $request = Request::createFromGlobals();
169 $class_loader = require $this->container->get('app.root') . '/autoload.php';
170 Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader);
171 foreach ($GLOBALS['config_directories'] as $type => $path) {
172 $this->configDirectories[$type] = $path;
175 // After writing settings.php, the installer removes write permissions
176 // from the site directory. To allow drupal_generate_test_ua() to write
177 // a file containing the private key for drupal_valid_test_ua(), the site
178 // directory has to be writable.
179 // BrowserTestBase::tearDown() will delete the entire test site directory.
180 // Not using File API; a potential error must trigger a PHP warning.
181 chmod($this->container->get('app.root') . '/' . $this->siteDirectory, 0777);
182 $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE);
183 $this->kernel->prepareLegacyRequest($request);
184 $this->container = $this->kernel->getContainer();
186 // Manually configure the test mail collector implementation to prevent
187 // tests from sending out emails and collect them in state instead.
188 $this->container->get('config.factory')
189 ->getEditable('system.mail')
190 ->set('interface.default', 'test_mail_collector')
198 protected function initFrontPage() {
199 // We don't want to visit the front page with the installer when
200 // initializing Mink, so we do nothing here.
204 * Visits the interactive installer.
206 protected function visitInstaller() {
207 $this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
211 * Installer step: Select language.
213 protected function setUpLanguage() {
215 'langcode' => $this->langcode,
217 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
221 * Installer step: Select installation profile.
223 protected function setUpProfile() {
225 'profile' => $this->profile,
227 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
231 * Installer step: Configure settings.
233 protected function setUpSettings() {
234 $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']);
235 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
239 * Installer step: Requirements problem.
241 * Override this method to test specific requirements warnings or errors
242 * during the installer.
244 * @see system_requirements()
246 protected function setUpRequirementsProblem() {
247 // By default, skip the "recommended PHP version" warning on older test
248 // environments. This allows the installer to be tested consistently on
249 // both recommended PHP versions and older (but still supported) versions.
250 if (version_compare(phpversion(), '7.0') < 0) {
251 $this->continueOnExpectedWarnings(['PHP']);
256 * Final installer step: Configure site.
258 protected function setUpSite() {
259 $edit = $this->translatePostValues($this->parameters['forms']['install_configure_form']);
260 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
261 // If we've got to this point the site is installed using the regular
262 // installation workflow.
263 $this->isInstalled = TRUE;
269 * FunctionalTestSetupTrait::refreshVariables() tries to operate on persistent
270 * storage, which is only available after the installer completed.
272 protected function refreshVariables() {
273 if ($this->isInstalled) {
274 parent::refreshVariables();
279 * Continues installation when an expected warning is found.
281 * @param string[] $expected_warnings
282 * A list of warning summaries to expect on the requirements screen (e.g.
283 * 'PHP', 'PHP OPcode caching', etc.). If only the expected warnings
284 * are found, the test will click the "continue anyway" link to go to the
285 * next screen of the installer. If an expected warning is not found, or if
286 * a warning not in the list is present, a fail is raised.
288 protected function continueOnExpectedWarnings($expected_warnings = []) {
289 // Don't try to continue if there are errors.
290 if (strpos($this->getTextContent(), 'Errors found') !== FALSE) {
293 // Allow only details elements that are directly after the warning header
294 // or each other. There is no guaranteed wrapper we can rely on across
295 // distributions. When there are multiple warnings, the selectors will be:
296 // - h3#warning+details summary
297 // - h3#warning+details+details summary
299 // We add one more selector than expected warnings to confirm that there
300 // isn't any other warning before clicking the link.
301 // @todo Make this more reliable in
302 // https://www.drupal.org/project/drupal/issues/2927345.
304 for ($i = 0; $i <= count($expected_warnings); $i++) {
305 $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary';
307 $warning_elements = $this->cssSelect(implode(', ', $selectors));
309 // Confirm that there are only the expected warnings.
311 foreach ($warning_elements as $warning) {
312 $warnings[] = trim($warning->getText());
314 $this->assertEquals($expected_warnings, $warnings);
315 $this->clickLink('continue anyway');
316 $this->checkForMetaRefresh();