3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\entity_test\Entity\EntityTestMulRev;
6 use Drupal\language\Entity\ConfigurableLanguage;
9 * Tests the loaded Revision of an entity.
13 class EntityLoadedRevisionTest extends EntityKernelTestBase {
20 public static $modules = [
24 'content_translation',
30 protected function setUp() {
33 $this->installEntitySchema('entity_test_mulrev');
37 * Test getLoadedRevisionId() returns the correct ID throughout the process.
39 public function testLoadedRevisionId() {
40 // Create a basic EntityTestMulRev entity and save it.
41 $entity = EntityTestMulRev::create();
44 // Load the created entity and create a new revision.
45 $loaded = EntityTestMulRev::load($entity->id());
46 $loaded->setNewRevision(TRUE);
48 // Before saving, the loaded Revision ID should be the same as the created
49 // entity, not the same as the loaded entity (which does not have a revision
51 $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
52 $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
53 $this->assertSame(NULL, $loaded->getRevisionId());
55 // After updating the loaded Revision ID the result should be the same.
56 $loaded->updateLoadedRevisionId();
57 $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
58 $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
59 $this->assertSame(NULL, $loaded->getRevisionId());
63 // In entity_test_entity_update() the loaded Revision ID was stored in
64 // state. This should be the same as we had before calling $loaded->save().
65 /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */
66 $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
67 $this->assertEquals($entity->getRevisionId(), $loadedRevisionId);
68 $this->assertNotEquals($loaded->getRevisionId(), $loadedRevisionId);
70 // The revision ID and loaded Revision ID should be different for the two
71 // versions of the entity, but the same for a saved entity.
72 $this->assertNotEquals($loaded->getRevisionId(), $entity->getRevisionId());
73 $this->assertNotEquals($loaded->getLoadedRevisionId(), $entity->getLoadedRevisionId());
74 $this->assertEquals($entity->getRevisionId(), $entity->getLoadedRevisionId());
75 $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
79 * Tests the loaded revision ID after an entity re-save, clone and duplicate.
81 public function testLoadedRevisionIdWithNoNewRevision() {
82 // Create a basic EntityTestMulRev entity and save it.
83 $entity = EntityTestMulRev::create();
86 // Load the created entity and create a new revision.
87 $loaded = EntityTestMulRev::load($entity->id());
88 $loaded->setNewRevision(TRUE);
91 // Make a change to the loaded entity.
92 $loaded->set('name', 'dublin');
94 // The revision id and loaded Revision id should still be the same.
95 $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
99 // After saving, the loaded Revision id set in entity_test_entity_update()
100 // and returned from the entity should be the same as the entity's revision
101 // id because a new revision wasn't created, the existing revision was
103 $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
104 $this->assertEquals($loaded->getRevisionId(), $loadedRevisionId);
105 $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
107 // Creating a clone should keep the loaded Revision ID.
108 $clone = clone $loaded;
109 $this->assertSame($loaded->getLoadedRevisionId(), $clone->getLoadedRevisionId());
111 // Creating a duplicate should set a NULL loaded Revision ID.
112 $duplicate = $loaded->createDuplicate();
113 $this->assertSame(NULL, $duplicate->getLoadedRevisionId());
117 * Tests the loaded revision ID for translatable entities.
119 public function testTranslatedLoadedRevisionId() {
120 ConfigurableLanguage::createFromLangcode('fr')->save();
122 // Create a basic EntityTestMulRev entity and save it.
123 $entity = EntityTestMulRev::create();
126 // Load the created entity and create a new revision.
127 $loaded = EntityTestMulRev::load($entity->id());
128 $loaded->setNewRevision(TRUE);
131 // Check it all works with translations.
132 $french = $loaded->addTranslation('fr');
133 // Adding a revision should return the same for each language.
134 $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
135 $this->assertEquals($loaded->getRevisionId(), $french->getLoadedRevisionId());
136 $this->assertEquals($loaded->getLoadedRevisionId(), $french->getLoadedRevisionId());
138 // After saving nothing should change.
139 $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
140 $this->assertEquals($loaded->getRevisionId(), $french->getLoadedRevisionId());
141 $this->assertEquals($loaded->getLoadedRevisionId(), $french->getLoadedRevisionId());
142 $first_revision_id = $french->getRevisionId();
143 $french->setNewRevision();
144 // Setting a new revision will reset the loaded Revision ID.
145 $this->assertEquals($first_revision_id, $french->getLoadedRevisionId());
146 $this->assertEquals($first_revision_id, $loaded->getLoadedRevisionId());
147 $this->assertNotEquals($french->getRevisionId(), $french->getLoadedRevisionId());
148 $this->assertGreaterThan($french->getRevisionId(), $french->getLoadedRevisionId());
149 $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
150 $this->assertGreaterThan($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
152 // Saving the new revision will reset the origin revision ID again.
153 $this->assertEquals($french->getRevisionId(), $french->getLoadedRevisionId());
154 $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
158 * Tests re-saving the entity in entity_test_entity_insert().
160 public function testSaveInHookEntityInsert() {
161 // Create an entity which will be saved again in entity_test_entity_insert().
162 $entity = EntityTestMulRev::create(['name' => 'EntityLoadedRevisionTest']);
164 $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
165 $this->assertEquals($entity->getLoadedRevisionId(), $loadedRevisionId);
166 $this->assertEquals($entity->getRevisionId(), $entity->getLoadedRevisionId());