You are here

protected function KernelTestBase::installSchema in Drupal 9

Same name and namespace in other branches
  1. 8 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.

225 calls to KernelTestBase::installSchema()
AccessPermissionTest::setUp in core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
AccessTest::setUp in core/modules/file/tests/src/Kernel/AccessTest.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

... See full list

File

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

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) {

    // The tables key_value and key_value_expire are lazy loaded and therefore
    // no longer have to be created with the installSchema() method.
    // @see https://www.drupal.org/node/3143286
    if ($module === 'system' && in_array($table, [
      'key_value',
      'key_value_expire',
    ])) {
      @trigger_error('Installing the tables key_value and key_value_expire with the method KernelTestBase::installSchema() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. The tables are now lazy loaded and therefore will be installed automatically when used. See https://www.drupal.org/node/3143286', E_USER_DEPRECATED);
      continue;
    }
    if (empty($specification[$table])) {
      throw new \LogicException("{$module} module does not define a schema for table '{$table}'.");
    }
    $schema
      ->createTable($table, $specification[$table]);
  }
}