Pull merge.
[yaffs-website] / web / core / tests / Drupal / FunctionalTests / Installer / InstallerTestBase.php
1 <?php
2
3 namespace Drupal\FunctionalTests\Installer;
4
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;
16
17 /**
18  * Base class for testing the interactive installer.
19  */
20 abstract class InstallerTestBase extends BrowserTestBase {
21
22   /**
23    * Custom settings.php values to write for a test run.
24    *
25    * @var array
26    *   An array of settings to write out, in the format expected by
27    *   drupal_rewrite_settings().
28    */
29   protected $settings = [];
30
31   /**
32    * The language code in which to install Drupal.
33    *
34    * @var string
35    */
36   protected $langcode = 'en';
37
38   /**
39    * The installation profile to install.
40    *
41    * @var string
42    */
43   protected $profile = 'testing';
44
45   /**
46    * Additional parameters to use for installer screens.
47    *
48    * @see FunctionalTestSetupTrait::installParameters()
49    *
50    * @var array
51    */
52   protected $parameters = [];
53
54   /**
55    * A string translation map used for translated installer screens.
56    *
57    * Keys are English strings, values are translated strings.
58    *
59    * @var array
60    */
61   protected $translations = [
62     'Save and continue' => 'Save and continue',
63   ];
64
65   /**
66    * Whether the installer has completed.
67    *
68    * @var bool
69    */
70   protected $isInstalled = FALSE;
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function setUp() {
76     $this->isInstalled = FALSE;
77
78     $this->setupBaseUrl();
79
80     $this->prepareDatabasePrefix();
81
82     // Install Drupal test site.
83     $this->prepareEnvironment();
84
85     // Define information about the user 1 account.
86     $this->rootUser = new UserSession([
87       'uid' => 1,
88       'name' => 'admin',
89       'mail' => 'admin@example.com',
90       'pass_raw' => $this->randomMachineName(),
91     ]);
92
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);
99     }
100
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();
106
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);
114     $this->container
115       ->set('request_stack', $request_stack);
116     $this->container
117       ->setParameter('language.default_values', Language::$defaultValues);
118     $this->container
119       ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
120       ->addArgument('%language.default_values%');
121     $this->container
122       ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
123       ->addArgument(new Reference('language.default'));
124     $this->container
125       ->register('http_client', 'GuzzleHttp\Client')
126       ->setFactory('http_client_factory:fromOptions');
127     $this->container
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');
133     $this->container
134       ->set('http_handler_stack', $handler_stack);
135
136     $this->container
137       ->set('app.root', DRUPAL_ROOT);
138     \Drupal::setContainer($this->container);
139
140     // Setup Mink.
141     $this->initMink();
142
143     // Set up the browser test output file.
144     $this->initBrowserOutputFile();
145
146     $this->visitInstaller();
147
148     // Select language.
149     $this->setUpLanguage();
150
151     // Select profile.
152     $this->setUpProfile();
153
154     // Address the requirements problem screen, if any.
155     $this->setUpRequirementsProblem();
156
157     // Configure settings.
158     $this->setUpSettings();
159
160     // @todo Allow test classes based on this class to act on further installer
161     //   screens.
162
163     // Configure site.
164     $this->setUpSite();
165
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;
173       }
174
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();
185
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')
191         ->save();
192     }
193   }
194
195   /**
196    * {@inheritdoc}
197    */
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.
201   }
202
203   /**
204    * Visits the interactive installer.
205    */
206   protected function visitInstaller() {
207     $this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
208   }
209
210   /**
211    * Installer step: Select language.
212    */
213   protected function setUpLanguage() {
214     $edit = [
215       'langcode' => $this->langcode,
216     ];
217     $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
218   }
219
220   /**
221    * Installer step: Select installation profile.
222    */
223   protected function setUpProfile() {
224     $edit = [
225       'profile' => $this->profile,
226     ];
227     $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
228   }
229
230   /**
231    * Installer step: Configure settings.
232    */
233   protected function setUpSettings() {
234     $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']);
235     $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
236   }
237
238   /**
239    * Installer step: Requirements problem.
240    *
241    * Override this method to test specific requirements warnings or errors
242    * during the installer.
243    *
244    * @see system_requirements()
245    */
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']);
252     }
253   }
254
255   /**
256    * Final installer step: Configure site.
257    */
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;
264   }
265
266   /**
267    * {@inheritdoc}
268    *
269    * FunctionalTestSetupTrait::refreshVariables() tries to operate on persistent
270    * storage, which is only available after the installer completed.
271    */
272   protected function refreshVariables() {
273     if ($this->isInstalled) {
274       parent::refreshVariables();
275     }
276   }
277
278   /**
279    * Continues installation when an expected warning is found.
280    *
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.
287    */
288   protected function continueOnExpectedWarnings($expected_warnings = []) {
289     // Don't try to continue if there are errors.
290     if (strpos($this->getTextContent(), 'Errors found') !== FALSE) {
291       return;
292     }
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
298     // - etc.
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.
303     $selectors = [];
304     for ($i = 0; $i <= count($expected_warnings); $i++) {
305       $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary';
306     }
307     $warning_elements = $this->cssSelect(implode(', ', $selectors));
308
309     // Confirm that there are only the expected warnings.
310     $warnings = [];
311     foreach ($warning_elements as $warning) {
312       $warnings[] = trim($warning->getText());
313     }
314     $this->assertEquals($expected_warnings, $warnings);
315     $this->clickLink('continue anyway');
316     $this->checkForMetaRefresh();
317   }
318
319 }