public function EntityDisplayTest::testFieldComponent

Tests the behavior of a field component within an EntityDisplay object.

File

drupal/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php, line 133
Contains \Drupal\entity\Tests\EntityDisplayTest.

Class

EntityDisplayTest
Tests the EntityDisplay configuration entities.

Namespace

Drupal\entity\Tests

Code

public function testFieldComponent() {
  $this
    ->enableModules(array(
    'field_sql_storage',
    'field_test',
  ));
  $display = entity_create('entity_display', array(
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ));

  // Create a field and an instance.
  $field = array(
    'field_name' => 'test_field',
    'type' => 'test_field',
  );
  field_create_field($field);
  $instance = array(
    'field_name' => $field['field_name'],
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  );
  field_create_instance($instance);

  // Check that providing no options results in default values being used.
  $display
    ->setComponent($field['field_name']);
  $field_type_info = field_info_field_types($field['type']);
  $default_formatter = $field_type_info['default_formatter'];
  $default_settings = field_info_formatter_settings($default_formatter);
  $expected = array(
    'weight' => 0,
    'label' => 'above',
    'type' => $default_formatter,
    'settings' => $default_settings,
  );
  $this
    ->assertEqual($display
    ->getComponent($field['field_name']), $expected);

  // Check that the getFormatter() method returns the correct formatter plugin.
  $formatter = $display
    ->getFormatter($field['field_name']);
  $this
    ->assertEqual($formatter
    ->getPluginId(), $default_formatter);
  $this
    ->assertEqual($formatter
    ->getSettings(), $default_settings);

  // Check that the formatter is statically persisted, by assigning an
  // arbitrary property and reading it back.
  $random_value = $this
    ->randomString();
  $formatter->randomValue = $random_value;
  $formatter = $display
    ->getFormatter($field['field_name']);
  $this
    ->assertEqual($formatter->randomValue, $random_value);

  // Check that changing the definition creates a new formatter.
  $display
    ->setComponent($field['field_name'], array(
    'type' => 'field_test_multiple',
  ));
  $formatter = $display
    ->getFormatter($field['field_name']);
  $this
    ->assertEqual($formatter
    ->getPluginId(), 'field_test_multiple');
  $this
    ->assertFalse(isset($formatter->randomValue));

  // Check that specifying an unknown formatter (e.g. case of a disabled
  // module) gets stored as is in the display, but results in the default
  // formatter being used.
  $display
    ->setComponent($field['field_name'], array(
    'type' => 'unknown_formatter',
  ));
  $options = $display
    ->getComponent($field['field_name']);
  $this
    ->assertEqual($options['type'], 'unknown_formatter');
  $formatter = $display
    ->getFormatter($field['field_name']);
  $this
    ->assertEqual($formatter
    ->getPluginId(), $default_formatter);
}