3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\entity_test\Entity\EntityTestMulRev;
6 use Drupal\language\Entity\ConfigurableLanguage;
9 * Tests proper revision propagation of entities.
13 class EntityRevisionTranslationTest extends EntityKernelTestBase {
18 public static $modules = ['language'];
23 protected function setUp() {
26 // Enable an additional language.
27 ConfigurableLanguage::createFromLangcode('de')->save();
29 $this->installEntitySchema('entity_test_mulrev');
33 * Tests if the translation object has the right revision id after new revision.
35 public function testNewRevisionAfterTranslation() {
36 $user = $this->createUser();
38 // Create a test entity.
39 $entity = EntityTestMulRev::create([
40 'name' => $this->randomString(),
41 'user_id' => $user->id(),
45 $old_rev_id = $entity->getRevisionId();
47 $translation = $entity->addTranslation('de');
48 $translation->setNewRevision();
51 $this->assertTrue($translation->getRevisionId() > $old_rev_id, 'The saved translation in new revision has a newer revision id.');
52 $this->assertTrue($this->reloadEntity($entity)->getRevisionId() > $old_rev_id, 'The entity from the storage has a newer revision id.');
56 * Tests if the translation object has the right revision id after new revision.
58 public function testRevertRevisionAfterTranslation() {
59 $user = $this->createUser();
60 $storage = $this->entityManager->getStorage('entity_test_mulrev');
62 // Create a test entity.
63 $entity = EntityTestMulRev::create([
64 'name' => $this->randomString(),
65 'user_id' => $user->id(),
69 $old_rev_id = $entity->getRevisionId();
71 $translation = $entity->addTranslation('de');
72 $translation->setNewRevision();
75 $entity = $this->reloadEntity($entity);
77 $this->assertTrue($entity->hasTranslation('de'));
79 $entity = $storage->loadRevision($old_rev_id);
81 $entity->setNewRevision();
82 $entity->isDefaultRevision(TRUE);
85 $entity = $this->reloadEntity($entity);
87 $this->assertFalse($entity->hasTranslation('de'));
91 * Tests the translation values when saving a pending revision.
93 public function testTranslationValuesWhenSavingPendingRevisions() {
94 $user = $this->createUser();
95 $storage = $this->entityManager->getStorage('entity_test_mulrev');
97 // Create a test entity and a translation for it.
98 $entity = EntityTestMulRev::create([
99 'name' => 'default revision - en',
100 'user_id' => $user->id(),
103 $entity->addTranslation('de', ['name' => 'default revision - de']);
106 // Create a pending revision for the entity and change a field value for
108 $pending_revision = $this->reloadEntity($entity);
110 $pending_revision->setNewRevision();
111 $pending_revision->isDefaultRevision(FALSE);
113 $pending_revision->name = 'pending revision - en';
114 $pending_revision->save();
116 $pending_revision_translation = $pending_revision->getTranslation('de');
117 $pending_revision_translation->name = 'pending revision - de';
118 $pending_revision_translation->save();
120 $pending_revision_id = $pending_revision->getRevisionId();
121 $pending_revision = $storage->loadRevision($pending_revision_id);
123 // Change the value of the field in the default language, save the pending
124 // revision and check that the value of the field in the second language is
125 // also taken from the pending revision, *not* from the default revision.
126 $pending_revision->name = 'updated pending revision - en';
127 $pending_revision->save();
129 $pending_revision = $storage->loadRevision($pending_revision_id);
131 $this->assertEquals($pending_revision->name->value, 'updated pending revision - en');
132 $this->assertEquals($pending_revision->getTranslation('de')->name->value, 'pending revision - de');
136 * Tests changing the default revision flag is propagated to all translations.
138 public function testDefaultRevision() {
139 // Create a test entity with a translation, which will internally trigger
140 // entity cloning for the new translation and create references for some of
141 // the entity properties.
142 $entity = EntityTestMulRev::create([
143 'name' => 'original',
146 $translation = $entity->addTranslation('de');
149 // Assert that the entity is in the default revision.
150 $this->assertTrue($entity->isDefaultRevision());
151 $this->assertTrue($translation->isDefaultRevision());
153 // Change the default revision flag on one of the entity translations and
154 // assert that the change is propagated to all entity translation objects.
155 $translation->isDefaultRevision(FALSE);
156 $this->assertFalse($entity->isDefaultRevision());
157 $this->assertFalse($translation->isDefaultRevision());