1 field.test FieldInstanceCrudTestCase::testCreateFieldInstance()

Test the creation of a field instance.

File

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

Class

FieldInstanceCrudTestCase

Code

function testCreateFieldInstance() {
  field_create_instance($this->instance_definition);

  // Read the raw record from the config
  $field_name = $this->instance_definition['field_name'];
  $bundle = $this->instance_definition['bundle'];
  $entity_type = $this->instance_definition['entity_type'];
  $config_data = config('field.instance.' . $entity_type . '.' . $bundle . '.' . $field_name)->get();

  $field_type = field_info_field_types($this->field['type']);
  $widget_type = field_info_widget_types($field_type['default_widget']);
  $formatter_type = field_info_formatter_types($field_type['default_formatter']);

  // Check that default values are set.
  $this->assertIdentical($config_data['required'], 0, 'Required defaults to false.');
  $this->assertIdentical($config_data['label'], $this->instance_definition['field_name'], 'Label defaults to field name.');
  $this->assertIdentical($config_data['description'], '', 'Description defaults to empty string.');
  $this->assertIdentical($config_data['widget']['type'], $field_type['default_widget'], 'Default widget has been written.');
  $this->assertTrue(isset($config_data['display']['default']), 'Display for "full" view_mode has been written.');
  $this->assertIdentical($config_data['display']['default']['type'], $field_type['default_formatter'], 'Default formatter for "full" view_mode has been written.');

  // Check that default settings are set.
  $this->assertIdentical($config_data['settings'], $field_type['instance_settings'], 'Default instance settings have been written.');
  $this->assertIdentical($config_data['widget']['settings'], $widget_type['settings'], 'Default widget settings have been written.');
  $this->assertIdentical($config_data['display']['default']['settings'], $formatter_type['settings'], 'Default formatter settings for "full" view_mode have been written.');

  // Guarantee that the field/bundle combination is unique.
  try {
    field_create_instance($this->instance_definition);
    $this->fail(t('Cannot create two instances with the same field / bundle combination.'));
  }
  catch (FieldException $e) {
    $this->pass(t('Cannot create two instances with the same field / bundle combination.'));
  }

  // Check that the specified field exists.
  try {
    $this->instance_definition['field_name'] = $this->randomName();
    field_create_instance($this->instance_definition);
    $this->fail(t('Cannot create an instance of a non-existing field.'));
  }
  catch (FieldException $e) {
    $this->pass(t('Cannot create an instance of a non-existing field.'));
  }

  // Create a field restricted to a specific entity type.
  $field_restricted = array(
    'field_name' => backdrop_strtolower($this->randomName()),
    'type' => 'test_field',
    'entity_types' => array('test_cacheable_entity'),
  );
  field_create_field($field_restricted);

  // Check that an instance can be added to an entity type allowed
  // by the field.
  try {
    $instance = $this->instance_definition;
    $instance['field_name'] = $field_restricted['field_name'];
    $instance['entity_type'] = 'test_cacheable_entity';
    field_create_instance($instance);
    $this->pass(t('Can create an instance on an entity type allowed by the field.'));
  }
  catch (FieldException $e) {
    $this->fail(t('Can create an instance on an entity type allowed by the field.'));
  }

  // Check that an instance cannot be added to an entity type
  // forbidden by the field.
  try {
    $instance = $this->instance_definition;
    $instance['field_name'] = $field_restricted['field_name'];
    field_create_instance($instance);
    $this->fail(t('Cannot create an instance on an entity type forbidden by the field.'));
  }
  catch (FieldException $e) {
    $this->pass(t('Cannot create an instance on an entity type forbidden by the field.'));
  }

  // TODO: test other failures.
}