You are here

protected function KernelTestBase::installSchema in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::installSchema()
  2. 9 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::installSchema()

Installs database tables 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

\LogicException If $module is not enabled or the table schema cannot be found.

92 calls to KernelTestBase::installSchema()
AccessPermissionTest::setUp in core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
ActionTest::setUp in core/modules/system/tests/src/Kernel/Action/ActionTest.php
ArgumentDefaultTest::testPluginArgumentDefaultCurrentUser in core/modules/user/tests/src/Kernel/Views/ArgumentDefaultTest.php
Tests the current user with argument default.
ArgumentNodeRevisionIdTest::setUp in core/modules/node/tests/src/Kernel/Views/ArgumentNodeRevisionIdTest.php
ArgumentUidRevisionTest::setUp in core/modules/node/tests/src/Kernel/Views/ArgumentUidRevisionTest.php

... See full list

File

core/tests/Drupal/KernelTests/KernelTestBase.php, line 718

Class

KernelTestBase
Base class for functional integration tests.

Namespace

Drupal\KernelTests

Code

protected function installSchema($module, $tables) {

  /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
  $module_handler = $this->container
    ->get('module_handler');

  // Database connection schema is technically able to create database tables
  // using any valid specification, for example of a non-enabled module. But
  // 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 (!$module_handler
    ->moduleExists($module)) {
    throw new \LogicException("{$module} module is not enabled.");
  }
  $specification = SchemaInspector::getTablesSpecification($module_handler, $module);

  /** @var \Drupal\Core\Database\Schema $schema */
  $schema = $this->container
    ->get('database')
    ->schema();
  $tables = (array) $tables;
  foreach ($tables as $table) {
    if (empty($specification[$table])) {
      throw new \LogicException("{$module} module does not define a schema for table '{$table}'.");
    }
    $schema
      ->createTable($table, $specification[$table]);
  }
}