Commit 4b9ecbbb authored by webchick's avatar webchick

Issue #2175017 by tim.plunkett, yched: FieldDefinition::create() doesn't...

Issue #2175017 by tim.plunkett, yched: FieldDefinition::create() doesn't populate default 'settings' for the field type.
parent d63c8fae
......@@ -34,7 +34,15 @@ class FieldDefinition extends ListDefinition implements FieldDefinitionInterface
* A new field definition object.
*/
public static function create($type) {
return new static(array(), DataDefinition::create('field_item:' . $type));
// Create a definition for the items, and initialize it with the default
// settings for the field type.
// @todo Cleanup in https://drupal.org/node/2116341.
$item_definition = DataDefinition::create('field_item:' . $type);
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$default_settings = $field_type_manager->getDefaultSettings($type) + $field_type_manager->getDefaultInstanceSettings($type);
$item_definition->setSettings($default_settings);
return new static(array(), $item_definition);
}
/**
......
......@@ -14,6 +14,8 @@
/**
* Plugin manager for 'field type' plugins.
*
* @todo Add FieldTypePluginManagerInterface in https://drupal.org/node/2175415.
*/
class FieldTypePluginManager extends DefaultPluginManager {
......
......@@ -7,6 +7,7 @@
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Tests\UnitTestCase;
......@@ -17,6 +18,21 @@
*/
class FieldDefinitionTest extends UnitTestCase {
/**
* A dummy field type name.
*
* @var string
*/
protected $fieldType;
/**
* A dummy field type definition.
*
* @var string
*/
protected $fieldTypeDefinition;
public static function getInfo() {
return array(
'name' => 'Field definition test',
......@@ -28,28 +44,42 @@ public static function getInfo() {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Prepare a container with a mock typed data object, that returns no
// type definitions.
// @todo: Overhaul how field definitions deal with dependencies and improve
// unit tests. See https://drupal.org/node/2143555.
$typed_data = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManager')
public function setUp() {
// Mock the field type manager and place it in the container.
// @todo Add FieldTypePluginManagerInterface in https://drupal.org/node/2175415.
$field_type_manager = $this->getMockBuilder('Drupal\Core\Field\FieldTypePluginManager')
->disableOriginalConstructor()
->getMock();
$typed_data
->expects($this->any())
->method('getDefinition')
->will($this->returnValue(NULL));
$container = $this->getMock('Drupal\Core\DependencyInjection\Container');
$container
->expects($this->any())
->method('get')
->will($this->returnValue($typed_data));
$this->fieldType = $this->randomName();
$this->fieldTypeDefinition = array(
'id' => $this->fieldType,
'settings' => array(
'some_setting' => 'value 1'
),
'instance_settings' => array(
'some_instance_setting' => 'value 2',
),
);
$field_type_manager->expects($this->any())
->method('getDefinitions')
->will($this->returnValue(array($this->fieldType => $this->fieldTypeDefinition)));
$field_type_manager->expects($this->any())
->method('getDefinition')
->with($this->fieldType)
->will($this->returnValue($this->fieldTypeDefinition));
$field_type_manager->expects($this->any())
->method('getDefaultSettings')
->with($this->fieldType)
->will($this->returnValue($this->fieldTypeDefinition['settings']));
$field_type_manager->expects($this->any())
->method('getDefaultInstanceSettings')
->with($this->fieldType)
->will($this->returnValue($this->fieldTypeDefinition['instance_settings']));
$container = new ContainerBuilder();
$container->set('plugin.manager.field.field_type', $field_type_manager);
\Drupal::setContainer($container);
}
......@@ -87,9 +117,8 @@ public function testFieldDescription() {
* Tests field type methods.
*/
public function testFieldType() {
$field_type = $this->randomName();
$definition = FieldDefinition::create($field_type);
$this->assertEquals($field_type, $definition->getType());
$definition = FieldDefinition::create($this->fieldType);
$this->assertEquals($this->fieldType, $definition->getType());
}
/**
......@@ -104,6 +133,18 @@ public function testFieldSettings() {
$this->assertEquals(array($setting => $value), $definition->getSettings());
}
/**
* Tests the initialization of default field settings.
*/
public function testDefaultFieldSettings() {
$definition = FieldDefinition::create($this->fieldType);
$expected_settings = $this->fieldTypeDefinition['settings'] + $this->fieldTypeDefinition['instance_settings'];
$this->assertEquals($expected_settings, $definition->getSettings());
foreach ($expected_settings as $setting => $value) {
$this->assertEquals($value, $definition->getSetting($setting));
}
}
/**
* Tests field default value.
*/
......
......@@ -40,35 +40,50 @@ public static function getInfo() {
* @see \Drupal\Core\Entity\Controller\FieldableDatabaseStorageController::_fieldSqlSchema()
*/
public function testFieldSqlSchemaForEntityWithStringIdentifier() {
// @todo Add FieldTypePluginManagerInterface in https://drupal.org/node/2175415.
$field_type_manager = $this->getMockBuilder('Drupal\Core\Field\FieldTypePluginManager')
->disableOriginalConstructor()
->getMock();
$entity_manager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$container = new ContainerBuilder();
$container->set('plugin.manager.field.field_type', $field_type_manager);
$container->set('entity.manager', $entity_manager);
\Drupal::setContainer($container);
$definition = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$definition->expects($this->any())
->method('getKey')
->will($this->returnValueMap(array(
array('id', 'id'),
array('revision_id', 'revision_id'),
)));
$definition->expects($this->once())
->method('hasKey')
->with('revision_id')
->will($this->returnValue(TRUE));
$field_type_manager->expects($this->exactly(2))
->method('getDefaultSettings')
->will($this->returnValue(array()));
$field_type_manager->expects($this->exactly(2))
->method('getDefaultInstanceSettings')
->will($this->returnValue(array()));
// Mock the entity manager to return the minimal entity and field
// definitions for the test_entity entity.
$definition = new EntityType(array(
'entity_keys' => array(
'id' => 'id',
'revision_id' => 'revision_id',
),
));
$fields['id'] = FieldDefinition::create('string')
->setName('id');
$fields['revision_id'] = FieldDefinition::create('string')
->setName('revision_id');
$entity_manager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$entity_manager->expects($this->any())
->method('getDefinition')
->with('test_entity')
->will($this->returnValue($definition));
$entity_manager->expects($this->any())
->method('getFieldDefinitions')
->with('test_entity')
->will($this->returnValue($fields));
$container = new ContainerBuilder();
$container->set('entity.manager', $entity_manager);
\Drupal::setContainer($container);
// Define a field definition for a test_field field.
$field = $this->getMock('\Drupal\field\FieldInterface');
$field->deleted = FALSE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment