You are here

public function FieldTypePluginManagerTest::testCreateInstanceWithConfig in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/field/src/Tests/FieldTypePluginManagerTest.php \Drupal\field\Tests\FieldTypePluginManagerTest::testCreateInstanceWithConfig()

Tests creation of field item instances.

File

core/modules/field/src/Tests/FieldTypePluginManagerTest.php, line 63
Contains \Drupal\field\Tests\FieldTypePluginManagerTest.

Class

FieldTypePluginManagerTest
Tests the field type manager.

Namespace

Drupal\field\Tests

Code

public function testCreateInstanceWithConfig() {

  /** @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 = array(
    'field_definition' => $field_definition,
    'name' => $field_name,
    'parent' => NULL,
  );
  $entity = EntityTest::create();
  $instance = $field_type_manager
    ->createInstance($type, $configuration);
  $this
    ->assertTrue($instance instanceof $class, SafeMarkup::format('Created a @class instance', array(
    '@class' => $class,
  )));
  $this
    ->assertEqual($field_name, $instance
    ->getName(), SafeMarkup::format('Instance name is @name', array(
    '@name' => $field_name,
  )));
  $this
    ->assertEqual($instance
    ->getFieldDefinition()
    ->getLabel(), 'Jenny', 'Instance label is Jenny');
  $this
    ->assertEqual($instance
    ->getFieldDefinition()
    ->getDefaultValue($entity), [
    [
      'value' => 8675309,
    ],
  ], 'Instance default_value is 8675309');
}