function CrudTest::testCreateField

Test the creation of a field.

File

drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php, line 38
Definition of Drupal\field\Tests\CrudTest.

Class

CrudTest

Namespace

Drupal\field\Tests

Code

function testCreateField() {
  $field_definition = array(
    'field_name' => 'field_2',
    'type' => 'test_field',
  );
  field_test_memorize();
  $field = field_create_field($field_definition);
  $mem = field_test_memorize();
  $this
    ->assertIdentical($mem['field_test_field_create_field'][0][0]['field_name'], $field_definition['field_name'], 'hook_field_create_field() called with correct arguments.');
  $this
    ->assertIdentical($mem['field_test_field_create_field'][0][0]['type'], $field_definition['type'], 'hook_field_create_field() called with correct arguments.');

  // 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.
  $field_config = \Drupal::config('field.field.' . $field
    ->id())
    ->get();

  // Ensure that basic properties are preserved.
  $this
    ->assertEqual($field_config['id'], $field_definition['field_name'], 'The field name is properly saved.');
  $this
    ->assertEqual($field_config['type'], $field_definition['type'], 'The field type is properly saved.');

  // Ensure that cardinality defaults to 1.
  $this
    ->assertEqual($field_config['cardinality'], 1, 'Cardinality defaults to 1.');

  // Ensure that default settings are present.
  $field_type = field_info_field_types($field_definition['type']);
  $this
    ->assertEqual($field_config['settings'], $field_type['settings'], 'Default field settings have been written.');

  // Ensure that default storage was set.
  $this
    ->assertEqual($field_config['storage']['type'], config('field.settings')
    ->get('default_storage'), 'The field type is properly saved.');

  // Guarantee that the name is unique.
  try {
    field_create_field($field_definition);
    $this
      ->fail(t('Cannot create two fields with the same name.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create two fields with the same name.'));
  }

  // Check that field type is required.
  try {
    $field_definition = array(
      'field_name' => 'field_1',
    );
    field_create_field($field_definition);
    $this
      ->fail(t('Cannot create a field with no type.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create a field with no type.'));
  }

  // Check that field name is required.
  try {
    $field_definition = array(
      'type' => 'test_field',
    );
    field_create_field($field_definition);
    $this
      ->fail(t('Cannot create an unnamed field.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create an unnamed field.'));
  }

  // Check that field name must start with a letter or _.
  try {
    $field_definition = array(
      'field_name' => '2field_2',
      'type' => 'test_field',
    );
    field_create_field($field_definition);
    $this
      ->fail(t('Cannot create a field with a name starting with a digit.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create a field with a name starting with a digit.'));
  }

  // Check that field name must only contain lowercase alphanumeric or _.
  try {
    $field_definition = array(
      'field_name' => 'field#_3',
      'type' => 'test_field',
    );
    field_create_field($field_definition);
    $this
      ->fail(t('Cannot create a field with a name containing an illegal character.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create a field with a name containing an illegal character.'));
  }

  // Check that field name cannot be longer than 32 characters long.
  try {
    $field_definition = array(
      'field_name' => '_12345678901234567890123456789012',
      'type' => 'test_field',
    );
    field_create_field($field_definition);
    $this
      ->fail(t('Cannot create a field with a name longer than 32 characters.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create a field with a name longer than 32 characters.'));
  }

  // Check that field name can not be an entity key.
  // "ftvid" is known as an entity key from the "test_entity" type.
  try {
    $field_definition = array(
      'type' => 'test_field',
      'field_name' => 'ftvid',
    );
    field_create_field($field_definition);
    $this
      ->fail(t('Cannot create a field bearing the name of an entity key.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create a field bearing the name of an entity key.'));
  }
}