KernelTestBase.php 25.4 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\simpletest;

5
use Drupal\Component\Utility\Html;
6
use Drupal\Component\Render\FormattableMarkup;
7
use Drupal\Component\Utility\Variable;
8
use Drupal\Core\Config\Development\ConfigSchemaChecker;
9 10 11
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DrupalKernel;
12
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
13
use Drupal\Core\Extension\ExtensionDiscovery;
14 15
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\Language\Language;
16
use Drupal\Core\Site\Settings;
17
use Drupal\KernelTests\TestServiceProvider;
18
use Symfony\Component\DependencyInjection\Parameter;
19
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
20 21 22 23
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;

/**
24
 * Base class for functional integration tests.
25
 *
26 27 28
 * This base class should be useful for testing some types of integrations which
 * don't require the overhead of a fully-installed Drupal instance, but which
 * have many dependencies on parts of Drupal which can't or shouldn't be mocked.
29
 *
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 * This base class partially boots a fixture Drupal. The state of the fixture
 * Drupal is comparable to the state of a system during the early part of the
 * installation process.
 *
 * Tests extending this base class can access services and the database, but the
 * system is initially empty. This Drupal runs in a minimal mocked filesystem
 * which operates within vfsStream.
 *
 * Modules specified in the $modules property are added to the service container
 * for each test. The module/hook system is functional. Additional modules
 * needed in a test should override $modules. Modules specified in this way will
 * be added to those specified in superclasses.
 *
 * Unlike \Drupal\Tests\BrowserTestBase, the modules are not installed. They are
 * loaded such that their services and hooks are available, but the install
 * process has not been performed.
 *
 * Other modules can be made available in this way using
 * KernelTestBase::enableModules().
 *
 * Some modules can be brought into a fully-installed state using
 * KernelTestBase::installConfig(), KernelTestBase::installSchema(), and
 * KernelTestBase::installEntitySchema(). Alternately, tests which need modules
 * to be fully installed could inherit from \Drupal\Tests\BrowserTestBase.
 *
 * @see \Drupal\Tests\KernelTestBase::$modules
 * @see \Drupal\Tests\KernelTestBase::enableModules()
 * @see \Drupal\Tests\KernelTestBase::installConfig()
 * @see \Drupal\Tests\KernelTestBase::installEntitySchema()
 * @see \Drupal\Tests\KernelTestBase::installSchema()
 * @see \Drupal\Tests\BrowserTestBase
61
 *
62
 * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
63 64
 *   \Drupal\KernelTests\KernelTestBase instead.
 *
65
 * @ingroup testing
66
 */
67
abstract class KernelTestBase extends TestBase {
68

69 70
  use AssertContentTrait;

71 72 73 74 75 76 77 78
  /**
   * Modules to enable.
   *
   * Test classes extending this class, and any classes in the hierarchy up to
   * this class, may specify individual lists of modules to enable by setting
   * this property. The values of all properties in all classes in the hierarchy
   * are merged.
   *
79 80
   * Any modules specified in the $modules property are automatically loaded and
   * set as the fixed module list.
81 82 83 84 85 86 87 88 89
   *
   * Unlike WebTestBase::setUp(), the specified modules are loaded only, but not
   * automatically installed. Modules need to be installed manually, if needed.
   *
   * @see \Drupal\simpletest\KernelTestBase::enableModules()
   * @see \Drupal\simpletest\KernelTestBase::setUp()
   *
   * @var array
   */
90
  public static $modules = [];
91 92 93 94 95 96 97 98 99

  private $moduleFiles;
  private $themeFiles;

  /**
   * The configuration directories for this test run.
   *
   * @var array
   */
100
  protected $configDirectories = [];
101 102 103 104

  /**
   * A KeyValueMemoryFactory instance to use when building the container.
   *
105
   * @var \Drupal\Core\KeyValueStore\KeyValueMemoryFactory
106 107 108 109
   */
  protected $keyValueFactory;

  /**
110
   * Array of registered stream wrappers.
111 112 113
   *
   * @var array
   */
114
  protected $streamWrappers = [];
115 116 117 118

  /**
   * {@inheritdoc}
   */
119
  public function __construct($test_id = NULL) {
120 121 122 123 124 125 126 127 128 129
    parent::__construct($test_id);
    $this->skipClasses[__CLASS__] = TRUE;
  }

