function EntityDisplayTest::testBaseFieldComponent

Same name in other branches
  1. 8.9.x core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testBaseFieldComponent()
  2. 10 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testBaseFieldComponent()
  3. 11.x 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 289

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.