You are here

protected function KernelTestBase::installSchema in SimpleTest 8.3

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.

1 call to KernelTestBase::installSchema()
KernelTestBaseTest::testInstallSchema in src/Tests/KernelTestBaseTest.php
Tests expected behavior of installSchema().

File

src/KernelTestBase.php, line 454

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,
  ]));
}