View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\Tests\UnitTestCase;
class FieldDefinitionTest extends UnitTestCase {
protected $fieldType;
protected $fieldTypeDefinition;
protected $storageDefinition;
protected $storageSupportsTranslation = TRUE;
protected function setUp() : void {
$this->fieldType = $this
->randomMachineName();
$this->fieldTypeDefinition = [
'id' => $this->fieldType,
'field_settings' => [
'some_instance_setting' => 'value 2',
],
'storage_settings' => [
'some_storage_setting' => 'some value',
],
];
$field_type_manager = $this
->prophesize(FieldTypePluginManagerInterface::class);
$field_type_manager
->getDefinitions()
->willReturn([
$this->fieldType => $this->fieldTypeDefinition,
]);
$field_type_manager
->getDefinition()
->willReturn($this->fieldTypeDefinition);
$field_type_manager
->getDefaultFieldSettings($this->fieldType)
->willReturn($this->fieldTypeDefinition['field_settings']);
$field_type_manager
->getDefaultStorageSettings($this->fieldType)
->willReturn($this->fieldTypeDefinition['storage_settings']);
$storage_definition = $this
->prophesize(FieldStorageDefinitionInterface::class);
$storage_definition
->getMainPropertyName()
->willReturn('value');
$storage_definition
->getType()
->willReturn($this->fieldType);
$storage_definition
->getName()
->willReturn('test_field_name');
$storage_supports_translation =& $this->storageSupportsTranslation;
$storage_definition
->isTranslatable()
->will(function () use (&$storage_supports_translation) {
return $storage_supports_translation;
});
$storage_definition
->getSettings()
->willReturn($this->fieldTypeDefinition['storage_settings']);
$storage_definition
->getSetting('some_storage_setting')
->willReturn($this->fieldTypeDefinition['storage_settings']['some_storage_setting']);
$this->storageDefinition = $storage_definition
->reveal();
$entity_field_manager = $this
->prophesize(EntityFieldManagerInterface::class);
$entity_field_manager
->getFieldStorageDefinitions('entity_test')
->willReturn([
'foo' => $storage_definition
->reveal(),
]);
$typed_data_manager = $this
->prophesize(TypedDataManager::class);
$container = new ContainerBuilder();
$container
->set('plugin.manager.field.field_type', $field_type_manager
->reveal());
$container
->set('entity_field.manager', $entity_field_manager
->reveal());
$container
->set('typed_data_manager', $typed_data_manager
->reveal());
\Drupal::setContainer($container);
}
public function testFieldName($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->assertEquals($this->storageDefinition
->getName(), $definition
->getName());
}
public function testFieldLabel($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$label = $this
->randomMachineName();
$definition
->setLabel($label);
$this
->assertEquals($label, $definition
->getLabel());
}
public function testBundle($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$bundle = $this
->randomMachineName();
$definition
->setTargetBundle($bundle);
$this
->assertEquals($bundle, $definition
->getTargetBundle());
}
public function testFieldDescription($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$description = $this
->randomMachineName();
$definition
->setDescription($description);
$this
->assertEquals($description, $definition
->getDescription());
}
public function testFieldType($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->assertEquals($this->fieldType, $definition
->getType());
}
public function testFieldSettings($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$setting = $this
->randomMachineName();
$value = $this
->randomMachineName();
$definition
->setSetting($setting, $value);
$this
->assertEquals($value, $definition
->getSetting($setting));
$default_settings = $this->fieldTypeDefinition['field_settings'] + $this->fieldTypeDefinition['storage_settings'];
$this
->assertEquals([
$setting => $value,
] + $default_settings, $definition
->getSettings());
}
public function testDefaultFieldSettings($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$expected_settings = $this->fieldTypeDefinition['field_settings'] + $this->fieldTypeDefinition['storage_settings'];
$this
->assertEquals($expected_settings, $definition
->getSettings());
foreach ($expected_settings as $setting => $value) {
$this
->assertEquals($value, $definition
->getSetting($setting));
}
}
public function testFieldDefaultValue($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->assertEquals([], $definition
->getDefaultValueLiteral());
$default_value = [
'value' => $this
->randomMachineName(),
];
$expected_default_value = [
$default_value,
];
$definition
->setDefaultValue($default_value);
$entity = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->getMock();
$definition
->setClass('Drupal\\Core\\Field\\FieldItemList');
$this
->assertEquals($expected_default_value, $definition
->getDefaultValue($entity));
$data_definition = $this
->getMockBuilder('Drupal\\Core\\TypedData\\DataDefinition')
->disableOriginalConstructor()
->getMock();
$data_definition
->expects($this
->any())
->method('getClass')
->will($this
->returnValue('Drupal\\Core\\Field\\FieldItemBase'));
$definition
->setItemDefinition($data_definition);
$definition
->setDefaultValue($default_value['value']);
$this
->assertEquals($expected_default_value, $definition
->getDefaultValue($entity));
$definition
->setDefaultValue($expected_default_value);
$this
->assertEquals($expected_default_value, $definition
->getDefaultValue($entity));
$definition
->setDefaultValue([]);
$this
->assertEquals([], $definition
->getDefaultValue($entity));
$definition
->setDefaultValue(NULL);
$this
->assertEquals([], $definition
->getDefaultValue($entity));
}
public function testFieldTranslatable($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->assertFalse($definition
->isTranslatable());
$definition
->setTranslatable(TRUE);
$this
->assertTrue($definition
->isTranslatable());
$definition
->setTranslatable(FALSE);
$this
->assertFalse($definition
->isTranslatable());
$this->storageSupportsTranslation = FALSE;
$definition
->setTranslatable(TRUE);
$this
->assertFalse($this->storageDefinition
->isTranslatable());
$this
->assertFalse($definition
->isTranslatable());
}
public function testFieldRequired($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->assertFalse($definition
->isRequired());
$definition
->setRequired(TRUE);
$this
->assertTrue($definition
->isRequired());
$definition
->setRequired(FALSE);
$this
->assertFalse($definition
->isRequired());
}
public function testDefaultValueCallback($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$callback = static::class . '::mockDefaultValueCallback';
$this
->assertSame($definition, $definition
->setDefaultValueCallback($callback));
$this
->assertSame($callback, $definition
->getDefaultValueCallback());
}
public function testInvalidDefaultValueCallback($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->expectException(\InvalidArgumentException::class);
$definition
->setDefaultValueCallback([
static::class,
'mockDefaultValueCallback',
]);
}
public function testNullDefaultValueCallback($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->assertSame($definition, $definition
->setDefaultValueCallback(NULL));
$this
->assertSame(NULL, $definition
->getDefaultValueCallback());
}
public function testDisplayConfigurationSettings($factory_name) {
$definition = $this
->initializeFieldUsingFactory($factory_name);
$this
->assertEquals(FALSE, $definition
->isDisplayConfigurable('foo'));
$this
->assertEquals(NULL, $definition
->getDisplayOptions('foo'));
$definition
->setDisplayConfigurable('foo', TRUE);
$this
->assertEquals(TRUE, $definition
->isDisplayConfigurable('foo'));
$this
->assertEquals([
'region' => 'hidden',
], $definition
->getDisplayOptions('foo'));
$definition
->setDisplayOptions('foo', [
'foo' => 'bar',
]);
$this
->assertEquals([
'foo' => 'bar',
], $definition
->getDisplayOptions('foo'));
}
public static function mockDefaultValueCallback($entity, $definition) {
return 'a default value';
}
public function factoryTypeProvider() {
return [
'::createFromFieldStorageDefinition factory' => [
'createFromFieldStorageDefinition',
],
'::create factory' => [
'create',
],
'::createFromDataType factory' => [
'createFromDataType',
],
'::createFromItemType factory' => [
'createFromItemType',
],
];
}
protected function initializeFieldUsingFactory($factory_name) {
switch ($factory_name) {
case 'createFromFieldStorageDefinition':
return FieldDefinition::createFromFieldStorageDefinition($this->storageDefinition);
case 'create':
$definition = FieldDefinition::create($this->fieldType);
$definition
->setFieldStorageDefinition($this->storageDefinition);
return $definition;
case 'createFromDataType':
$definition = FieldDefinition::createFromDataType($this->fieldType);
$definition
->setFieldStorageDefinition($this->storageDefinition);
return $definition;
case 'createFromItemType':
$definition = FieldDefinition::createFromItemType($this->fieldType);
$definition
->setFieldStorageDefinition($this->storageDefinition);
return $definition;
}
throw new \InvalidArgumentException("Invalid factory name '{$factory_name}' passed to " . __METHOD__);
}
}