KeyValueContentEntityStorageTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Drupal\KernelTests\Core\KeyValueStore;
  3. use Drupal\Core\Entity\EntityMalformedException;
  4. use Drupal\Core\Entity\EntityStorageException;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\entity_test\Entity\EntityTestLabel;
  7. /**
  8. * Tests KeyValueEntityStorage for content entities.
  9. *
  10. * @group KeyValueStore
  11. */
  12. class KeyValueContentEntityStorageTest extends KernelTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['user', 'entity_test', 'keyvalue_test'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->installEntitySchema('user');
  25. }
  26. /**
  27. * Tests CRUD operations.
  28. *
  29. * @covers \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::hasData
  30. */
  31. public function testCRUD() {
  32. $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
  33. $storage = \Drupal::entityTypeManager()->getStorage('entity_test_label');
  34. $this->assertFalse($storage->hasData());
  35. // Verify default properties on a newly created empty entity.
  36. $empty = EntityTestLabel::create();
  37. $this->assertIdentical($empty->id->value, NULL);
  38. $this->assertIdentical($empty->name->value, NULL);
  39. $this->assertTrue($empty->uuid->value);
  40. $this->assertIdentical($empty->langcode->value, $default_langcode);
  41. // Verify ConfigEntity properties/methods on the newly created empty entity.
  42. $this->assertIdentical($empty->isNew(), TRUE);
  43. $this->assertIdentical($empty->bundle(), 'entity_test_label');
  44. $this->assertIdentical($empty->id(), NULL);
  45. $this->assertTrue($empty->uuid());
  46. $this->assertIdentical($empty->label(), NULL);
  47. // Verify Entity properties/methods on the newly created empty entity.
  48. $this->assertIdentical($empty->getEntityTypeId(), 'entity_test_label');
  49. // The URI can only be checked after saving.
  50. try {
  51. $empty->urlInfo();
  52. $this->fail('EntityMalformedException was thrown.');
  53. }
  54. catch (EntityMalformedException $e) {
  55. $this->pass('EntityMalformedException was thrown.');
  56. }
  57. // Verify that an empty entity cannot be saved.
  58. try {
  59. $empty->save();
  60. $this->fail('EntityMalformedException was thrown.');
  61. }
  62. catch (EntityMalformedException $e) {
  63. $this->pass('EntityMalformedException was thrown.');
  64. }
  65. // Verify that an entity with an empty ID string is considered empty, too.
  66. $empty_id = EntityTestLabel::create([
  67. 'id' => '',
  68. ]);
  69. $this->assertIdentical($empty_id->isNew(), TRUE);
  70. try {
  71. $empty_id->save();
  72. $this->fail('EntityMalformedException was thrown.');
  73. }
  74. catch (EntityMalformedException $e) {
  75. $this->pass('EntityMalformedException was thrown.');
  76. }
  77. // Verify properties on a newly created entity.
  78. $entity_test = EntityTestLabel::create($expected = [
  79. 'id' => $this->randomMachineName(),
  80. 'name' => $this->randomString(),
  81. ]);
  82. $this->assertIdentical($entity_test->id->value, $expected['id']);
  83. $this->assertTrue($entity_test->uuid->value);
  84. $this->assertNotEqual($entity_test->uuid->value, $empty->uuid->value);
  85. $this->assertIdentical($entity_test->name->value, $expected['name']);
  86. $this->assertIdentical($entity_test->langcode->value, $default_langcode);
  87. // Verify methods on the newly created entity.
  88. $this->assertIdentical($entity_test->isNew(), TRUE);
  89. $this->assertIdentical($entity_test->id(), $expected['id']);
  90. $this->assertTrue($entity_test->uuid());
  91. $expected['uuid'] = $entity_test->uuid();
  92. $this->assertIdentical($entity_test->label(), $expected['name']);
  93. // Verify that the entity can be saved.
  94. try {
  95. $status = $entity_test->save();
  96. $this->pass('EntityMalformedException was not thrown.');
  97. }
  98. catch (EntityMalformedException $e) {
  99. $this->fail('EntityMalformedException was not thrown.');
  100. }
  101. // Verify that hasData() returns the expected result.
  102. $this->assertTrue($storage->hasData());
  103. // Verify that the correct status is returned and properties did not change.
  104. $this->assertIdentical($status, SAVED_NEW);
  105. $this->assertIdentical($entity_test->id(), $expected['id']);
  106. $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
  107. $this->assertIdentical($entity_test->label(), $expected['name']);
  108. $this->assertIdentical($entity_test->isNew(), FALSE);
  109. // Save again, and verify correct status and properties again.
  110. $status = $entity_test->save();
  111. $this->assertIdentical($status, SAVED_UPDATED);
  112. $this->assertIdentical($entity_test->id(), $expected['id']);
  113. $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
  114. $this->assertIdentical($entity_test->label(), $expected['name']);
  115. $this->assertIdentical($entity_test->isNew(), FALSE);
  116. // Ensure that creating an entity with the same id as an existing one is not
  117. // possible.
  118. $same_id = EntityTestLabel::create([
  119. 'id' => $entity_test->id(),
  120. ]);
  121. $this->assertIdentical($same_id->isNew(), TRUE);
  122. try {
  123. $same_id->save();
  124. $this->fail('Not possible to overwrite an entity entity.');
  125. }
  126. catch (EntityStorageException $e) {
  127. $this->pass('Not possible to overwrite an entity entity.');
  128. }
  129. // Verify that renaming the ID returns correct status and properties.
  130. $ids = [$expected['id'], 'second_' . $this->randomMachineName(4), 'third_' . $this->randomMachineName(4)];
  131. for ($i = 1; $i < 3; $i++) {
  132. $old_id = $ids[$i - 1];
  133. $new_id = $ids[$i];
  134. // Before renaming, everything should point to the current ID.
  135. $this->assertIdentical($entity_test->id(), $old_id);
  136. // Rename.
  137. $entity_test->id = $new_id;
  138. $this->assertIdentical($entity_test->id(), $new_id);
  139. $status = $entity_test->save();
  140. $this->assertIdentical($status, SAVED_UPDATED);
  141. $this->assertIdentical($entity_test->isNew(), FALSE);
  142. // Verify that originalID points to new ID directly after renaming.
  143. $this->assertIdentical($entity_test->id(), $new_id);
  144. }
  145. }
  146. /**
  147. * Tests uninstallation of a module that does not use the SQL entity storage.
  148. */
  149. public function testUninstall() {
  150. $uninstall_validator_reasons = \Drupal::service('content_uninstall_validator')->validate('keyvalue_test');
  151. $this->assertEmpty($uninstall_validator_reasons);
  152. }
  153. }