You are here

protected function KernelTestBase::installSchema in Drupal 8

Same name in this branch
  1. 8 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::installSchema()
  2. 8 core/modules/simpletest/src/KernelTestBase.php \Drupal\simpletest\KernelTestBase::installSchema()

Installs a specific table from a module schema definition.

Parameters

string $module: The name of the module that defines the table's schema.

string|array $tables: The name or an array of the names of the tables to install.

Throws

\RuntimeException Thrown when $module is not enabled or when the table schema cannot be found in the module specified.

4 calls to KernelTestBase::installSchema()
EntityUnitTestBase::setUp in core/modules/system/src/Tests/Entity/EntityUnitTestBase.php
Performs setup tasks before each individual test method is run.
KernelTestBaseTest::testInstallSchema in core/modules/simpletest/src/Tests/KernelTestBaseTest.php
Tests expected behavior of installSchema().
ViewKernelTestBase::setUp in core/modules/views/src/Tests/ViewKernelTestBase.php
ViewKernelTestBase::setUpFixtures in core/modules/views/src/Tests/ViewKernelTestBase.php
Sets up the configuration and schema of views and views_test_data modules.

File

core/modules/simpletest/src/KernelTestBase.php, line 460

Class

KernelTestBase
Base class for functional integration tests.

Namespace

Drupal\simpletest

Code

protected function installSchema($module, $tables) {

  // drupal_get_module_schema() is technically able to install a schema
  // 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)) {
    throw new \RuntimeException("'{$module}' module is not enabled");
  }
  $tables = (array) $tables;
  foreach ($tables as $table) {
    $schema = drupal_get_module_schema($module, $table);
    if (empty($schema)) {

      // 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;
      }
      throw new \RuntimeException("Unknown '{$table}' table schema in '{$module}' module.");
    }
    $this->container
      ->get('database')
      ->schema()
      ->createTable($table, $schema);
  }
  $this
    ->pass(new FormattableMarkup('Installed %module tables: %tables.', [
    '%tables' => '{' . implode('}, {', $tables) . '}',
    '%module' => $module,
  ]));
}