You are here

public function EntityFormDisplayTest::testFieldComponent in Zircon Profile 8

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

Tests the behavior of a field component within an EntityFormDisplay object.

File

core/modules/field_ui/src/Tests/EntityFormDisplayTest.php, line 57
Contains \Drupal\field_ui\Tests\EntityFormDisplayTest.

Class

EntityFormDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\field_ui\Tests

Code

public function testFieldComponent() {

  // Create a field storage and a field.
  $field_name = 'test_field';
  $field_storage = FieldStorageConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ));
  $field_storage
    ->save();
  $field = FieldConfig::create(array(
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
  ));
  $field
    ->save();
  $form_display = EntityFormDisplay::create(array(
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ));

  // Check that providing no options results in default values being used.
  $form_display
    ->setComponent($field_name);
  $field_type_info = \Drupal::service('plugin.manager.field.field_type')
    ->getDefinition($field_storage
    ->getType());
  $default_widget = $field_type_info['default_widget'];
  $widget_settings = \Drupal::service('plugin.manager.field.widget')
    ->getDefaultSettings($default_widget);
  $expected = array(
    'weight' => 0,
    'type' => $default_widget,
    'settings' => $widget_settings,
    'third_party_settings' => array(),
  );
  $this
    ->assertEqual($form_display
    ->getComponent($field_name), $expected);

  // Check that the getWidget() method returns the correct widget plugin.
  $widget = $form_display
    ->getRenderer($field_name);
  $this
    ->assertEqual($widget
    ->getPluginId(), $default_widget);
  $this
    ->assertEqual($widget
    ->getSettings(), $widget_settings);

  // Check that the widget is statically persisted, by assigning an
  // arbitrary property and reading it back.
  $random_value = $this
    ->randomString();
  $widget->randomValue = $random_value;
  $widget = $form_display
    ->getRenderer($field_name);
  $this
    ->assertEqual($widget->randomValue, $random_value);

  // Check that changing the definition creates a new widget.
  $form_display
    ->setComponent($field_name, array(
    'type' => 'field_test_multiple',
  ));
  $widget = $form_display
    ->getRenderer($field_name);
  $this
    ->assertEqual($widget
    ->getPluginId(), 'test_field_widget');
  $this
    ->assertFalse(isset($widget->randomValue));

  // Check that specifying an unknown widget (e.g. case of a disabled module)
  // gets stored as is in the display, but results in the default widget being
  // used.
  $form_display
    ->setComponent($field_name, array(
    'type' => 'unknown_widget',
  ));
  $options = $form_display
    ->getComponent($field_name);
  $this
    ->assertEqual($options['type'], 'unknown_widget');
  $widget = $form_display
    ->getRenderer($field_name);
  $this
    ->assertEqual($widget
    ->getPluginId(), $default_widget);
}