function FieldCrudTest::testCreateField

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testCreateField()
  2. 8.9.x core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testCreateField()
  3. 10 core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testCreateField()

Tests the creation of a field.

File

core/modules/field/tests/src/Kernel/FieldCrudTest.php, line 72

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\Tests\field\Kernel

Code

public function testCreateField() : void {
    $field = FieldConfig::create($this->fieldDefinition);
    $field->save();
    $field = FieldConfig::load($field->id());
    $this->assertEquals('TRUE', $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->assertFalse($config['required'], 'Required defaults to false.');
    $this->assertSame($config['label'], $this->fieldDefinition['field_name'], 'Label defaults to field name.');
    $this->assertSame('', $config['description'], 'Description defaults to empty string.');
    // Check that default settings are set.
    $this->assertEquals($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->assertEquals($config['field_type'], $this->fieldStorageDefinition['type']);
    // Guarantee that the field/bundle combination is unique.
    try {
        FieldConfig::create($this->fieldDefinition)
            ->save();
        $this->fail('Cannot create two fields with the same field / bundle combination.');
    } catch (EntityStorageException) {
        // Expected exception; just continue testing.
    }
    // Check that the specified field exists.
    try {
        $this->fieldDefinition['field_name'] = $this->randomMachineName();
        FieldConfig::create($this->fieldDefinition)
            ->save();
        $this->fail('Cannot create a field with a non-existing storage.');
    } catch (FieldException) {
        // Expected exception; just continue testing.
    }
    // @todo Test other failures.
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.