class FieldDefinitionTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php \Drupal\Tests\Core\Entity\FieldDefinitionTest
- 8.9.x core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php \Drupal\Tests\Core\Entity\FieldDefinitionTest
- 10 core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php \Drupal\Tests\Core\Entity\FieldDefinitionTest
Unit test for the FieldDefinition class.
@group Entity @group field @coversDefaultClass \Drupal\Core\Field\FieldDefinition
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Entity\FieldDefinitionTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of FieldDefinitionTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ FieldDefinitionTest.php, line 22
Namespace
Drupal\Tests\Core\EntityView source
class FieldDefinitionTest extends UnitTestCase {
/**
* A dummy field type name.
*
* @var string
*/
protected $fieldType;
/**
* A dummy field type definition.
*
* @var array
*/
protected $fieldTypeDefinition;
/**
* The test field storage definition.
*
* @var \Drupal\Core\Field\FieldStorageDefinitionInterface
*/
protected $storageDefinition;
/**
* A flag for setting if the field storage supports translation.
*
* @var bool
*/
protected $storageSupportsTranslation = TRUE;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$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);
}
/**
* @covers ::getName
* @dataProvider factoryTypeProvider
*/
public function testFieldName($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
$this->assertEquals($this->storageDefinition
->getName(), $definition->getName());
}
/**
* @covers ::getLabel
* @dataProvider factoryTypeProvider
*/
public function testFieldLabel($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
$label = $this->randomMachineName();
$definition->setLabel($label);
$this->assertEquals($label, $definition->getLabel());
}
/**
* @covers ::setTargetBundle
* @covers ::getTargetBundle
* @dataProvider factoryTypeProvider
*/
public function testBundle($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
$bundle = $this->randomMachineName();
$definition->setTargetBundle($bundle);
$this->assertEquals($bundle, $definition->getTargetBundle());
}
/**
* @covers ::getDescription
* @dataProvider factoryTypeProvider
*/
public function testFieldDescription($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
$description = $this->randomMachineName();
$definition->setDescription($description);
$this->assertEquals($description, $definition->getDescription());
}
/**
* @covers ::getType
* @dataProvider factoryTypeProvider
*/
public function testFieldType($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
$this->assertEquals($this->fieldType, $definition->getType());
}
/**
* @covers ::getSetting
* @covers ::setSetting
* @covers ::getSettings
* @dataProvider factoryTypeProvider
*/
public function testFieldSettings($factory_name) : void {
$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());
}
/**
* @covers ::getSetting
* @covers ::setSetting
* @covers ::getSettings
* @dataProvider factoryTypeProvider
*/
public function testDefaultFieldSettings($factory_name) : void {
$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));
}
}
/**
* @covers ::getDefaultValue
* @covers ::setDefaultValue
* @dataProvider factoryTypeProvider
*/
public function testFieldDefaultValue($factory_name) : void {
$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(ContentEntityBaseMockableClass::class)
->disableOriginalConstructor()
->getMock();
// Set the field item list class to be used to avoid requiring the typed
// data manager to retrieve it.
$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')
->willReturn('Drupal\\Core\\Field\\FieldItemBase');
$definition->setItemDefinition($data_definition);
// Set default value only with a literal.
$definition->setDefaultValue($default_value['value']);
$this->assertEquals($expected_default_value, $definition->getDefaultValue($entity));
// Set default value with an indexed array.
$definition->setDefaultValue($expected_default_value);
$this->assertEquals($expected_default_value, $definition->getDefaultValue($entity));
// Set default value with an empty array.
$definition->setDefaultValue([]);
$this->assertEquals([], $definition->getDefaultValue($entity));
// Set default value with NULL.
$definition->setDefaultValue(NULL);
$this->assertEquals([], $definition->getDefaultValue($entity));
}
/**
* Tests field translatable methods.
*
* @covers ::isTranslatable
* @covers ::setTranslatable
* @dataProvider factoryTypeProvider
*/
public function testFieldTranslatable($factory_name) : void {
$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());
}
/**
* Tests required.
*
* @covers ::isRequired
* @covers ::setRequired
* @dataProvider factoryTypeProvider
*/
public function testFieldRequired($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
$this->assertFalse($definition->isRequired());
$definition->setRequired(TRUE);
$this->assertTrue($definition->isRequired());
$definition->setRequired(FALSE);
$this->assertFalse($definition->isRequired());
}
/**
* Tests default value callbacks.
*
* @covers ::setDefaultValueCallback
* @dataProvider factoryTypeProvider
*/
public function testDefaultValueCallback($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
$callback = static::class . '::mockDefaultValueCallback';
// setDefaultValueCallback returns $this.
$this->assertSame($definition, $definition->setDefaultValueCallback($callback));
$this->assertSame($callback, $definition->getDefaultValueCallback());
}
/**
* Tests invalid default value callbacks.
*
* @covers ::setDefaultValueCallback
* @dataProvider factoryTypeProvider
*/
public function testInvalidDefaultValueCallback($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
// setDefaultValueCallback returns $this.
$this->expectException(\InvalidArgumentException::class);
$definition->setDefaultValueCallback([
static::class,
'mockDefaultValueCallback',
]);
}
/**
* Tests NULL default value callbacks.
*
* @covers ::setDefaultValueCallback
* @dataProvider factoryTypeProvider
*/
public function testNullDefaultValueCallback($factory_name) : void {
$definition = $this->initializeFieldUsingFactory($factory_name);
// setDefaultValueCallback returns $this.
$this->assertSame($definition, $definition->setDefaultValueCallback(NULL));
$this->assertNull($definition->getDefaultValueCallback());
}
/**
* Tests the display configuration settings.
*
* @covers ::isDisplayConfigurable
* @covers ::getDisplayOptions
* @dataProvider factoryTypeProvider
*/
public function testDisplayConfigurationSettings($factory_name) : void {
$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'));
}
/**
* Provides a Mock base field default value callback.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity interface.
* @param \Drupal\Core\Field\FieldDefinitionInterface $definition
* Field definition.
*
* @return string
* Default value.
*/
public static function mockDefaultValueCallback($entity, $definition) {
return 'a default value';
}
/**
* A data provider for all the types of factories that can create definitions.
*/
public static function factoryTypeProvider() {
return [
'::createFromFieldStorageDefinition factory' => [
'createFromFieldStorageDefinition',
],
'::create factory' => [
'create',
],
'::createFromDataType factory' => [
'createFromDataType',
],
'::createFromItemType factory' => [
'createFromItemType',
],
];
}
/**
* Creates a bundle field using a specified factory.
*
* @param string $factory_name
* The factory name to use.
*
* @return \Drupal\Core\Field\FieldDefinition
*/
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__);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
FieldDefinitionTest::$fieldType | protected | property | A dummy field type name. | |
FieldDefinitionTest::$fieldTypeDefinition | protected | property | A dummy field type definition. | |
FieldDefinitionTest::$storageDefinition | protected | property | The test field storage definition. | |
FieldDefinitionTest::$storageSupportsTranslation | protected | property | A flag for setting if the field storage supports translation. | |
FieldDefinitionTest::factoryTypeProvider | public static | function | A data provider for all the types of factories that can create definitions. | |
FieldDefinitionTest::initializeFieldUsingFactory | protected | function | Creates a bundle field using a specified factory. | |
FieldDefinitionTest::mockDefaultValueCallback | public static | function | Provides a Mock base field default value callback. | |
FieldDefinitionTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
FieldDefinitionTest::testBundle | public | function | @covers ::setTargetBundle @covers ::getTargetBundle @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testDefaultFieldSettings | public | function | @covers ::getSetting @covers ::setSetting @covers ::getSettings @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testDefaultValueCallback | public | function | Tests default value callbacks. | |
FieldDefinitionTest::testDisplayConfigurationSettings | public | function | Tests the display configuration settings. | |
FieldDefinitionTest::testFieldDefaultValue | public | function | @covers ::getDefaultValue @covers ::setDefaultValue @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testFieldDescription | public | function | @covers ::getDescription @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testFieldLabel | public | function | @covers ::getLabel @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testFieldName | public | function | @covers ::getName @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testFieldRequired | public | function | Tests required. | |
FieldDefinitionTest::testFieldSettings | public | function | @covers ::getSetting @covers ::setSetting @covers ::getSettings @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testFieldTranslatable | public | function | Tests field translatable methods. | |
FieldDefinitionTest::testFieldType | public | function | @covers ::getType @dataProvider factoryTypeProvider |
|
FieldDefinitionTest::testInvalidDefaultValueCallback | public | function | Tests invalid default value callbacks. | |
FieldDefinitionTest::testNullDefaultValueCallback | public | function | Tests NULL default value callbacks. | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
UnitTestCase::$root | protected | property | The app root. | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.