  /**
   * {@inheritdoc}
   */
  protected function beforePrepareEnvironment() {
    // Copy/prime extension file lists once to avoid filesystem scans.
    if (!isset($this->moduleFiles)) {
130 131
      $this->moduleFiles = \Drupal::state()->get('system.module.files') ?: [];
      $this->themeFiles = \Drupal::state()->get('system.theme.files') ?: [];
132 133 134 135 136 137 138
    }
  }

  /**
   * Create and set new configuration directories.
   *
   * @see config_get_config_directory()
139 140
   *
   * @throws \RuntimeException
141
   *   Thrown when CONFIG_SYNC_DIRECTORY cannot be created or made writable.
142 143
   */
  protected function prepareConfigDirectories() {
144
    $this->configDirectories = [];
145
    include_once DRUPAL_ROOT . '/core/includes/install.inc';
146
    // Assign the relative path to the global variable.
147 148
    $path = $this->siteDirectory . '/config_' . CONFIG_SYNC_DIRECTORY;
    $GLOBALS['config_directories'][CONFIG_SYNC_DIRECTORY] = $path;
149
    // Ensure the directory can be created and is writeable.
150
    if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
151
      throw new \RuntimeException("Failed to create '" . CONFIG_SYNC_DIRECTORY . "' config directory $path");
152
    }
153
    // Provide the already resolved path for tests.
154
    $this->configDirectories[CONFIG_SYNC_DIRECTORY] = $path;
155 156 157 158 159 160 161 162
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->keyValueFactory = new KeyValueMemoryFactory();

163 164 165
    // Back up settings from TestBase::prepareEnvironment().
    $settings = Settings::getAll();

166
    // Allow for test-specific overrides.
167
    $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
168
    $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
169
    $container_yamls = [];
170 171
    if (file_exists($settings_services_file)) {
      // Copy the testing-specific service overrides in place.
172
      $testing_services_file = $directory . '/services.yml';
173
      copy($settings_services_file, $testing_services_file);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      $container_yamls[] = $testing_services_file;
    }
    $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php';
    if (file_exists($settings_testing_file)) {
      // Copy the testing-specific settings.php overrides in place.
      copy($settings_testing_file, $directory . '/settings.testing.php');
    }

    if (file_exists($directory . '/settings.testing.php')) {
      // Add the name of the testing class to settings.php and include the
      // testing specific overrides
      $hash_salt = Settings::getHashSalt();
      $test_class = get_class($this);
      $container_yamls_export = Variable::export($container_yamls);
      $php = <<<EOD
<?php

\$settings['hash_salt'] = '$hash_salt';
\$settings['container_yamls'] = $container_yamls_export;

\$test_class = '$test_class';
include DRUPAL_ROOT . '/' . \$site_path . '/settings.testing.php';
EOD;
      file_put_contents($directory . '/settings.php', $php);
198 199
    }

200 201
    // Add this test class as a service provider.
    // @todo Remove the indirection; implement ServiceProviderInterface instead.
202
    $GLOBALS['conf']['container_service_providers']['TestServiceProvider'] = TestServiceProvider::class;
203

204
    // Bootstrap a new kernel.
205
    $class_loader = require DRUPAL_ROOT . '/autoload.php';
206
    $this->kernel = new DrupalKernel('testing', $class_loader, FALSE);
207
    $request = Request::create('/');
208 209 210 211 212
    $site_path = DrupalKernel::findSitePath($request);
    $this->kernel->setSitePath($site_path);
    if (file_exists($directory . '/settings.testing.php')) {
      Settings::initialize(DRUPAL_ROOT, $site_path, $class_loader);
    }
213
    $this->kernel->boot();
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    // Ensure database install tasks have been run.
    require_once __DIR__ . '/../../../includes/install.inc';
    $connection = Database::getConnection();
    $errors = db_installer_object($connection->driver())->runTasks();
    if (!empty($errors)) {
      $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
    }

