Setup tasks in Kernel tests

Last updated on
19 December 2025

This documentation needs work. See "Help improve this page" in the sidebar.

Because Kernel tests don't perform a full installation of modules, you need to manually perform some of the installation and configuration steps in the test. This is typically done in the test class's setUp() method.

Installing an entity type

A content entity type's schema must be installed as follows:

$this->installEntitySchema('node');

Note that depending on the operations your test carries out, some entity types may need additional tables installing, for example:

// Needed for some node operations.
$this->installSchema('node', 'node_access');

// Needed for some file operations.
$this->installSchema('file', 'file_usage');

// Needed for some user operations.
$this->installSchema('system', ['sequences']);

Create fields

You can create config fields thus:

$field_storage = $this->entityTypeManager->getStorage('field_storage_config')->create([
  'field_name' => 'test_integer',
  'entity_type' => 'entity_test',
  'type' => 'integer',
]);
$field_storage->save();
$field = $this->entityTypeManager->getStorage('field_config')->create([
  'field_name' => 'test_integer',
  'entity_type' => 'entity_test',
  'bundle' => 'entity_test',
  'label' => 'Label',
]);
$field->save();

but if you are using any of the test entity types from the entity_test module, their base fields can be set in state, like this:

// The state key is formed from the entity type ID.
$this->container->get('state')->set('entity_test.additional_base_field_definitions', [
  'test_integer' => BaseFieldDefinition::create('integer'),
]);

// Install the entity type afterwards!
$this->installEntitySchema('entity_test');

This means your test has less setup to do, as no config entities need to be created.

Installing module config

Configuration defined in a module's config/install folder can be installed as follows:

$this->installConfig(['my_module']);

Installing database tables

Tables defined in a module's hook_schema() are installed as follows:

$this->installSchema('my_module', ['table_name_one', 'table_name_two']);

Setting config

Setting up config that's different from a module's config defaults can be done as follows:

$config = $this->config('system.date');
$config->set('country.default', 'UK');
$config->save();

Configuring Drupal settings

Configuration done in settings.php can be done in a kernel test with the setSetting() method:

$test_base_url = 'http://www.example.com/cdn';
$this->setSetting('file_public_base_url', $test_base_url);

Configuring languages

// Configure languages.
$this->installConfig(['language']);

// Add a second language, e.g. German.
ConfigurableLanguage::create(['id' => 'de'])->save();

// Configure the node type to be translatable.
$config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
$config->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT);
$config->setLanguageAlterable(TRUE);
$config->save();

$content_translation_manager = $this->container->get('content_translation.manager');
$content_translation_manager->setEnabled('node', 'page', TRUE);

// This is optional.
$content_translation_manager->setBundleTranslationSettings('node', 'article', [
  'untranslatable_fields_hide' => FALSE,
]);

// Configure language negotiation.
// This will make the German translation of a node be available 
// at '/de/node/NID'.
$config = $this->config('language.negotiation');
$config->set('url.prefixes', ['en' => 'en', 'de' => 'de'])
  ->save();

// The kernel needs to be rebuild after translation changes have been made.
$this->rebuildContainer();

Configuring permissions

If you render anything, or make HTTP requests, you will probably want to set permissions for the anonymous user:

use Drupal\Core\Session\AccountInterface;

// Install the user entity.
$this->installEntitySchema('user');
// The config from user module creates the anonymous role.
$this->installConfig('user');

// Grant the permissions.
$role = $this->entityTypeManager->getStorage('user_role')->load(AccountInterface::ANONYMOUS_ROLE);
$role->grantPermission('my permission')
      ->save();

Installing and uninstalling modules

If a test needs to uninstall or install modules as part of the test itself:

$this->container->get('module_installer')->uninstall(['comment']);
$status = $this->container->get('module_installer')->install(['book']);

Help improve this page

Page status: Needs work

You can: