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