3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\Component\Uuid\Uuid;
6 use Drupal\Component\Render\FormattableMarkup;
9 * Tests default values for entity fields.
13 class EntityFieldDefaultValueTest extends EntityKernelTestBase {
16 * The UUID object to be used for generating UUIDs.
18 * @var \Drupal\Component\Uuid\UuidInterface
22 protected function setUp() {
24 // Initiate the generator object.
25 $this->uuid = $this->container->get('uuid');
29 * Tests default values on entities and fields.
31 public function testDefaultValues() {
32 // All entity variations have to have the same results.
33 foreach (entity_test_entity_types() as $entity_type) {
34 $this->assertDefaultValues($entity_type);
39 * Executes a test set for a defined entity type.
41 * @param string $entity_type_id
42 * The entity type to run the tests with.
44 protected function assertDefaultValues($entity_type_id) {
45 $entity = $this->container->get('entity_type.manager')
46 ->getStorage($entity_type_id)
48 $definition = $this->entityManager->getDefinition($entity_type_id);
49 $langcode_key = $definition->getKey('langcode');
50 $this->assertEqual($entity->{$langcode_key}->value, 'en', new FormattableMarkup('%entity_type: Default language', ['%entity_type' => $entity_type_id]));
51 $this->assertTrue(Uuid::isValid($entity->uuid->value), new FormattableMarkup('%entity_type: Default UUID', ['%entity_type' => $entity_type_id]));
52 $this->assertEqual($entity->name->getValue(), [], 'Field has one empty value by default.');
56 * Tests custom default value callbacks.
58 public function testDefaultValueCallback() {
59 $entity = $this->entityManager->getStorage('entity_test_default_value')->create();
60 // The description field has a default value callback for testing, see
61 // entity_test_field_default_value().
62 $string = 'description_' . $entity->language()->getId();
65 'shape' => "shape:0:$string",
66 'color' => "color:0:$string",
69 'shape' => "shape:1:$string",
70 'color' => "color:1:$string",
73 $this->assertEqual($entity->description->getValue(), $expected);