You are here

protected function EntityFieldTest::doTestIntrospection in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestIntrospection()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestIntrospection()

Executes the introspection tests for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php, line 417

Class

EntityFieldTest
Tests the Entity Field API.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestIntrospection($entity_type) {

  // Test getting metadata upfront. The entity types used for this test have
  // a default bundle that is the same as the entity type.
  $definitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions($entity_type, $entity_type);
  $this
    ->assertEquals('string', $definitions['name']
    ->getType(), $entity_type . ': Name field found.');
  $this
    ->assertEquals('entity_reference', $definitions['user_id']
    ->getType(), $entity_type . ': User field found.');
  $this
    ->assertEquals('text', $definitions['field_test_text']
    ->getType(), $entity_type . ': Test-text-field field found.');

  // Test deriving further metadata.
  $this
    ->assertInstanceOf(FieldDefinitionInterface::class, $definitions['name']);
  $field_item_definition = $definitions['name']
    ->getItemDefinition();
  $this
    ->assertInstanceOf(ComplexDataDefinitionInterface::class, $field_item_definition);
  $this
    ->assertEquals('field_item:string', $field_item_definition
    ->getDataType());
  $value_definition = $field_item_definition
    ->getPropertyDefinition('value');
  $this
    ->assertInstanceOf(DataDefinitionInterface::class, $value_definition);
  $this
    ->assertEquals('string', $value_definition
    ->getDataType());

  // Test deriving metadata from references.
  $entity_definition = EntityDataDefinition::create($entity_type);
  $langcode_key = $this->entityTypeManager
    ->getDefinition($entity_type)
    ->getKey('langcode');
  $reference_definition = $entity_definition
    ->getPropertyDefinition($langcode_key)
    ->getPropertyDefinition('language')
    ->getTargetDefinition();
  $this
    ->assertEquals('language', $reference_definition
    ->getDataType());
  $reference_definition = $entity_definition
    ->getPropertyDefinition('user_id')
    ->getPropertyDefinition('entity')
    ->getTargetDefinition();
  $this
    ->assertInstanceOf(EntityDataDefinitionInterface::class, $reference_definition);
  $this
    ->assertEquals('user', $reference_definition
    ->getEntityTypeId(), 'Referenced entity is of type "user".');

  // Test propagating down.
  $name_definition = $reference_definition
    ->getPropertyDefinition('name');
  $this
    ->assertInstanceOf(FieldDefinitionInterface::class, $name_definition);
  $this
    ->assertEquals('string', $name_definition
    ->getPropertyDefinition('value')
    ->getDataType());

  // Test introspecting an entity object.
  // @todo: Add bundles and test bundles as well.
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create();
  $definitions = $entity
    ->getFieldDefinitions();
  $this
    ->assertEquals('string', $definitions['name']
    ->getType(), $entity_type . ': Name field found.');
  $this
    ->assertEquals('entity_reference', $definitions['user_id']
    ->getType(), $entity_type . ': User field found.');
  $this
    ->assertEquals('text', $definitions['field_test_text']
    ->getType(), $entity_type . ': Test-text-field field found.');
  $name_properties = $entity->name
    ->getFieldDefinition()
    ->getPropertyDefinitions();
  $this
    ->assertEquals('string', $name_properties['value']
    ->getDataType(), $entity_type . ': String value property of the name found.');
  $userref_properties = $entity->user_id
    ->getFieldDefinition()
    ->getPropertyDefinitions();
  $this
    ->assertEquals('integer', $userref_properties['target_id']
    ->getDataType(), $entity_type . ': Entity id property of the user found.');
  $this
    ->assertEquals('entity_reference', $userref_properties['entity']
    ->getDataType(), $entity_type . ': Entity reference property of the user found.');
  $textfield_properties = $entity->field_test_text
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getPropertyDefinitions();
  $this
    ->assertEquals('string', $textfield_properties['value']
    ->getDataType(), $entity_type . ': String value property of the test-text field found.');
  $this
    ->assertEquals('filter_format', $textfield_properties['format']
    ->getDataType(), $entity_type . ': String format field of the test-text field found.');
  $this
    ->assertEquals('string', $textfield_properties['processed']
    ->getDataType(), $entity_type . ': String processed property of the test-text field found.');

  // Make sure provided contextual information is right.
  $entity_adapter = $entity
    ->getTypedData();
  $this
    ->assertSame($entity_adapter
    ->getRoot(), $entity_adapter, 'Entity is root object.');
  $this
    ->assertEquals('', $entity_adapter
    ->getPropertyPath());
  $this
    ->assertEquals('', $entity_adapter
    ->getName());
  $this
    ->assertNull($entity_adapter
    ->getParent());
  $field = $entity->user_id;
  $this
    ->assertSame($field
    ->getRoot()
    ->getValue(), $entity, 'Entity is root object.');
  $this
    ->assertSame($field
    ->getEntity(), $entity, 'getEntity() returns the entity.');
  $this
    ->assertEquals('user_id', $field
    ->getPropertyPath());
  $this
    ->assertEquals('user_id', $field
    ->getName());
  $this
    ->assertSame($field
    ->getParent()
    ->getValue(), $entity, 'Parent object matches.');
  $field_item = $field[0];
  $this
    ->assertSame($field_item
    ->getRoot()
    ->getValue(), $entity, 'Entity is root object.');
  $this
    ->assertSame($field_item
    ->getEntity(), $entity, 'getEntity() returns the entity.');
  $this
    ->assertEquals('user_id.0', $field_item
    ->getPropertyPath());
  $this
    ->assertEquals('0', $field_item
    ->getName());
  $this
    ->assertSame($field_item
    ->getParent(), $field, 'Parent object matches.');
  $item_value = $field_item
    ->get('entity');
  $this
    ->assertSame($item_value
    ->getRoot()
    ->getValue(), $entity, 'Entity is root object.');
  $this
    ->assertEquals('user_id.0.entity', $item_value
    ->getPropertyPath());
  $this
    ->assertEquals('entity', $item_value
    ->getName());
  $this
    ->assertSame($item_value
    ->getParent(), $field_item, 'Parent object matches.');
}