function EntityFieldTest::doTestIntrospection

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

1 call to EntityFieldTest::doTestIntrospection()
EntityFieldTest::testIntrospection in core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
Tests introspection and getting metadata upfront.

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->assertEqual($definitions['name']->getType(), 'string', $entity_type . ': Name field found.');
    $this->assertEqual($definitions['user_id']->getType(), 'entity_reference', $entity_type . ': User field found.');
    $this->assertEqual($definitions['field_test_text']->getType(), 'text', $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->assertEqual($field_item_definition->getDataType(), 'field_item:string');
    $value_definition = $field_item_definition->getPropertyDefinition('value');
    $this->assertInstanceOf(DataDefinitionInterface::class, $value_definition);
    $this->assertEqual($value_definition->getDataType(), 'string');
    // 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->assertEqual($reference_definition->getDataType(), 'language');
    $reference_definition = $entity_definition->getPropertyDefinition('user_id')
        ->getPropertyDefinition('entity')
        ->getTargetDefinition();
    $this->assertInstanceOf(EntityDataDefinitionInterface::class, $reference_definition);
    $this->assertEqual($reference_definition->getEntityTypeId(), 'user', 'Referenced entity is of type "user".');
    // Test propagating down.
    $name_definition = $reference_definition->getPropertyDefinition('name');
    $this->assertInstanceOf(FieldDefinitionInterface::class, $name_definition);
    $this->assertEqual($name_definition->getPropertyDefinition('value')
        ->getDataType(), 'string');
    // 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->assertEqual($definitions['name']->getType(), 'string', $entity_type . ': Name field found.');
    $this->assertEqual($definitions['user_id']->getType(), 'entity_reference', $entity_type . ': User field found.');
    $this->assertEqual($definitions['field_test_text']->getType(), 'text', $entity_type . ': Test-text-field field found.');
    $name_properties = $entity->name
        ->getFieldDefinition()
        ->getPropertyDefinitions();
    $this->assertEqual($name_properties['value']->getDataType(), 'string', $entity_type . ': String value property of the name found.');
    $userref_properties = $entity->user_id
        ->getFieldDefinition()
        ->getPropertyDefinitions();
    $this->assertEqual($userref_properties['target_id']->getDataType(), 'integer', $entity_type . ': Entity id property of the user found.');
    $this->assertEqual($userref_properties['entity']->getDataType(), 'entity_reference', $entity_type . ': Entity reference property of the user found.');
    $textfield_properties = $entity->field_test_text
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getPropertyDefinitions();
    $this->assertEqual($textfield_properties['value']->getDataType(), 'string', $entity_type . ': String value property of the test-text field found.');
    $this->assertEqual($textfield_properties['format']->getDataType(), 'filter_format', $entity_type . ': String format field of the test-text field found.');
    $this->assertEqual($textfield_properties['processed']->getDataType(), 'string', $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->assertEqual($entity_adapter->getPropertyPath(), '');
    $this->assertEqual($entity_adapter->getName(), '');
    $this->assertEqual($entity_adapter->getParent(), NULL);
    $field = $entity->user_id;
    $this->assertSame($field->getRoot()
        ->getValue(), $entity, 'Entity is root object.');
    $this->assertSame($field->getEntity(), $entity, 'getEntity() returns the entity.');
    $this->assertEqual($field->getPropertyPath(), 'user_id');
    $this->assertEqual($field->getName(), 'user_id');
    $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->assertEqual($field_item->getPropertyPath(), 'user_id.0');
    $this->assertEqual($field_item->getName(), '0');
    $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->assertEqual($item_value->getPropertyPath(), 'user_id.0.entity');
    $this->assertEqual($item_value->getName(), 'entity');
    $this->assertSame($item_value->getParent(), $field_item, 'Parent object matches.');
}

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