3 namespace Drupal\diff\Tests;
6 * Test diff functionality with localization and translation.
10 class DiffLocaleTest extends DiffTestBase {
17 public static $modules = [
19 'content_translation',
25 protected function setUp() {
28 $this->drupalLogin($this->rootUser);
30 // Add French language.
32 'predefined_langcode' => 'fr',
34 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
36 // Enable content translation on articles.
37 $this->drupalGet('admin/config/regional/content-language');
39 'entity_types[node]' => TRUE,
40 'settings[node][article][translatable]' => TRUE,
41 'settings[node][article][settings][language][language_alterable]' => TRUE,
43 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
47 * Run all independent tests.
49 public function testAll() {
50 $this->doTestTranslationRevisions();
51 $this->doTestUndefinedTranslationFilter();
52 $this->doTestTranslationFilter();
56 * Test Diff functionality for the revisions of a translated node.
58 protected function doTestTranslationRevisions() {
60 // Create an article and its translation. Assert aliases.
62 'title[0][value]' => 'English node',
63 'langcode[0][value]' => 'en',
65 $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
66 $english_node = $this->drupalGetNodeByTitle('English node');
68 $this->drupalGet('node/' . $english_node->id() . '/translations');
69 $this->clickLink(t('Add'));
71 'title[0][value]' => 'French node',
74 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
75 $this->rebuildContainer();
76 $english_node = $this->drupalGetNodeByTitle('English node');
77 $french_node = $english_node->getTranslation('fr');
79 // Create a new revision on both languages.
81 'title[0][value]' => 'Updated title',
84 $this->drupalPostForm('node/' . $english_node->id() . '/edit', $edit, t('Save and keep published (this translation)'));
86 'title[0][value]' => 'Le titre',
89 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', $edit, t('Save and keep published (this translation)'));
91 // View differences between revisions. Check that they don't mix up.
92 $this->drupalGet('node/' . $english_node->id() . '/revisions');
93 $this->drupalGet('node/' . $english_node->id() . '/revisions/view/1/2/split_fields');
94 $this->assertText('Title');
95 $this->assertText('English node');
96 $this->assertText('Updated title');
97 $this->drupalGet('fr/node/' . $english_node->id() . '/revisions');
98 $this->drupalGet('fr/node/' . $english_node->id() . '/revisions/view/1/3/split_fields');
99 $this->assertText('Title');
100 $this->assertNoText('English node');
101 $this->assertNoText('Updated title');
102 $this->assertText('French node');
103 $this->assertText('Le titre');
107 * Tests the translation filtering when navigating trough revisions.
109 protected function doTestTranslationFilter() {
110 // Create a node in English.
111 $node = $this->drupalCreateNode([
113 'title' => 'english_revision_0',
115 $revision1 = $node->getRevisionId();
117 // Translate to french.
118 $node->addTranslation('fr', ['title' => 'french_revision_0']);
121 // Create a revision in English.
122 $english_node = $node->getTranslation('en');
123 $english_node->setTitle('english_revision_1');
124 $english_node->setNewRevision(TRUE);
125 $english_node->save();
126 $revision2 = $node->getRevisionId();
128 // Create a revision in French.
129 $french_node = $node->getTranslation('fr');
130 $french_node->setTitle('french_revision_1');
131 $french_node->setNewRevision(TRUE);
132 $french_node->save();
134 // Create a new revision in English.
135 $english_node = $node->getTranslation('en');
136 $english_node->setTitle('english_revision_2');
137 $english_node->setNewRevision(TRUE);
138 $english_node->save();
140 // Create a new revision in French.
141 $french_node = $node->getTranslation('fr');
142 $french_node->setTitle('french_revision_2');
143 $french_node->setNewRevision(TRUE);
144 $french_node->save();
146 // Compare first two revisions.
147 $this->drupalGet('node/' . $node->id() . '/revisions/view/' . $revision1 . '/' . $revision2 . '/split_fields');
148 $diffs = $this->xpath('//span[@class="diffchange"]');
149 $this->assertEqual($diffs[0], 'english_revision_0');
150 $this->assertEqual($diffs[1], 'english_revision_1');
152 // Check next difference.
153 $this->clickLink('Next change');
154 $diffs = $this->xpath('//span[@class="diffchange"]');
155 $this->assertEqual($diffs[0], 'english_revision_1');
156 $this->assertEqual($diffs[1], 'english_revision_2');
158 // There shouldn't be other differences in the current language.
159 $this->assertNoLink('Next change');
163 * Tests the undefined translation filtering when navigating trough revisions.
165 protected function doTestUndefinedTranslationFilter() {
166 // Create a node in with undefined langcode.
167 $node = $this->drupalCreateNode([
169 'title' => 'undefined_language_revision_0',
172 $revision1 = $node->getRevisionId();
174 // Create 3 new revisions of the node.
175 $node->setTitle('undefined_language_revision_1');
176 $node->setNewRevision(TRUE);
178 $revision2 = $node->getRevisionId();
180 $node->setTitle('undefined_language_revision_2');
181 $node->setNewRevision(TRUE);
184 $node->setTitle('undefined_language_revision_3');
185 $node->setNewRevision(TRUE);
188 // Check the amount of revisions displayed.
189 $this->drupalGet('node/' . $node->id() . '/revisions');
190 $element = $this->xpath('//*[@id="edit-node-revisions-table"]/tbody/tr');
191 $this->assertEqual(count($element), 4);
193 // Compare the first two revisions.
194 $this->drupalGet('node/' . $node->id() . '/revisions/view/' . $revision1 . '/' . $revision2 . '/split_fields');
195 $diffs = $this->xpath('//span[@class="diffchange"]');
196 $this->assertEqual($diffs[0], 'undefined_language_revision_0');
197 $this->assertEqual($diffs[1], 'undefined_language_revision_1');
199 // Compare the next two revisions.
200 $this->clickLink('Next change');
201 $diffs = $this->xpath('//span[@class="diffchange"]');
202 $this->assertEqual($diffs[0], 'undefined_language_revision_1');
203 $this->assertEqual($diffs[1], 'undefined_language_revision_2');