EntityUUIDTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. /**
  4. * Tests creation, saving, and loading of entity UUIDs.
  5. *
  6. * @group Entity
  7. */
  8. class EntityUUIDTest extends EntityKernelTestBase {
  9. protected function setUp() {
  10. parent::setUp();
  11. foreach (entity_test_entity_types() as $entity_type_id) {
  12. // The entity_test schema is installed by the parent.
  13. if ($entity_type_id != 'entity_test') {
  14. $this->installEntitySchema($entity_type_id);
  15. }
  16. }
  17. }
  18. /**
  19. * Tests UUID generation in entity CRUD operations.
  20. */
  21. public function testCRUD() {
  22. // All entity variations have to have the same results.
  23. foreach (entity_test_entity_types() as $entity_type) {
  24. $this->assertCRUD($entity_type);
  25. }
  26. }
  27. /**
  28. * Executes the UUID CRUD tests for the given entity type.
  29. *
  30. * @param string $entity_type
  31. * The entity type to run the tests with.
  32. */
  33. protected function assertCRUD($entity_type) {
  34. // Verify that no UUID is auto-generated when passing one for creation.
  35. $uuid_service = $this->container->get('uuid');
  36. $uuid = $uuid_service->generate();
  37. $custom_entity = $this->container->get('entity_type.manager')
  38. ->getStorage($entity_type)
  39. ->create([
  40. 'name' => $this->randomMachineName(),
  41. 'uuid' => $uuid,
  42. ]);
  43. $this->assertIdentical($custom_entity->uuid(), $uuid);
  44. // Save this entity, so we have more than one later.
  45. $custom_entity->save();
  46. // Verify that a new UUID is generated upon creating an entity.
  47. $entity = $this->container->get('entity_type.manager')
  48. ->getStorage($entity_type)
  49. ->create(['name' => $this->randomMachineName()]);
  50. $uuid = $entity->uuid();
  51. $this->assertTrue($uuid);
  52. // Verify that the new UUID is different.
  53. $this->assertNotEqual($custom_entity->uuid(), $uuid);
  54. // Verify that the UUID is retained upon saving.
  55. $entity->save();
  56. $this->assertIdentical($entity->uuid(), $uuid);
  57. // Verify that the UUID is retained upon loading.
  58. /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
  59. $storage = $this->container->get('entity_type.manager')
  60. ->getStorage($entity_type);
  61. $storage->resetCache([$entity->id()]);
  62. $entity_loaded = $storage->load($entity->id());
  63. $this->assertIdentical($entity_loaded->uuid(), $uuid);
  64. // Verify that \Drupal::entityManager()->loadEntityByUuid() loads the same entity.
  65. $entity_loaded_by_uuid = \Drupal::entityManager()->loadEntityByUuid($entity_type, $uuid, TRUE);
  66. $this->assertIdentical($entity_loaded_by_uuid->uuid(), $uuid);
  67. $this->assertEqual($entity_loaded_by_uuid->id(), $entity_loaded->id());
  68. // Creating a duplicate needs to result in a new UUID.
  69. $entity_duplicate = $entity->createDuplicate();
  70. foreach ($entity->getFields() as $property => $value) {
  71. switch ($property) {
  72. case 'uuid':
  73. $this->assertNotNull($entity_duplicate->uuid());
  74. $this->assertNotNull($entity->uuid());
  75. $this->assertNotEqual($entity_duplicate->uuid(), $entity->uuid());
  76. break;
  77. case 'id':
  78. $this->assertNull($entity_duplicate->id());
  79. $this->assertNotNull($entity->id());
  80. $this->assertNotEqual($entity_duplicate->id(), $entity->id());
  81. break;
  82. case 'revision_id':
  83. $this->assertNull($entity_duplicate->getRevisionId());
  84. $this->assertNotNull($entity->getRevisionId());
  85. $this->assertNotEqual($entity_duplicate->getRevisionId(), $entity->getRevisionId());
  86. $this->assertNotEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
  87. break;
  88. default:
  89. $this->assertEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
  90. }
  91. }
  92. $entity_duplicate->save();
  93. $this->assertNotEqual($entity->id(), $entity_duplicate->id());
  94. }
  95. }