You are here

public function EntityDisplayTest::testFieldComponent in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testFieldComponent()
  2. 10 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testFieldComponent()

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

File

core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php, line 230

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

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

  // Create a field storage and a field.
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
  ]);
  $field
    ->save();
  $display = EntityViewDisplay::create([
    '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 = [
    'weight' => -4,
    'label' => 'above',
    'type' => $default_formatter,
    'settings' => $formatter_settings,
    'third_party_settings' => [],
  ];
  $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, [
    '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([
    'config' => [
      'field.field.entity_test.entity_test.test_field',
    ],
    'module' => [
      'entity_test',
      'field_test',
    ],
  ], $dependencies);
}