function EntityTypedDataDefinitionTest::testEntities
Same name in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php \Drupal\KernelTests\Core\Entity\EntityTypedDataDefinitionTest::testEntities()
- 10 core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php \Drupal\KernelTests\Core\Entity\EntityTypedDataDefinitionTest::testEntities()
- 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php \Drupal\KernelTests\Core\Entity\EntityTypedDataDefinitionTest::testEntities()
Tests deriving metadata about entities.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityTypedDataDefinitionTest.php, line 91
Class
- EntityTypedDataDefinitionTest
- Tests deriving metadata of entity and field data types.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testEntities() {
NodeType::create([
'type' => 'article',
'name' => 'Article',
])->save();
$entity_definition = EntityDataDefinition::create('node');
$bundle_definition = EntityDataDefinition::create('node', 'article');
// Entities are complex data.
$this->assertNotInstanceOf(ListDataDefinitionInterface::class, $entity_definition);
$this->assertInstanceOf(ComplexDataDefinitionInterface::class, $entity_definition);
// Entity definitions should inherit their labels from the entity type.
$this->assertEquals('Content', $entity_definition->getLabel());
$this->assertEquals('Article', $bundle_definition->getLabel());
$field_definitions = $entity_definition->getPropertyDefinitions();
// Comparison should ignore the internal static cache, so compare the
// serialized objects instead.
$this->assertEquals(serialize(\Drupal::service('entity_field.manager')->getBaseFieldDefinitions('node')), serialize($field_definitions));
$this->assertEquals('field_item:string', $entity_definition->getPropertyDefinition('title')
->getItemDefinition()
->getDataType());
$this->assertNull($entity_definition->getMainPropertyName());
$this->assertNull($entity_definition->getPropertyDefinition('invalid'));
$entity_definition2 = $this->typedDataManager
->createDataDefinition('entity:node');
$this->assertNotInstanceOf(ListDataDefinitionInterface::class, $entity_definition2);
$this->assertInstanceOf(ComplexDataDefinitionInterface::class, $entity_definition2);
$this->assertEquals(serialize($entity_definition), serialize($entity_definition2));
// Test that the definition factory creates the right definitions for all
// entity data types variants.
$this->assertEquals(serialize(EntityDataDefinition::create()), serialize($this->typedDataManager
->createDataDefinition('entity')));
$this->assertEquals(serialize(EntityDataDefinition::create('node')), serialize($this->typedDataManager
->createDataDefinition('entity:node')));
// Config entities don't support typed data.
$entity_definition = EntityDataDefinition::create('node_type');
$this->assertEquals([], $entity_definition->getPropertyDefinitions());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.