public function EntityFormDisplayTest::testFieldComponent

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

File

drupal/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php, line 55
Contains \Drupal\entity\Tests\EntityFormDisplayTest.

Class

EntityFormDisplayTest
Tests the EntityDisplay configuration entities.

Namespace

Drupal\entity\Tests

Code

public function testFieldComponent() {
  $this
    ->enableModules(array(
    'field_sql_storage',
    'field_test',
  ));
  $form_display = entity_create('entity_form_display', array(
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ));

  // Create a field and an instance.
  $field = array(
    'field_name' => 'test_field',
    'type' => 'test_field',
  );
  field_create_field($field);
  $instance = array(
    'field_name' => $field['field_name'],
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  );
  field_create_instance($instance);

  // Check that providing no options results in default values being used.
  $form_display
    ->setComponent($field['field_name']);
  $field_type_info = field_info_field_types($field['type']);
  $default_widget = $field_type_info['default_widget'];
  $default_settings = field_info_widget_settings($default_widget);
  $expected = array(
    'weight' => 0,
    'type' => $default_widget,
    'settings' => $default_settings,
  );
  $this
    ->assertEqual($form_display
    ->getComponent($field['field_name']), $expected);

  // Check that the getWidget() method returns the correct widget plugin.
  $widget = $form_display
    ->getWidget($field['field_name']);
  $this
    ->assertEqual($widget
    ->getPluginId(), $default_widget);
  $this
    ->assertEqual($widget
    ->getSettings(), $default_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
    ->getWidget($field['field_name']);
  $this
    ->assertEqual($widget->randomValue, $random_value);

  // Check that changing the definition creates a new widget.
  $form_display
    ->setComponent($field['field_name'], array(
    'type' => 'field_test_multiple',
  ));
  $widget = $form_display
    ->getWidget($field['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['field_name'], array(
    'type' => 'unknown_widget',
  ));
  $options = $form_display
    ->getComponent($field['field_name']);
  $this
    ->assertEqual($options['type'], 'unknown_widget');
  $widget = $form_display
    ->getWidget($field['field_name']);
  $this
    ->assertEqual($widget
    ->getPluginId(), $default_widget);
}