You are here

public function EntityDisplayTest::testBaseFieldComponent in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field_ui/src/Tests/EntityDisplayTest.php \Drupal\field_ui\Tests\EntityDisplayTest::testBaseFieldComponent()

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

File

core/modules/field_ui/src/Tests/EntityDisplayTest.php, line 249
Contains \Drupal\field_ui\Tests\EntityDisplayTest.

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\field_ui\Tests

Code

public function testBaseFieldComponent() {
  $display = EntityViewDisplay::create(array(
    '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 = array(
    'test_no_display' => NULL,
    'test_display_configurable' => array(
      'label' => 'above',
      'type' => 'text_default',
      'settings' => $formatter_settings,
      'third_party_settings' => array(),
      'weight' => 10,
    ),
    'test_display_non_configurable' => array(
      'label' => 'above',
      'type' => 'text_default',
      'settings' => $formatter_settings,
      'third_party_settings' => array(),
      'weight' => 11,
    ),
  );
  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 = entity_load('entity_view_display', $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 = entity_load('entity_view_display', $display
    ->id());
  foreach ($expected as $field_name => $options) {
    $this
      ->assertEqual($display
      ->getComponent($field_name), $options);
  }
}