Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / content_translation / tests / src / Functional / ContentTranslationRevisionTranslationDeletionTest.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Functional;
4
5 use Drupal\Core\Url;
6 use Drupal\language\Entity\ConfigurableLanguage;
7
8 /**
9  * Tests that revision translation deletion is handled correctly.
10  *
11  * @group content_translation
12  */
13 class ContentTranslationRevisionTranslationDeletionTest extends ContentTranslationPendingRevisionTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function setUp() {
19     parent::setUp();
20     $this->enableContentModeration();
21   }
22
23   /**
24    * Tests that translation overview handles pending revisions correctly.
25    */
26   public function testOverview() {
27     $index = 1;
28     $accounts = [
29       $this->rootUser,
30       $this->editor,
31       $this->translator,
32     ];
33     foreach ($accounts as $account) {
34       $this->currentAccount = $account;
35       $this->doTestOverview($index++);
36     }
37   }
38
39   /**
40    * Performs a test run.
41    *
42    * @param int $index
43    *   The test run index.
44    */
45   public function doTestOverview($index) {
46     $this->drupalLogin($this->currentAccount);
47
48     // Create a test node.
49     $values = [
50       'title' => "Test $index.1 EN",
51       'moderation_state' => 'published',
52     ];
53     $id = $this->createEntity($values, 'en');
54     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
55     $entity = $this->storage->load($id);
56
57     // Add a draft translation and check that it is available only in the latest
58     // revision.
59     $add_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
60         $entity->getEntityTypeId() => $id,
61         'source' => 'en',
62         'target' => 'it',
63       ],
64       [
65         'language' => ConfigurableLanguage::load('it'),
66         'absolute' => FALSE,
67       ]
68     );
69     $add_translation_href = $add_translation_url->toString();
70     $this->drupalGet($add_translation_url);
71     $edit = [
72       'title[0][value]' => "Test $index.2 IT",
73       'moderation_state[0][state]' => 'draft',
74     ];
75     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
76     $entity = $this->storage->loadUnchanged($id);
77     $this->assertFalse($entity->hasTranslation('it'));
78     $it_revision = $this->loadRevisionTranslation($entity, 'it');
79     $this->assertTrue($it_revision->hasTranslation('it'));
80
81     // Check that translations cannot be deleted in drafts.
82     $overview_url = $entity->toUrl('drupal:content-translation-overview');
83     $this->drupalGet($overview_url);
84     $it_delete_url = $this->getDeleteUrl($it_revision);
85     $it_delete_href = $it_delete_url->toString();
86     $this->assertSession()->linkByHrefNotExists($it_delete_href);
87     $warning = 'The "Delete translation" action is only available for published translations.';
88     $this->assertSession()->pageTextContains($warning);
89     $this->drupalGet($this->getEditUrl($it_revision));
90     $this->assertSession()->buttonNotExists('Delete translation');
91
92     // Publish the translation and verify it can be deleted.
93     $edit = [
94       'title[0][value]' => "Test $index.3 IT",
95       'moderation_state[0][state]' => 'published',
96     ];
97     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
98     $entity = $this->storage->loadUnchanged($id);
99     $this->assertTrue($entity->hasTranslation('it'));
100     $it_revision = $this->loadRevisionTranslation($entity, 'it');
101     $this->assertTrue($it_revision->hasTranslation('it'));
102     $this->drupalGet($overview_url);
103     $this->assertSession()->linkByHrefExists($it_delete_href);
104     $this->assertSession()->pageTextNotContains($warning);
105     $this->drupalGet($this->getEditUrl($it_revision));
106     $this->assertSession()->buttonExists('Delete translation');
107
108     // Create an English draft and verify the published translation was
109     // preserved.
110     $this->drupalLogin($this->editor);
111     $en_revision = $this->loadRevisionTranslation($entity, 'en');
112     $this->drupalGet($this->getEditUrl($en_revision));
113     $edit = [
114       'title[0][value]' => "Test $index.4 EN",
115       'moderation_state[0][state]' => 'draft',
116     ];
117     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
118     $entity = $this->storage->loadUnchanged($id);
119     $this->assertTrue($entity->hasTranslation('it'));
120     $en_revision = $this->loadRevisionTranslation($entity, 'en');
121     $this->assertTrue($en_revision->hasTranslation('it'));
122     $this->drupalLogin($this->currentAccount);
123
124     // Delete the translation and verify that it is actually gone and that it is
125     // possible to create it again.
126     $this->drupalGet($it_delete_url);
127     $this->drupalPostForm(NULL, [], 'Delete Italian translation');
128     $entity = $this->storage->loadUnchanged($id);
129     $this->assertFalse($entity->hasTranslation('it'));
130     $it_revision = $this->loadRevisionTranslation($entity, 'it');
131     $this->assertTrue($it_revision->wasDefaultRevision());
132     $this->assertTrue($it_revision->hasTranslation('it'));
133     $this->assertTrue($it_revision->getRevisionId() < $entity->getRevisionId());
134     $this->drupalGet($overview_url);
135     $this->assertSession()->linkByHrefNotExists($this->getEditUrl($it_revision)->toString());
136     $this->assertSession()->linkByHrefExists($add_translation_href);
137
138     // Publish the English draft and verify the translation is not accidentally
139     // restored.
140     $this->drupalLogin($this->editor);
141     $en_revision = $this->loadRevisionTranslation($entity, 'en');
142     $this->drupalGet($this->getEditUrl($en_revision));
143     $edit = [
144       'title[0][value]' => "Test $index.6 EN",
145       'moderation_state[0][state]' => 'published',
146     ];
147     $this->drupalPostForm(NULL, $edit, t('Save'));
148     $entity = $this->storage->loadUnchanged($id);
149     $this->assertFalse($entity->hasTranslation('it'));
150     $this->drupalLogin($this->currentAccount);
151
152     // Create a published translation again and verify it could be deleted.
153     $this->drupalGet($add_translation_url);
154     $edit = [
155       'title[0][value]' => "Test $index.7 IT",
156       'moderation_state[0][state]' => 'published',
157     ];
158     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
159     $entity = $this->storage->loadUnchanged($id);
160     $this->assertTrue($entity->hasTranslation('it'));
161     $it_revision = $this->loadRevisionTranslation($entity, 'it');
162     $this->assertTrue($it_revision->hasTranslation('it'));
163     $this->drupalGet($overview_url);
164     $this->assertSession()->linkByHrefExists($it_delete_href);
165
166     // Create a translation draft again and verify it cannot be deleted.
167     $this->drupalGet($this->getEditUrl($it_revision));
168     $edit = [
169       'title[0][value]' => "Test $index.8 IT",
170       'moderation_state[0][state]' => 'draft',
171     ];
172     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
173     $entity = $this->storage->loadUnchanged($id);
174     $this->assertTrue($entity->hasTranslation('it'));
175     $it_revision = $this->loadRevisionTranslation($entity, 'it');
176     $this->assertTrue($it_revision->hasTranslation('it'));
177     $this->drupalGet($overview_url);
178     $this->assertSession()->linkByHrefNotExists($it_delete_href);
179
180     // Delete the translation draft and verify the translation can be deleted
181     // again, since the active revision is now a default revision.
182     $this->drupalLogin($this->editor);
183     $this->drupalGet($it_revision->toUrl('version-history'));
184     $revision_deletion_url = Url::fromRoute('node.revision_delete_confirm', [
185         'node' => $id,
186         'node_revision' => $it_revision->getRevisionId(),
187       ],
188       [
189         'language' => ConfigurableLanguage::load('it'),
190         'absolute' => FALSE,
191       ]
192     );
193     $revision_deletion_href = $revision_deletion_url->toString();
194     $this->getSession()->getDriver()->click("//a[@href='$revision_deletion_href']");
195     $this->drupalPostForm(NULL, [], 'Delete');
196     $this->drupalLogin($this->currentAccount);
197     $this->drupalGet($overview_url);
198     $this->assertSession()->linkByHrefExists($it_delete_href);
199
200     // Verify that now the translation can be deleted.
201     $this->drupalGet($it_delete_url);
202     $this->drupalPostForm(NULL, [], 'Delete Italian translation');
203     $entity = $this->storage->loadUnchanged($id);
204     $this->assertFalse($entity->hasTranslation('it'));
205     $it_revision = $this->loadRevisionTranslation($entity, 'it');
206     $this->assertTrue($it_revision->wasDefaultRevision());
207     $this->assertTrue($it_revision->hasTranslation('it'));
208     $this->assertTrue($it_revision->getRevisionId() < $entity->getRevisionId());
209     $this->drupalGet($overview_url);
210     $this->assertSession()->linkByHrefNotExists($this->getEditUrl($it_revision)->toString());
211     $this->assertSession()->linkByHrefExists($add_translation_href);
212   }
213
214 }