$default_value = [ 'value' => $this->randomMachineName(), ]; $expected_default_value = [$default_value]; $definition->setDefaultValue($default_value); $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase') ->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'); $definition->setItemDefinition(DataDefinition::createFromDataType('string')->setClass(FieldItemBase::class)); $this->assertEquals($expected_default_value, $definition->getDefaultValue($entity));
// 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));
// 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([]);