function FieldTypePluginManagerTest::testCreateInstanceWithConfig

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

Tests creation of field item instances.

File

core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php, line 60

Class

FieldTypePluginManagerTest
Tests the field type manager.

Namespace

Drupal\Tests\field\Kernel

Code

public function testCreateInstanceWithConfig() : void {
    
    /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    $type = 'test_field';
    $definition = $field_type_manager->getDefinition($type);
    $class = $definition['class'];
    $field_name = 'field_' . $type;
    $field_definition = BaseFieldDefinition::create($type)->setLabel('Jenny')
        ->setDefaultValue(8675309);
    $configuration = [
        'field_definition' => $field_definition,
        'name' => $field_name,
        'parent' => NULL,
    ];
    $entity = EntityTest::create();
    $instance = $field_type_manager->createInstance($type, $configuration);
    $this->assertInstanceOf($class, $instance);
    $this->assertEquals($field_name, $instance->getName(), "Instance name is {$field_name}");
    $this->assertEquals('Jenny', $instance->getFieldDefinition()
        ->getLabel(), 'Instance label is Jenny');
    $this->assertEquals([
        [
            'value' => 8675309,
        ],
    ], $instance->getFieldDefinition()
        ->getDefaultValue($entity), 'Instance default_value is 8675309');
}

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