function BaseFieldDefinitionTest::testFieldInitialValue
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php \Drupal\Tests\Core\Entity\BaseFieldDefinitionTest::testFieldInitialValue()
- 8.9.x core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php \Drupal\Tests\Core\Entity\BaseFieldDefinitionTest::testFieldInitialValue()
- 10 core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php \Drupal\Tests\Core\Entity\BaseFieldDefinitionTest::testFieldInitialValue()
Tests field initial value.
@covers ::getInitialValue @covers ::setInitialValue
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ BaseFieldDefinitionTest.php, line 210
Class
- BaseFieldDefinitionTest
- Unit test for BaseFieldDefinition.
Namespace
Drupal\Tests\Core\EntityCode
public function testFieldInitialValue() : void {
$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(ContentEntityBaseMockableClass::class)
->disableOriginalConstructor()
->onlyMethods([])
->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->getInitialValue());
$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->setInitialValue($default_value['value']);
$this->assertEquals($expected_default_value, $definition->getInitialValue());
// Set default value with an indexed array.
$definition->setInitialValue($expected_default_value);
$this->assertEquals($expected_default_value, $definition->getInitialValue());
// Set default value with an empty array.
$definition->setInitialValue([]);
$this->assertEquals([], $definition->getInitialValue());
// Set default value with NULL.
$definition->setInitialValue(NULL);
$this->assertEquals([], $definition->getInitialValue());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.