Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Test;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\FileCache\FileCacheFactory;
|
Chris@0
|
6 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
7 use Drupal\Core\Cache\Cache;
|
Chris@0
|
8 use Drupal\Core\Config\Development\ConfigSchemaChecker;
|
Chris@0
|
9 use Drupal\Core\Database\Database;
|
Chris@0
|
10 use Drupal\Core\DrupalKernel;
|
Chris@0
|
11 use Drupal\Core\Extension\MissingDependencyException;
|
Chris@0
|
12 use Drupal\Core\Serialization\Yaml;
|
Chris@0
|
13 use Drupal\Core\Session\UserSession;
|
Chris@0
|
14 use Drupal\Core\Site\Settings;
|
Chris@0
|
15 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
17 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
18 use Symfony\Component\Yaml\Yaml as SymfonyYaml;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Defines a trait for shared functional test setup functionality.
|
Chris@0
|
22 */
|
Chris@0
|
23 trait FunctionalTestSetupTrait {
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The "#1" admin user.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $rootUser;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * The class loader to use for installation and initialization of setup.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var \Symfony\Component\Classloader\Classloader
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $classLoader;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * The config directories used in this test.
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $configDirectories = [];
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Prepares site settings and services before installation.
|
Chris@0
|
46 */
|
Chris@0
|
47 protected function prepareSettings() {
|
Chris@0
|
48 // Prepare installer settings that are not install_drupal() parameters.
|
Chris@0
|
49 // Copy and prepare an actual settings.php, so as to resemble a regular
|
Chris@0
|
50 // installation.
|
Chris@0
|
51 // Not using File API; a potential error must trigger a PHP warning.
|
Chris@0
|
52 $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
|
Chris@0
|
53 copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $directory . '/settings.php');
|
Chris@0
|
54
|
Chris@0
|
55 // The public file system path is created during installation. Additionally,
|
Chris@0
|
56 // during tests:
|
Chris@0
|
57 // - The temporary directory is set and created by install_base_system().
|
Chris@0
|
58 // - The private file directory is created post install by
|
Chris@0
|
59 // FunctionalTestSetupTrait::initConfig().
|
Chris@0
|
60 // @see system_requirements()
|
Chris@0
|
61 // @see TestBase::prepareEnvironment()
|
Chris@0
|
62 // @see install_base_system()
|
Chris@0
|
63 // @see \Drupal\Core\Test\FunctionalTestSetupTrait::initConfig()
|
Chris@0
|
64 $settings['settings']['file_public_path'] = (object) [
|
Chris@0
|
65 'value' => $this->publicFilesDirectory,
|
Chris@0
|
66 'required' => TRUE,
|
Chris@0
|
67 ];
|
Chris@0
|
68 $settings['settings']['file_private_path'] = (object) [
|
Chris@0
|
69 'value' => $this->privateFilesDirectory,
|
Chris@0
|
70 'required' => TRUE,
|
Chris@0
|
71 ];
|
Chris@0
|
72 // Save the original site directory path, so that extensions in the
|
Chris@0
|
73 // site-specific directory can still be discovered in the test site
|
Chris@0
|
74 // environment.
|
Chris@0
|
75 // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
|
Chris@0
|
76 $settings['settings']['test_parent_site'] = (object) [
|
Chris@0
|
77 'value' => $this->originalSite,
|
Chris@0
|
78 'required' => TRUE,
|
Chris@0
|
79 ];
|
Chris@0
|
80 // Add the parent profile's search path to the child site's search paths.
|
Chris@0
|
81 // @see \Drupal\Core\Extension\ExtensionDiscovery::getProfileDirectories()
|
Chris@0
|
82 $settings['conf']['simpletest.settings']['parent_profile'] = (object) [
|
Chris@0
|
83 'value' => $this->originalProfile,
|
Chris@0
|
84 'required' => TRUE,
|
Chris@0
|
85 ];
|
Chris@0
|
86 $this->writeSettings($settings);
|
Chris@0
|
87 // Allow for test-specific overrides.
|
Chris@0
|
88 $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php';
|
Chris@0
|
89 if (file_exists($settings_testing_file)) {
|
Chris@0
|
90 // Copy the testing-specific settings.php overrides in place.
|
Chris@0
|
91 copy($settings_testing_file, $directory . '/settings.testing.php');
|
Chris@0
|
92 // Add the name of the testing class to settings.php and include the
|
Chris@0
|
93 // testing specific overrides.
|
Chris@0
|
94 file_put_contents($directory . '/settings.php', "\n\$test_class = '" . get_class($this) . "';\n" . 'include DRUPAL_ROOT . \'/\' . $site_path . \'/settings.testing.php\';' . "\n", FILE_APPEND);
|
Chris@0
|
95 }
|
Chris@0
|
96 $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
|
Chris@0
|
97 if (!file_exists($settings_services_file)) {
|
Chris@0
|
98 // Otherwise, use the default services as a starting point for overrides.
|
Chris@0
|
99 $settings_services_file = DRUPAL_ROOT . '/sites/default/default.services.yml';
|
Chris@0
|
100 }
|
Chris@0
|
101 // Copy the testing-specific service overrides in place.
|
Chris@0
|
102 copy($settings_services_file, $directory . '/services.yml');
|
Chris@0
|
103 if ($this->strictConfigSchema) {
|
Chris@0
|
104 // Add a listener to validate configuration schema on save.
|
Chris@0
|
105 $yaml = new SymfonyYaml();
|
Chris@0
|
106 $content = file_get_contents($directory . '/services.yml');
|
Chris@0
|
107 $services = $yaml->parse($content);
|
Chris@0
|
108 $services['services']['simpletest.config_schema_checker'] = [
|
Chris@0
|
109 'class' => ConfigSchemaChecker::class,
|
Chris@0
|
110 'arguments' => ['@config.typed', $this->getConfigSchemaExclusions()],
|
Chris@0
|
111 'tags' => [['name' => 'event_subscriber']],
|
Chris@0
|
112 ];
|
Chris@0
|
113 file_put_contents($directory . '/services.yml', $yaml->dump($services));
|
Chris@0
|
114 }
|
Chris@0
|
115 // Since Drupal is bootstrapped already, install_begin_request() will not
|
Chris@0
|
116 // bootstrap again. Hence, we have to reload the newly written custom
|
Chris@0
|
117 // settings.php manually.
|
Chris@0
|
118 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Rewrites the settings.php file of the test site.
|
Chris@0
|
123 *
|
Chris@0
|
124 * @param array $settings
|
Chris@0
|
125 * An array of settings to write out, in the format expected by
|
Chris@0
|
126 * drupal_rewrite_settings().
|
Chris@0
|
127 *
|
Chris@0
|
128 * @see drupal_rewrite_settings()
|
Chris@0
|
129 */
|
Chris@0
|
130 protected function writeSettings(array $settings) {
|
Chris@0
|
131 include_once DRUPAL_ROOT . '/core/includes/install.inc';
|
Chris@0
|
132 $filename = $this->siteDirectory . '/settings.php';
|
Chris@0
|
133 // system_requirements() removes write permissions from settings.php
|
Chris@0
|
134 // whenever it is invoked.
|
Chris@0
|
135 // Not using File API; a potential error must trigger a PHP warning.
|
Chris@0
|
136 chmod($filename, 0666);
|
Chris@0
|
137 drupal_rewrite_settings($settings, $filename);
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * Changes parameters in the services.yml file.
|
Chris@0
|
142 *
|
Chris@0
|
143 * @param string $name
|
Chris@0
|
144 * The name of the parameter.
|
Chris@0
|
145 * @param string $value
|
Chris@0
|
146 * The value of the parameter.
|
Chris@0
|
147 */
|
Chris@0
|
148 protected function setContainerParameter($name, $value) {
|
Chris@0
|
149 $filename = $this->siteDirectory . '/services.yml';
|
Chris@0
|
150 chmod($filename, 0666);
|
Chris@0
|
151
|
Chris@0
|
152 $services = Yaml::decode(file_get_contents($filename));
|
Chris@0
|
153 $services['parameters'][$name] = $value;
|
Chris@0
|
154 file_put_contents($filename, Yaml::encode($services));
|
Chris@0
|
155
|
Chris@0
|
156 // Ensure that the cache is deleted for the yaml file loader.
|
Chris@0
|
157 $file_cache = FileCacheFactory::get('container_yaml_loader');
|
Chris@0
|
158 $file_cache->delete($filename);
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 /**
|
Chris@0
|
162 * Rebuilds \Drupal::getContainer().
|
Chris@0
|
163 *
|
Chris@0
|
164 * Use this to update the test process's kernel with a new service container.
|
Chris@0
|
165 * For example, when the list of enabled modules is changed via the internal
|
Chris@0
|
166 * browser the test process's kernel has a service container with an out of
|
Chris@0
|
167 * date module list.
|
Chris@0
|
168 *
|
Chris@0
|
169 * @see TestBase::prepareEnvironment()
|
Chris@0
|
170 * @see TestBase::restoreEnvironment()
|
Chris@0
|
171 *
|
Chris@0
|
172 * @todo Fix https://www.drupal.org/node/2021959 so that module enable/disable
|
Chris@0
|
173 * changes are immediately reflected in \Drupal::getContainer(). Until then,
|
Chris@0
|
174 * tests can invoke this workaround when requiring services from newly
|
Chris@0
|
175 * enabled modules to be immediately available in the same request.
|
Chris@0
|
176 */
|
Chris@0
|
177 protected function rebuildContainer() {
|
Chris@0
|
178 // Rebuild the kernel and bring it back to a fully bootstrapped state.
|
Chris@0
|
179 $this->container = $this->kernel->rebuildContainer();
|
Chris@0
|
180
|
Chris@0
|
181 // Make sure the url generator has a request object, otherwise calls to
|
Chris@0
|
182 // $this->drupalGet() will fail.
|
Chris@0
|
183 $this->prepareRequestForGenerator();
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 /**
|
Chris@0
|
187 * Resets all data structures after having enabled new modules.
|
Chris@0
|
188 *
|
Chris@0
|
189 * This method is called by FunctionalTestSetupTrait::rebuildAll() after
|
Chris@0
|
190 * enabling the requested modules. It must be called again when additional
|
Chris@0
|
191 * modules are enabled later.
|
Chris@0
|
192 *
|
Chris@0
|
193 * @see \Drupal\Core\Test\FunctionalTestSetupTrait::rebuildAll()
|
Chris@0
|
194 * @see \Drupal\Tests\BrowserTestBase::installDrupal()
|
Chris@0
|
195 * @see \Drupal\simpletest\WebTestBase::setUp()
|
Chris@0
|
196 */
|
Chris@0
|
197 protected function resetAll() {
|
Chris@0
|
198 // Clear all database and static caches and rebuild data structures.
|
Chris@0
|
199 drupal_flush_all_caches();
|
Chris@0
|
200 $this->container = \Drupal::getContainer();
|
Chris@0
|
201
|
Chris@0
|
202 // Reset static variables and reload permissions.
|
Chris@0
|
203 $this->refreshVariables();
|
Chris@0
|
204 }
|
Chris@0
|
205
|
Chris@0
|
206 /**
|
Chris@0
|
207 * Refreshes in-memory configuration and state information.
|
Chris@0
|
208 *
|
Chris@0
|
209 * Useful after a page request is made that changes configuration or state in
|
Chris@0
|
210 * a different thread.
|
Chris@0
|
211 *
|
Chris@0
|
212 * In other words calling a settings page with $this->drupalPostForm() with a
|
Chris@0
|
213 * changed value would update configuration to reflect that change, but in the
|
Chris@0
|
214 * thread that made the call (thread running the test) the changed values
|
Chris@0
|
215 * would not be picked up.
|
Chris@0
|
216 *
|
Chris@0
|
217 * This method clears the cache and loads a fresh copy.
|
Chris@0
|
218 */
|
Chris@0
|
219 protected function refreshVariables() {
|
Chris@0
|
220 // Clear the tag cache.
|
Chris@0
|
221 \Drupal::service('cache_tags.invalidator')->resetChecksums();
|
Chris@0
|
222 foreach (Cache::getBins() as $backend) {
|
Chris@0
|
223 if (is_callable([$backend, 'reset'])) {
|
Chris@0
|
224 $backend->reset();
|
Chris@0
|
225 }
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 $this->container->get('config.factory')->reset();
|
Chris@0
|
229 $this->container->get('state')->resetCache();
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 /**
|
Chris@0
|
233 * Creates a mock request and sets it on the generator.
|
Chris@0
|
234 *
|
Chris@0
|
235 * This is used to manipulate how the generator generates paths during tests.
|
Chris@0
|
236 * It also ensures that calls to $this->drupalGet() will work when running
|
Chris@0
|
237 * from run-tests.sh because the url generator no longer looks at the global
|
Chris@0
|
238 * variables that are set there but relies on getting this information from a
|
Chris@0
|
239 * request object.
|
Chris@0
|
240 *
|
Chris@0
|
241 * @param bool $clean_urls
|
Chris@0
|
242 * Whether to mock the request using clean urls.
|
Chris@0
|
243 * @param array $override_server_vars
|
Chris@0
|
244 * An array of server variables to override.
|
Chris@0
|
245 *
|
Chris@0
|
246 * @return \Symfony\Component\HttpFoundation\Request
|
Chris@0
|
247 * The mocked request object.
|
Chris@0
|
248 */
|
Chris@0
|
249 protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = []) {
|
Chris@0
|
250 $request = Request::createFromGlobals();
|
Chris@0
|
251 $server = $request->server->all();
|
Chris@0
|
252 if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
|
Chris@0
|
253 // We need this for when the test is executed by run-tests.sh.
|
Chris@0
|
254 // @todo Remove this once run-tests.sh has been converted to use a Request
|
Chris@0
|
255 // object.
|
Chris@0
|
256 $cwd = getcwd();
|
Chris@0
|
257 $server['SCRIPT_FILENAME'] = $cwd . '/' . basename($server['SCRIPT_NAME']);
|
Chris@0
|
258 $base_path = rtrim($server['REQUEST_URI'], '/');
|
Chris@0
|
259 }
|
Chris@0
|
260 else {
|
Chris@0
|
261 $base_path = $request->getBasePath();
|
Chris@0
|
262 }
|
Chris@0
|
263 if ($clean_urls) {
|
Chris@0
|
264 $request_path = $base_path ? $base_path . '/user' : 'user';
|
Chris@0
|
265 }
|
Chris@0
|
266 else {
|
Chris@0
|
267 $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
|
Chris@0
|
268 }
|
Chris@0
|
269 $server = array_merge($server, $override_server_vars);
|
Chris@0
|
270
|
Chris@0
|
271 $request = Request::create($request_path, 'GET', [], [], [], $server);
|
Chris@0
|
272 // Ensure the request time is REQUEST_TIME to ensure that API calls
|
Chris@0
|
273 // in the test use the right timestamp.
|
Chris@0
|
274 $request->server->set('REQUEST_TIME', REQUEST_TIME);
|
Chris@0
|
275 $this->container->get('request_stack')->push($request);
|
Chris@0
|
276
|
Chris@0
|
277 // The request context is normally set by the router_listener from within
|
Chris@0
|
278 // its KernelEvents::REQUEST listener. In the simpletest parent site this
|
Chris@0
|
279 // event is not fired, therefore it is necessary to updated the request
|
Chris@0
|
280 // context manually here.
|
Chris@0
|
281 $this->container->get('router.request_context')->fromRequest($request);
|
Chris@0
|
282
|
Chris@0
|
283 return $request;
|
Chris@0
|
284 }
|
Chris@0
|
285
|
Chris@0
|
286 /**
|
Chris@0
|
287 * Execute the non-interactive installer.
|
Chris@0
|
288 *
|
Chris@0
|
289 * @see install_drupal()
|
Chris@0
|
290 */
|
Chris@0
|
291 protected function doInstall() {
|
Chris@0
|
292 require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
|
Chris@0
|
293 install_drupal($this->classLoader, $this->installParameters());
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 /**
|
Chris@0
|
297 * Initialize settings created during install.
|
Chris@0
|
298 */
|
Chris@0
|
299 protected function initSettings() {
|
Chris@0
|
300 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
|
Chris@0
|
301 foreach ($GLOBALS['config_directories'] as $type => $path) {
|
Chris@0
|
302 $this->configDirectories[$type] = $path;
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 // After writing settings.php, the installer removes write permissions
|
Chris@0
|
306 // from the site directory. To allow drupal_generate_test_ua() to write
|
Chris@0
|
307 // a file containing the private key for drupal_valid_test_ua(), the site
|
Chris@0
|
308 // directory has to be writable.
|
Chris@0
|
309 // TestBase::restoreEnvironment() will delete the entire site directory.
|
Chris@0
|
310 // Not using File API; a potential error must trigger a PHP warning.
|
Chris@0
|
311 chmod(DRUPAL_ROOT . '/' . $this->siteDirectory, 0777);
|
Chris@0
|
312
|
Chris@0
|
313 // During tests, cacheable responses should get the debugging cacheability
|
Chris@0
|
314 // headers by default.
|
Chris@0
|
315 $this->setContainerParameter('http.response.debug_cacheability_headers', TRUE);
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * Initialize various configurations post-installation.
|
Chris@0
|
320 *
|
Chris@0
|
321 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
|
Chris@0
|
322 * The container.
|
Chris@0
|
323 */
|
Chris@0
|
324 protected function initConfig(ContainerInterface $container) {
|
Chris@0
|
325 $config = $container->get('config.factory');
|
Chris@0
|
326
|
Chris@0
|
327 // Manually create the private directory.
|
Chris@0
|
328 file_prepare_directory($this->privateFilesDirectory, FILE_CREATE_DIRECTORY);
|
Chris@0
|
329
|
Chris@0
|
330 // Manually configure the test mail collector implementation to prevent
|
Chris@0
|
331 // tests from sending out emails and collect them in state instead.
|
Chris@0
|
332 // While this should be enforced via settings.php prior to installation,
|
Chris@0
|
333 // some tests expect to be able to test mail system implementations.
|
Chris@0
|
334 $config->getEditable('system.mail')
|
Chris@0
|
335 ->set('interface.default', 'test_mail_collector')
|
Chris@0
|
336 ->save();
|
Chris@0
|
337
|
Chris@0
|
338 // By default, verbosely display all errors and disable all production
|
Chris@0
|
339 // environment optimizations for all tests to avoid needless overhead and
|
Chris@0
|
340 // ensure a sane default experience for test authors.
|
Chris@0
|
341 // @see https://www.drupal.org/node/2259167
|
Chris@0
|
342 $config->getEditable('system.logging')
|
Chris@0
|
343 ->set('error_level', 'verbose')
|
Chris@0
|
344 ->save();
|
Chris@0
|
345 $config->getEditable('system.performance')
|
Chris@0
|
346 ->set('css.preprocess', FALSE)
|
Chris@0
|
347 ->set('js.preprocess', FALSE)
|
Chris@0
|
348 ->save();
|
Chris@0
|
349
|
Chris@0
|
350 // Set an explicit time zone to not rely on the system one, which may vary
|
Chris@0
|
351 // from setup to setup. The Australia/Sydney time zone is chosen so all
|
Chris@0
|
352 // tests are run using an edge case scenario (UTC10 and DST). This choice
|
Chris@0
|
353 // is made to prevent time zone related regressions and reduce the
|
Chris@0
|
354 // fragility of the testing system in general.
|
Chris@0
|
355 $config->getEditable('system.date')
|
Chris@0
|
356 ->set('timezone.default', 'Australia/Sydney')
|
Chris@0
|
357 ->save();
|
Chris@0
|
358 }
|
Chris@0
|
359
|
Chris@0
|
360 /**
|
Chris@0
|
361 * Initializes user 1 for the site to be installed.
|
Chris@0
|
362 */
|
Chris@0
|
363 protected function initUserSession() {
|
Chris@0
|
364 $password = $this->randomMachineName();
|
Chris@0
|
365 // Define information about the user 1 account.
|
Chris@0
|
366 $this->rootUser = new UserSession([
|
Chris@0
|
367 'uid' => 1,
|
Chris@0
|
368 'name' => 'admin',
|
Chris@0
|
369 'mail' => 'admin@example.com',
|
Chris@0
|
370 'pass_raw' => $password,
|
Chris@0
|
371 'passRaw' => $password,
|
Chris@0
|
372 'timezone' => date_default_timezone_get(),
|
Chris@0
|
373 ]);
|
Chris@0
|
374
|
Chris@0
|
375 // The child site derives its session name from the database prefix when
|
Chris@0
|
376 // running web tests.
|
Chris@0
|
377 $this->generateSessionName($this->databasePrefix);
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 /**
|
Chris@0
|
381 * Initializes the kernel after installation.
|
Chris@0
|
382 *
|
Chris@0
|
383 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
384 * Request object.
|
Chris@0
|
385 *
|
Chris@0
|
386 * @return \Symfony\Component\DependencyInjection\ContainerInterface
|
Chris@0
|
387 * The container.
|
Chris@0
|
388 */
|
Chris@0
|
389 protected function initKernel(Request $request) {
|
Chris@0
|
390 $this->kernel = DrupalKernel::createFromRequest($request, $this->classLoader, 'prod', TRUE);
|
Chris@0
|
391 $this->kernel->prepareLegacyRequest($request);
|
Chris@0
|
392 // Force the container to be built from scratch instead of loaded from the
|
Chris@0
|
393 // disk. This forces us to not accidentally load the parent site.
|
Chris@0
|
394 return $this->kernel->rebuildContainer();
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * Install modules defined by `static::$modules`.
|
Chris@0
|
399 *
|
Chris@0
|
400 * To install test modules outside of the testing environment, add
|
Chris@0
|
401 * @code
|
Chris@0
|
402 * $settings['extension_discovery_scan_tests'] = TRUE;
|
Chris@0
|
403 * @endcode
|
Chris@0
|
404 * to your settings.php.
|
Chris@0
|
405 *
|
Chris@0
|
406 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
|
Chris@0
|
407 * The container.
|
Chris@0
|
408 */
|
Chris@0
|
409 protected function installModulesFromClassProperty(ContainerInterface $container) {
|
Chris@0
|
410 $class = get_class($this);
|
Chris@0
|
411 $modules = [];
|
Chris@0
|
412 while ($class) {
|
Chris@0
|
413 if (property_exists($class, 'modules')) {
|
Chris@0
|
414 $modules = array_merge($modules, $class::$modules);
|
Chris@0
|
415 }
|
Chris@0
|
416 $class = get_parent_class($class);
|
Chris@0
|
417 }
|
Chris@0
|
418 if ($modules) {
|
Chris@0
|
419 $modules = array_unique($modules);
|
Chris@0
|
420 try {
|
Chris@0
|
421 $success = $container->get('module_installer')->install($modules, TRUE);
|
Chris@0
|
422 $this->assertTrue($success, SafeMarkup::format('Enabled modules: %modules', ['%modules' => implode(', ', $modules)]));
|
Chris@0
|
423 }
|
Chris@0
|
424 catch (MissingDependencyException $e) {
|
Chris@0
|
425 // The exception message has all the details.
|
Chris@0
|
426 $this->fail($e->getMessage());
|
Chris@0
|
427 }
|
Chris@0
|
428
|
Chris@0
|
429 $this->rebuildContainer();
|
Chris@0
|
430 }
|
Chris@0
|
431 }
|
Chris@0
|
432
|
Chris@0
|
433 /**
|
Chris@0
|
434 * Resets and rebuilds the environment after setup.
|
Chris@0
|
435 */
|
Chris@0
|
436 protected function rebuildAll() {
|
Chris@0
|
437 // Reset/rebuild all data structures after enabling the modules, primarily
|
Chris@0
|
438 // to synchronize all data structures and caches between the test runner and
|
Chris@0
|
439 // the child site.
|
Chris@0
|
440 // @see \Drupal\Core\DrupalKernel::bootCode()
|
Chris@0
|
441 // @todo Test-specific setUp() methods may set up further fixtures; find a
|
Chris@0
|
442 // way to execute this after setUp() is done, or to eliminate it entirely.
|
Chris@0
|
443 $this->resetAll();
|
Chris@0
|
444 $this->kernel->prepareLegacyRequest(\Drupal::request());
|
Chris@0
|
445
|
Chris@0
|
446 // Explicitly call register() again on the container registered in \Drupal.
|
Chris@0
|
447 // @todo This should already be called through
|
Chris@0
|
448 // DrupalKernel::prepareLegacyRequest() -> DrupalKernel::boot() but that
|
Chris@0
|
449 // appears to be calling a different container.
|
Chris@0
|
450 $this->container->get('stream_wrapper_manager')->register();
|
Chris@0
|
451 }
|
Chris@0
|
452
|
Chris@0
|
453 /**
|
Chris@0
|
454 * Returns the parameters that will be used when Simpletest installs Drupal.
|
Chris@0
|
455 *
|
Chris@0
|
456 * @see install_drupal()
|
Chris@0
|
457 * @see install_state_defaults()
|
Chris@0
|
458 *
|
Chris@0
|
459 * @return array
|
Chris@0
|
460 * Array of parameters for use in install_drupal().
|
Chris@0
|
461 */
|
Chris@0
|
462 protected function installParameters() {
|
Chris@0
|
463 $connection_info = Database::getConnectionInfo();
|
Chris@0
|
464 $driver = $connection_info['default']['driver'];
|
Chris@0
|
465 $connection_info['default']['prefix'] = $connection_info['default']['prefix']['default'];
|
Chris@0
|
466 unset($connection_info['default']['driver']);
|
Chris@0
|
467 unset($connection_info['default']['namespace']);
|
Chris@0
|
468 unset($connection_info['default']['pdo']);
|
Chris@0
|
469 unset($connection_info['default']['init_commands']);
|
Chris@0
|
470 // Remove database connection info that is not used by SQLite.
|
Chris@0
|
471 if ($driver === 'sqlite') {
|
Chris@0
|
472 unset($connection_info['default']['username']);
|
Chris@0
|
473 unset($connection_info['default']['password']);
|
Chris@0
|
474 unset($connection_info['default']['host']);
|
Chris@0
|
475 unset($connection_info['default']['port']);
|
Chris@0
|
476 }
|
Chris@0
|
477 $parameters = [
|
Chris@0
|
478 'interactive' => FALSE,
|
Chris@0
|
479 'parameters' => [
|
Chris@0
|
480 'profile' => $this->profile,
|
Chris@0
|
481 'langcode' => 'en',
|
Chris@0
|
482 ],
|
Chris@0
|
483 'forms' => [
|
Chris@0
|
484 'install_settings_form' => [
|
Chris@0
|
485 'driver' => $driver,
|
Chris@0
|
486 $driver => $connection_info['default'],
|
Chris@0
|
487 ],
|
Chris@0
|
488 'install_configure_form' => [
|
Chris@0
|
489 'site_name' => 'Drupal',
|
Chris@0
|
490 'site_mail' => 'simpletest@example.com',
|
Chris@0
|
491 'account' => [
|
Chris@0
|
492 'name' => $this->rootUser->name,
|
Chris@0
|
493 'mail' => $this->rootUser->getEmail(),
|
Chris@0
|
494 'pass' => [
|
Chris@0
|
495 'pass1' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
|
Chris@0
|
496 'pass2' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
|
Chris@0
|
497 ],
|
Chris@0
|
498 ],
|
Chris@0
|
499 // form_type_checkboxes_value() requires NULL instead of FALSE values
|
Chris@0
|
500 // for programmatic form submissions to disable a checkbox.
|
Chris@0
|
501 'enable_update_status_module' => NULL,
|
Chris@0
|
502 'enable_update_status_emails' => NULL,
|
Chris@0
|
503 ],
|
Chris@0
|
504 ],
|
Chris@0
|
505 ];
|
Chris@0
|
506
|
Chris@0
|
507 // If we only have one db driver available, we cannot set the driver.
|
Chris@0
|
508 include_once DRUPAL_ROOT . '/core/includes/install.inc';
|
Chris@0
|
509 if (count($this->getDatabaseTypes()) == 1) {
|
Chris@0
|
510 unset($parameters['forms']['install_settings_form']['driver']);
|
Chris@0
|
511 }
|
Chris@0
|
512 return $parameters;
|
Chris@0
|
513 }
|
Chris@0
|
514
|
Chris@0
|
515 /**
|
Chris@0
|
516 * Sets up the base URL based upon the environment variable.
|
Chris@0
|
517 *
|
Chris@0
|
518 * @throws \Exception
|
Chris@0
|
519 * Thrown when no SIMPLETEST_BASE_URL environment variable is provided.
|
Chris@0
|
520 */
|
Chris@0
|
521 protected function setupBaseUrl() {
|
Chris@0
|
522 global $base_url;
|
Chris@0
|
523
|
Chris@0
|
524 // Get and set the domain of the environment we are running our test
|
Chris@0
|
525 // coverage against.
|
Chris@0
|
526 $base_url = getenv('SIMPLETEST_BASE_URL');
|
Chris@0
|
527 if (!$base_url) {
|
Chris@0
|
528 throw new \Exception(
|
Chris@0
|
529 'You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.'
|
Chris@0
|
530 );
|
Chris@0
|
531 }
|
Chris@0
|
532
|
Chris@0
|
533 // Setup $_SERVER variable.
|
Chris@0
|
534 $parsed_url = parse_url($base_url);
|
Chris@0
|
535 $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
|
Chris@0
|
536 $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
|
Chris@0
|
537 $port = isset($parsed_url['port']) ? $parsed_url['port'] : 80;
|
Chris@0
|
538
|
Chris@0
|
539 $this->baseUrl = $base_url;
|
Chris@0
|
540
|
Chris@0
|
541 // If the passed URL schema is 'https' then setup the $_SERVER variables
|
Chris@0
|
542 // properly so that testing will run under HTTPS.
|
Chris@0
|
543 if ($parsed_url['scheme'] === 'https') {
|
Chris@0
|
544 $_SERVER['HTTPS'] = 'on';
|
Chris@0
|
545 }
|
Chris@0
|
546 $_SERVER['HTTP_HOST'] = $host;
|
Chris@0
|
547 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
Chris@0
|
548 $_SERVER['SERVER_ADDR'] = '127.0.0.1';
|
Chris@0
|
549 $_SERVER['SERVER_PORT'] = $port;
|
Chris@0
|
550 $_SERVER['SERVER_SOFTWARE'] = NULL;
|
Chris@0
|
551 $_SERVER['SERVER_NAME'] = 'localhost';
|
Chris@0
|
552 $_SERVER['REQUEST_URI'] = $path . '/';
|
Chris@0
|
553 $_SERVER['REQUEST_METHOD'] = 'GET';
|
Chris@0
|
554 $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
|
Chris@0
|
555 $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
|
Chris@0
|
556 $_SERVER['PHP_SELF'] = $path . '/index.php';
|
Chris@0
|
557 $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
|
Chris@0
|
558 }
|
Chris@0
|
559
|
Chris@0
|
560 /**
|
Chris@0
|
561 * Prepares the current environment for running the test.
|
Chris@0
|
562 *
|
Chris@0
|
563 * Also sets up new resources for the testing environment, such as the public
|
Chris@0
|
564 * filesystem and configuration directories.
|
Chris@0
|
565 *
|
Chris@0
|
566 * This method is private as it must only be called once by
|
Chris@0
|
567 * BrowserTestBase::setUp() (multiple invocations for the same test would have
|
Chris@0
|
568 * unpredictable consequences) and it must not be callable or overridable by
|
Chris@0
|
569 * test classes.
|
Chris@0
|
570 */
|
Chris@0
|
571 protected function prepareEnvironment() {
|
Chris@0
|
572 // Bootstrap Drupal so we can use Drupal's built in functions.
|
Chris@0
|
573 $this->classLoader = require __DIR__ . '/../../../../../autoload.php';
|
Chris@0
|
574 $request = Request::createFromGlobals();
|
Chris@0
|
575 $kernel = TestRunnerKernel::createFromRequest($request, $this->classLoader);
|
Chris@0
|
576 // TestRunnerKernel expects the working directory to be DRUPAL_ROOT.
|
Chris@0
|
577 chdir(DRUPAL_ROOT);
|
Chris@0
|
578 $kernel->prepareLegacyRequest($request);
|
Chris@0
|
579 $this->prepareDatabasePrefix();
|
Chris@0
|
580
|
Chris@0
|
581 $this->originalSite = $kernel->findSitePath($request);
|
Chris@0
|
582
|
Chris@0
|
583 // Create test directory ahead of installation so fatal errors and debug
|
Chris@0
|
584 // information can be logged during installation process.
|
Chris@0
|
585 file_prepare_directory($this->siteDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
Chris@0
|
586
|
Chris@0
|
587 // Prepare filesystem directory paths.
|
Chris@0
|
588 $this->publicFilesDirectory = $this->siteDirectory . '/files';
|
Chris@0
|
589 $this->privateFilesDirectory = $this->siteDirectory . '/private';
|
Chris@0
|
590 $this->tempFilesDirectory = $this->siteDirectory . '/temp';
|
Chris@0
|
591 $this->translationFilesDirectory = $this->siteDirectory . '/translations';
|
Chris@0
|
592
|
Chris@0
|
593 // Ensure the configImporter is refreshed for each test.
|
Chris@0
|
594 $this->configImporter = NULL;
|
Chris@0
|
595
|
Chris@0
|
596 // Unregister all custom stream wrappers of the parent site.
|
Chris@0
|
597 $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::ALL);
|
Chris@0
|
598 foreach ($wrappers as $scheme => $info) {
|
Chris@0
|
599 stream_wrapper_unregister($scheme);
|
Chris@0
|
600 }
|
Chris@0
|
601
|
Chris@0
|
602 // Reset statics.
|
Chris@0
|
603 drupal_static_reset();
|
Chris@0
|
604
|
Chris@0
|
605 $this->container = NULL;
|
Chris@0
|
606
|
Chris@0
|
607 // Unset globals.
|
Chris@0
|
608 unset($GLOBALS['config_directories']);
|
Chris@0
|
609 unset($GLOBALS['config']);
|
Chris@0
|
610 unset($GLOBALS['conf']);
|
Chris@0
|
611
|
Chris@0
|
612 // Log fatal errors.
|
Chris@0
|
613 ini_set('log_errors', 1);
|
Chris@0
|
614 ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
|
Chris@0
|
615
|
Chris@0
|
616 // Change the database prefix.
|
Chris@0
|
617 $this->changeDatabasePrefix();
|
Chris@0
|
618
|
Chris@0
|
619 // After preparing the environment and changing the database prefix, we are
|
Chris@0
|
620 // in a valid test environment.
|
Chris@0
|
621 drupal_valid_test_ua($this->databasePrefix);
|
Chris@0
|
622
|
Chris@0
|
623 // Reset settings.
|
Chris@0
|
624 new Settings([
|
Chris@0
|
625 // For performance, simply use the database prefix as hash salt.
|
Chris@0
|
626 'hash_salt' => $this->databasePrefix,
|
Chris@0
|
627 ]);
|
Chris@0
|
628
|
Chris@0
|
629 drupal_set_time_limit($this->timeLimit);
|
Chris@0
|
630
|
Chris@0
|
631 // Save and clean the shutdown callbacks array because it is static cached
|
Chris@0
|
632 // and will be changed by the test run. Otherwise it will contain callbacks
|
Chris@0
|
633 // from both environments and the testing environment will try to call the
|
Chris@0
|
634 // handlers defined by the original one.
|
Chris@0
|
635 $callbacks = &drupal_register_shutdown_function();
|
Chris@0
|
636 $this->originalShutdownCallbacks = $callbacks;
|
Chris@0
|
637 $callbacks = [];
|
Chris@0
|
638 }
|
Chris@0
|
639
|
Chris@0
|
640 /**
|
Chris@0
|
641 * Returns all supported database driver installer objects.
|
Chris@0
|
642 *
|
Chris@0
|
643 * This wraps drupal_get_database_types() for use without a current container.
|
Chris@0
|
644 *
|
Chris@0
|
645 * @return \Drupal\Core\Database\Install\Tasks[]
|
Chris@0
|
646 * An array of available database driver installer objects.
|
Chris@0
|
647 */
|
Chris@0
|
648 protected function getDatabaseTypes() {
|
Chris@0
|
649 if ($this->originalContainer) {
|
Chris@0
|
650 \Drupal::setContainer($this->originalContainer);
|
Chris@0
|
651 }
|
Chris@0
|
652 $database_types = drupal_get_database_types();
|
Chris@0
|
653 if ($this->originalContainer) {
|
Chris@0
|
654 \Drupal::unsetContainer();
|
Chris@0
|
655 }
|
Chris@0
|
656 return $database_types;
|
Chris@0
|
657 }
|
Chris@0
|
658
|
Chris@0
|
659 }
|