EntityFieldDefaultValueTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Entity/EntityFieldDefaultValueTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Render\FormattableMarkup;
class EntityFieldDefaultValueTest extends EntityKernelTestBase {
protected $uuid;
protected function setUp() : void {
parent::setUp();
$this->uuid = $this->container
->get('uuid');
}
public function testDefaultValues() {
foreach (entity_test_entity_types() as $entity_type) {
$this
->assertDefaultValues($entity_type);
}
}
protected function assertDefaultValues($entity_type_id) {
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type_id)
->create();
$definition = $this->entityTypeManager
->getDefinition($entity_type_id);
$langcode_key = $definition
->getKey('langcode');
$this
->assertEquals('en', $entity->{$langcode_key}->value, new FormattableMarkup('%entity_type: Default language', [
'%entity_type' => $entity_type_id,
]));
$this
->assertTrue(Uuid::isValid($entity->uuid->value), new FormattableMarkup('%entity_type: Default UUID', [
'%entity_type' => $entity_type_id,
]));
$this
->assertEquals([], $entity->name
->getValue(), 'Field has one empty value by default.');
}
public function testDefaultValueCallback() {
$entity = $this->entityTypeManager
->getStorage('entity_test_default_value')
->create();
$string = 'description_' . $entity
->language()
->getId();
$expected = [
[
'shape' => "shape:0:{$string}",
'color' => "color:0:{$string}",
],
[
'shape' => "shape:1:{$string}",
'color' => "color:1:{$string}",
],
];
$this
->assertEquals($expected, $entity->description
->getValue());
}
}