1 field.test FieldCrudTestCase::testCreateField()

Test the creation of a field.

File

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

Class

FieldCrudTestCase

Code

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

  // Read the raw record from the config file.
  $config = config('field.field.' . $field_definition['field_name']);
  $config_data = $config->get();

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

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

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

  // Ensure that default storage was set.
  $this->assertEqual($config_data['storage']['type'], 'field_sql_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.'));
  }
}