You are here

function FieldCrudTest::testCreateField in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldCrudTest.php \Drupal\field\Tests\FieldCrudTest::testCreateField()

Test the creation of a field.

File

core/modules/field/src/Tests/FieldCrudTest.php, line 71
Contains \Drupal\field\Tests\FieldCrudTest.

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\field\Tests

Code

function testCreateField() {

  // Set a state flag so that field_test.module knows to add an in-memory
  // constraint for this field.
  \Drupal::state()
    ->set('field_test_add_constraint', $this->fieldStorage
    ->getName());

  /** @var \Drupal\Core\Field\FieldConfigInterface $field */
  $field = entity_create('field_config', $this->fieldDefinition);
  $field
    ->save();
  $field = FieldConfig::load($field
    ->id());
  $this
    ->assertTrue($field
    ->getSetting('field_setting_from_config_data'));
  $this
    ->assertNull($field
    ->getSetting('config_data_from_field_setting'));

  // 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 = $this
    ->config('field.field.' . $field
    ->id())
    ->get();
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $this
    ->assertTrue($config['settings']['config_data_from_field_setting']);
  $this
    ->assertTrue(!isset($config['settings']['field_setting_from_config_data']));

  // Since we are working with raw configuration, this needs to be unset
  // manually.
  // @see Drupal\field_test\Plugin\Field\FieldType\TestItem::fieldSettingsFromConfigData()
  unset($config['settings']['config_data_from_field_setting']);

  // Check that default values are set.
  $this
    ->assertEqual($config['required'], FALSE, 'Required defaults to false.');
  $this
    ->assertIdentical($config['label'], $this->fieldDefinition['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_manager
    ->getDefaultFieldSettings($this->fieldStorageDefinition['type']), 'Default field settings have been written.');

  // Check that the denormalized 'field_type' was properly written.
  $this
    ->assertEqual($config['field_type'], $this->fieldStorageDefinition['type']);

  // Test constraints are applied. A Range constraint is added dynamically to
  // limit the field to values between 0 and 32.
  // @see field_test_entity_bundle_field_info_alter()
  $this
    ->doFieldValidationTests();

  // Test FieldConfigBase::setPropertyConstraints().
  \Drupal::state()
    ->set('field_test_set_constraint', $this->fieldStorage
    ->getName());
  \Drupal::state()
    ->set('field_test_add_constraint', FALSE);
  \Drupal::entityManager()
    ->clearCachedFieldDefinitions();
  $this
    ->doFieldValidationTests();

  // Guarantee that the field/bundle combination is unique.
  try {
    entity_create('field_config', $this->fieldDefinition)
      ->save();
    $this
      ->fail(t('Cannot create two fields with the same field / bundle combination.'));
  } catch (EntityStorageException $e) {
    $this
      ->pass(t('Cannot create two fields with the same field / bundle combination.'));
  }

  // Check that the specified field exists.
  try {
    $this->fieldDefinition['field_name'] = $this
      ->randomMachineName();
    entity_create('field_config', $this->fieldDefinition)
      ->save();
    $this
      ->fail(t('Cannot create a field with a non-existing storage.'));
  } catch (FieldException $e) {
    $this
      ->pass(t('Cannot create a field with a non-existing storage.'));
  }

  // TODO: test other failures.
}