Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/simpletest/src/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\simpletest; | |
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 Symfony\Component\DependencyInjection\ContainerBuilder; | |
10 use Symfony\Component\DependencyInjection\Reference; | |
11 use Symfony\Component\HttpFoundation\Request; | |
12 use Symfony\Component\HttpFoundation\RequestStack; | |
13 | |
14 /** | |
15 * Base class for testing the interactive installer. | |
16 */ | |
17 abstract class InstallerTestBase extends WebTestBase { | |
18 | |
19 /** | |
20 * Custom settings.php values to write for a test run. | |
21 * | |
22 * @var array | |
23 * An array of settings to write out, in the format expected by | |
24 * drupal_rewrite_settings(). | |
25 */ | |
26 protected $settings = []; | |
27 | |
28 /** | |
29 * The language code in which to install Drupal. | |
30 * | |
31 * @var string | |
32 */ | |
33 protected $langcode = 'en'; | |
34 | |
35 /** | |
36 * The installation profile to install. | |
37 * | |
38 * @var string | |
39 */ | |
40 protected $profile = 'testing'; | |
41 | |
42 /** | |
43 * Additional parameters to use for installer screens. | |
44 * | |
45 * @see WebTestBase::installParameters() | |
46 * | |
47 * @var array | |
48 */ | |
49 protected $parameters = []; | |
50 | |
51 /** | |
52 * A string translation map used for translated installer screens. | |
53 * | |
54 * Keys are English strings, values are translated strings. | |
55 * | |
56 * @var array | |
57 */ | |
58 protected $translations = [ | |
59 'Save and continue' => 'Save and continue', | |
60 ]; | |
61 | |
62 /** | |
63 * Whether the installer has completed. | |
64 * | |
65 * @var bool | |
66 */ | |
67 protected $isInstalled = FALSE; | |
68 | |
69 /** | |
70 * {@inheritdoc} | |
71 */ | |
72 protected function setUp() { | |
73 $this->isInstalled = FALSE; | |
74 | |
75 // Define information about the user 1 account. | |
76 $this->rootUser = new UserSession([ | |
77 'uid' => 1, | |
78 'name' => 'admin', | |
79 'mail' => 'admin@example.com', | |
80 'pass_raw' => $this->randomMachineName(), | |
81 ]); | |
82 | |
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); | |
89 } | |
90 | |
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(); | |
95 | |
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); | |
103 $this->container | |
104 ->set('request_stack', $request_stack); | |
105 $this->container | |
106 ->setParameter('language.default_values', Language::$defaultValues); | |
107 $this->container | |
108 ->register('language.default', 'Drupal\Core\Language\LanguageDefault') | |
109 ->addArgument('%language.default_values%'); | |
110 $this->container | |
111 ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') | |
112 ->addArgument(new Reference('language.default')); | |
113 $this->container | |
114 ->set('app.root', DRUPAL_ROOT); | |
115 \Drupal::setContainer($this->container); | |
116 | |
117 $this->visitInstaller(); | |
118 | |
119 // Select language. | |
120 $this->setUpLanguage(); | |
121 | |
122 // Select profile. | |
123 $this->setUpProfile(); | |
124 | |
125 // Address the requirements problem screen, if any. | |
126 $this->setUpRequirementsProblem(); | |
127 | |
128 // Configure settings. | |
129 $this->setUpSettings(); | |
130 | |
131 // @todo Allow test classes based on this class to act on further installer | |
132 // screens. | |
133 | |
134 // Configure site. | |
135 $this->setUpSite(); | |
136 | |
137 if ($this->isInstalled) { | |
138 // Import new settings.php written by the installer. | |
139 $request = Request::createFromGlobals(); | |
140 $class_loader = require $this->container->get('app.root') . '/autoload.php'; | |
141 Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader); | |
142 foreach ($GLOBALS['config_directories'] as $type => $path) { | |
143 $this->configDirectories[$type] = $path; | |
144 } | |
145 | |
146 // After writing settings.php, the installer removes write permissions | |
147 // from the site directory. To allow drupal_generate_test_ua() to write | |
148 // a file containing the private key for drupal_valid_test_ua(), the site | |
149 // directory has to be writable. | |
150 // WebTestBase::tearDown() will delete the entire test site directory. | |
151 // Not using File API; a potential error must trigger a PHP warning. | |
152 chmod($this->container->get('app.root') . '/' . $this->siteDirectory, 0777); | |
153 $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE); | |
154 $this->kernel->prepareLegacyRequest($request); | |
155 $this->container = $this->kernel->getContainer(); | |
156 | |
157 // Manually configure the test mail collector implementation to prevent | |
158 // tests from sending out emails and collect them in state instead. | |
159 $this->container->get('config.factory') | |
160 ->getEditable('system.mail') | |
161 ->set('interface.default', 'test_mail_collector') | |
162 ->save(); | |
163 } | |
164 } | |
165 | |
166 /** | |
167 * Visits the interactive installer. | |
168 */ | |
169 protected function visitInstaller() { | |
170 $this->drupalGet($GLOBALS['base_url'] . '/core/install.php'); | |
171 } | |
172 | |
173 /** | |
174 * Installer step: Select language. | |
175 */ | |
176 protected function setUpLanguage() { | |
177 $edit = [ | |
178 'langcode' => $this->langcode, | |
179 ]; | |
180 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); | |
181 } | |
182 | |
183 /** | |
184 * Installer step: Select installation profile. | |
185 */ | |
186 protected function setUpProfile() { | |
187 $edit = [ | |
188 'profile' => $this->profile, | |
189 ]; | |
190 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); | |
191 } | |
192 | |
193 /** | |
194 * Installer step: Configure settings. | |
195 */ | |
196 protected function setUpSettings() { | |
197 $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']); | |
198 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); | |
199 } | |
200 | |
201 /** | |
202 * Installer step: Requirements problem. | |
203 * | |
204 * Override this method to test specific requirements warnings or errors | |
205 * during the installer. | |
206 * | |
207 * @see system_requirements() | |
208 */ | |
209 protected function setUpRequirementsProblem() { | |
210 // By default, skip the "recommended PHP version" warning on older test | |
211 // environments. This allows the installer to be tested consistently on | |
212 // both recommended PHP versions and older (but still supported) versions. | |
213 if (version_compare(phpversion(), '7.0') < 0) { | |
214 $this->continueOnExpectedWarnings(['PHP']); | |
215 } | |
216 } | |
217 | |
218 /** | |
219 * Final installer step: Configure site. | |
220 */ | |
221 protected function setUpSite() { | |
222 $edit = $this->translatePostValues($this->parameters['forms']['install_configure_form']); | |
223 $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); | |
224 // If we've got to this point the site is installed using the regular | |
225 // installation workflow. | |
226 $this->isInstalled = TRUE; | |
227 } | |
228 | |
229 /** | |
230 * {@inheritdoc} | |
231 * | |
232 * WebTestBase::refreshVariables() tries to operate on persistent storage, | |
233 * which is only available after the installer completed. | |
234 */ | |
235 protected function refreshVariables() { | |
236 if ($this->isInstalled) { | |
237 parent::refreshVariables(); | |
238 } | |
239 } | |
240 | |
241 /** | |
242 * Continues installation when an expected warning is found. | |
243 * | |
244 * @param string[] $expected_warnings | |
245 * A list of warning summaries to expect on the requirements screen (e.g. | |
246 * 'PHP', 'PHP OPcode caching', etc.). If only the expected warnings | |
247 * are found, the test will click the "continue anyway" link to go to the | |
248 * next screen of the installer. If an expected warning is not found, or if | |
249 * a warning not in the list is present, a fail is raised. | |
250 */ | |
251 protected function continueOnExpectedWarnings($expected_warnings = []) { | |
252 // Don't try to continue if there are errors. | |
253 if (strpos($this->getTextContent(), 'Errors found') !== FALSE) { | |
254 return; | |
255 } | |
256 // Allow only details elements that are directly after the warning header | |
257 // or each other. There is no guaranteed wrapper we can rely on across | |
258 // distributions. When there are multiple warnings, the selectors will be: | |
259 // - h3#warning+details summary | |
260 // - h3#warning+details+details summary | |
261 // - etc. | |
262 // We add one more selector than expected warnings to confirm that there | |
263 // isn't any other warning before clicking the link. | |
264 // @todo Make this more reliable in | |
265 // https://www.drupal.org/project/drupal/issues/2927345. | |
266 $selectors = []; | |
267 for ($i = 0; $i <= count($expected_warnings); $i++) { | |
268 $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary'; | |
269 } | |
270 $warning_elements = $this->cssSelect(implode(', ', $selectors)); | |
271 | |
272 // Confirm that there are only the expected warnings. | |
273 $warnings = []; | |
274 foreach ($warning_elements as $warning) { | |
275 $warnings[] = trim((string) $warning); | |
276 } | |
277 $this->assertEqual($expected_warnings, $warnings); | |
278 $this->clickLink('continue anyway'); | |
279 } | |
280 | |
281 } |