What to set up in a Kernel test to get custom content type to be available I have a custom module that defines a service and a custom content type. I have successfully written a Functional test for the service. Now, I'm seeing if I can write a Kernel test instead.

The issue that I'm encountering is that my custom content type has custom fields (naturally). When I create a piece of content of that type in the Kernel test environment, Drupal is not recognizing the custom fields.


I am using the EntityKernelTestBase.
I have set public static $modules = array('my_custom_module', 'node');


What piece(s) am I missing? I'm guessing there is some schema I need to say to install or some other method I need to call so that those custom fields are recognized.

&lt;?php

namespace Drupal\Tests\my_custom_module\Kernel;

use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;

/**
 * Tests the my custom service.
 *
 * @group my_custom_module
 */
class MyCustomServiceKernelTest extends EntityKernelTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  public static $modules = array('my_custom_module', 'node');

  /**
   * {@inheritdoc}
   */
  public function setUp() {

    parent::setup();

    $user = $this-&gt;createUser();

    $container = \Drupal::getContainer();
    $container-&gt;get('current_user')-&gt;setAccount($user);

    // Create some aquifer pieces of content
    $node = Node::create(array(
      'title' =&gt; t('Some title'),
      'type' =&gt; 'my_custom_content_type',
      'language' =&gt; 'en',
      'field_my_custom_field_1' =&gt; 'Put stuff here',
      'field_my_custom_field_2' =&gt; 'more stuff',
      'field_my_custom_field' =&gt; 1000000,
    ));
    $node-&gt;save();
    $reloaded_entity = $this-&gt;reloadEntity($node);
    $this-&gt;assertSame('debug', $reloaded_entity);
  }
}


The debug assert statement shows that the reloaded entity does not contain my custom fields.

If I put a $node-&gt;set('field_my_custom_field_1', 'some value') statement in the code instead of passing those fields in the Node::create logic I get an error that the field name is not valid.
For kernel tests you need to specify which configurations you want installed. For instance, you'll need something like this in your test's setUp() method:

$this-&gt;installConfig(['my_custom_module']);


That takes an array of modules, so you'll probably need to specify additional modules (like filter, node, and field potentially).

This assumes your field configurations are bundled in your module's config/install directory.