1 field.test FieldInfoTestCase::testInstancePrepare()

Test that cached instance definitions are ready for current runtime context.

File

core/modules/field/tests/field.test, line 1347
Tests for field.module.

Class

FieldInfoTestCase

Code

function testInstancePrepare() {
  $field_definition = array(
    'field_name' => 'field',
    'type' => 'test_field',
  );
  field_create_field($field_definition);
  $instance_definition = array(
    'field_name' => $field_definition['field_name'],
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
  );
  field_create_instance($instance_definition);

  // Simulate a stored instance definition missing various settings (e.g. a
  // third-party module adding instance, widget or display settings has been
  // enabled, but existing instances do not know the new settings).
  $config = config('field.instance.test_entity.' . $instance_definition['bundle'] . '.' . $instance_definition['field_name']);
  $instance = $config->get();
  $instance['settings'] = array();
  $instance['widget']['settings'] = 'unavailable_widget';
  $instance['widget']['settings'] = array();
  $instance['display']['default']['type'] = 'unavailable_formatter';
  $instance['display']['default']['settings'] = array();
  $config->setData($instance);
  $config->save();

  field_cache_clear();

  // Read the instance back.
  $instance = field_info_instance($instance_definition['entity_type'], $instance_definition['field_name'], $instance_definition['bundle']);

  // Check that all expected instance settings are in place.
  $field_type = field_info_field_types($field_definition['type']);
  $this->assertIdentical($instance['settings'], $field_type['instance_settings'], 'All expected instance settings are present.');

  // Check that the default widget is used and expected settings are in place.
  $this->assertIdentical($instance['widget']['type'], $field_type['default_widget'], 'Unavailable widget replaced with default widget.');
  $widget_type = field_info_widget_types($instance['widget']['type']);
  $this->assertIdentical($instance['widget']['settings'], $widget_type['settings'], 'All expected widget settings are present.');

  // Check that display settings are set for the 'default' mode.
  $display = $instance['display']['default'];
  $this->assertIdentical($display['type'], $field_type['default_formatter'], "Formatter is set for the 'default' display mode");
  $formatter_type = field_info_formatter_types($display['type']);
  $this->assertIdentical($display['settings'], $formatter_type['settings'], "Formatter settings are set for the 'default' display mode");
}