    // Reboot the kernel because the container might contain a connection to the
    // database that has been closed during the database install tasks. This
    // prevents any services created during the first boot from having stale
    // database connections, for example, \Drupal\Core\Config\DatabaseStorage.
    $this->kernel->shutdown();
    $this->kernel->boot();

230 231 232 233 234 235
    // Save the original site directory path, so that extensions in the
    // site-specific directory can still be discovered in the test site
    // environment.
    // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
    $settings['test_parent_site'] = $this->originalSite;

236 237 238 239 240
    // Restore and merge settings.
    // DrupalKernel::boot() initializes new Settings, and the containerBuild()
    // method sets additional settings.
    new Settings($settings + Settings::getAll());

241 242 243
    // Create and set new configuration directories.
    $this->prepareConfigDirectories();

244 245
    // Set the request scope.
    $this->container = $this->kernel->getContainer();
246 247
    $this->container->get('request_stack')->push($request);

248 249 250 251 252 253
    // Re-inject extension file listings into state, unless the key/value
    // service was overridden (in which case its storage does not exist yet).
    if ($this->container->get('keyvalue') instanceof KeyValueMemoryFactory) {
      $this->container->get('state')->set('system.module.files', $this->moduleFiles);
      $this->container->get('state')->set('system.theme.files', $this->themeFiles);
    }
254

255 256 257 258 259
    // Create a minimal core.extension configuration object so that the list of
    // enabled modules can be maintained allowing
    // \Drupal\Core\Config\ConfigInstaller::installDefaultConfig() to work.
    // Write directly to active storage to avoid early instantiation of
    // the event dispatcher which can prevent modules from registering events.
260
    \Drupal::service('config.storage')->write('core.extension', ['module' => [], 'theme' => [], 'profile' => '']);
261 262 263

    // Collect and set a fixed module list.
    $class = get_class($this);
264
    $modules = [];
265 266 267 268 269 270 271 272 273 274 275 276 277
    while ($class) {
      if (property_exists($class, 'modules')) {
        // Only add the modules, if the $modules property was not inherited.
        $rp = new \ReflectionProperty($class, 'modules');
        if ($rp->class == $class) {
          $modules[$class] = $class::$modules;
        }
      }
      $class = get_parent_class($class);
    }
    // Modules have been collected in reverse class hierarchy order; modules
    // defined by base classes should be sorted first. Then, merge the results
    // together.
278 279
    $modules = array_reverse($modules);
    $modules = call_user_func_array('array_merge_recursive', $modules);
280
    if ($modules) {
281
      $this->enableModules($modules);
282 283 284 285 286
    }

    // Tests based on this class are entitled to use Drupal's File and
    // StreamWrapper APIs.
    // @todo Move StreamWrapper management into DrupalKernel.
287
    // @see https://www.drupal.org/node/2028109
288 289
    file_prepare_directory($this->publicFilesDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
    $this->settingsSet('file_public_path', $this->publicFilesDirectory);
290
    $this->streamWrappers = [];
291 292 293 294
    $this->registerStreamWrapper('public', 'Drupal\Core\StreamWrapper\PublicStream');
    // The temporary stream wrapper is able to operate both with and without
    // configuration.
    $this->registerStreamWrapper('temporary', 'Drupal\Core\StreamWrapper\TemporaryStream');
295 296 297 298 299 300

    // Manually configure the test mail collector implementation to prevent
    // tests from sending out emails and collect them in state instead.
    // While this should be enforced via settings.php prior to installation,
    // some tests expect to be able to test mail system implementations.
    $GLOBALS['config']['system.mail']['interface']['default'] = 'test_mail_collector';
301 302 303 304 305 306 307 308 309 310 311 312 313 314
  }

  /**
   * {@inheritdoc}
   */
  protected function tearDown() {
    if ($this->kernel instanceof DrupalKernel) {
      $this->kernel->shutdown();
    }
    // Before tearing down the test environment, ensure that no stream wrapper
    // of this test leaks into the parent environment. Unlike all other global
    // state variables in Drupal, stream wrappers are a global state construct
    // of PHP core, which has to be maintained manually.
    // @todo Move StreamWrapper management into DrupalKernel.
315
    // @see https://www.drupal.org/node/2028109
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    foreach ($this->streamWrappers as $scheme => $type) {
      $this->unregisterStreamWrapper($scheme, $type);
    }
    parent::tearDown();
  }

