You are here

public function EntityFormDisplayTest::testBaseFieldComponent in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testBaseFieldComponent()

Tests the behavior of a field component for a base field.

File

core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php, line 133

Class

EntityFormDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testBaseFieldComponent() {
  $display = EntityFormDisplay::create([
    'targetEntityType' => 'entity_test_base_field_display',
    'bundle' => 'entity_test_base_field_display',
    'mode' => 'default',
  ]);

  // Check that default options are correctly filled in.
  $formatter_settings = \Drupal::service('plugin.manager.field.widget')
    ->getDefaultSettings('text_textfield');
  $expected = [
    'test_no_display' => NULL,
    'test_display_configurable' => [
      'type' => 'text_textfield',
      'settings' => $formatter_settings,
      'third_party_settings' => [],
      'weight' => 10,
      'region' => 'content',
    ],
    'test_display_non_configurable' => [
      'type' => 'text_textfield',
      'settings' => $formatter_settings,
      'third_party_settings' => [],
      'weight' => 11,
      'region' => 'content',
    ],
  ];
  foreach ($expected as $field_name => $options) {
    $this
      ->assertEquals($options, $display
      ->getComponent($field_name));
  }

  // Check that saving the display only writes data for fields whose display
  // is configurable.
  $display
    ->save();
  $config = $this
    ->config('core.entity_form_display.' . $display
    ->id());
  $data = $config
    ->get();
  $this
    ->assertFalse(isset($data['content']['test_no_display']));
  $this
    ->assertFalse(isset($data['hidden']['test_no_display']));
  $this
    ->assertEquals($expected['test_display_configurable'], $data['content']['test_display_configurable']);
  $this
    ->assertFalse(isset($data['content']['test_display_non_configurable']));
  $this
    ->assertFalse(isset($data['hidden']['test_display_non_configurable']));

  // Check that defaults are correctly filled when loading the display.
  $display = EntityFormDisplay::load($display
    ->id());
  foreach ($expected as $field_name => $options) {
    $this
      ->assertEquals($options, $display
      ->getComponent($field_name));
  }

  // Check that data manually written for fields whose display is not
  // configurable is discarded when loading the display.
  $data['content']['test_display_non_configurable'] = $expected['test_display_non_configurable'];
  $data['content']['test_display_non_configurable']['weight']++;
  $config
    ->setData($data)
    ->save();
  $display = EntityFormDisplay::load($display
    ->id());
  foreach ($expected as $field_name => $options) {
    $this
      ->assertEquals($options, $display
      ->getComponent($field_name));
  }
}