b43b376f5fa0767f0ecfc85e8e3e60838188e31a
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityRevisionTranslationTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\entity_test\Entity\EntityTestMulRev;
6 use Drupal\language\Entity\ConfigurableLanguage;
7
8 /**
9  * Tests proper revision propagation of entities.
10  *
11  * @group Entity
12  */
13 class EntityRevisionTranslationTest extends EntityKernelTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['language'];
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25
26     // Enable an additional language.
27     ConfigurableLanguage::createFromLangcode('de')->save();
28
29     $this->installEntitySchema('entity_test_mulrev');
30   }
31
32   /**
33    * Tests if the translation object has the right revision id after new revision.
34    */
35   public function testNewRevisionAfterTranslation() {
36     $user = $this->createUser();
37
38     // Create a test entity.
39     $entity = EntityTestMulRev::create([
40       'name' => $this->randomString(),
41       'user_id' => $user->id(),
42       'language' => 'en',
43     ]);
44     $entity->save();
45     $old_rev_id = $entity->getRevisionId();
46
47     $translation = $entity->addTranslation('de');
48     $translation->setNewRevision();
49     $translation->save();
50
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.');
53   }
54
55   /**
56    * Tests if the translation object has the right revision id after new revision.
57    */
58   public function testRevertRevisionAfterTranslation() {
59     $user = $this->createUser();
60     $storage = $this->entityManager->getStorage('entity_test_mulrev');
61
62     // Create a test entity.
63     $entity = EntityTestMulRev::create([
64       'name' => $this->randomString(),
65       'user_id' => $user->id(),
66       'language' => 'en',
67     ]);
68     $entity->save();
69     $old_rev_id = $entity->getRevisionId();
70
71     $translation = $entity->addTranslation('de');
72     $translation->setNewRevision();
73     $translation->save();
74
75     $entity = $this->reloadEntity($entity);
76
77     $this->assertTrue($entity->hasTranslation('de'));
78
79     $entity = $storage->loadRevision($old_rev_id);
80
81     $entity->setNewRevision();
82     $entity->isDefaultRevision(TRUE);
83     $entity->save();
84
85     $entity = $this->reloadEntity($entity);
86
87     $this->assertFalse($entity->hasTranslation('de'));
88   }
89
90   /**
91    * Tests the translation values when saving a pending revision.
92    */
93   public function testTranslationValuesWhenSavingPendingRevisions() {
94     $user = $this->createUser();
95     $storage = $this->entityManager->getStorage('entity_test_mulrev');
96
97     // Create a test entity and a translation for it.
98     $entity = EntityTestMulRev::create([
99       'name' => 'default revision - en',
100       'user_id' => $user->id(),
101       'language' => 'en',
102     ]);
103     $entity->addTranslation('de', ['name' => 'default revision - de']);
104     $entity->save();
105
106     // Create a pending revision for the entity and change a field value for
107     // both languages.
108     $pending_revision = $this->reloadEntity($entity);
109
110     $pending_revision->setNewRevision();
111     $pending_revision->isDefaultRevision(FALSE);
112
113     $pending_revision->name = 'pending revision - en';
114     $pending_revision->save();
115
116     $pending_revision_translation = $pending_revision->getTranslation('de');
117     $pending_revision_translation->name = 'pending revision - de';
118     $pending_revision_translation->save();
119
120     $pending_revision_id = $pending_revision->getRevisionId();
121     $pending_revision = $storage->loadRevision($pending_revision_id);
122
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();
128
129     $pending_revision = $storage->loadRevision($pending_revision_id);
130
131     $this->assertEquals($pending_revision->name->value, 'updated pending revision - en');
132     $this->assertEquals($pending_revision->getTranslation('de')->name->value, 'pending revision - de');
133   }
134
135   /**
136    * Tests changing the default revision flag is propagated to all translations.
137    */
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',
144       'language' => 'en',
145     ]);
146     $translation = $entity->addTranslation('de');
147     $entity->save();
148
149     // Assert that the entity is in the default revision.
150     $this->assertTrue($entity->isDefaultRevision());
151     $this->assertTrue($translation->isDefaultRevision());
152
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());
158   }
159
160 }