protected function EntityFieldTest::assertIntrospection

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::assertIntrospection()
EntityFieldTest::testIntrospection in drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php
Tests introspection and getting metadata upfront.

File

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

Class

EntityFieldTest
Tests Entity API base functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function assertIntrospection($entity_type) {

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

  // Test introspecting an entity object.
  // @todo: Add bundles and test bundles as well.
  $entity = entity_create($entity_type, array());
  $definitions = $entity
    ->getPropertyDefinitions();
  $this
    ->assertEqual($definitions['name']['type'], 'string_field', $entity_type . ': Name field found.');
  $this
    ->assertEqual($definitions['user_id']['type'], 'entity_reference_field', $entity_type . ': User field found.');
  $this
    ->assertEqual($definitions['field_test_text']['type'], 'text_field', $entity_type . ': Test-text-field field found.');
  $name_properties = $entity->name
    ->getPropertyDefinitions();
  $this
    ->assertEqual($name_properties['value']['type'], 'string', $entity_type . ': String value property of the name found.');
  $userref_properties = $entity->user_id
    ->getPropertyDefinitions();
  $this
    ->assertEqual($userref_properties['target_id']['type'], 'integer', $entity_type . ': Entity id property of the user found.');
  $this
    ->assertEqual($userref_properties['entity']['type'], 'entity', $entity_type . ': Entity reference property of the user found.');
  $textfield_properties = $entity->field_test_text
    ->getPropertyDefinitions();
  $this
    ->assertEqual($textfield_properties['value']['type'], 'string', $entity_type . ': String value property of the test-text field found.');
  $this
    ->assertEqual($textfield_properties['format']['type'], 'string', $entity_type . ': String format field of the test-text field found.');
  $this
    ->assertEqual($textfield_properties['processed']['type'], 'string', $entity_type . ': 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.
  // Make sure provided contextual information is right.
  $this
    ->assertIdentical($entity
    ->getRoot(), $entity, 'Entity is root object.');
  $this
    ->assertEqual($entity
    ->getPropertyPath(), '');
  $this
    ->assertEqual($entity
    ->getName(), '');
  $this
    ->assertEqual($entity
    ->getParent(), NULL);
  $field = $entity->user_id;
  $this
    ->assertIdentical($field
    ->getRoot(), $entity, 'Entity is root object.');
  $this
    ->assertEqual($field
    ->getPropertyPath(), 'user_id');
  $this
    ->assertEqual($field
    ->getName(), 'user_id');
  $this
    ->assertIdentical($field
    ->getParent(), $entity, 'Parent object matches.');
  $field_item = $field[0];
  $this
    ->assertIdentical($field_item
    ->getRoot(), $entity, 'Entity is root object.');
  $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(), $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.');
}