  /**
   * Sets up the base service container for this test.
   *
   * Extend this method in your test to register additional service overrides
   * that need to persist a DrupalKernel reboot. This method is called whenever
   * the kernel is rebuilt.
   *
   * @see \Drupal\simpletest\KernelTestBase::setUp()
   * @see \Drupal\simpletest\KernelTestBase::enableModules()
   * @see \Drupal\simpletest\KernelTestBase::disableModules()
   */
  public function containerBuild(ContainerBuilder $container) {
    // Keep the container object around for tests.
    $this->container = $container;

    // Set the default language on the minimal container.
338
    $this->container->setParameter('language.default_values', $this->defaultLanguageData());
339 340 341 342 343

    $container->register('lock', 'Drupal\Core\Lock\NullLockBackend');
    $container->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');

    $container
344
      ->register('config.storage', 'Drupal\Core\Config\DatabaseStorage')
345 346 347
      ->addArgument(Database::getConnection())
      ->addArgument('config');

348 349
    if ($this->strictConfigSchema) {
      $container
350
        ->register('simpletest.config_schema_checker', ConfigSchemaChecker::class)
351
        ->addArgument(new Reference('config.typed'))
352
        ->addArgument($this->getConfigSchemaExclusions())
353 354 355
        ->addTag('event_subscriber');
    }

356
    $keyvalue_options = $container->getParameter('factory.keyvalue') ?: [];
357 358
    $keyvalue_options['default'] = 'keyvalue.memory';
    $container->setParameter('factory.keyvalue', $keyvalue_options);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    $container->set('keyvalue.memory', $this->keyValueFactory);
    if (!$container->has('keyvalue')) {
      // TestBase::setUp puts a completely empty container in
      // $this->container which is somewhat the mirror of the empty
      // environment being set up. Unit tests need not to waste time with
      // getting a container set up for them. Drupal Unit Tests might just get
      // away with a simple container holding the absolute bare minimum. When
      // a kernel is overridden then there's no need to re-register the keyvalue
      // service but when a test is happy with the superminimal container put
      // together here, it still might a keyvalue storage for anything using
      // \Drupal::state() -- that's why a memory service was added in the first
      // place.
      $container->register('settings', 'Drupal\Core\Site\Settings')
        ->setFactoryClass('Drupal\Core\Site\Settings')
        ->setFactoryMethod('getInstance');

      $container
        ->register('keyvalue', 'Drupal\Core\KeyValueStore\KeyValueFactory')
        ->addArgument(new Reference('service_container'))
378
        ->addArgument(new Parameter('factory.keyvalue'));
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

      $container->register('state', 'Drupal\Core\State\State')
        ->addArgument(new Reference('keyvalue'));
    }

    if ($container->hasDefinition('path_processor_alias')) {
      // Prevent the alias-based path processor, which requires a url_alias db
      // table, from being registered to the path processor manager. We do this
      // by removing the tags that the compiler pass looks for. This means the
      // url generator can safely be used within tests.
      $definition = $container->getDefinition('path_processor_alias');
      $definition->clearTag('path_processor_inbound')->clearTag('path_processor_outbound');
    }

    if ($container->hasDefinition('password')) {
394
      $container->getDefinition('password')->setArguments([1]);
395 396
    }

397 398 399 400
    // Register the stream wrapper manager.
    $container
      ->register('stream_wrapper_manager', 'Drupal\Core\StreamWrapper\StreamWrapperManager')
      ->addArgument(new Reference('module_handler'))
401
      ->addMethodCall('setContainer', [new Reference('service_container')]);
402

403
    $request = Request::create('/');
404
    $container->get('request_stack')->push($request);
405 406
  }

407 408 409 410 411 412 413 414 415 416
  /**
   * Provides the data for setting the default language on the container.
   *
   * @return array
   *   The data array for the default language.
   */
  protected function defaultLanguageData() {
    return Language::$defaultValues;
  }

417 418 419 420 421
  /**
   * Installs default configuration for a given list of modules.
   *
   * @param array $modules
   *   A list of modules for which to install default configuration.
422 423 424
   *
   * @throws \RuntimeException
   *   Thrown when any module listed in $modules is not enabled.
425 426 427 428
   */
  protected function installConfig(array $modules) {
    foreach ($modules as $module) {
      if (!$this->container->get('module_handler')->moduleExists($module)) {
429
        throw new \RuntimeException("'$module' module is not enabled");
430 431 432
      }
      \Drupal::service('config.installer')->installDefaultConfig('module', $module);
    }
433
    $this->pass(format_string('Installed default config: %modules.', [
434
      '%modules' => implode(', ', $modules),
435
    ]));
436 437 438 439 440 441 442 443 444
  }

