Setup tasks in Kernel tests

Last updated on
13 November 2023

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']);

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();

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: