PageRenderTime 34ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionsTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 264 lines | 132 code | 38 blank | 94 comment | 0 complexity | dfa8706a24b15d6eaef96c47f634c2b3 MD5 | raw file
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\entity_test\Entity\EntityTestMulRev;
  4. use Drupal\language\Entity\ConfigurableLanguage;
  5. /**
  6. * Tests the loaded Revision of an entity.
  7. *
  8. * @coversDefaultClass \Drupal\Core\Entity\ContentEntityBase
  9. *
  10. * @group entity
  11. */
  12. class EntityRevisionsTest extends EntityKernelTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. protected static $modules = [
  19. 'system',
  20. 'entity_test',
  21. 'language',
  22. 'content_translation',
  23. ];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->installEntitySchema('entity_test_mulrev');
  30. }
  31. /**
  32. * Tests getLoadedRevisionId() returns the correct ID throughout the process.
  33. */
  34. public function testLoadedRevisionId() {
  35. // Create a basic EntityTestMulRev entity and save it.
  36. $entity = EntityTestMulRev::create();
  37. $entity->save();
  38. // Load the created entity and create a new revision.
  39. $loaded = EntityTestMulRev::load($entity->id());
  40. $loaded->setNewRevision(TRUE);
  41. // Before saving, the loaded Revision ID should be the same as the created
  42. // entity, not the same as the loaded entity (which does not have a revision
  43. // ID yet).
  44. $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
  45. $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  46. $this->assertSame(NULL, $loaded->getRevisionId());
  47. // After updating the loaded Revision ID the result should be the same.
  48. $loaded->updateLoadedRevisionId();
  49. $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
  50. $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  51. $this->assertSame(NULL, $loaded->getRevisionId());
  52. $loaded->save();
  53. // In entity_test_entity_update() the loaded Revision ID was stored in
  54. // state. This should be the same as we had before calling $loaded->save().
  55. /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */
  56. $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
  57. $this->assertEquals($entity->getRevisionId(), $loadedRevisionId);
  58. $this->assertNotEquals($loaded->getRevisionId(), $loadedRevisionId);
  59. // The revision ID and loaded Revision ID should be different for the two
  60. // versions of the entity, but the same for a saved entity.
  61. $this->assertNotEquals($loaded->getRevisionId(), $entity->getRevisionId());
  62. $this->assertNotEquals($loaded->getLoadedRevisionId(), $entity->getLoadedRevisionId());
  63. $this->assertEquals($entity->getRevisionId(), $entity->getLoadedRevisionId());
  64. $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  65. }
  66. /**
  67. * Tests the loaded revision ID after an entity re-save, clone and duplicate.
  68. */
  69. public function testLoadedRevisionIdWithNoNewRevision() {
  70. // Create a basic EntityTestMulRev entity and save it.
  71. $entity = EntityTestMulRev::create();
  72. $entity->save();
  73. // Load the created entity and create a new revision.
  74. $loaded = EntityTestMulRev::load($entity->id());
  75. $loaded->setNewRevision(TRUE);
  76. $loaded->save();
  77. // Make a change to the loaded entity.
  78. $loaded->set('name', 'dublin');
  79. // The revision id and loaded Revision id should still be the same.
  80. $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  81. $loaded->save();
  82. // After saving, the loaded Revision id set in entity_test_entity_update()
  83. // and returned from the entity should be the same as the entity's revision
  84. // id because a new revision wasn't created, the existing revision was
  85. // updated.
  86. $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
  87. $this->assertEquals($loaded->getRevisionId(), $loadedRevisionId);
  88. $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  89. // Creating a clone should keep the loaded Revision ID.
  90. $clone = clone $loaded;
  91. $this->assertSame($loaded->getLoadedRevisionId(), $clone->getLoadedRevisionId());
  92. // Creating a duplicate should set a NULL loaded Revision ID.
  93. $duplicate = $loaded->createDuplicate();
  94. $this->assertSame(NULL, $duplicate->getLoadedRevisionId());
  95. }
  96. /**
  97. * Tests the loaded revision ID for translatable entities.
  98. */
  99. public function testTranslatedLoadedRevisionId() {
  100. ConfigurableLanguage::createFromLangcode('fr')->save();
  101. // Create a basic EntityTestMulRev entity and save it.
  102. $entity = EntityTestMulRev::create();
  103. $entity->save();
  104. // Load the created entity and create a new revision.
  105. $loaded = EntityTestMulRev::load($entity->id());
  106. $loaded->setNewRevision(TRUE);
  107. $loaded->save();
  108. // Check it all works with translations.
  109. $french = $loaded->addTranslation('fr');
  110. // Adding a revision should return the same for each language.
  111. $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
  112. $this->assertEquals($loaded->getRevisionId(), $french->getLoadedRevisionId());
  113. $this->assertEquals($loaded->getLoadedRevisionId(), $french->getLoadedRevisionId());
  114. $french->save();
  115. // After saving nothing should change.
  116. $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
  117. $this->assertEquals($loaded->getRevisionId(), $french->getLoadedRevisionId());
  118. $this->assertEquals($loaded->getLoadedRevisionId(), $french->getLoadedRevisionId());
  119. $first_revision_id = $french->getRevisionId();
  120. $french->setNewRevision();
  121. // Setting a new revision will reset the loaded Revision ID.
  122. $this->assertEquals($first_revision_id, $french->getLoadedRevisionId());
  123. $this->assertEquals($first_revision_id, $loaded->getLoadedRevisionId());
  124. $this->assertNotEquals($french->getRevisionId(), $french->getLoadedRevisionId());
  125. $this->assertGreaterThan($french->getRevisionId(), $french->getLoadedRevisionId());
  126. $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  127. $this->assertGreaterThan($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  128. $french->save();
  129. // Saving the new revision will reset the origin revision ID again.
  130. $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
  131. $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
  132. }
  133. /**
  134. * Tests re-saving the entity in entity_test_entity_insert().
  135. */
  136. public function testSaveInHookEntityInsert() {
  137. // Create an entity which will be saved again in entity_test_entity_insert().
  138. $entity = EntityTestMulRev::create(['name' => 'EntityLoadedRevisionTest']);
  139. $entity->save();
  140. $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
  141. $this->assertEquals($entity->getLoadedRevisionId(), $loadedRevisionId);
  142. $this->assertEquals($entity->getRevisionId(), $entity->getLoadedRevisionId());
  143. }
  144. /**
  145. * Tests that latest revisions are working as expected.
  146. *
  147. * @covers ::isLatestRevision
  148. */
  149. public function testIsLatestRevision() {
  150. // Create a basic EntityTestMulRev entity and save it.
  151. $entity = EntityTestMulRev::create();
  152. $entity->save();
  153. $this->assertTrue($entity->isLatestRevision());
  154. // Load the created entity and create a new pending revision.
  155. $pending_revision = EntityTestMulRev::load($entity->id());
  156. $pending_revision->setNewRevision(TRUE);
  157. $pending_revision->isDefaultRevision(FALSE);
  158. // The pending revision should still be marked as the latest one before it
  159. // is saved.
  160. $this->assertTrue($pending_revision->isLatestRevision());
  161. $pending_revision->save();
  162. $this->assertTrue($pending_revision->isLatestRevision());
  163. // Load the default revision and check that it is not marked as the latest
  164. // revision.
  165. $default_revision = EntityTestMulRev::load($entity->id());
  166. $this->assertFalse($default_revision->isLatestRevision());
  167. }
  168. /**
  169. * Tests that latest affected revisions are working as expected.
  170. *
  171. * The latest revision affecting a particular translation behaves as the
  172. * latest revision for monolingual entities.
  173. *
  174. * @covers ::isLatestTranslationAffectedRevision
  175. * @covers \Drupal\Core\Entity\ContentEntityStorageBase::getLatestRevisionId
  176. * @covers \Drupal\Core\Entity\ContentEntityStorageBase::getLatestTranslationAffectedRevisionId
  177. */
  178. public function testIsLatestAffectedRevisionTranslation() {
  179. ConfigurableLanguage::createFromLangcode('it')->save();
  180. // Create a basic EntityTestMulRev entity and save it.
  181. $entity = EntityTestMulRev::create();
  182. $entity->setName($this->randomString());
  183. $entity->save();
  184. $this->assertTrue($entity->isLatestTranslationAffectedRevision());
  185. // Load the created entity and create a new pending revision.
  186. $pending_revision = EntityTestMulRev::load($entity->id());
  187. $pending_revision->setName($this->randomString());
  188. $pending_revision->setNewRevision(TRUE);
  189. $pending_revision->isDefaultRevision(FALSE);
  190. // Check that no revision affecting Italian is available, given that no
  191. // Italian translation has been created yet.
  192. /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  193. $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
  194. $this->assertNull($storage->getLatestTranslationAffectedRevisionId($entity->id(), 'it'));
  195. $this->assertEquals($pending_revision->getLoadedRevisionId(), $storage->getLatestRevisionId($entity->id()));
  196. // The pending revision should still be marked as the latest affected one
  197. // before it is saved.
  198. $this->assertTrue($pending_revision->isLatestTranslationAffectedRevision());
  199. $pending_revision->save();
  200. $this->assertTrue($pending_revision->isLatestTranslationAffectedRevision());
  201. // Load the default revision and check that it is not marked as the latest
  202. // (translation-affected) revision.
  203. $default_revision = EntityTestMulRev::load($entity->id());
  204. $this->assertFalse($default_revision->isLatestRevision());
  205. $this->assertFalse($default_revision->isLatestTranslationAffectedRevision());
  206. // Add a translation in a new pending revision and verify that both the
  207. // English and Italian revision translations are the latest affected
  208. // revisions for their respective languages, while the English revision is
  209. // not the latest revision.
  210. /** @var \Drupal\entity_test\Entity\EntityTestMulRev $en_revision */
  211. $en_revision = clone $pending_revision;
  212. /** @var \Drupal\entity_test\Entity\EntityTestMulRev $it_revision */
  213. $it_revision = $pending_revision->addTranslation('it');
  214. $it_revision->setName($this->randomString());
  215. $it_revision->setNewRevision(TRUE);
  216. $it_revision->isDefaultRevision(FALSE);
  217. // @todo Remove this once the "original" property works with revisions. See
  218. // https://www.drupal.org/project/drupal/issues/2859042.
  219. $it_revision->original = $storage->loadRevision($it_revision->getLoadedRevisionId());
  220. $it_revision->save();
  221. $this->assertTrue($it_revision->isLatestRevision());
  222. $this->assertTrue($it_revision->isLatestTranslationAffectedRevision());
  223. $this->assertFalse($en_revision->isLatestRevision());
  224. $this->assertTrue($en_revision->isLatestTranslationAffectedRevision());
  225. }
  226. }