function FieldInstanceCrudTest::testCreateFieldInstance

Test the creation of a field instance.

File

drupal/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php, line 49
Definition of Drupal\field\Tests\FieldInstanceCrudTest.

Class

FieldInstanceCrudTest

Namespace

Drupal\field\Tests

Code

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

  // Read the configuration. Check against raw configuration data rather than
  // the loaded ConfigEntity, to be sure we check that the defaults are
  // applied on write.
  $config = \Drupal::config('field.instance.' . $instance
    ->id())
    ->get();
  $field_type = field_info_field_types($this->field['type']);

  // Check that default values are set.
  $this
    ->assertEqual($config['required'], FALSE, 'Required defaults to false.');
  $this
    ->assertIdentical($config['label'], $this->instance_definition['field_name'], 'Label defaults to field name.');
  $this
    ->assertIdentical($config['description'], '', 'Description defaults to empty string.');

  // Check that default settings are set.
  $this
    ->assertEqual($config['settings'], $field_type['instance_settings'], 'Default instance settings 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' => drupal_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.
}