View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\UnitTestCase;
class BaseFieldDefinitionTest extends UnitTestCase {
protected $fieldType;
protected $fieldTypeDefinition;
protected function setUp() {
$field_type_manager = $this
->getMock('Drupal\\Core\\Field\\FieldTypePluginManagerInterface');
$this->fieldType = $this
->randomMachineName();
$this->fieldTypeDefinition = array(
'id' => $this->fieldType,
'storage_settings' => array(
'some_setting' => 'value 1',
),
'field_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('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(array(
$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 = array(
'value' => $this
->randomMachineName(),
);
$expected_default_value = array(
$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(array());
$this
->assertEquals(array(), $definition
->getDefaultValue($entity));
$definition
->setDefaultValue(NULL);
$this
->assertEquals([], $definition
->getDefaultValue($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 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 = get_class($this) . '::mockDefaultValueCallback';
$this
->assertSame($definition, $definition
->setDefaultValueCallback($callback));
}
public function testInvalidDefaultValueCallback() {
$definition = BaseFieldDefinition::create($this->fieldType);
$this
->assertSame($definition, $definition
->setDefaultValueCallback([
get_class($this),
'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';
}
}