You are here

public function EntityDisplayTest::testFieldComponent in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field_ui/src/Tests/EntityDisplayTest.php \Drupal\field_ui\Tests\EntityDisplayTest::testFieldComponent()

Tests the behavior of a field component within an entity display object.

File

core/modules/field_ui/src/Tests/EntityDisplayTest.php, line 185
Contains \Drupal\field_ui\Tests\EntityDisplayTest.

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\field_ui\Tests

Code

public function testFieldComponent() {
  $field_name = 'test_field';

  // Create a field storage and a field.
  $field_storage = FieldStorageConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ));
  $field_storage
    ->save();
  $field = FieldConfig::create(array(
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
  ));
  $field
    ->save();
  $display = EntityViewDisplay::create(array(
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ));

  // Check that providing no options results in default values being used.
  $display
    ->setComponent($field_name);
  $field_type_info = \Drupal::service('plugin.manager.field.field_type')
    ->getDefinition($field_storage
    ->getType());
  $default_formatter = $field_type_info['default_formatter'];
  $formatter_settings = \Drupal::service('plugin.manager.field.formatter')
    ->getDefaultSettings($default_formatter);
  $expected = array(
    'weight' => -4,
    'label' => 'above',
    'type' => $default_formatter,
    'settings' => $formatter_settings,
    'third_party_settings' => array(),
  );
  $this
    ->assertEqual($display
    ->getComponent($field_name), $expected);

  // Check that the getFormatter() method returns the correct formatter plugin.
  $formatter = $display
    ->getRenderer($field_name);
  $this
    ->assertEqual($formatter
    ->getPluginId(), $default_formatter);
  $this
    ->assertEqual($formatter
    ->getSettings(), $formatter_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
    ->getRenderer($field_name);
  $this
    ->assertEqual($formatter->randomValue, $random_value);

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

  // Check that the display has dependencies on the field and the module that
  // provides the formatter.
  $dependencies = $display
    ->calculateDependencies()
    ->getDependencies();
  $this
    ->assertEqual(array(
    'config' => array(
      'field.field.entity_test.entity_test.test_field',
    ),
    'module' => array(
      'entity_test',
      'field_test',
    ),
  ), $dependencies);
}