protected function EntityFieldTest::doTestIntrospection in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/EntityFieldTest.php \Drupal\system\Tests\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/
modules/ system/ src/ Tests/ Entity/ EntityFieldTest.php - Tests introspection and getting metadata upfront.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityFieldTest.php, line 399 - Contains \Drupal\system\Tests\Entity\EntityFieldTest.
Class
- EntityFieldTest
- Tests the Entity Field API.
Namespace
Drupal\system\Tests\EntityCode
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::entityManager()
->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
->assertTrue($definitions['name'] instanceof FieldDefinitionInterface);
$field_item_definition = $definitions['name']
->getItemDefinition();
$this
->assertTrue($field_item_definition instanceof ComplexDataDefinitionInterface);
$this
->assertEqual($field_item_definition
->getDataType(), 'field_item:string');
$value_definition = $field_item_definition
->getPropertyDefinition('value');
$this
->assertTrue($value_definition instanceof DataDefinitionInterface);
$this
->assertEqual($value_definition
->getDataType(), 'string');
// Test deriving metadata from references.
$entity_definition = \Drupal\Core\Entity\TypedData\EntityDataDefinition::create($entity_type);
$langcode_key = $this->entityManager
->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
->assertTrue($reference_definition instanceof \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface, 'Definition of the referenced user retrieved.');
$this
->assertEqual($reference_definition
->getEntityTypeId(), 'user', 'Referenced entity is of type "user".');
// Test propagating down.
$name_definition = $reference_definition
->getPropertyDefinition('name');
$this
->assertTrue($name_definition instanceof FieldDefinitionInterface);
$this
->assertEqual($name_definition
->getPropertyDefinition('value')
->getDataType(), 'string');
// Test introspecting an entity object.
// @todo: Add bundles and test bundles as well.
$entity = entity_create($entity_type);
$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
->assertIdentical($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
->assertIdentical($field
->getRoot()
->getValue(), $entity, 'Entity is root object.');
$this
->assertIdentical($field
->getEntity(), $entity, 'getEntity() returns the entity.');
$this
->assertEqual($field
->getPropertyPath(), 'user_id');
$this
->assertEqual($field
->getName(), 'user_id');
$this
->assertIdentical($field
->getParent()
->getValue(), $entity, 'Parent object matches.');
$field_item = $field[0];
$this
->assertIdentical($field_item
->getRoot()
->getValue(), $entity, 'Entity is root object.');
$this
->assertIdentical($field_item
->getEntity(), $entity, 'getEntity() returns the entity.');
$this
->assertEqual($field_item
->getPropertyPath(), 'user_id.0');
$this
->assertEqual($field_item
->getName(), '0');
$this
->assertIdentical($field_item
->getParent(), $field, 'Parent object matches.');
$item_value = $field_item
->get('entity');
$this
->assertIdentical($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
->assertIdentical($item_value
->getParent(), $field_item, 'Parent object matches.');
}