function EntityTypedDataDefinitionTest::testEntities

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php \Drupal\KernelTests\Core\Entity\EntityTypedDataDefinitionTest::testEntities()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php \Drupal\KernelTests\Core\Entity\EntityTypedDataDefinitionTest::testEntities()
  3. 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 88

Class

EntityTypedDataDefinitionTest
Tests deriving metadata of entity and field data types.

Namespace

Drupal\KernelTests\Core\Entity

Code

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->assertEqual(serialize($field_definitions), serialize(\Drupal::service('entity_field.manager')->getBaseFieldDefinitions('node')));
    $this->assertEqual($entity_definition->getPropertyDefinition('title')
        ->getItemDefinition()
        ->getDataType(), 'field_item:string');
    $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->assertEqual(serialize($entity_definition), serialize($entity_definition2));
    // Test that the definition factory creates the right definitions for all
    // entity data types variants.
    $this->assertEqual(serialize($this->typedDataManager
        ->createDataDefinition('entity')), serialize(EntityDataDefinition::create()));
    $this->assertEqual(serialize($this->typedDataManager
        ->createDataDefinition('entity:node')), serialize(EntityDataDefinition::create('node')));
    // Config entities don't support typed data.
    $entity_definition = EntityDataDefinition::create('node_type');
    $this->assertEqual([], $entity_definition->getPropertyDefinitions());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.