Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\KernelTests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\FileCache\ApcuFileCacheBackend;
|
Chris@0
|
6 use Drupal\Component\FileCache\FileCache;
|
Chris@0
|
7 use Drupal\Component\FileCache\FileCacheFactory;
|
Chris@0
|
8 use Drupal\Component\Utility\Html;
|
Chris@17
|
9 use Drupal\Component\Render\FormattableMarkup;
|
Chris@0
|
10 use Drupal\Core\Config\Development\ConfigSchemaChecker;
|
Chris@0
|
11 use Drupal\Core\Database\Database;
|
Chris@0
|
12 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
13 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
|
Chris@0
|
14 use Drupal\Core\DrupalKernel;
|
Chris@0
|
15 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
|
Chris@0
|
16 use Drupal\Core\Extension\ExtensionDiscovery;
|
Chris@0
|
17 use Drupal\Core\Language\Language;
|
Chris@0
|
18 use Drupal\Core\Site\Settings;
|
Chris@0
|
19 use Drupal\Core\Test\TestDatabase;
|
Chris@0
|
20 use Drupal\Tests\AssertHelperTrait;
|
Chris@0
|
21 use Drupal\Tests\ConfigTestTrait;
|
Chris@12
|
22 use Drupal\Tests\PhpunitCompatibilityTrait;
|
Chris@0
|
23 use Drupal\Tests\RandomGeneratorTrait;
|
Chris@0
|
24 use Drupal\Tests\TestRequirementsTrait;
|
Chris@0
|
25 use PHPUnit\Framework\TestCase;
|
Chris@0
|
26 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
27 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
28 use org\bovigo\vfs\vfsStream;
|
Chris@0
|
29 use org\bovigo\vfs\visitor\vfsStreamPrintVisitor;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Base class for functional integration tests.
|
Chris@0
|
33 *
|
Chris@0
|
34 * This base class should be useful for testing some types of integrations which
|
Chris@0
|
35 * don't require the overhead of a fully-installed Drupal instance, but which
|
Chris@0
|
36 * have many dependencies on parts of Drupal which can't or shouldn't be mocked.
|
Chris@0
|
37 *
|
Chris@0
|
38 * This base class partially boots a fixture Drupal. The state of the fixture
|
Chris@0
|
39 * Drupal is comparable to the state of a system during the early part of the
|
Chris@0
|
40 * installation process.
|
Chris@0
|
41 *
|
Chris@0
|
42 * Tests extending this base class can access services and the database, but the
|
Chris@0
|
43 * system is initially empty. This Drupal runs in a minimal mocked filesystem
|
Chris@0
|
44 * which operates within vfsStream.
|
Chris@0
|
45 *
|
Chris@0
|
46 * Modules specified in the $modules property are added to the service container
|
Chris@0
|
47 * for each test. The module/hook system is functional. Additional modules
|
Chris@0
|
48 * needed in a test should override $modules. Modules specified in this way will
|
Chris@0
|
49 * be added to those specified in superclasses.
|
Chris@0
|
50 *
|
Chris@0
|
51 * Unlike \Drupal\Tests\BrowserTestBase, the modules are not installed. They are
|
Chris@0
|
52 * loaded such that their services and hooks are available, but the install
|
Chris@0
|
53 * process has not been performed.
|
Chris@0
|
54 *
|
Chris@0
|
55 * Other modules can be made available in this way using
|
Chris@0
|
56 * KernelTestBase::enableModules().
|
Chris@0
|
57 *
|
Chris@0
|
58 * Some modules can be brought into a fully-installed state using
|
Chris@0
|
59 * KernelTestBase::installConfig(), KernelTestBase::installSchema(), and
|
Chris@0
|
60 * KernelTestBase::installEntitySchema(). Alternately, tests which need modules
|
Chris@0
|
61 * to be fully installed could inherit from \Drupal\Tests\BrowserTestBase.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @see \Drupal\Tests\KernelTestBase::$modules
|
Chris@0
|
64 * @see \Drupal\Tests\KernelTestBase::enableModules()
|
Chris@0
|
65 * @see \Drupal\Tests\KernelTestBase::installConfig()
|
Chris@0
|
66 * @see \Drupal\Tests\KernelTestBase::installEntitySchema()
|
Chris@0
|
67 * @see \Drupal\Tests\KernelTestBase::installSchema()
|
Chris@0
|
68 * @see \Drupal\Tests\BrowserTestBase
|
Chris@0
|
69 */
|
Chris@0
|
70 abstract class KernelTestBase extends TestCase implements ServiceProviderInterface {
|
Chris@0
|
71
|
Chris@0
|
72 use AssertLegacyTrait;
|
Chris@0
|
73 use AssertContentTrait;
|
Chris@0
|
74 use AssertHelperTrait;
|
Chris@0
|
75 use RandomGeneratorTrait;
|
Chris@0
|
76 use ConfigTestTrait;
|
Chris@0
|
77 use TestRequirementsTrait;
|
Chris@12
|
78 use PhpunitCompatibilityTrait;
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * {@inheritdoc}
|
Chris@0
|
82 *
|
Chris@0
|
83 * Back up and restore any global variables that may be changed by tests.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @see self::runTestInSeparateProcess
|
Chris@0
|
86 */
|
Chris@0
|
87 protected $backupGlobals = TRUE;
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 *
|
Chris@0
|
92 * Kernel tests are run in separate processes because they allow autoloading
|
Chris@0
|
93 * of code from extensions. Running the test in a separate process isolates
|
Chris@0
|
94 * this behavior from other tests. Subclasses should not override this
|
Chris@0
|
95 * property.
|
Chris@0
|
96 */
|
Chris@0
|
97 protected $runTestInSeparateProcess = TRUE;
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * {@inheritdoc}
|
Chris@0
|
101 *
|
Chris@0
|
102 * Back up and restore static class properties that may be changed by tests.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @see self::runTestInSeparateProcess
|
Chris@0
|
105 */
|
Chris@0
|
106 protected $backupStaticAttributes = TRUE;
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * {@inheritdoc}
|
Chris@0
|
110 *
|
Chris@0
|
111 * Contains a few static class properties for performance.
|
Chris@0
|
112 */
|
Chris@0
|
113 protected $backupStaticAttributesBlacklist = [
|
Chris@0
|
114 // Ignore static discovery/parser caches to speed up tests.
|
Chris@0
|
115 'Drupal\Component\Discovery\YamlDiscovery' => ['parsedFiles'],
|
Chris@0
|
116 'Drupal\Core\DependencyInjection\YamlFileLoader' => ['yaml'],
|
Chris@0
|
117 'Drupal\Core\Extension\ExtensionDiscovery' => ['files'],
|
Chris@0
|
118 'Drupal\Core\Extension\InfoParser' => ['parsedInfos'],
|
Chris@0
|
119 // Drupal::$container cannot be serialized.
|
Chris@0
|
120 'Drupal' => ['container'],
|
Chris@0
|
121 // Settings cannot be serialized.
|
Chris@0
|
122 'Drupal\Core\Site\Settings' => ['instance'],
|
Chris@0
|
123 ];
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 *
|
Chris@0
|
128 * Do not forward any global state from the parent process to the processes
|
Chris@0
|
129 * that run the actual tests.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @see self::runTestInSeparateProcess
|
Chris@0
|
132 */
|
Chris@0
|
133 protected $preserveGlobalState = FALSE;
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * @var \Composer\Autoload\Classloader
|
Chris@0
|
137 */
|
Chris@0
|
138 protected $classLoader;
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * @var string
|
Chris@0
|
142 */
|
Chris@0
|
143 protected $siteDirectory;
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * @var string
|
Chris@0
|
147 */
|
Chris@0
|
148 protected $databasePrefix;
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
|
Chris@0
|
152 */
|
Chris@0
|
153 protected $container;
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * Modules to enable.
|
Chris@0
|
157 *
|
Chris@0
|
158 * The test runner will merge the $modules lists from this class, the class
|
Chris@0
|
159 * it extends, and so on up the class hierarchy. It is not necessary to
|
Chris@0
|
160 * include modules in your list that a parent class has already declared.
|
Chris@0
|
161 *
|
Chris@0
|
162 * @see \Drupal\Tests\KernelTestBase::enableModules()
|
Chris@0
|
163 * @see \Drupal\Tests\KernelTestBase::bootKernel()
|
Chris@0
|
164 *
|
Chris@0
|
165 * @var array
|
Chris@0
|
166 */
|
Chris@0
|
167 protected static $modules = [];
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * The virtual filesystem root directory.
|
Chris@0
|
171 *
|
Chris@0
|
172 * @var \org\bovigo\vfs\vfsStreamDirectory
|
Chris@0
|
173 */
|
Chris@0
|
174 protected $vfsRoot;
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * @var int
|
Chris@0
|
178 */
|
Chris@0
|
179 protected $expectedLogSeverity;
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * @var string
|
Chris@0
|
183 */
|
Chris@0
|
184 protected $expectedLogMessage;
|
Chris@0
|
185
|
Chris@0
|
186 /**
|
Chris@0
|
187 * @todo Move into Config test base class.
|
Chris@0
|
188 * @var \Drupal\Core\Config\ConfigImporter
|
Chris@0
|
189 */
|
Chris@0
|
190 protected $configImporter;
|
Chris@0
|
191
|
Chris@0
|
192 /**
|
Chris@0
|
193 * The app root.
|
Chris@0
|
194 *
|
Chris@0
|
195 * @var string
|
Chris@0
|
196 */
|
Chris@0
|
197 protected $root;
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 * Set to TRUE to strict check all configuration saved.
|
Chris@0
|
201 *
|
Chris@0
|
202 * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
|
Chris@0
|
203 *
|
Chris@0
|
204 * @var bool
|
Chris@0
|
205 */
|
Chris@0
|
206 protected $strictConfigSchema = TRUE;
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * An array of config object names that are excluded from schema checking.
|
Chris@0
|
210 *
|
Chris@0
|
211 * @var string[]
|
Chris@0
|
212 */
|
Chris@0
|
213 protected static $configSchemaCheckerExclusions = [
|
Chris@0
|
214 // Following are used to test lack of or partial schema. Where partial
|
Chris@0
|
215 // schema is provided, that is explicitly tested in specific tests.
|
Chris@0
|
216 'config_schema_test.noschema',
|
Chris@0
|
217 'config_schema_test.someschema',
|
Chris@0
|
218 'config_schema_test.schema_data_types',
|
Chris@0
|
219 'config_schema_test.no_schema_data_types',
|
Chris@0
|
220 // Used to test application of schema to filtering of configuration.
|
Chris@0
|
221 'config_test.dynamic.system',
|
Chris@0
|
222 ];
|
Chris@0
|
223
|
Chris@0
|
224 /**
|
Chris@0
|
225 * {@inheritdoc}
|
Chris@0
|
226 */
|
Chris@0
|
227 public static function setUpBeforeClass() {
|
Chris@0
|
228 parent::setUpBeforeClass();
|
Chris@0
|
229
|
Chris@0
|
230 // Change the current dir to DRUPAL_ROOT.
|
Chris@0
|
231 chdir(static::getDrupalRoot());
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 /**
|
Chris@0
|
235 * {@inheritdoc}
|
Chris@0
|
236 */
|
Chris@0
|
237 protected function setUp() {
|
Chris@0
|
238 parent::setUp();
|
Chris@0
|
239
|
Chris@0
|
240 $this->root = static::getDrupalRoot();
|
Chris@0
|
241 $this->initFileCache();
|
Chris@0
|
242 $this->bootEnvironment();
|
Chris@0
|
243 $this->bootKernel();
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 /**
|
Chris@0
|
247 * Bootstraps a basic test environment.
|
Chris@0
|
248 *
|
Chris@0
|
249 * Should not be called by tests. Only visible for DrupalKernel integration
|
Chris@0
|
250 * tests.
|
Chris@0
|
251 *
|
Chris@14
|
252 * @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
|
Chris@0
|
253 * @internal
|
Chris@0
|
254 */
|
Chris@0
|
255 protected function bootEnvironment() {
|
Chris@0
|
256 $this->streamWrappers = [];
|
Chris@0
|
257 \Drupal::unsetContainer();
|
Chris@0
|
258
|
Chris@0
|
259 $this->classLoader = require $this->root . '/autoload.php';
|
Chris@0
|
260
|
Chris@0
|
261 require_once $this->root . '/core/includes/bootstrap.inc';
|
Chris@0
|
262
|
Chris@0
|
263 // Set up virtual filesystem.
|
Chris@0
|
264 Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
|
Chris@0
|
265 $test_db = new TestDatabase();
|
Chris@0
|
266 $this->siteDirectory = $test_db->getTestSitePath();
|
Chris@0
|
267
|
Chris@0
|
268 // Ensure that all code that relies on drupal_valid_test_ua() can still be
|
Chris@0
|
269 // safely executed. This primarily affects the (test) site directory
|
Chris@0
|
270 // resolution (used by e.g. LocalStream and PhpStorage).
|
Chris@0
|
271 $this->databasePrefix = $test_db->getDatabasePrefix();
|
Chris@0
|
272 drupal_valid_test_ua($this->databasePrefix);
|
Chris@0
|
273
|
Chris@0
|
274 $settings = [
|
Chris@0
|
275 'hash_salt' => get_class($this),
|
Chris@0
|
276 'file_public_path' => $this->siteDirectory . '/files',
|
Chris@0
|
277 // Disable Twig template caching/dumping.
|
Chris@0
|
278 'twig_cache' => FALSE,
|
Chris@0
|
279 // @see \Drupal\KernelTests\KernelTestBase::register()
|
Chris@0
|
280 ];
|
Chris@0
|
281 new Settings($settings);
|
Chris@0
|
282
|
Chris@0
|
283 $this->setUpFilesystem();
|
Chris@0
|
284
|
Chris@0
|
285 foreach (Database::getAllConnectionInfo() as $key => $targets) {
|
Chris@0
|
286 Database::removeConnection($key);
|
Chris@0
|
287 }
|
Chris@0
|
288 Database::addConnectionInfo('default', 'default', $this->getDatabaseConnectionInfo()['default']);
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 /**
|
Chris@0
|
292 * Sets up the filesystem, so things like the file directory.
|
Chris@0
|
293 */
|
Chris@0
|
294 protected function setUpFilesystem() {
|
Chris@0
|
295 $test_db = new TestDatabase($this->databasePrefix);
|
Chris@0
|
296 $test_site_path = $test_db->getTestSitePath();
|
Chris@0
|
297
|
Chris@0
|
298 $this->vfsRoot = vfsStream::setup('root');
|
Chris@0
|
299 $this->vfsRoot->addChild(vfsStream::newDirectory($test_site_path));
|
Chris@0
|
300 $this->siteDirectory = vfsStream::url('root/' . $test_site_path);
|
Chris@0
|
301
|
Chris@0
|
302 mkdir($this->siteDirectory . '/files', 0775);
|
Chris@0
|
303 mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
|
Chris@0
|
304
|
Chris@0
|
305 $settings = Settings::getInstance() ? Settings::getAll() : [];
|
Chris@0
|
306 $settings['file_public_path'] = $this->siteDirectory . '/files';
|
Chris@0
|
307 new Settings($settings);
|
Chris@0
|
308
|
Chris@0
|
309 $GLOBALS['config_directories'] = [
|
Chris@0
|
310 CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
|
Chris@0
|
311 ];
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 /**
|
Chris@0
|
315 * @return string
|
Chris@0
|
316 */
|
Chris@0
|
317 public function getDatabasePrefix() {
|
Chris@0
|
318 return $this->databasePrefix;
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 /**
|
Chris@0
|
322 * Bootstraps a kernel for a test.
|
Chris@0
|
323 */
|
Chris@0
|
324 private function bootKernel() {
|
Chris@0
|
325 $this->setSetting('container_yamls', []);
|
Chris@0
|
326 // Allow for test-specific overrides.
|
Chris@12
|
327 $settings_services_file = $this->root . '/sites/default/testing.services.yml';
|
Chris@0
|
328 if (file_exists($settings_services_file)) {
|
Chris@0
|
329 // Copy the testing-specific service overrides in place.
|
Chris@12
|
330 $testing_services_file = $this->siteDirectory . '/services.yml';
|
Chris@0
|
331 copy($settings_services_file, $testing_services_file);
|
Chris@0
|
332 $this->setSetting('container_yamls', [$testing_services_file]);
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 // Allow for global test environment overrides.
|
Chris@0
|
336 if (file_exists($test_env = $this->root . '/sites/default/testing.services.yml')) {
|
Chris@0
|
337 $GLOBALS['conf']['container_yamls']['testing'] = $test_env;
|
Chris@0
|
338 }
|
Chris@0
|
339 // Add this test class as a service provider.
|
Chris@0
|
340 $GLOBALS['conf']['container_service_providers']['test'] = $this;
|
Chris@0
|
341
|
Chris@0
|
342 $modules = self::getModulesToEnable(get_class($this));
|
Chris@0
|
343
|
Chris@0
|
344 // Prepare a precompiled container for all tests of this class.
|
Chris@0
|
345 // Substantially improves performance, since ContainerBuilder::compile()
|
Chris@0
|
346 // is very expensive. Encourages testing best practices (small tests).
|
Chris@0
|
347 // Normally a setUpBeforeClass() operation, but object scope is required to
|
Chris@0
|
348 // inject $this test class instance as a service provider (see above).
|
Chris@0
|
349 $rc = new \ReflectionClass(get_class($this));
|
Chris@0
|
350 $test_method_count = count(array_filter($rc->getMethods(), function ($method) {
|
Chris@0
|
351 // PHPUnit's @test annotations are intentionally ignored/not supported.
|
Chris@0
|
352 return strpos($method->getName(), 'test') === 0;
|
Chris@0
|
353 }));
|
Chris@0
|
354
|
Chris@0
|
355 // Bootstrap the kernel. Do not use createFromRequest() to retain Settings.
|
Chris@0
|
356 $kernel = new DrupalKernel('testing', $this->classLoader, FALSE);
|
Chris@0
|
357 $kernel->setSitePath($this->siteDirectory);
|
Chris@0
|
358 // Boot a new one-time container from scratch. Ensure to set the module list
|
Chris@0
|
359 // upfront to avoid a subsequent rebuild.
|
Chris@0
|
360 if ($modules && $extensions = $this->getExtensionsForModules($modules)) {
|
Chris@0
|
361 $kernel->updateModules($extensions, $extensions);
|
Chris@0
|
362 }
|
Chris@0
|
363 // DrupalKernel::boot() is not sufficient as it does not invoke preHandle(),
|
Chris@0
|
364 // which is required to initialize legacy global variables.
|
Chris@0
|
365 $request = Request::create('/');
|
Chris@0
|
366 $kernel->prepareLegacyRequest($request);
|
Chris@0
|
367
|
Chris@0
|
368 // register() is only called if a new container was built/compiled.
|
Chris@0
|
369 $this->container = $kernel->getContainer();
|
Chris@0
|
370
|
Chris@0
|
371 // Ensure database tasks have been run.
|
Chris@0
|
372 require_once __DIR__ . '/../../../includes/install.inc';
|
Chris@0
|
373 $connection = Database::getConnection();
|
Chris@0
|
374 $errors = db_installer_object($connection->driver())->runTasks();
|
Chris@0
|
375 if (!empty($errors)) {
|
Chris@0
|
376 $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
|
Chris@0
|
377 }
|
Chris@0
|
378
|
Chris@0
|
379 if ($modules) {
|
Chris@0
|
380 $this->container->get('module_handler')->loadAll();
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@0
|
383 $this->container->get('request_stack')->push($request);
|
Chris@0
|
384
|
Chris@0
|
385 // Setup the destion to the be frontpage by default.
|
Chris@0
|
386 \Drupal::destination()->set('/');
|
Chris@0
|
387
|
Chris@0
|
388 // Write the core.extension configuration.
|
Chris@0
|
389 // Required for ConfigInstaller::installDefaultConfig() to work.
|
Chris@0
|
390 $this->container->get('config.storage')->write('core.extension', [
|
Chris@0
|
391 'module' => array_fill_keys($modules, 0),
|
Chris@0
|
392 'theme' => [],
|
Chris@0
|
393 'profile' => '',
|
Chris@0
|
394 ]);
|
Chris@0
|
395
|
Chris@0
|
396 $settings = Settings::getAll();
|
Chris@0
|
397 $settings['php_storage']['default'] = [
|
Chris@0
|
398 'class' => '\Drupal\Component\PhpStorage\FileStorage',
|
Chris@0
|
399 ];
|
Chris@0
|
400 new Settings($settings);
|
Chris@0
|
401
|
Chris@0
|
402 // Manually configure the test mail collector implementation to prevent
|
Chris@0
|
403 // tests from sending out emails and collect them in state instead.
|
Chris@0
|
404 // While this should be enforced via settings.php prior to installation,
|
Chris@0
|
405 // some tests expect to be able to test mail system implementations.
|
Chris@0
|
406 $GLOBALS['config']['system.mail']['interface']['default'] = 'test_mail_collector';
|
Chris@0
|
407
|
Chris@0
|
408 // Manually configure the default file scheme so that modules that use file
|
Chris@0
|
409 // functions don't have to install system and its configuration.
|
Chris@0
|
410 // @see file_default_scheme()
|
Chris@0
|
411 $GLOBALS['config']['system.file']['default_scheme'] = 'public';
|
Chris@0
|
412 }
|
Chris@0
|
413
|
Chris@0
|
414 /**
|
Chris@0
|
415 * Configuration accessor for tests. Returns non-overridden configuration.
|
Chris@0
|
416 *
|
Chris@0
|
417 * @param string $name
|
Chris@0
|
418 * The configuration name.
|
Chris@0
|
419 *
|
Chris@0
|
420 * @return \Drupal\Core\Config\Config
|
Chris@0
|
421 * The configuration object with original configuration data.
|
Chris@0
|
422 */
|
Chris@0
|
423 protected function config($name) {
|
Chris@0
|
424 return $this->container->get('config.factory')->getEditable($name);
|
Chris@0
|
425 }
|
Chris@0
|
426
|
Chris@0
|
427 /**
|
Chris@0
|
428 * Returns the Database connection info to be used for this test.
|
Chris@0
|
429 *
|
Chris@0
|
430 * This method only exists for tests of the Database component itself, because
|
Chris@0
|
431 * they require multiple database connections. Each SQLite :memory: connection
|
Chris@0
|
432 * creates a new/separate database in memory. A shared-memory SQLite file URI
|
Chris@0
|
433 * triggers PHP open_basedir/allow_url_fopen/allow_url_include restrictions.
|
Chris@0
|
434 * Due to that, Database tests are running against a SQLite database that is
|
Chris@0
|
435 * located in an actual file in the system's temporary directory.
|
Chris@0
|
436 *
|
Chris@0
|
437 * Other tests should not override this method.
|
Chris@0
|
438 *
|
Chris@0
|
439 * @return array
|
Chris@0
|
440 * A Database connection info array.
|
Chris@0
|
441 *
|
Chris@0
|
442 * @internal
|
Chris@0
|
443 */
|
Chris@0
|
444 protected function getDatabaseConnectionInfo() {
|
Chris@0
|
445 // If the test is run with argument dburl then use it.
|
Chris@0
|
446 $db_url = getenv('SIMPLETEST_DB');
|
Chris@0
|
447 if (empty($db_url)) {
|
Chris@0
|
448 throw new \Exception('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh. See https://www.drupal.org/node/2116263#skipped-tests for more information.');
|
Chris@0
|
449 }
|
Chris@0
|
450 else {
|
Chris@0
|
451 $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root);
|
Chris@0
|
452 Database::addConnectionInfo('default', 'default', $database);
|
Chris@0
|
453 }
|
Chris@0
|
454
|
Chris@0
|
455 // Clone the current connection and replace the current prefix.
|
Chris@0
|
456 $connection_info = Database::getConnectionInfo('default');
|
Chris@0
|
457 if (!empty($connection_info)) {
|
Chris@0
|
458 Database::renameConnection('default', 'simpletest_original_default');
|
Chris@0
|
459 foreach ($connection_info as $target => $value) {
|
Chris@0
|
460 // Replace the full table prefix definition to ensure that no table
|
Chris@0
|
461 // prefixes of the test runner leak into the test.
|
Chris@0
|
462 $connection_info[$target]['prefix'] = [
|
Chris@17
|
463 'default' => $this->databasePrefix,
|
Chris@0
|
464 ];
|
Chris@0
|
465 }
|
Chris@0
|
466 }
|
Chris@0
|
467 return $connection_info;
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@0
|
470 /**
|
Chris@0
|
471 * Initializes the FileCache component.
|
Chris@0
|
472 *
|
Chris@0
|
473 * We can not use the Settings object in a component, that's why we have to do
|
Chris@0
|
474 * it here instead of \Drupal\Component\FileCache\FileCacheFactory.
|
Chris@0
|
475 */
|
Chris@0
|
476 protected function initFileCache() {
|
Chris@0
|
477 $configuration = Settings::get('file_cache');
|
Chris@0
|
478
|
Chris@0
|
479 // Provide a default configuration, if not set.
|
Chris@0
|
480 if (!isset($configuration['default'])) {
|
Chris@0
|
481 // @todo Use extension_loaded('apcu') for non-testbot
|
Chris@0
|
482 // https://www.drupal.org/node/2447753.
|
Chris@0
|
483 if (function_exists('apcu_fetch')) {
|
Chris@0
|
484 $configuration['default']['cache_backend_class'] = ApcuFileCacheBackend::class;
|
Chris@0
|
485 }
|
Chris@0
|
486 }
|
Chris@0
|
487 FileCacheFactory::setConfiguration($configuration);
|
Chris@0
|
488 FileCacheFactory::setPrefix(Settings::getApcuPrefix('file_cache', $this->root));
|
Chris@0
|
489 }
|
Chris@0
|
490
|
Chris@0
|
491 /**
|
Chris@0
|
492 * Returns Extension objects for $modules to enable.
|
Chris@0
|
493 *
|
Chris@0
|
494 * @param string[] $modules
|
Chris@0
|
495 * The list of modules to enable.
|
Chris@0
|
496 *
|
Chris@0
|
497 * @return \Drupal\Core\Extension\Extension[]
|
Chris@0
|
498 * Extension objects for $modules, keyed by module name.
|
Chris@0
|
499 *
|
Chris@0
|
500 * @throws \PHPUnit_Framework_Exception
|
Chris@0
|
501 * If a module is not available.
|
Chris@0
|
502 *
|
Chris@0
|
503 * @see \Drupal\Tests\KernelTestBase::enableModules()
|
Chris@0
|
504 * @see \Drupal\Core\Extension\ModuleHandler::add()
|
Chris@0
|
505 */
|
Chris@0
|
506 private function getExtensionsForModules(array $modules) {
|
Chris@0
|
507 $extensions = [];
|
Chris@0
|
508 $discovery = new ExtensionDiscovery($this->root);
|
Chris@0
|
509 $discovery->setProfileDirectories([]);
|
Chris@0
|
510 $list = $discovery->scan('module');
|
Chris@0
|
511 foreach ($modules as $name) {
|
Chris@0
|
512 if (!isset($list[$name])) {
|
Chris@0
|
513 throw new \PHPUnit_Framework_Exception("Unavailable module: '$name'. If this module needs to be downloaded separately, annotate the test class with '@requires module $name'.");
|
Chris@0
|
514 }
|
Chris@0
|
515 $extensions[$name] = $list[$name];
|
Chris@0
|
516 }
|
Chris@0
|
517 return $extensions;
|
Chris@0
|
518 }
|
Chris@0
|
519
|
Chris@0
|
520 /**
|
Chris@0
|
521 * Registers test-specific services.
|
Chris@0
|
522 *
|
Chris@0
|
523 * Extend this method in your test to register additional services. This
|
Chris@0
|
524 * method is called whenever the kernel is rebuilt.
|
Chris@0
|
525 *
|
Chris@0
|
526 * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
|
Chris@0
|
527 * The service container to enhance.
|
Chris@0
|
528 *
|
Chris@0
|
529 * @see \Drupal\Tests\KernelTestBase::bootKernel()
|
Chris@0
|
530 */
|
Chris@0
|
531 public function register(ContainerBuilder $container) {
|
Chris@0
|
532 // Keep the container object around for tests.
|
Chris@0
|
533 $this->container = $container;
|
Chris@0
|
534
|
Chris@0
|
535 $container
|
Chris@0
|
536 ->register('flood', 'Drupal\Core\Flood\MemoryBackend')
|
Chris@0
|
537 ->addArgument(new Reference('request_stack'));
|
Chris@0
|
538 $container
|
Chris@0
|
539 ->register('lock', 'Drupal\Core\Lock\NullLockBackend');
|
Chris@0
|
540 $container
|
Chris@0
|
541 ->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');
|
Chris@0
|
542 $container
|
Chris@0
|
543 ->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory')
|
Chris@0
|
544 // Must persist container rebuilds, or all data would vanish otherwise.
|
Chris@0
|
545 ->addTag('persist');
|
Chris@0
|
546 $container
|
Chris@0
|
547 ->setAlias('keyvalue', 'keyvalue.memory');
|
Chris@0
|
548
|
Chris@0
|
549 // Set the default language on the minimal container.
|
Chris@0
|
550 $container->setParameter('language.default_values', Language::$defaultValues);
|
Chris@0
|
551
|
Chris@0
|
552 if ($this->strictConfigSchema) {
|
Chris@0
|
553 $container
|
Chris@0
|
554 ->register('simpletest.config_schema_checker', ConfigSchemaChecker::class)
|
Chris@0
|
555 ->addArgument(new Reference('config.typed'))
|
Chris@0
|
556 ->addArgument($this->getConfigSchemaExclusions())
|
Chris@0
|
557 ->addTag('event_subscriber');
|
Chris@0
|
558 }
|
Chris@0
|
559
|
Chris@0
|
560 if ($container->hasDefinition('path_processor_alias')) {
|
Chris@0
|
561 // Prevent the alias-based path processor, which requires a url_alias db
|
Chris@0
|
562 // table, from being registered to the path processor manager. We do this
|
Chris@0
|
563 // by removing the tags that the compiler pass looks for. This means the
|
Chris@0
|
564 // url generator can safely be used within tests.
|
Chris@0
|
565 $container->getDefinition('path_processor_alias')
|
Chris@0
|
566 ->clearTag('path_processor_inbound')
|
Chris@0
|
567 ->clearTag('path_processor_outbound');
|
Chris@0
|
568 }
|
Chris@0
|
569
|
Chris@0
|
570 if ($container->hasDefinition('password')) {
|
Chris@0
|
571 $container->getDefinition('password')
|
Chris@0
|
572 ->setArguments([1]);
|
Chris@0
|
573 }
|
Chris@0
|
574 TestServiceProvider::addRouteProvider($container);
|
Chris@0
|
575 }
|
Chris@0
|
576
|
Chris@0
|
577 /**
|
Chris@0
|
578 * Gets the config schema exclusions for this test.
|
Chris@0
|
579 *
|
Chris@0
|
580 * @return string[]
|
Chris@0
|
581 * An array of config object names that are excluded from schema checking.
|
Chris@0
|
582 */
|
Chris@0
|
583 protected function getConfigSchemaExclusions() {
|
Chris@0
|
584 $class = get_class($this);
|
Chris@0
|
585 $exceptions = [];
|
Chris@0
|
586 while ($class) {
|
Chris@0
|
587 if (property_exists($class, 'configSchemaCheckerExclusions')) {
|
Chris@0
|
588 $exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
|
Chris@0
|
589 }
|
Chris@0
|
590 $class = get_parent_class($class);
|
Chris@0
|
591 }
|
Chris@0
|
592 // Filter out any duplicates.
|
Chris@0
|
593 return array_unique($exceptions);
|
Chris@0
|
594 }
|
Chris@0
|
595
|
Chris@0
|
596 /**
|
Chris@0
|
597 * {@inheritdoc}
|
Chris@0
|
598 */
|
Chris@0
|
599 protected function assertPostConditions() {
|
Chris@0
|
600 // Execute registered Drupal shutdown functions prior to tearing down.
|
Chris@0
|
601 // @see _drupal_shutdown_function()
|
Chris@0
|
602 $callbacks = &drupal_register_shutdown_function();
|
Chris@0
|
603 while ($callback = array_shift($callbacks)) {
|
Chris@0
|
604 call_user_func_array($callback['callback'], $callback['arguments']);
|
Chris@0
|
605 }
|
Chris@0
|
606
|
Chris@0
|
607 // Shut down the kernel (if bootKernel() was called).
|
Chris@0
|
608 // @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
|
Chris@0
|
609 if ($this->container) {
|
Chris@0
|
610 $this->container->get('kernel')->shutdown();
|
Chris@0
|
611 }
|
Chris@0
|
612
|
Chris@0
|
613 // Fail in case any (new) shutdown functions exist.
|
Chris@0
|
614 $this->assertCount(0, drupal_register_shutdown_function(), 'Unexpected Drupal shutdown callbacks exist after running shutdown functions.');
|
Chris@0
|
615
|
Chris@0
|
616 parent::assertPostConditions();
|
Chris@0
|
617 }
|
Chris@0
|
618
|
Chris@0
|
619 /**
|
Chris@0
|
620 * {@inheritdoc}
|
Chris@0
|
621 */
|
Chris@0
|
622 protected function tearDown() {
|
Chris@0
|
623 // Destroy the testing kernel.
|
Chris@0
|
624 if (isset($this->kernel)) {
|
Chris@0
|
625 $this->kernel->shutdown();
|
Chris@0
|
626 }
|
Chris@0
|
627
|
Chris@0
|
628 // Remove all prefixed tables.
|
Chris@0
|
629 $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
|
Chris@0
|
630 $original_prefix = $original_connection_info['default']['prefix']['default'];
|
Chris@0
|
631 $test_connection_info = Database::getConnectionInfo('default');
|
Chris@0
|
632 $test_prefix = $test_connection_info['default']['prefix']['default'];
|
Chris@0
|
633 if ($original_prefix != $test_prefix) {
|
Chris@0
|
634 $tables = Database::getConnection()->schema()->findTables('%');
|
Chris@0
|
635 foreach ($tables as $table) {
|
Chris@0
|
636 if (Database::getConnection()->schema()->dropTable($table)) {
|
Chris@0
|
637 unset($tables[$table]);
|
Chris@0
|
638 }
|
Chris@0
|
639 }
|
Chris@0
|
640 }
|
Chris@0
|
641
|
Chris@0
|
642 // Free up memory: Own properties.
|
Chris@0
|
643 $this->classLoader = NULL;
|
Chris@0
|
644 $this->vfsRoot = NULL;
|
Chris@0
|
645 $this->configImporter = NULL;
|
Chris@0
|
646
|
Chris@0
|
647 // Free up memory: Custom test class properties.
|
Chris@0
|
648 // Note: Private properties cannot be cleaned up.
|
Chris@0
|
649 $rc = new \ReflectionClass(__CLASS__);
|
Chris@0
|
650 $blacklist = [];
|
Chris@0
|
651 foreach ($rc->getProperties() as $property) {
|
Chris@0
|
652 $blacklist[$property->name] = $property->getDeclaringClass()->name;
|
Chris@0
|
653 }
|
Chris@0
|
654 $rc = new \ReflectionClass($this);
|
Chris@0
|
655 foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $property) {
|
Chris@0
|
656 if (!$property->isStatic() && !isset($blacklist[$property->name])) {
|
Chris@0
|
657 $this->{$property->name} = NULL;
|
Chris@0
|
658 }
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 // Clean FileCache cache.
|
Chris@0
|
662 FileCache::reset();
|
Chris@0
|
663
|
Chris@0
|
664 // Clean up statics, container, and settings.
|
Chris@0
|
665 if (function_exists('drupal_static_reset')) {
|
Chris@0
|
666 drupal_static_reset();
|
Chris@0
|
667 }
|
Chris@0
|
668 \Drupal::unsetContainer();
|
Chris@0
|
669 $this->container = NULL;
|
Chris@0
|
670 new Settings([]);
|
Chris@0
|
671
|
Chris@0
|
672 parent::tearDown();
|
Chris@0
|
673 }
|
Chris@0
|
674
|
Chris@0
|
675 /**
|
Chris@0
|
676 * @after
|
Chris@0
|
677 *
|
Chris@0
|
678 * Additional tear down method to close the connection at the end.
|
Chris@0
|
679 */
|
Chris@0
|
680 public function tearDownCloseDatabaseConnection() {
|
Chris@0
|
681 // Destroy the database connection, which for example removes the memory
|
Chris@0
|
682 // from sqlite in memory.
|
Chris@0
|
683 foreach (Database::getAllConnectionInfo() as $key => $targets) {
|
Chris@0
|
684 Database::removeConnection($key);
|
Chris@0
|
685 }
|
Chris@0
|
686 }
|
Chris@0
|
687
|
Chris@0
|
688 /**
|
Chris@0
|
689 * Installs default configuration for a given list of modules.
|
Chris@0
|
690 *
|
Chris@0
|
691 * @param string|string[] $modules
|
Chris@0
|
692 * A module or list of modules for which to install default configuration.
|
Chris@0
|
693 *
|
Chris@0
|
694 * @throws \LogicException
|
Chris@0
|
695 * If any module in $modules is not enabled.
|
Chris@0
|
696 */
|
Chris@0
|
697 protected function installConfig($modules) {
|
Chris@0
|
698 foreach ((array) $modules as $module) {
|
Chris@0
|
699 if (!$this->container->get('module_handler')->moduleExists($module)) {
|
Chris@0
|
700 throw new \LogicException("$module module is not enabled.");
|
Chris@0
|
701 }
|
Chris@0
|
702 $this->container->get('config.installer')->installDefaultConfig('module', $module);
|
Chris@0
|
703 }
|
Chris@0
|
704 }
|
Chris@0
|
705
|
Chris@0
|
706 /**
|
Chris@0
|
707 * Installs database tables from a module schema definition.
|
Chris@0
|
708 *
|
Chris@0
|
709 * @param string $module
|
Chris@0
|
710 * The name of the module that defines the table's schema.
|
Chris@0
|
711 * @param string|array $tables
|
Chris@0
|
712 * The name or an array of the names of the tables to install.
|
Chris@0
|
713 *
|
Chris@0
|
714 * @throws \LogicException
|
Chris@0
|
715 * If $module is not enabled or the table schema cannot be found.
|
Chris@0
|
716 */
|
Chris@0
|
717 protected function installSchema($module, $tables) {
|
Chris@0
|
718 // drupal_get_module_schema() is technically able to install a schema
|
Chris@0
|
719 // of a non-enabled module, but its ability to load the module's .install
|
Chris@0
|
720 // file depends on many other factors. To prevent differences in test
|
Chris@0
|
721 // behavior and non-reproducible test failures, we only allow the schema of
|
Chris@0
|
722 // explicitly loaded/enabled modules to be installed.
|
Chris@0
|
723 if (!$this->container->get('module_handler')->moduleExists($module)) {
|
Chris@0
|
724 throw new \LogicException("$module module is not enabled.");
|
Chris@0
|
725 }
|
Chris@0
|
726 $tables = (array) $tables;
|
Chris@0
|
727 foreach ($tables as $table) {
|
Chris@0
|
728 $schema = drupal_get_module_schema($module, $table);
|
Chris@0
|
729 if (empty($schema)) {
|
Chris@0
|
730 // BC layer to avoid some contrib tests to fail.
|
Chris@0
|
731 // @todo Remove the BC layer before 8.1.x release.
|
Chris@0
|
732 // @see https://www.drupal.org/node/2670360
|
Chris@0
|
733 // @see https://www.drupal.org/node/2670454
|
Chris@0
|
734 if ($module == 'system') {
|
Chris@0
|
735 continue;
|
Chris@0
|
736 }
|
Chris@0
|
737 throw new \LogicException("$module module does not define a schema for table '$table'.");
|
Chris@0
|
738 }
|
Chris@0
|
739 $this->container->get('database')->schema()->createTable($table, $schema);
|
Chris@0
|
740 }
|
Chris@0
|
741 }
|
Chris@0
|
742
|
Chris@0
|
743 /**
|
Chris@0
|
744 * Installs the storage schema for a specific entity type.
|
Chris@0
|
745 *
|
Chris@0
|
746 * @param string $entity_type_id
|
Chris@0
|
747 * The ID of the entity type.
|
Chris@0
|
748 */
|
Chris@0
|
749 protected function installEntitySchema($entity_type_id) {
|
Chris@0
|
750 /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
|
Chris@0
|
751 $entity_manager = $this->container->get('entity.manager');
|
Chris@0
|
752 $entity_type = $entity_manager->getDefinition($entity_type_id);
|
Chris@0
|
753 $entity_manager->onEntityTypeCreate($entity_type);
|
Chris@0
|
754
|
Chris@0
|
755 // For test runs, the most common storage backend is a SQL database. For
|
Chris@0
|
756 // this case, ensure the tables got created.
|
Chris@0
|
757 $storage = $entity_manager->getStorage($entity_type_id);
|
Chris@0
|
758 if ($storage instanceof SqlEntityStorageInterface) {
|
Chris@0
|
759 $tables = $storage->getTableMapping()->getTableNames();
|
Chris@0
|
760 $db_schema = $this->container->get('database')->schema();
|
Chris@0
|
761 $all_tables_exist = TRUE;
|
Chris@0
|
762 foreach ($tables as $table) {
|
Chris@0
|
763 if (!$db_schema->tableExists($table)) {
|
Chris@17
|
764 $this->fail(new FormattableMarkup('Installed entity type table for the %entity_type entity type: %table', [
|
Chris@0
|
765 '%entity_type' => $entity_type_id,
|
Chris@0
|
766 '%table' => $table,
|
Chris@0
|
767 ]));
|
Chris@0
|
768 $all_tables_exist = FALSE;
|
Chris@0
|
769 }
|
Chris@0
|
770 }
|
Chris@0
|
771 if ($all_tables_exist) {
|
Chris@17
|
772 $this->pass(new FormattableMarkup('Installed entity type tables for the %entity_type entity type: %tables', [
|
Chris@0
|
773 '%entity_type' => $entity_type_id,
|
Chris@0
|
774 '%tables' => '{' . implode('}, {', $tables) . '}',
|
Chris@0
|
775 ]));
|
Chris@0
|
776 }
|
Chris@0
|
777 }
|
Chris@0
|
778 }
|
Chris@0
|
779
|
Chris@0
|
780 /**
|
Chris@0
|
781 * Enables modules for this test.
|
Chris@0
|
782 *
|
Chris@0
|
783 * This method does not install modules fully. Services and hooks for the
|
Chris@0
|
784 * module are available, but the install process is not performed.
|
Chris@0
|
785 *
|
Chris@0
|
786 * To install test modules outside of the testing environment, add
|
Chris@0
|
787 * @code
|
Chris@0
|
788 * $settings['extension_discovery_scan_tests'] = TRUE;
|
Chris@0
|
789 * @endcode
|
Chris@0
|
790 * to your settings.php.
|
Chris@0
|
791 *
|
Chris@0
|
792 * @param string[] $modules
|
Chris@0
|
793 * A list of modules to enable. Dependencies are not resolved; i.e.,
|
Chris@0
|
794 * multiple modules have to be specified individually. The modules are only
|
Chris@0
|
795 * added to the active module list and loaded; i.e., their database schema
|
Chris@0
|
796 * is not installed. hook_install() is not invoked. A custom module weight
|
Chris@0
|
797 * is not applied.
|
Chris@0
|
798 *
|
Chris@0
|
799 * @throws \LogicException
|
Chris@0
|
800 * If any module in $modules is already enabled.
|
Chris@0
|
801 * @throws \RuntimeException
|
Chris@0
|
802 * If a module is not enabled after enabling it.
|
Chris@0
|
803 */
|
Chris@0
|
804 protected function enableModules(array $modules) {
|
Chris@0
|
805 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
Chris@0
|
806 if ($trace[1]['function'] === 'setUp') {
|
Chris@0
|
807 trigger_error('KernelTestBase::enableModules() should not be called from setUp(). Use the $modules property instead.', E_USER_DEPRECATED);
|
Chris@0
|
808 }
|
Chris@0
|
809 unset($trace);
|
Chris@0
|
810
|
Chris@0
|
811 // Perform an ExtensionDiscovery scan as this function may receive a
|
Chris@0
|
812 // profile that is not the current profile, and we don't yet have a cached
|
Chris@0
|
813 // way to receive inactive profile information.
|
Chris@0
|
814 // @todo Remove as part of https://www.drupal.org/node/2186491
|
Chris@17
|
815 $listing = new ExtensionDiscovery($this->root);
|
Chris@0
|
816 $module_list = $listing->scan('module');
|
Chris@0
|
817 // In ModuleHandlerTest we pass in a profile as if it were a module.
|
Chris@0
|
818 $module_list += $listing->scan('profile');
|
Chris@0
|
819
|
Chris@0
|
820 // Set the list of modules in the extension handler.
|
Chris@0
|
821 $module_handler = $this->container->get('module_handler');
|
Chris@0
|
822
|
Chris@0
|
823 // Write directly to active storage to avoid early instantiation of
|
Chris@0
|
824 // the event dispatcher which can prevent modules from registering events.
|
Chris@0
|
825 $active_storage = $this->container->get('config.storage');
|
Chris@0
|
826 $extension_config = $active_storage->read('core.extension');
|
Chris@0
|
827
|
Chris@0
|
828 foreach ($modules as $module) {
|
Chris@0
|
829 if ($module_handler->moduleExists($module)) {
|
Chris@12
|
830 continue;
|
Chris@0
|
831 }
|
Chris@0
|
832 $module_handler->addModule($module, $module_list[$module]->getPath());
|
Chris@0
|
833 // Maintain the list of enabled modules in configuration.
|
Chris@0
|
834 $extension_config['module'][$module] = 0;
|
Chris@0
|
835 }
|
Chris@0
|
836 $active_storage->write('core.extension', $extension_config);
|
Chris@0
|
837
|
Chris@0
|
838 // Update the kernel to make their services available.
|
Chris@0
|
839 $extensions = $module_handler->getModuleList();
|
Chris@0
|
840 $this->container->get('kernel')->updateModules($extensions, $extensions);
|
Chris@0
|
841
|
Chris@0
|
842 // Ensure isLoaded() is TRUE in order to make
|
Chris@0
|
843 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
|
Chris@0
|
844 // Note that the kernel has rebuilt the container; this $module_handler is
|
Chris@0
|
845 // no longer the $module_handler instance from above.
|
Chris@0
|
846 $module_handler = $this->container->get('module_handler');
|
Chris@0
|
847 $module_handler->reload();
|
Chris@0
|
848 foreach ($modules as $module) {
|
Chris@0
|
849 if (!$module_handler->moduleExists($module)) {
|
Chris@0
|
850 throw new \RuntimeException("$module module is not enabled after enabling it.");
|
Chris@0
|
851 }
|
Chris@0
|
852 }
|
Chris@0
|
853 }
|
Chris@0
|
854
|
Chris@0
|
855 /**
|
Chris@0
|
856 * Disables modules for this test.
|
Chris@0
|
857 *
|
Chris@0
|
858 * @param string[] $modules
|
Chris@0
|
859 * A list of modules to disable. Dependencies are not resolved; i.e.,
|
Chris@0
|
860 * multiple modules have to be specified with dependent modules first.
|
Chris@0
|
861 * Code of previously enabled modules is still loaded. The modules are only
|
Chris@0
|
862 * removed from the active module list.
|
Chris@0
|
863 *
|
Chris@0
|
864 * @throws \LogicException
|
Chris@0
|
865 * If any module in $modules is already disabled.
|
Chris@0
|
866 * @throws \RuntimeException
|
Chris@0
|
867 * If a module is not disabled after disabling it.
|
Chris@0
|
868 */
|
Chris@0
|
869 protected function disableModules(array $modules) {
|
Chris@0
|
870 // Unset the list of modules in the extension handler.
|
Chris@0
|
871 $module_handler = $this->container->get('module_handler');
|
Chris@0
|
872 $module_filenames = $module_handler->getModuleList();
|
Chris@0
|
873 $extension_config = $this->config('core.extension');
|
Chris@0
|
874 foreach ($modules as $module) {
|
Chris@0
|
875 if (!$module_handler->moduleExists($module)) {
|
Chris@0
|
876 throw new \LogicException("$module module cannot be disabled because it is not enabled.");
|
Chris@0
|
877 }
|
Chris@0
|
878 unset($module_filenames[$module]);
|
Chris@0
|
879 $extension_config->clear('module.' . $module);
|
Chris@0
|
880 }
|
Chris@0
|
881 $extension_config->save();
|
Chris@0
|
882 $module_handler->setModuleList($module_filenames);
|
Chris@0
|
883 $module_handler->resetImplementations();
|
Chris@0
|
884 // Update the kernel to remove their services.
|
Chris@0
|
885 $this->container->get('kernel')->updateModules($module_filenames, $module_filenames);
|
Chris@0
|
886
|
Chris@0
|
887 // Ensure isLoaded() is TRUE in order to make
|
Chris@0
|
888 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
|
Chris@0
|
889 // Note that the kernel has rebuilt the container; this $module_handler is
|
Chris@0
|
890 // no longer the $module_handler instance from above.
|
Chris@0
|
891 $module_handler = $this->container->get('module_handler');
|
Chris@0
|
892 $module_handler->reload();
|
Chris@0
|
893 foreach ($modules as $module) {
|
Chris@0
|
894 if ($module_handler->moduleExists($module)) {
|
Chris@0
|
895 throw new \RuntimeException("$module module is not disabled after disabling it.");
|
Chris@0
|
896 }
|
Chris@0
|
897 }
|
Chris@0
|
898 }
|
Chris@0
|
899
|
Chris@0
|
900 /**
|
Chris@0
|
901 * Renders a render array.
|
Chris@0
|
902 *
|
Chris@0
|
903 * @param array $elements
|
Chris@0
|
904 * The elements to render.
|
Chris@0
|
905 *
|
Chris@0
|
906 * @return string
|
Chris@0
|
907 * The rendered string output (typically HTML).
|
Chris@0
|
908 */
|
Chris@0
|
909 protected function render(array &$elements) {
|
Chris@0
|
910 // \Drupal\Core\Render\BareHtmlPageRenderer::renderBarePage calls out to
|
Chris@0
|
911 // system_page_attachments() directly.
|
Chris@0
|
912 if (!\Drupal::moduleHandler()->moduleExists('system')) {
|
Chris@0
|
913 throw new \Exception(__METHOD__ . ' requires system module to be installed.');
|
Chris@0
|
914 }
|
Chris@0
|
915
|
Chris@0
|
916 // Use the bare HTML page renderer to render our links.
|
Chris@0
|
917 $renderer = $this->container->get('bare_html_page_renderer');
|
Chris@0
|
918 $response = $renderer->renderBarePage($elements, '', 'maintenance_page');
|
Chris@0
|
919
|
Chris@0
|
920 // Glean the content from the response object.
|
Chris@0
|
921 $content = $response->getContent();
|
Chris@0
|
922 $this->setRawContent($content);
|
Chris@0
|
923 $this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
|
Chris@0
|
924 return $content;
|
Chris@0
|
925 }
|
Chris@0
|
926
|
Chris@0
|
927 /**
|
Chris@0
|
928 * Sets an in-memory Settings variable.
|
Chris@0
|
929 *
|
Chris@0
|
930 * @param string $name
|
Chris@0
|
931 * The name of the setting to set.
|
Chris@0
|
932 * @param bool|string|int|array|null $value
|
Chris@0
|
933 * The value to set. Note that array values are replaced entirely; use
|
Chris@0
|
934 * \Drupal\Core\Site\Settings::get() to perform custom merges.
|
Chris@0
|
935 */
|
Chris@0
|
936 protected function setSetting($name, $value) {
|
Chris@17
|
937 if ($name === 'install_profile') {
|
Chris@17
|
938 @trigger_error('Use \Drupal\KernelTests\KernelTestBase::setInstallProfile() to set the install profile in kernel tests. See https://www.drupal.org/node/2538996', E_USER_DEPRECATED);
|
Chris@17
|
939 $this->setInstallProfile($value);
|
Chris@17
|
940 }
|
Chris@0
|
941 $settings = Settings::getInstance() ? Settings::getAll() : [];
|
Chris@0
|
942 $settings[$name] = $value;
|
Chris@0
|
943 new Settings($settings);
|
Chris@0
|
944 }
|
Chris@0
|
945
|
Chris@0
|
946 /**
|
Chris@17
|
947 * Sets the install profile and rebuilds the container to update it.
|
Chris@17
|
948 *
|
Chris@17
|
949 * @param string $profile
|
Chris@17
|
950 * The install profile to set.
|
Chris@17
|
951 */
|
Chris@17
|
952 protected function setInstallProfile($profile) {
|
Chris@17
|
953 $this->container->get('config.factory')
|
Chris@17
|
954 ->getEditable('core.extension')
|
Chris@17
|
955 ->set('profile', $profile)
|
Chris@17
|
956 ->save();
|
Chris@17
|
957
|
Chris@17
|
958 // The installation profile is provided by a container parameter. Saving
|
Chris@17
|
959 // the configuration doesn't automatically trigger invalidation
|
Chris@17
|
960 $this->container->get('kernel')->rebuildContainer();
|
Chris@17
|
961 }
|
Chris@17
|
962
|
Chris@17
|
963 /**
|
Chris@0
|
964 * Stops test execution.
|
Chris@0
|
965 */
|
Chris@0
|
966 protected function stop() {
|
Chris@0
|
967 $this->getTestResultObject()->stop();
|
Chris@0
|
968 }
|
Chris@0
|
969
|
Chris@0
|
970 /**
|
Chris@0
|
971 * Dumps the current state of the virtual filesystem to STDOUT.
|
Chris@0
|
972 */
|
Chris@0
|
973 protected function vfsDump() {
|
Chris@0
|
974 vfsStream::inspect(new vfsStreamPrintVisitor());
|
Chris@0
|
975 }
|
Chris@0
|
976
|
Chris@0
|
977 /**
|
Chris@0
|
978 * Returns the modules to enable for this test.
|
Chris@0
|
979 *
|
Chris@0
|
980 * @param string $class
|
Chris@0
|
981 * The fully-qualified class name of this test.
|
Chris@0
|
982 *
|
Chris@0
|
983 * @return array
|
Chris@0
|
984 */
|
Chris@0
|
985 private static function getModulesToEnable($class) {
|
Chris@0
|
986 $modules = [];
|
Chris@0
|
987 while ($class) {
|
Chris@0
|
988 if (property_exists($class, 'modules')) {
|
Chris@0
|
989 // Only add the modules, if the $modules property was not inherited.
|
Chris@0
|
990 $rp = new \ReflectionProperty($class, 'modules');
|
Chris@0
|
991 if ($rp->class == $class) {
|
Chris@0
|
992 $modules[$class] = $class::$modules;
|
Chris@0
|
993 }
|
Chris@0
|
994 }
|
Chris@0
|
995 $class = get_parent_class($class);
|
Chris@0
|
996 }
|
Chris@0
|
997 // Modules have been collected in reverse class hierarchy order; modules
|
Chris@0
|
998 // defined by base classes should be sorted first. Then, merge the results
|
Chris@0
|
999 // together.
|
Chris@0
|
1000 $modules = array_reverse($modules);
|
Chris@0
|
1001 return call_user_func_array('array_merge_recursive', $modules);
|
Chris@0
|
1002 }
|
Chris@0
|
1003
|
Chris@0
|
1004 /**
|
Chris@0
|
1005 * {@inheritdoc}
|
Chris@0
|
1006 */
|
Chris@0
|
1007 protected function prepareTemplate(\Text_Template $template) {
|
Chris@0
|
1008 $bootstrap_globals = '';
|
Chris@0
|
1009
|
Chris@0
|
1010 // Fix missing bootstrap.php when $preserveGlobalState is FALSE.
|
Chris@0
|
1011 // @see https://github.com/sebastianbergmann/phpunit/pull/797
|
Chris@0
|
1012 $bootstrap_globals .= '$__PHPUNIT_BOOTSTRAP = ' . var_export($GLOBALS['__PHPUNIT_BOOTSTRAP'], TRUE) . ";\n";
|
Chris@0
|
1013
|
Chris@0
|
1014 // Avoid repetitive test namespace discoveries to improve performance.
|
Chris@0
|
1015 // @see /core/tests/bootstrap.php
|
Chris@0
|
1016 $bootstrap_globals .= '$namespaces = ' . var_export($GLOBALS['namespaces'], TRUE) . ";\n";
|
Chris@0
|
1017
|
Chris@0
|
1018 $template->setVar([
|
Chris@0
|
1019 'constants' => '',
|
Chris@0
|
1020 'included_files' => '',
|
Chris@0
|
1021 'globals' => $bootstrap_globals,
|
Chris@0
|
1022 ]);
|
Chris@0
|
1023 }
|
Chris@0
|
1024
|
Chris@0
|
1025 /**
|
Chris@0
|
1026 * Returns whether the current test method is running in a separate process.
|
Chris@0
|
1027 *
|
Chris@0
|
1028 * Note that KernelTestBase will run in a separate process by default.
|
Chris@0
|
1029 *
|
Chris@0
|
1030 * @return bool
|
Chris@0
|
1031 *
|
Chris@0
|
1032 * @see \Drupal\KernelTests\KernelTestBase::$runTestInSeparateProcess
|
Chris@0
|
1033 * @see https://github.com/sebastianbergmann/phpunit/pull/1350
|
Chris@0
|
1034 *
|
Chris@0
|
1035 * @deprecated in Drupal 8.4.x, for removal before the Drupal 9.0.0 release.
|
Chris@0
|
1036 * KernelTestBase tests are always run in isolated processes.
|
Chris@0
|
1037 */
|
Chris@0
|
1038 protected function isTestInIsolation() {
|
Chris@0
|
1039 @trigger_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated in Drupal 8.4.x, for removal before the Drupal 9.0.0 release. KernelTestBase tests are always run in isolated processes.', E_USER_DEPRECATED);
|
Chris@0
|
1040 return function_exists('__phpunit_run_isolated_test');
|
Chris@0
|
1041 }
|
Chris@0
|
1042
|
Chris@0
|
1043 /**
|
Chris@0
|
1044 * BC: Automatically resolve former KernelTestBase class properties.
|
Chris@0
|
1045 *
|
Chris@0
|
1046 * Test authors should follow the provided instructions and adjust their tests
|
Chris@0
|
1047 * accordingly.
|
Chris@0
|
1048 *
|
Chris@16
|
1049 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
1050 */
|
Chris@0
|
1051 public function __get($name) {
|
Chris@0
|
1052 if (in_array($name, [
|
Chris@0
|
1053 'public_files_directory',
|
Chris@0
|
1054 'private_files_directory',
|
Chris@0
|
1055 'temp_files_directory',
|
Chris@0
|
1056 'translation_files_directory',
|
Chris@0
|
1057 ])) {
|
Chris@0
|
1058 // @comment it in again.
|
Chris@0
|
1059 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use the regular API method to retrieve it instead (e.g., Settings).", $name), E_USER_DEPRECATED);
|
Chris@0
|
1060 switch ($name) {
|
Chris@0
|
1061 case 'public_files_directory':
|
Chris@0
|
1062 return Settings::get('file_public_path', \Drupal::service('site.path') . '/files');
|
Chris@0
|
1063
|
Chris@0
|
1064 case 'private_files_directory':
|
Chris@0
|
1065 return Settings::get('file_private_path');
|
Chris@0
|
1066
|
Chris@0
|
1067 case 'temp_files_directory':
|
Chris@0
|
1068 return file_directory_temp();
|
Chris@0
|
1069
|
Chris@0
|
1070 case 'translation_files_directory':
|
Chris@0
|
1071 return Settings::get('file_public_path', \Drupal::service('site.path') . '/translations');
|
Chris@0
|
1072 }
|
Chris@0
|
1073 }
|
Chris@0
|
1074
|
Chris@0
|
1075 if ($name === 'configDirectories') {
|
Chris@0
|
1076 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use config_get_config_directory() directly instead.", $name), E_USER_DEPRECATED);
|
Chris@0
|
1077 return [
|
Chris@0
|
1078 CONFIG_SYNC_DIRECTORY => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
|
Chris@0
|
1079 ];
|
Chris@0
|
1080 }
|
Chris@0
|
1081
|
Chris@0
|
1082 $denied = [
|
Chris@0
|
1083 // @see \Drupal\simpletest\TestBase
|
Chris@0
|
1084 'testId',
|
Chris@0
|
1085 'timeLimit',
|
Chris@0
|
1086 'results',
|
Chris@0
|
1087 'assertions',
|
Chris@0
|
1088 'skipClasses',
|
Chris@0
|
1089 'verbose',
|
Chris@0
|
1090 'verboseId',
|
Chris@0
|
1091 'verboseClassName',
|
Chris@0
|
1092 'verboseDirectory',
|
Chris@0
|
1093 'verboseDirectoryUrl',
|
Chris@0
|
1094 'dieOnFail',
|
Chris@0
|
1095 'kernel',
|
Chris@0
|
1096 // @see \Drupal\simpletest\TestBase::prepareEnvironment()
|
Chris@0
|
1097 'generatedTestFiles',
|
Chris@0
|
1098 // Properties from the old KernelTestBase class that has been removed.
|
Chris@0
|
1099 'keyValueFactory',
|
Chris@0
|
1100 ];
|
Chris@0
|
1101 if (in_array($name, $denied) || strpos($name, 'original') === 0) {
|
Chris@0
|
1102 throw new \RuntimeException(sprintf('TestBase::$%s property no longer exists', $name));
|
Chris@0
|
1103 }
|
Chris@0
|
1104 }
|
Chris@0
|
1105
|
Chris@0
|
1106 /**
|
Chris@0
|
1107 * Prevents serializing any properties.
|
Chris@0
|
1108 *
|
Chris@0
|
1109 * Kernel tests are run in a separate process. To do this PHPUnit creates a
|
Chris@0
|
1110 * script to run the test. If it fails, the test result object will contain a
|
Chris@0
|
1111 * stack trace which includes the test object. It will attempt to serialize
|
Chris@0
|
1112 * it. Returning an empty array prevents it from serializing anything it
|
Chris@0
|
1113 * should not.
|
Chris@0
|
1114 *
|
Chris@0
|
1115 * @return array
|
Chris@0
|
1116 * An empty array.
|
Chris@0
|
1117 *
|
Chris@0
|
1118 * @see vendor/phpunit/phpunit/src/Util/PHP/Template/TestCaseMethod.tpl.dist
|
Chris@0
|
1119 */
|
Chris@0
|
1120 public function __sleep() {
|
Chris@0
|
1121 return [];
|
Chris@0
|
1122 }
|
Chris@0
|
1123
|
Chris@0
|
1124 /**
|
Chris@0
|
1125 * {@inheritdoc}
|
Chris@0
|
1126 */
|
Chris@0
|
1127 public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) {
|
Chris@0
|
1128 // Cast objects implementing MarkupInterface to string instead of
|
Chris@0
|
1129 // relying on PHP casting them to string depending on what they are being
|
Chris@0
|
1130 // comparing with.
|
Chris@0
|
1131 $expected = static::castSafeStrings($expected);
|
Chris@0
|
1132 $actual = static::castSafeStrings($actual);
|
Chris@0
|
1133 parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
|
Chris@0
|
1134 }
|
Chris@0
|
1135
|
Chris@0
|
1136 }
|