View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Tests\UnitTestCase;
class BaseFieldDefinitionTest extends UnitTestCase {
protected $fieldType;
protected $fieldTypeDefinition;
protected function setUp() : void {
$field_type_manager = $this
->createMock('Drupal\\Core\\Field\\FieldTypePluginManagerInterface');
$this->fieldType = $this
->randomMachineName();
$this->fieldTypeDefinition = [
'id' => $this->fieldType,
'storage_settings' => [
'some_setting' => 'value 1',
],
'field_settings' => [
'some_instance_setting' => 'value 2',
],
];
$field_type_manager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue([
$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('getDefaultStorageSettings')
->with($this->fieldType)
->will($this
->returnValue($this->fieldTypeDefinition['storage_settings']));
$field_type_manager
->expects($this
->any())
->method('getDefaultFieldSettings')
->with($this->fieldType)
->will($this
->returnValue($this->fieldTypeDefinition['field_settings']));
$container = new ContainerBuilder();
$container
->set('plugin.manager.field.field_type', $field_type_manager);
\Drupal::setContainer($container);
}
public function testFieldName() {
$definition = BaseFieldDefinition::create($this->fieldType);
$field_name = $this
->randomMachineName();
$definition
->setName($field_name);
$this
->assertEquals($field_name, $definition
->getName());
}
public function testFieldLabel() {
$definition = BaseFieldDefinition::create($this->fieldType);
$label = $this
->randomMachineName();
$definition
->setLabel($label);
$this
->assertEquals($label, $definition
->getLabel());
}
public function testFieldDescription() {
$definition = BaseFieldDefinition::create($this->fieldType);
$description = $this
->randomMachineName();
$definition
->setDescription($description);
$this
->assertEquals($description, $definition
->getDescription());
}
public function testFieldType() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertEquals($this->fieldType, $definition
->getType());
}
public function testFieldSettings() {
$definition = BaseFieldDefinition::create($this->fieldType);
$setting = $this
->randomMachineName();
$value = $this
->randomMachineName();
$definition
->setSetting($setting, $value);
$this
->assertEquals($value, $definition
->getSetting($setting));
$default_settings = $this->fieldTypeDefinition['storage_settings'] + $this->fieldTypeDefinition['field_settings'];
$this
->assertEquals([
$setting => $value,
] + $default_settings, $definition
->getSettings());
}
public function testDefaultFieldSettings() {
$definition = BaseFieldDefinition::create($this->fieldType);
$expected_settings = $this->fieldTypeDefinition['storage_settings'] + $this->fieldTypeDefinition['field_settings'];
$this
->assertEquals($expected_settings, $definition
->getSettings());
foreach ($expected_settings as $setting => $value) {
$this
->assertEquals($value, $definition
->getSetting($setting));
}
}
public function testFieldDefaultValue() {
$definition = BaseFieldDefinition::create($this->fieldType);
$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');
$definition
->setItemDefinition(DataDefinition::createFromDataType('string')
->setClass(FieldItemBase::class));
$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 testFieldInitialValue() {
$definition = BaseFieldDefinition::create($this->fieldType);
$definition
->setItemDefinition(DataDefinition::createFromDataType('string')
->setClass(FieldItemBase::class));
$default_value = [
'value' => $this
->randomMachineName(),
];
$expected_default_value = [
$default_value,
];
$definition
->setInitialValue($default_value);
$entity = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->getMock();
$definition
->setClass('Drupal\\Core\\Field\\FieldItemList');
$this
->assertEquals($expected_default_value, $definition
->getInitialValue($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
->setInitialValue($default_value['value']);
$this
->assertEquals($expected_default_value, $definition
->getInitialValue($entity));
$definition
->setInitialValue($expected_default_value);
$this
->assertEquals($expected_default_value, $definition
->getInitialValue($entity));
$definition
->setInitialValue([]);
$this
->assertEquals([], $definition
->getInitialValue($entity));
$definition
->setInitialValue(NULL);
$this
->assertEquals([], $definition
->getInitialValue($entity));
}
public function testFieldTranslatable() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertFalse($definition
->isTranslatable());
$definition
->setTranslatable(TRUE);
$this
->assertTrue($definition
->isTranslatable());
$definition
->setTranslatable(FALSE);
$this
->assertFalse($definition
->isTranslatable());
}
public function testFieldRevisionable() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertFalse($definition
->isRevisionable());
$definition
->setRevisionable(TRUE);
$this
->assertTrue($definition
->isRevisionable());
$definition
->setRevisionable(FALSE);
$this
->assertFalse($definition
->isRevisionable());
}
public function testFieldCardinality() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertEquals(1, $definition
->getCardinality());
$definition
->setCardinality(2);
$this
->assertEquals(2, $definition
->getCardinality());
$definition
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this
->assertEquals(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, $definition
->getCardinality());
}
public function testFieldRequired() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertFalse($definition
->isRequired());
$definition
->setRequired(TRUE);
$this
->assertTrue($definition
->isRequired());
$definition
->setRequired(FALSE);
$this
->assertFalse($definition
->isRequired());
}
public function testFieldStorageRequired() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertFalse($definition
->isStorageRequired());
$definition
->setStorageRequired(TRUE);
$this
->assertTrue($definition
->isStorageRequired());
$definition
->setStorageRequired(FALSE);
$this
->assertFalse($definition
->isStorageRequired());
}
public function testFieldProvider() {
$definition = BaseFieldDefinition::create($this->fieldType);
$provider = $this
->randomMachineName();
$definition
->setProvider($provider);
$this
->assertEquals($provider, $definition
->getProvider());
}
public function testCustomStorage() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertFalse($definition
->hasCustomStorage());
$definition
->setCustomStorage(TRUE);
$this
->assertTrue($definition
->hasCustomStorage());
$definition
->setCustomStorage(FALSE);
$this
->assertFalse($definition
->hasCustomStorage());
}
public function testDefaultValueCallback() {
$definition = BaseFieldDefinition::create($this->fieldType);
$callback = static::class . '::mockDefaultValueCallback';
$this
->assertSame($definition, $definition
->setDefaultValueCallback($callback));
}
public function testInvalidDefaultValueCallback() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->expectException(\InvalidArgumentException::class);
$definition
->setDefaultValueCallback([
static::class,
'mockDefaultValueCallback',
]);
}
public function testNullDefaultValueCallback() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertSame($definition, $definition
->setDefaultValueCallback(NULL));
}
public static function mockDefaultValueCallback($entity, $definition) {
return 'a default value';
}
}