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.
<?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->createUser();
$container = \Drupal::getContainer();
$container->get('current_user')->setAccount($user);
// Create some aquifer pieces of content
$node = Node::create(array(
'title' => t('Some title'),
'type' => 'my_custom_content_type',
'language' => 'en',
'field_my_custom_field_1' => 'Put stuff here',
'field_my_custom_field_2' => 'more stuff',
'field_my_custom_field' => 1000000,
));
$node->save();
$reloaded_entity = $this->reloadEntity($node);
$this->assertSame('debug', $reloaded_entity);
}
}
The debug assert statement shows that the reloaded entity does not contain my custom fields.
If I put a $node->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.