  /**
   * Installs a specific table from a module schema definition.
   *
   * @param string $module
   *   The name of the module that defines the table's schema.
   * @param string|array $tables
   *   The name or an array of the names of the tables to install.
445 446 447 448
   *
   * @throws \RuntimeException
   *   Thrown when $module is not enabled or when the table schema cannot be
   *   found in the module specified.
449 450
   */
  protected function installSchema($module, $tables) {
451
    // drupal_get_module_schema() is technically able to install a schema
452 453 454 455 456
    // of a non-enabled module, but its ability to load the module's .install
    // file depends on many other factors. To prevent differences in test
    // behavior and non-reproducible test failures, we only allow the schema of
    // explicitly loaded/enabled modules to be installed.
    if (!$this->container->get('module_handler')->moduleExists($module)) {
457
      throw new \RuntimeException("'$module' module is not enabled");
458
    }
459

460 461
    $tables = (array) $tables;
    foreach ($tables as $table) {
462
      $schema = drupal_get_module_schema($module, $table);
463
      if (empty($schema)) {
464 465 466 467 468 469 470
        // BC layer to avoid some contrib tests to fail.
        // @todo Remove the BC layer before 8.1.x release.
        // @see https://www.drupal.org/node/2670360
        // @see https://www.drupal.org/node/2670454
        if ($module == 'system') {
          continue;
        }
471
        throw new \RuntimeException("Unknown '$table' table schema in '$module' module.");
472 473 474
      }
      $this->container->get('database')->schema()->createTable($table, $schema);
    }
475
    $this->pass(format_string('Installed %module tables: %tables.', [
476 477
      '%tables' => '{' . implode('}, {', $tables) . '}',
      '%module' => $module,
478
    ]));
479 480
  }

481
  /**
482
   * Installs the storage schema for a specific entity type.
483 484 485 486 487 488 489
   *
   * @param string $entity_type_id
   *   The ID of the entity type.
   */
  protected function installEntitySchema($entity_type_id) {
    /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
    $entity_manager = $this->container->get('entity.manager');
490 491
    $entity_type = $entity_manager->getDefinition($entity_type_id);
    $entity_manager->onEntityTypeCreate($entity_type);
492

493 494
    // For test runs, the most common storage backend is a SQL database. For
    // this case, ensure the tables got created.
495
    $storage = $entity_manager->getStorage($entity_type_id);
496 497 498 499 500 501
    if ($storage instanceof SqlEntityStorageInterface) {
      $tables = $storage->getTableMapping()->getTableNames();
      $db_schema = $this->container->get('database')->schema();
      $all_tables_exist = TRUE;
      foreach ($tables as $table) {
        if (!$db_schema->tableExists($table)) {
502
          $this->fail(new FormattableMarkup('Installed entity type table for the %entity_type entity type: %table', [
503 504
            '%entity_type' => $entity_type_id,
            '%table' => $table,
505
          ]));
506 507 508 509
          $all_tables_exist = FALSE;
        }
      }
      if ($all_tables_exist) {
510
        $this->pass(new FormattableMarkup('Installed entity type tables for the %entity_type entity type: %tables', [
511 512
          '%entity_type' => $entity_type_id,
          '%tables' => '{' . implode('}, {', $tables) . '}',
513
        ]));
514 515 516 517
      }
    }
  }

518 519 520
  /**
   * Enables modules for this test.
   *
521 522 523
   * To install test modules outside of the testing environment, add
   * @code
   * $settings['extension_discovery_scan_tests'] = TRUE;
524
   * @endcode
525 526
   * to your settings.php.
   *
527 528 529 530 531 532
   * @param array $modules
   *   A list of modules to enable. Dependencies are not resolved; i.e.,
   *   multiple modules have to be specified with dependent modules first.
   *   The new modules are only added to the active module list and loaded.
   */
  protected function enableModules(array $modules) {
533 534 535 536 537 538 539 540
    // Perform an ExtensionDiscovery scan as this function may receive a
    // profile that is not the current profile, and we don't yet have a cached
    // way to receive inactive profile information.
    // @todo Remove as part of https://www.drupal.org/node/2186491
    $listing = new ExtensionDiscovery(\Drupal::root());
    $module_list = $listing->scan('module');
    // In ModuleHandlerTest we pass in a profile as if it were a module.
    $module_list += $listing->scan('profile');
541 542 543 544 545
    // Set the list of modules in the extension handler.
    $module_handler = $this->container->get('module_handler');

    // Write directly to active storage to avoid early instantiation of
    // the event dispatcher which can prevent modules from registering events.
546
    $active_storage = \Drupal::service('config.storage');
547 548 549
    $extensions = $active_storage->read('core.extension');

    foreach ($modules as $module) {
550
      $module_handler->addModule($module, $module_list[$module]->getPath());
551 552 553 554 555 556 557 558 559
      // Maintain the list of enabled modules in configuration.
      $extensions['module'][$module] = 0;
    }
    $active_storage->write('core.extension', $extensions);

    // Update the kernel to make their services available.
    $module_filenames = $module_handler->getModuleList();
    $this->kernel->updateModules($module_filenames, $module_filenames);

560 561
    // Ensure isLoaded() is TRUE in order to make
    // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
562 563
    // Note that the kernel has rebuilt the container; this $module_handler is
    // no longer the $module_handler instance from above.
564
    $this->container->get('module_handler')->reload();
565
    $this->pass(format_string('Enabled modules: %modules.', [
566
      '%modules' => implode(', ', $modules),
567
    ]));
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  }

