public function EntityFieldTest::testIntrospection

Tests introspection and getting metadata upfront.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php, line 278
Definition of Drupal\Core\Entity\Tests\EntityFieldTest.

Class

EntityFieldTest
Tests Entity API base functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testIntrospection() {

  // Test getting metadata upfront, i.e. without having an entity object.
  $definition = array(
    'type' => 'entity',
    'constraints' => array(
      'entity type' => 'entity_test',
    ),
    'label' => t('Test entity'),
  );
  $wrapped_entity = typed_data()
    ->create($definition);
  $definitions = $wrapped_entity
    ->getPropertyDefinitions($definition);
  $this
    ->assertEqual($definitions['name']['type'], 'string_field', 'Name field found.');
  $this
    ->assertEqual($definitions['user_id']['type'], 'entityreference_field', 'User field found.');
  $this
    ->assertEqual($definitions['field_test_text']['type'], 'text_field', 'Test-text-field field found.');

  // Test introspecting an entity object.
  // @todo: Add bundles and test bundles as well.
  $entity = entity_create('entity_test', array());
  $definitions = $entity
    ->getPropertyDefinitions();
  $this
    ->assertEqual($definitions['name']['type'], 'string_field', 'Name field found.');
  $this
    ->assertEqual($definitions['user_id']['type'], 'entityreference_field', 'User field found.');
  $this
    ->assertEqual($definitions['field_test_text']['type'], 'text_field', 'Test-text-field field found.');
  $name_properties = $entity->name
    ->getPropertyDefinitions();
  $this
    ->assertEqual($name_properties['value']['type'], 'string', 'String value property of the name found.');
  $userref_properties = $entity->user_id
    ->getPropertyDefinitions();
  $this
    ->assertEqual($userref_properties['value']['type'], 'integer', 'Entity id property of the user found.');
  $this
    ->assertEqual($userref_properties['entity']['type'], 'entity', 'Entity reference property of the user found.');
  $textfield_properties = $entity->field_test_text
    ->getPropertyDefinitions();
  $this
    ->assertEqual($textfield_properties['value']['type'], 'string', 'String value property of the test-text field found.');
  $this
    ->assertEqual($textfield_properties['format']['type'], 'string', 'String format field of the test-text field found.');
  $this
    ->assertEqual($textfield_properties['processed']['type'], 'string', 'String processed property of the test-text field found.');

  // @todo: Once the user entity has definitions, continue testing getting
  // them from the $userref_values['entity'] property.
}