You are here

public function EntityDisplayTest::testFieldComponent in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testFieldComponent()
  2. 9 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 222

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
    ->assertEquals($expected, $display
    ->getComponent($field_name));

  // Check that the getFormatter() method returns the correct formatter plugin.
  $formatter = $display
    ->getRenderer($field_name);
  $this
    ->assertEquals($default_formatter, $formatter
    ->getPluginId());
  $this
    ->assertEquals($formatter_settings, $formatter
    ->getSettings());

  // 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
    ->assertEquals($random_value, $formatter->randomValue);

  // Check that changing the definition creates a new formatter.
  $display
    ->setComponent($field_name, [
    'type' => 'field_test_multiple',
  ]);
  $formatter = $display
    ->getRenderer($field_name);
  $this
    ->assertEquals('field_test_multiple', $formatter
    ->getPluginId());
  $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
    ->assertEquals([
    'config' => [
      'field.field.entity_test.entity_test.test_field',
    ],
    'module' => [
      'entity_test',
      'field_test',
    ],
  ], $dependencies);
}