comparison core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 $this->visitInstaller();
144
145 // Select language.
146 $this->setUpLanguage();
147
148 // Select profile.
149 $this->setUpProfile();
150
151 // Address the requirements problem screen, if any.
152 $this->setUpRequirementsProblem();
153
154 // Configure settings.
155 $this->setUpSettings();
156
157 // @todo Allow test classes based on this class to act on further installer
158 // screens.
159
160 // Configure site.
161 $this->setUpSite();
162
163 if ($this->isInstalled) {
164 // Import new settings.php written by the installer.
165 $request = Request::createFromGlobals();
166 $class_loader = require $this->container->get('app.root') . '/autoload.php';
167 Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader);
168 foreach ($GLOBALS['config_directories'] as $type => $path) {
169 $this->configDirectories[$type] = $path;
170 }
171
172 // After writing settings.php, the installer removes write permissions
173 // from the site directory. To allow drupal_generate_test_ua() to write
174 // a file containing the private key for drupal_valid_test_ua(), the site
175 // directory has to be writable.
176 // BrowserTestBase::tearDown() will delete the entire test site directory.
177 // Not using File API; a potential error must trigger a PHP warning.
178 chmod($this->container->get('app.root') . '/' . $this->siteDirectory, 0777);
179 $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE);
180 $this->kernel->prepareLegacyRequest($request);
181 $this->container = $this->kernel->getContainer();
182
183 // Manually configure the test mail collector implementation to prevent
184 // tests from sending out emails and collect them in state instead.
185 $this->container->get('config.factory')
186 ->getEditable('system.mail')
187 ->set('interface.default', 'test_mail_collector')
188 ->save();
189 }
190 }
191
192 /**
193 * {@inheritdoc}
194 */
195 protected function initFrontPage() {
196 // We don't want to visit the front page with the installer when
197 // initializing Mink, so we do nothing here.
198 }
199
200 /**
201 * Visits the interactive installer.
202 */
203 protected function visitInstaller() {
204 $this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
205 }
206
207 /**
208 * Installer step: Select language.
209 */
210 protected function setUpLanguage() {
211 $edit = [
212 'langcode' => $this->langcode,
213 ];
214 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
215 }
216
217 /**
218 * Installer step: Select installation profile.
219 */
220 protected function setUpProfile() {
221 $edit = [
222 'profile' => $this->profile,
223 ];
224 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
225 }
226
227 /**
228 * Installer step: Configure settings.
229 */
230 protected function setUpSettings() {
231 $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']);
232 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
233 }
234
235 /**
236 * Installer step: Requirements problem.
237 *
238 * Override this method to test specific requirements warnings or errors
239 * during the installer.
240 *
241 * @see system_requirements()
242 */
243 protected function setUpRequirementsProblem() {
244 // By default, skip the "recommended PHP version" warning on older test
245 // environments. This allows the installer to be tested consistently on
246 // both recommended PHP versions and older (but still supported) versions.
247 if (version_compare(phpversion(), '7.0') < 0) {
248 $this->continueOnExpectedWarnings(['PHP']);
249 }
250 }
251
252 /**
253 * Final installer step: Configure site.
254 */
255 protected function setUpSite() {
256 $edit = $this->translatePostValues($this->parameters['forms']['install_configure_form']);
257 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
258 // If we've got to this point the site is installed using the regular
259 // installation workflow.
260 $this->isInstalled = TRUE;
261 }
262
263 /**
264 * {@inheritdoc}
265 *
266 * FunctionalTestSetupTrait::refreshVariables() tries to operate on persistent
267 * storage, which is only available after the installer completed.
268 */
269 protected function refreshVariables() {
270 if ($this->isInstalled) {
271 parent::refreshVariables();
272 }
273 }
274
275 /**
276 * Continues installation when an expected warning is found.
277 *
278 * @param string[] $expected_warnings
279 * A list of warning summaries to expect on the requirements screen (e.g.
280 * 'PHP', 'PHP OPcode caching', etc.). If only the expected warnings
281 * are found, the test will click the "continue anyway" link to go to the
282 * next screen of the installer. If an expected warning is not found, or if
283 * a warning not in the list is present, a fail is raised.
284 */
285 protected function continueOnExpectedWarnings($expected_warnings = []) {
286 // Don't try to continue if there are errors.
287 if (strpos($this->getTextContent(), 'Errors found') !== FALSE) {
288 return;
289 }
290 // Allow only details elements that are directly after the warning header
291 // or each other. There is no guaranteed wrapper we can rely on across
292 // distributions. When there are multiple warnings, the selectors will be:
293 // - h3#warning+details summary
294 // - h3#warning+details+details summary
295 // - etc.
296 // We add one more selector than expected warnings to confirm that there
297 // isn't any other warning before clicking the link.
298 // @todo Make this more reliable in
299 // https://www.drupal.org/project/drupal/issues/2927345.
300 $selectors = [];
301 for ($i = 0; $i <= count($expected_warnings); $i++) {
302 $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary';
303 }
304 $warning_elements = $this->cssSelect(implode(', ', $selectors));
305
306 // Confirm that there are only the expected warnings.
307 $warnings = [];
308 foreach ($warning_elements as $warning) {
309 $warnings[] = trim($warning->getText());
310 }
311 $this->assertEquals($expected_warnings, $warnings);
312 $this->clickLink('continue anyway');
313 $this->checkForMetaRefresh();
314 }
315
316 }