You are here

public function EntityDisplayTest::testBaseFieldComponent 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::testBaseFieldComponent()

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

File

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

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testBaseFieldComponent() {
  $display = EntityViewDisplay::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.formatter')
    ->getDefaultSettings('text_default');
  $expected = [
    'test_no_display' => NULL,
    'test_display_configurable' => [
      'label' => 'above',
      'type' => 'text_default',
      'settings' => $formatter_settings,
      'third_party_settings' => [],
      'weight' => 10,
      'region' => 'content',
    ],
    'test_display_non_configurable' => [
      'label' => 'above',
      'type' => 'text_default',
      'settings' => $formatter_settings,
      'third_party_settings' => [],
      'weight' => 11,
      'region' => 'content',
    ],
  ];
  foreach ($expected as $field_name => $options) {
    $this
      ->assertEqual($display
      ->getComponent($field_name), $options);
  }

  // Check that saving the display only writes data for fields whose display
  // is configurable.
  $display
    ->save();
  $config = $this
    ->config('core.entity_view_display.' . $display
    ->id());
  $data = $config
    ->get();
  $this
    ->assertFalse(isset($data['content']['test_no_display']));
  $this
    ->assertFalse(isset($data['hidden']['test_no_display']));
  $this
    ->assertEqual($data['content']['test_display_configurable'], $expected['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 = EntityViewDisplay::load($display
    ->id());
  foreach ($expected as $field_name => $options) {
    $this
      ->assertEqual($display
      ->getComponent($field_name), $options);
  }

  // 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 = EntityViewDisplay::load($display
    ->id());
  foreach ($expected as $field_name => $options) {
    $this
      ->assertEqual($display
      ->getComponent($field_name), $options);
  }
}