  /**
   * Disables modules for this test.
   *
   * @param array $modules
   *   A list of modules to disable. Dependencies are not resolved; i.e.,
   *   multiple modules have to be specified with dependent modules first.
   *   Code of previously active modules is still loaded. The modules are only
   *   removed from the active module list.
   */
  protected function disableModules(array $modules) {
    // Unset the list of modules in the extension handler.
    $module_handler = $this->container->get('module_handler');
    $module_filenames = $module_handler->getModuleList();
583
    $extension_config = $this->config('core.extension');
584 585 586 587 588 589 590 591 592 593
    foreach ($modules as $module) {
      unset($module_filenames[$module]);
      $extension_config->clear('module.' . $module);
    }
    $extension_config->save();
    $module_handler->setModuleList($module_filenames);
    $module_handler->resetImplementations();
    // Update the kernel to remove their services.
    $this->kernel->updateModules($module_filenames, $module_filenames);

594 595
    // Ensure isLoaded() is TRUE in order to make
    // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
596 597 598 599
    // Note that the kernel has rebuilt the container; this $module_handler is
    // no longer the $module_handler instance from above.
    $module_handler = $this->container->get('module_handler');
    $module_handler->reload();
600
    $this->pass(format_string('Disabled modules: %modules.', [
601
      '%modules' => implode(', ', $modules),
602
    ]));
603 604 605 606 607 608 609 610 611 612 613
  }

  /**
   * Registers a stream wrapper for this test.
   *
   * @param string $scheme
   *   The scheme to register.
   * @param string $class
   *   The fully qualified class name to register.
   * @param int $type
   *   The Drupal Stream Wrapper API type. Defaults to
614
   *   StreamWrapperInterface::NORMAL.
615
   */
616 617
  protected function registerStreamWrapper($scheme, $class, $type = StreamWrapperInterface::NORMAL) {
    $this->container->get('stream_wrapper_manager')->registerWrapper($scheme, $class, $type);
618 619
  }

620 621 622 623 624 625 626 627 628
  /**
   * Renders a render array.
   *
   * @param array $elements
   *   The elements to render.
   *
   * @return string
   *   The rendered string output (typically HTML).
   */
629
  protected function render(array &$elements) {
630 631
    // Use the bare HTML page renderer to render our links.
    $renderer = $this->container->get('bare_html_page_renderer');
632
    $response = $renderer->renderBarePage($elements, '', 'maintenance_page');
633 634 635

    // Glean the content from the response object.
    $content = $response->getContent();
636
    $this->setRawContent($content);
637
    $this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
638 639 640
    return $content;
  }

641
}