function KernelTestBase::installSchema

Same name in this branch
  1. 8.9.x core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::installSchema()
Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::installSchema()
  2. 10 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::installSchema()
  3. 11.x core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\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,
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.