Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\simpletest;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
7 use Drupal\Component\Utility\Variable;
|
Chris@0
|
8 use Drupal\Core\Config\Development\ConfigSchemaChecker;
|
Chris@0
|
9 use Drupal\Core\Database\Database;
|
Chris@0
|
10 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
11 use Drupal\Core\DrupalKernel;
|
Chris@0
|
12 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
|
Chris@0
|
13 use Drupal\Core\Extension\ExtensionDiscovery;
|
Chris@0
|
14 use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
|
Chris@0
|
15 use Drupal\Core\Language\Language;
|
Chris@0
|
16 use Drupal\Core\Site\Settings;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\Parameter;
|
Chris@0
|
18 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
20 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Base class for functional integration tests.
|
Chris@0
|
24 *
|
Chris@0
|
25 * This base class should be useful for testing some types of integrations which
|
Chris@0
|
26 * don't require the overhead of a fully-installed Drupal instance, but which
|
Chris@0
|
27 * have many dependencies on parts of Drupal which can't or shouldn't be mocked.
|
Chris@0
|
28 *
|
Chris@0
|
29 * This base class partially boots a fixture Drupal. The state of the fixture
|
Chris@0
|
30 * Drupal is comparable to the state of a system during the early part of the
|
Chris@0
|
31 * installation process.
|
Chris@0
|
32 *
|
Chris@0
|
33 * Tests extending this base class can access services and the database, but the
|
Chris@0
|
34 * system is initially empty. This Drupal runs in a minimal mocked filesystem
|
Chris@0
|
35 * which operates within vfsStream.
|
Chris@0
|
36 *
|
Chris@0
|
37 * Modules specified in the $modules property are added to the service container
|
Chris@0
|
38 * for each test. The module/hook system is functional. Additional modules
|
Chris@0
|
39 * needed in a test should override $modules. Modules specified in this way will
|
Chris@0
|
40 * be added to those specified in superclasses.
|
Chris@0
|
41 *
|
Chris@0
|
42 * Unlike \Drupal\Tests\BrowserTestBase, the modules are not installed. They are
|
Chris@0
|
43 * loaded such that their services and hooks are available, but the install
|
Chris@0
|
44 * process has not been performed.
|
Chris@0
|
45 *
|
Chris@0
|
46 * Other modules can be made available in this way using
|
Chris@0
|
47 * KernelTestBase::enableModules().
|
Chris@0
|
48 *
|
Chris@0
|
49 * Some modules can be brought into a fully-installed state using
|
Chris@0
|
50 * KernelTestBase::installConfig(), KernelTestBase::installSchema(), and
|
Chris@0
|
51 * KernelTestBase::installEntitySchema(). Alternately, tests which need modules
|
Chris@0
|
52 * to be fully installed could inherit from \Drupal\Tests\BrowserTestBase.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @see \Drupal\Tests\KernelTestBase::$modules
|
Chris@0
|
55 * @see \Drupal\Tests\KernelTestBase::enableModules()
|
Chris@0
|
56 * @see \Drupal\Tests\KernelTestBase::installConfig()
|
Chris@0
|
57 * @see \Drupal\Tests\KernelTestBase::installEntitySchema()
|
Chris@0
|
58 * @see \Drupal\Tests\KernelTestBase::installSchema()
|
Chris@0
|
59 * @see \Drupal\Tests\BrowserTestBase
|
Chris@0
|
60 *
|
Chris@0
|
61 * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
62 * \Drupal\KernelTests\KernelTestBase instead.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @ingroup testing
|
Chris@0
|
65 */
|
Chris@0
|
66 abstract class KernelTestBase extends TestBase {
|
Chris@0
|
67
|
Chris@0
|
68 use AssertContentTrait;
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Modules to enable.
|
Chris@0
|
72 *
|
Chris@0
|
73 * Test classes extending this class, and any classes in the hierarchy up to
|
Chris@0
|
74 * this class, may specify individual lists of modules to enable by setting
|
Chris@0
|
75 * this property. The values of all properties in all classes in the hierarchy
|
Chris@0
|
76 * are merged.
|
Chris@0
|
77 *
|
Chris@0
|
78 * Any modules specified in the $modules property are automatically loaded and
|
Chris@0
|
79 * set as the fixed module list.
|
Chris@0
|
80 *
|
Chris@0
|
81 * Unlike WebTestBase::setUp(), the specified modules are loaded only, but not
|
Chris@0
|
82 * automatically installed. Modules need to be installed manually, if needed.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @see \Drupal\simpletest\KernelTestBase::enableModules()
|
Chris@0
|
85 * @see \Drupal\simpletest\KernelTestBase::setUp()
|
Chris@0
|
86 *
|
Chris@0
|
87 * @var array
|
Chris@0
|
88 */
|
Chris@0
|
89 public static $modules = [];
|
Chris@0
|
90
|
Chris@0
|
91 private $moduleFiles;
|
Chris@0
|
92 private $themeFiles;
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * The configuration directories for this test run.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @var array
|
Chris@0
|
98 */
|
Chris@0
|
99 protected $configDirectories = [];
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * A KeyValueMemoryFactory instance to use when building the container.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @var \Drupal\Core\KeyValueStore\KeyValueMemoryFactory.
|
Chris@0
|
105 */
|
Chris@0
|
106 protected $keyValueFactory;
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Array of registered stream wrappers.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @var array
|
Chris@0
|
112 */
|
Chris@0
|
113 protected $streamWrappers = [];
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * {@inheritdoc}
|
Chris@0
|
117 */
|
Chris@0
|
118 public function __construct($test_id = NULL) {
|
Chris@0
|
119 parent::__construct($test_id);
|
Chris@0
|
120 $this->skipClasses[__CLASS__] = TRUE;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * {@inheritdoc}
|
Chris@0
|
125 */
|
Chris@0
|
126 protected function beforePrepareEnvironment() {
|
Chris@0
|
127 // Copy/prime extension file lists once to avoid filesystem scans.
|
Chris@0
|
128 if (!isset($this->moduleFiles)) {
|
Chris@0
|
129 $this->moduleFiles = \Drupal::state()->get('system.module.files') ?: [];
|
Chris@0
|
130 $this->themeFiles = \Drupal::state()->get('system.theme.files') ?: [];
|
Chris@0
|
131 }
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * Create and set new configuration directories.
|
Chris@0
|
136 *
|
Chris@0
|
137 * @see config_get_config_directory()
|
Chris@0
|
138 *
|
Chris@0
|
139 * @throws \RuntimeException
|
Chris@0
|
140 * Thrown when CONFIG_SYNC_DIRECTORY cannot be created or made writable.
|
Chris@0
|
141 */
|
Chris@0
|
142 protected function prepareConfigDirectories() {
|
Chris@0
|
143 $this->configDirectories = [];
|
Chris@0
|
144 include_once DRUPAL_ROOT . '/core/includes/install.inc';
|
Chris@0
|
145 // Assign the relative path to the global variable.
|
Chris@0
|
146 $path = $this->siteDirectory . '/config_' . CONFIG_SYNC_DIRECTORY;
|
Chris@0
|
147 $GLOBALS['config_directories'][CONFIG_SYNC_DIRECTORY] = $path;
|
Chris@0
|
148 // Ensure the directory can be created and is writeable.
|
Chris@0
|
149 if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
|
Chris@0
|
150 throw new \RuntimeException("Failed to create '" . CONFIG_SYNC_DIRECTORY . "' config directory $path");
|
Chris@0
|
151 }
|
Chris@0
|
152 // Provide the already resolved path for tests.
|
Chris@0
|
153 $this->configDirectories[CONFIG_SYNC_DIRECTORY] = $path;
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * {@inheritdoc}
|
Chris@0
|
158 */
|
Chris@0
|
159 protected function setUp() {
|
Chris@0
|
160 $this->keyValueFactory = new KeyValueMemoryFactory();
|
Chris@0
|
161
|
Chris@0
|
162 // Back up settings from TestBase::prepareEnvironment().
|
Chris@0
|
163 $settings = Settings::getAll();
|
Chris@0
|
164
|
Chris@0
|
165 // Allow for test-specific overrides.
|
Chris@0
|
166 $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
|
Chris@0
|
167 $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
|
Chris@0
|
168 $container_yamls = [];
|
Chris@0
|
169 if (file_exists($settings_services_file)) {
|
Chris@0
|
170 // Copy the testing-specific service overrides in place.
|
Chris@0
|
171 $testing_services_file = $directory . '/services.yml';
|
Chris@0
|
172 copy($settings_services_file, $testing_services_file);
|
Chris@0
|
173 $container_yamls[] = $testing_services_file;
|
Chris@0
|
174 }
|
Chris@0
|
175 $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php';
|
Chris@0
|
176 if (file_exists($settings_testing_file)) {
|
Chris@0
|
177 // Copy the testing-specific settings.php overrides in place.
|
Chris@0
|
178 copy($settings_testing_file, $directory . '/settings.testing.php');
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 if (file_exists($directory . '/settings.testing.php')) {
|
Chris@0
|
182 // Add the name of the testing class to settings.php and include the
|
Chris@0
|
183 // testing specific overrides
|
Chris@0
|
184 $hash_salt = Settings::getHashSalt();
|
Chris@0
|
185 $test_class = get_class($this);
|
Chris@0
|
186 $container_yamls_export = Variable::export($container_yamls);
|
Chris@0
|
187 $php = <<<EOD
|
Chris@0
|
188 <?php
|
Chris@0
|
189
|
Chris@0
|
190 \$settings['hash_salt'] = '$hash_salt';
|
Chris@0
|
191 \$settings['container_yamls'] = $container_yamls_export;
|
Chris@0
|
192
|
Chris@0
|
193 \$test_class = '$test_class';
|
Chris@0
|
194 include DRUPAL_ROOT . '/' . \$site_path . '/settings.testing.php';
|
Chris@0
|
195 EOD;
|
Chris@0
|
196 file_put_contents($directory . '/settings.php', $php);
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 // Add this test class as a service provider.
|
Chris@0
|
200 // @todo Remove the indirection; implement ServiceProviderInterface instead.
|
Chris@0
|
201 $GLOBALS['conf']['container_service_providers']['TestServiceProvider'] = 'Drupal\simpletest\TestServiceProvider';
|
Chris@0
|
202
|
Chris@0
|
203 // Bootstrap a new kernel.
|
Chris@0
|
204 $class_loader = require DRUPAL_ROOT . '/autoload.php';
|
Chris@0
|
205 $this->kernel = new DrupalKernel('testing', $class_loader, FALSE);
|
Chris@0
|
206 $request = Request::create('/');
|
Chris@0
|
207 $site_path = DrupalKernel::findSitePath($request);
|
Chris@0
|
208 $this->kernel->setSitePath($site_path);
|
Chris@0
|
209 if (file_exists($directory . '/settings.testing.php')) {
|
Chris@0
|
210 Settings::initialize(DRUPAL_ROOT, $site_path, $class_loader);
|
Chris@0
|
211 }
|
Chris@0
|
212 $this->kernel->boot();
|
Chris@0
|
213
|
Chris@0
|
214 // Ensure database install tasks have been run.
|
Chris@0
|
215 require_once __DIR__ . '/../../../includes/install.inc';
|
Chris@0
|
216 $connection = Database::getConnection();
|
Chris@0
|
217 $errors = db_installer_object($connection->driver())->runTasks();
|
Chris@0
|
218 if (!empty($errors)) {
|
Chris@0
|
219 $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 // Reboot the kernel because the container might contain a connection to the
|
Chris@0
|
223 // database that has been closed during the database install tasks. This
|
Chris@0
|
224 // prevents any services created during the first boot from having stale
|
Chris@0
|
225 // database connections, for example, \Drupal\Core\Config\DatabaseStorage.
|
Chris@0
|
226 $this->kernel->shutdown();
|
Chris@0
|
227 $this->kernel->boot();
|
Chris@0
|
228
|
Chris@0
|
229 // Save the original site directory path, so that extensions in the
|
Chris@0
|
230 // site-specific directory can still be discovered in the test site
|
Chris@0
|
231 // environment.
|
Chris@0
|
232 // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
|
Chris@0
|
233 $settings['test_parent_site'] = $this->originalSite;
|
Chris@0
|
234
|
Chris@0
|
235 // Restore and merge settings.
|
Chris@0
|
236 // DrupalKernel::boot() initializes new Settings, and the containerBuild()
|
Chris@0
|
237 // method sets additional settings.
|
Chris@0
|
238 new Settings($settings + Settings::getAll());
|
Chris@0
|
239
|
Chris@0
|
240 // Create and set new configuration directories.
|
Chris@0
|
241 $this->prepareConfigDirectories();
|
Chris@0
|
242
|
Chris@0
|
243 // Set the request scope.
|
Chris@0
|
244 $this->container = $this->kernel->getContainer();
|
Chris@0
|
245 $this->container->get('request_stack')->push($request);
|
Chris@0
|
246
|
Chris@0
|
247 // Re-inject extension file listings into state, unless the key/value
|
Chris@0
|
248 // service was overridden (in which case its storage does not exist yet).
|
Chris@0
|
249 if ($this->container->get('keyvalue') instanceof KeyValueMemoryFactory) {
|
Chris@0
|
250 $this->container->get('state')->set('system.module.files', $this->moduleFiles);
|
Chris@0
|
251 $this->container->get('state')->set('system.theme.files', $this->themeFiles);
|
Chris@0
|
252 }
|
Chris@0
|
253
|
Chris@0
|
254 // Create a minimal core.extension configuration object so that the list of
|
Chris@0
|
255 // enabled modules can be maintained allowing
|
Chris@0
|
256 // \Drupal\Core\Config\ConfigInstaller::installDefaultConfig() to work.
|
Chris@0
|
257 // Write directly to active storage to avoid early instantiation of
|
Chris@0
|
258 // the event dispatcher which can prevent modules from registering events.
|
Chris@0
|
259 \Drupal::service('config.storage')->write('core.extension', ['module' => [], 'theme' => [], 'profile' => '']);
|
Chris@0
|
260
|
Chris@0
|
261 // Collect and set a fixed module list.
|
Chris@0
|
262 $class = get_class($this);
|
Chris@0
|
263 $modules = [];
|
Chris@0
|
264 while ($class) {
|
Chris@0
|
265 if (property_exists($class, 'modules')) {
|
Chris@0
|
266 // Only add the modules, if the $modules property was not inherited.
|
Chris@0
|
267 $rp = new \ReflectionProperty($class, 'modules');
|
Chris@0
|
268 if ($rp->class == $class) {
|
Chris@0
|
269 $modules[$class] = $class::$modules;
|
Chris@0
|
270 }
|
Chris@0
|
271 }
|
Chris@0
|
272 $class = get_parent_class($class);
|
Chris@0
|
273 }
|
Chris@0
|
274 // Modules have been collected in reverse class hierarchy order; modules
|
Chris@0
|
275 // defined by base classes should be sorted first. Then, merge the results
|
Chris@0
|
276 // together.
|
Chris@0
|
277 $modules = array_reverse($modules);
|
Chris@0
|
278 $modules = call_user_func_array('array_merge_recursive', $modules);
|
Chris@0
|
279 if ($modules) {
|
Chris@0
|
280 $this->enableModules($modules);
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 // Tests based on this class are entitled to use Drupal's File and
|
Chris@0
|
284 // StreamWrapper APIs.
|
Chris@0
|
285 // @todo Move StreamWrapper management into DrupalKernel.
|
Chris@0
|
286 // @see https://www.drupal.org/node/2028109
|
Chris@0
|
287 file_prepare_directory($this->publicFilesDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
Chris@0
|
288 $this->settingsSet('file_public_path', $this->publicFilesDirectory);
|
Chris@0
|
289 $this->streamWrappers = [];
|
Chris@0
|
290 $this->registerStreamWrapper('public', 'Drupal\Core\StreamWrapper\PublicStream');
|
Chris@0
|
291 // The temporary stream wrapper is able to operate both with and without
|
Chris@0
|
292 // configuration.
|
Chris@0
|
293 $this->registerStreamWrapper('temporary', 'Drupal\Core\StreamWrapper\TemporaryStream');
|
Chris@0
|
294
|
Chris@0
|
295 // Manually configure the test mail collector implementation to prevent
|
Chris@0
|
296 // tests from sending out emails and collect them in state instead.
|
Chris@0
|
297 // While this should be enforced via settings.php prior to installation,
|
Chris@0
|
298 // some tests expect to be able to test mail system implementations.
|
Chris@0
|
299 $GLOBALS['config']['system.mail']['interface']['default'] = 'test_mail_collector';
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 /**
|
Chris@0
|
303 * {@inheritdoc}
|
Chris@0
|
304 */
|
Chris@0
|
305 protected function tearDown() {
|
Chris@0
|
306 if ($this->kernel instanceof DrupalKernel) {
|
Chris@0
|
307 $this->kernel->shutdown();
|
Chris@0
|
308 }
|
Chris@0
|
309 // Before tearing down the test environment, ensure that no stream wrapper
|
Chris@0
|
310 // of this test leaks into the parent environment. Unlike all other global
|
Chris@0
|
311 // state variables in Drupal, stream wrappers are a global state construct
|
Chris@0
|
312 // of PHP core, which has to be maintained manually.
|
Chris@0
|
313 // @todo Move StreamWrapper management into DrupalKernel.
|
Chris@0
|
314 // @see https://www.drupal.org/node/2028109
|
Chris@0
|
315 foreach ($this->streamWrappers as $scheme => $type) {
|
Chris@0
|
316 $this->unregisterStreamWrapper($scheme, $type);
|
Chris@0
|
317 }
|
Chris@0
|
318 parent::tearDown();
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 /**
|
Chris@0
|
322 * Sets up the base service container for this test.
|
Chris@0
|
323 *
|
Chris@0
|
324 * Extend this method in your test to register additional service overrides
|
Chris@0
|
325 * that need to persist a DrupalKernel reboot. This method is called whenever
|
Chris@0
|
326 * the kernel is rebuilt.
|
Chris@0
|
327 *
|
Chris@0
|
328 * @see \Drupal\simpletest\KernelTestBase::setUp()
|
Chris@0
|
329 * @see \Drupal\simpletest\KernelTestBase::enableModules()
|
Chris@0
|
330 * @see \Drupal\simpletest\KernelTestBase::disableModules()
|
Chris@0
|
331 */
|
Chris@0
|
332 public function containerBuild(ContainerBuilder $container) {
|
Chris@0
|
333 // Keep the container object around for tests.
|
Chris@0
|
334 $this->container = $container;
|
Chris@0
|
335
|
Chris@0
|
336 // Set the default language on the minimal container.
|
Chris@0
|
337 $this->container->setParameter('language.default_values', $this->defaultLanguageData());
|
Chris@0
|
338
|
Chris@0
|
339 $container->register('lock', 'Drupal\Core\Lock\NullLockBackend');
|
Chris@0
|
340 $container->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');
|
Chris@0
|
341
|
Chris@0
|
342 $container
|
Chris@0
|
343 ->register('config.storage', 'Drupal\Core\Config\DatabaseStorage')
|
Chris@0
|
344 ->addArgument(Database::getConnection())
|
Chris@0
|
345 ->addArgument('config');
|
Chris@0
|
346
|
Chris@0
|
347 if ($this->strictConfigSchema) {
|
Chris@0
|
348 $container
|
Chris@0
|
349 ->register('simpletest.config_schema_checker', ConfigSchemaChecker::class)
|
Chris@0
|
350 ->addArgument(new Reference('config.typed'))
|
Chris@0
|
351 ->addArgument($this->getConfigSchemaExclusions())
|
Chris@0
|
352 ->addTag('event_subscriber');
|
Chris@0
|
353 }
|
Chris@0
|
354
|
Chris@0
|
355 $keyvalue_options = $container->getParameter('factory.keyvalue') ?: [];
|
Chris@0
|
356 $keyvalue_options['default'] = 'keyvalue.memory';
|
Chris@0
|
357 $container->setParameter('factory.keyvalue', $keyvalue_options);
|
Chris@0
|
358 $container->set('keyvalue.memory', $this->keyValueFactory);
|
Chris@0
|
359 if (!$container->has('keyvalue')) {
|
Chris@0
|
360 // TestBase::setUp puts a completely empty container in
|
Chris@0
|
361 // $this->container which is somewhat the mirror of the empty
|
Chris@0
|
362 // environment being set up. Unit tests need not to waste time with
|
Chris@0
|
363 // getting a container set up for them. Drupal Unit Tests might just get
|
Chris@0
|
364 // away with a simple container holding the absolute bare minimum. When
|
Chris@0
|
365 // a kernel is overridden then there's no need to re-register the keyvalue
|
Chris@0
|
366 // service but when a test is happy with the superminimal container put
|
Chris@0
|
367 // together here, it still might a keyvalue storage for anything using
|
Chris@0
|
368 // \Drupal::state() -- that's why a memory service was added in the first
|
Chris@0
|
369 // place.
|
Chris@0
|
370 $container->register('settings', 'Drupal\Core\Site\Settings')
|
Chris@0
|
371 ->setFactoryClass('Drupal\Core\Site\Settings')
|
Chris@0
|
372 ->setFactoryMethod('getInstance');
|
Chris@0
|
373
|
Chris@0
|
374 $container
|
Chris@0
|
375 ->register('keyvalue', 'Drupal\Core\KeyValueStore\KeyValueFactory')
|
Chris@0
|
376 ->addArgument(new Reference('service_container'))
|
Chris@0
|
377 ->addArgument(new Parameter('factory.keyvalue'));
|
Chris@0
|
378
|
Chris@0
|
379 $container->register('state', 'Drupal\Core\State\State')
|
Chris@0
|
380 ->addArgument(new Reference('keyvalue'));
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@0
|
383 if ($container->hasDefinition('path_processor_alias')) {
|
Chris@0
|
384 // Prevent the alias-based path processor, which requires a url_alias db
|
Chris@0
|
385 // table, from being registered to the path processor manager. We do this
|
Chris@0
|
386 // by removing the tags that the compiler pass looks for. This means the
|
Chris@0
|
387 // url generator can safely be used within tests.
|
Chris@0
|
388 $definition = $container->getDefinition('path_processor_alias');
|
Chris@0
|
389 $definition->clearTag('path_processor_inbound')->clearTag('path_processor_outbound');
|
Chris@0
|
390 }
|
Chris@0
|
391
|
Chris@0
|
392 if ($container->hasDefinition('password')) {
|
Chris@0
|
393 $container->getDefinition('password')->setArguments([1]);
|
Chris@0
|
394 }
|
Chris@0
|
395
|
Chris@0
|
396 // Register the stream wrapper manager.
|
Chris@0
|
397 $container
|
Chris@0
|
398 ->register('stream_wrapper_manager', 'Drupal\Core\StreamWrapper\StreamWrapperManager')
|
Chris@0
|
399 ->addArgument(new Reference('module_handler'))
|
Chris@0
|
400 ->addMethodCall('setContainer', [new Reference('service_container')]);
|
Chris@0
|
401
|
Chris@0
|
402 $request = Request::create('/');
|
Chris@0
|
403 $container->get('request_stack')->push($request);
|
Chris@0
|
404 }
|
Chris@0
|
405
|
Chris@0
|
406 /**
|
Chris@0
|
407 * Provides the data for setting the default language on the container.
|
Chris@0
|
408 *
|
Chris@0
|
409 * @return array
|
Chris@0
|
410 * The data array for the default language.
|
Chris@0
|
411 */
|
Chris@0
|
412 protected function defaultLanguageData() {
|
Chris@0
|
413 return Language::$defaultValues;
|
Chris@0
|
414 }
|
Chris@0
|
415
|
Chris@0
|
416 /**
|
Chris@0
|
417 * Installs default configuration for a given list of modules.
|
Chris@0
|
418 *
|
Chris@0
|
419 * @param array $modules
|
Chris@0
|
420 * A list of modules for which to install default configuration.
|
Chris@0
|
421 *
|
Chris@0
|
422 * @throws \RuntimeException
|
Chris@0
|
423 * Thrown when any module listed in $modules is not enabled.
|
Chris@0
|
424 */
|
Chris@0
|
425 protected function installConfig(array $modules) {
|
Chris@0
|
426 foreach ($modules as $module) {
|
Chris@0
|
427 if (!$this->container->get('module_handler')->moduleExists($module)) {
|
Chris@0
|
428 throw new \RuntimeException("'$module' module is not enabled");
|
Chris@0
|
429 }
|
Chris@0
|
430 \Drupal::service('config.installer')->installDefaultConfig('module', $module);
|
Chris@0
|
431 }
|
Chris@0
|
432 $this->pass(format_string('Installed default config: %modules.', [
|
Chris@0
|
433 '%modules' => implode(', ', $modules),
|
Chris@0
|
434 ]));
|
Chris@0
|
435 }
|
Chris@0
|
436
|
Chris@0
|
437 /**
|
Chris@0
|
438 * Installs a specific table from a module schema definition.
|
Chris@0
|
439 *
|
Chris@0
|
440 * @param string $module
|
Chris@0
|
441 * The name of the module that defines the table's schema.
|
Chris@0
|
442 * @param string|array $tables
|
Chris@0
|
443 * The name or an array of the names of the tables to install.
|
Chris@0
|
444 *
|
Chris@0
|
445 * @throws \RuntimeException
|
Chris@0
|
446 * Thrown when $module is not enabled or when the table schema cannot be
|
Chris@0
|
447 * found in the module specified.
|
Chris@0
|
448 */
|
Chris@0
|
449 protected function installSchema($module, $tables) {
|
Chris@0
|
450 // drupal_get_module_schema() is technically able to install a schema
|
Chris@0
|
451 // of a non-enabled module, but its ability to load the module's .install
|
Chris@0
|
452 // file depends on many other factors. To prevent differences in test
|
Chris@0
|
453 // behavior and non-reproducible test failures, we only allow the schema of
|
Chris@0
|
454 // explicitly loaded/enabled modules to be installed.
|
Chris@0
|
455 if (!$this->container->get('module_handler')->moduleExists($module)) {
|
Chris@0
|
456 throw new \RuntimeException("'$module' module is not enabled");
|
Chris@0
|
457 }
|
Chris@0
|
458
|
Chris@0
|
459 $tables = (array) $tables;
|
Chris@0
|
460 foreach ($tables as $table) {
|
Chris@0
|
461 $schema = drupal_get_module_schema($module, $table);
|
Chris@0
|
462 if (empty($schema)) {
|
Chris@0
|
463 // BC layer to avoid some contrib tests to fail.
|
Chris@0
|
464 // @todo Remove the BC layer before 8.1.x release.
|
Chris@0
|
465 // @see https://www.drupal.org/node/2670360
|
Chris@0
|
466 // @see https://www.drupal.org/node/2670454
|
Chris@0
|
467 if ($module == 'system') {
|
Chris@0
|
468 continue;
|
Chris@0
|
469 }
|
Chris@0
|
470 throw new \RuntimeException("Unknown '$table' table schema in '$module' module.");
|
Chris@0
|
471 }
|
Chris@0
|
472 $this->container->get('database')->schema()->createTable($table, $schema);
|
Chris@0
|
473 }
|
Chris@0
|
474 $this->pass(format_string('Installed %module tables: %tables.', [
|
Chris@0
|
475 '%tables' => '{' . implode('}, {', $tables) . '}',
|
Chris@0
|
476 '%module' => $module,
|
Chris@0
|
477 ]));
|
Chris@0
|
478 }
|
Chris@0
|
479
|
Chris@0
|
480
|
Chris@0
|
481 /**
|
Chris@0
|
482 * Installs the storage schema for a specific entity type.
|
Chris@0
|
483 *
|
Chris@0
|
484 * @param string $entity_type_id
|
Chris@0
|
485 * The ID of the entity type.
|
Chris@0
|
486 */
|
Chris@0
|
487 protected function installEntitySchema($entity_type_id) {
|
Chris@0
|
488 /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
|
Chris@0
|
489 $entity_manager = $this->container->get('entity.manager');
|
Chris@0
|
490 $entity_type = $entity_manager->getDefinition($entity_type_id);
|
Chris@0
|
491 $entity_manager->onEntityTypeCreate($entity_type);
|
Chris@0
|
492
|
Chris@0
|
493 // For test runs, the most common storage backend is a SQL database. For
|
Chris@0
|
494 // this case, ensure the tables got created.
|
Chris@0
|
495 $storage = $entity_manager->getStorage($entity_type_id);
|
Chris@0
|
496 if ($storage instanceof SqlEntityStorageInterface) {
|
Chris@0
|
497 $tables = $storage->getTableMapping()->getTableNames();
|
Chris@0
|
498 $db_schema = $this->container->get('database')->schema();
|
Chris@0
|
499 $all_tables_exist = TRUE;
|
Chris@0
|
500 foreach ($tables as $table) {
|
Chris@0
|
501 if (!$db_schema->tableExists($table)) {
|
Chris@0
|
502 $this->fail(SafeMarkup::format('Installed entity type table for the %entity_type entity type: %table', [
|
Chris@0
|
503 '%entity_type' => $entity_type_id,
|
Chris@0
|
504 '%table' => $table,
|
Chris@0
|
505 ]));
|
Chris@0
|
506 $all_tables_exist = FALSE;
|
Chris@0
|
507 }
|
Chris@0
|
508 }
|
Chris@0
|
509 if ($all_tables_exist) {
|
Chris@0
|
510 $this->pass(SafeMarkup::format('Installed entity type tables for the %entity_type entity type: %tables', [
|
Chris@0
|
511 '%entity_type' => $entity_type_id,
|
Chris@0
|
512 '%tables' => '{' . implode('}, {', $tables) . '}',
|
Chris@0
|
513 ]));
|
Chris@0
|
514 }
|
Chris@0
|
515 }
|
Chris@0
|
516 }
|
Chris@0
|
517
|
Chris@0
|
518 /**
|
Chris@0
|
519 * Enables modules for this test.
|
Chris@0
|
520 *
|
Chris@0
|
521 * To install test modules outside of the testing environment, add
|
Chris@0
|
522 * @code
|
Chris@0
|
523 * $settings['extension_discovery_scan_tests'] = TRUE;
|
Chris@0
|
524 * @endcode
|
Chris@0
|
525 * to your settings.php.
|
Chris@0
|
526 *
|
Chris@0
|
527 * @param array $modules
|
Chris@0
|
528 * A list of modules to enable. Dependencies are not resolved; i.e.,
|
Chris@0
|
529 * multiple modules have to be specified with dependent modules first.
|
Chris@0
|
530 * The new modules are only added to the active module list and loaded.
|
Chris@0
|
531 */
|
Chris@0
|
532 protected function enableModules(array $modules) {
|
Chris@0
|
533 // Perform an ExtensionDiscovery scan as this function may receive a
|
Chris@0
|
534 // profile that is not the current profile, and we don't yet have a cached
|
Chris@0
|
535 // way to receive inactive profile information.
|
Chris@0
|
536 // @todo Remove as part of https://www.drupal.org/node/2186491
|
Chris@0
|
537 $listing = new ExtensionDiscovery(\Drupal::root());
|
Chris@0
|
538 $module_list = $listing->scan('module');
|
Chris@0
|
539 // In ModuleHandlerTest we pass in a profile as if it were a module.
|
Chris@0
|
540 $module_list += $listing->scan('profile');
|
Chris@0
|
541 // Set the list of modules in the extension handler.
|
Chris@0
|
542 $module_handler = $this->container->get('module_handler');
|
Chris@0
|
543
|
Chris@0
|
544 // Write directly to active storage to avoid early instantiation of
|
Chris@0
|
545 // the event dispatcher which can prevent modules from registering events.
|
Chris@0
|
546 $active_storage = \Drupal::service('config.storage');
|
Chris@0
|
547 $extensions = $active_storage->read('core.extension');
|
Chris@0
|
548
|
Chris@0
|
549 foreach ($modules as $module) {
|
Chris@0
|
550 $module_handler->addModule($module, $module_list[$module]->getPath());
|
Chris@0
|
551 // Maintain the list of enabled modules in configuration.
|
Chris@0
|
552 $extensions['module'][$module] = 0;
|
Chris@0
|
553 }
|
Chris@0
|
554 $active_storage->write('core.extension', $extensions);
|
Chris@0
|
555
|
Chris@0
|
556 // Update the kernel to make their services available.
|
Chris@0
|
557 $module_filenames = $module_handler->getModuleList();
|
Chris@0
|
558 $this->kernel->updateModules($module_filenames, $module_filenames);
|
Chris@0
|
559
|
Chris@0
|
560 // Ensure isLoaded() is TRUE in order to make
|
Chris@0
|
561 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
|
Chris@0
|
562 // Note that the kernel has rebuilt the container; this $module_handler is
|
Chris@0
|
563 // no longer the $module_handler instance from above.
|
Chris@0
|
564 $this->container->get('module_handler')->reload();
|
Chris@0
|
565 $this->pass(format_string('Enabled modules: %modules.', [
|
Chris@0
|
566 '%modules' => implode(', ', $modules),
|
Chris@0
|
567 ]));
|
Chris@0
|
568 }
|
Chris@0
|
569
|
Chris@0
|
570 /**
|
Chris@0
|
571 * Disables modules for this test.
|
Chris@0
|
572 *
|
Chris@0
|
573 * @param array $modules
|
Chris@0
|
574 * A list of modules to disable. Dependencies are not resolved; i.e.,
|
Chris@0
|
575 * multiple modules have to be specified with dependent modules first.
|
Chris@0
|
576 * Code of previously active modules is still loaded. The modules are only
|
Chris@0
|
577 * removed from the active module list.
|
Chris@0
|
578 */
|
Chris@0
|
579 protected function disableModules(array $modules) {
|
Chris@0
|
580 // Unset the list of modules in the extension handler.
|
Chris@0
|
581 $module_handler = $this->container->get('module_handler');
|
Chris@0
|
582 $module_filenames = $module_handler->getModuleList();
|
Chris@0
|
583 $extension_config = $this->config('core.extension');
|
Chris@0
|
584 foreach ($modules as $module) {
|
Chris@0
|
585 unset($module_filenames[$module]);
|
Chris@0
|
586 $extension_config->clear('module.' . $module);
|
Chris@0
|
587 }
|
Chris@0
|
588 $extension_config->save();
|
Chris@0
|
589 $module_handler->setModuleList($module_filenames);
|
Chris@0
|
590 $module_handler->resetImplementations();
|
Chris@0
|
591 // Update the kernel to remove their services.
|
Chris@0
|
592 $this->kernel->updateModules($module_filenames, $module_filenames);
|
Chris@0
|
593
|
Chris@0
|
594 // Ensure isLoaded() is TRUE in order to make
|
Chris@0
|
595 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
|
Chris@0
|
596 // Note that the kernel has rebuilt the container; this $module_handler is
|
Chris@0
|
597 // no longer the $module_handler instance from above.
|
Chris@0
|
598 $module_handler = $this->container->get('module_handler');
|
Chris@0
|
599 $module_handler->reload();
|
Chris@0
|
600 $this->pass(format_string('Disabled modules: %modules.', [
|
Chris@0
|
601 '%modules' => implode(', ', $modules),
|
Chris@0
|
602 ]));
|
Chris@0
|
603 }
|
Chris@0
|
604
|
Chris@0
|
605 /**
|
Chris@0
|
606 * Registers a stream wrapper for this test.
|
Chris@0
|
607 *
|
Chris@0
|
608 * @param string $scheme
|
Chris@0
|
609 * The scheme to register.
|
Chris@0
|
610 * @param string $class
|
Chris@0
|
611 * The fully qualified class name to register.
|
Chris@0
|
612 * @param int $type
|
Chris@0
|
613 * The Drupal Stream Wrapper API type. Defaults to
|
Chris@0
|
614 * StreamWrapperInterface::NORMAL.
|
Chris@0
|
615 */
|
Chris@0
|
616 protected function registerStreamWrapper($scheme, $class, $type = StreamWrapperInterface::NORMAL) {
|
Chris@0
|
617 $this->container->get('stream_wrapper_manager')->registerWrapper($scheme, $class, $type);
|
Chris@0
|
618 }
|
Chris@0
|
619
|
Chris@0
|
620 /**
|
Chris@0
|
621 * Renders a render array.
|
Chris@0
|
622 *
|
Chris@0
|
623 * @param array $elements
|
Chris@0
|
624 * The elements to render.
|
Chris@0
|
625 *
|
Chris@0
|
626 * @return string
|
Chris@0
|
627 * The rendered string output (typically HTML).
|
Chris@0
|
628 */
|
Chris@0
|
629 protected function render(array &$elements) {
|
Chris@0
|
630 // Use the bare HTML page renderer to render our links.
|
Chris@0
|
631 $renderer = $this->container->get('bare_html_page_renderer');
|
Chris@0
|
632 $response = $renderer->renderBarePage($elements, '', 'maintenance_page');
|
Chris@0
|
633
|
Chris@0
|
634 // Glean the content from the response object.
|
Chris@0
|
635 $content = $response->getContent();
|
Chris@0
|
636 $this->setRawContent($content);
|
Chris@0
|
637 $this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
|
Chris@0
|
638 return $content;
|
Chris@0
|
639 }
|
Chris@0
|
640
|
Chris@0
|
641 }
|