3 namespace Drupal\KernelTests\Core\Entity;
6 * Tests creation, saving, and loading of entity UUIDs.
10 class EntityUUIDTest extends EntityKernelTestBase {
12 protected function setUp() {
15 foreach (entity_test_entity_types() as $entity_type_id) {
16 // The entity_test schema is installed by the parent.
17 if ($entity_type_id != 'entity_test') {
18 $this->installEntitySchema($entity_type_id);
24 * Tests UUID generation in entity CRUD operations.
26 public function testCRUD() {
27 // All entity variations have to have the same results.
28 foreach (entity_test_entity_types() as $entity_type) {
29 $this->assertCRUD($entity_type);
34 * Executes the UUID CRUD tests for the given entity type.
36 * @param string $entity_type
37 * The entity type to run the tests with.
39 protected function assertCRUD($entity_type) {
40 // Verify that no UUID is auto-generated when passing one for creation.
41 $uuid_service = $this->container->get('uuid');
42 $uuid = $uuid_service->generate();
43 $custom_entity = $this->container->get('entity_type.manager')
44 ->getStorage($entity_type)
46 'name' => $this->randomMachineName(),
49 $this->assertIdentical($custom_entity->uuid(), $uuid);
50 // Save this entity, so we have more than one later.
51 $custom_entity->save();
53 // Verify that a new UUID is generated upon creating an entity.
54 $entity = $this->container->get('entity_type.manager')
55 ->getStorage($entity_type)
56 ->create(['name' => $this->randomMachineName()]);
57 $uuid = $entity->uuid();
58 $this->assertTrue($uuid);
60 // Verify that the new UUID is different.
61 $this->assertNotEqual($custom_entity->uuid(), $uuid);
63 // Verify that the UUID is retained upon saving.
65 $this->assertIdentical($entity->uuid(), $uuid);
67 // Verify that the UUID is retained upon loading.
68 /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
69 $storage = $this->container->get('entity_type.manager')
70 ->getStorage($entity_type);
71 $storage->resetCache([$entity->id()]);
72 $entity_loaded = $storage->load($entity->id());
73 $this->assertIdentical($entity_loaded->uuid(), $uuid);
75 // Verify that \Drupal::entityManager()->loadEntityByUuid() loads the same entity.
76 $entity_loaded_by_uuid = \Drupal::entityManager()->loadEntityByUuid($entity_type, $uuid, TRUE);
77 $this->assertIdentical($entity_loaded_by_uuid->uuid(), $uuid);
78 $this->assertEqual($entity_loaded_by_uuid->id(), $entity_loaded->id());
80 // Creating a duplicate needs to result in a new UUID.
81 $entity_duplicate = $entity->createDuplicate();
82 foreach ($entity->getFields() as $property => $value) {
85 $this->assertNotNull($entity_duplicate->uuid());
86 $this->assertNotNull($entity->uuid());
87 $this->assertNotEqual($entity_duplicate->uuid(), $entity->uuid());
90 $this->assertNull($entity_duplicate->id());
91 $this->assertNotNull($entity->id());
92 $this->assertNotEqual($entity_duplicate->id(), $entity->id());
95 $this->assertNull($entity_duplicate->getRevisionId());
96 $this->assertNotNull($entity->getRevisionId());
97 $this->assertNotEqual($entity_duplicate->getRevisionId(), $entity->getRevisionId());
98 $this->assertNotEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
101 $this->assertEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
104 $entity_duplicate->save();
105 $this->assertNotEqual($entity->id(), $entity_duplicate->id());