15bd871d6b60844c53817d6976786cc5b6caad8a
[yaffs-website] / web / modules / contrib / diff / src / Tests / DiffLocaleTest.php
1 <?php
2
3 namespace Drupal\diff\Tests;
4
5 /**
6  * Test diff functionality with localization and translation.
7  *
8  * @group diff
9  */
10 class DiffLocaleTest extends DiffTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = [
18     'locale',
19     'content_translation',
20   ];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     $this->drupalLogin($this->rootUser);
29
30     // Add French language.
31     $edit = array(
32       'predefined_langcode' => 'fr',
33     );
34     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
35
36     // Enable content translation on articles.
37     $this->drupalGet('admin/config/regional/content-language');
38     $edit = array(
39       'entity_types[node]' => TRUE,
40       'settings[node][article][translatable]' => TRUE,
41       'settings[node][article][settings][language][language_alterable]' => TRUE,
42     );
43     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
44   }
45
46   /**
47    * Run all independent tests.
48    */
49   public function testAll() {
50     $this->doTestTranslationRevisions();
51     $this->doTestUndefinedTranslationFilter();
52     $this->doTestTranslationFilter();
53   }
54
55   /**
56    * Test Diff functionality for the revisions of a translated node.
57    */
58   protected function doTestTranslationRevisions() {
59
60     // Create an article and its translation. Assert aliases.
61     $edit = array(
62       'title[0][value]' => 'English node',
63       'langcode[0][value]' => 'en',
64     );
65     $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
66     $english_node = $this->drupalGetNodeByTitle('English node');
67
68     $this->drupalGet('node/' . $english_node->id() . '/translations');
69     $this->clickLink(t('Add'));
70     $edit = array(
71       'title[0][value]' => 'French node',
72       'revision' => FALSE,
73     );
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');
78
79     // Create a new revision on both languages.
80     $edit = array(
81       'title[0][value]' => 'Updated title',
82       'revision' => TRUE,
83     );
84     $this->drupalPostForm('node/' . $english_node->id() . '/edit', $edit, t('Save and keep published (this translation)'));
85     $edit = array(
86       'title[0][value]' => 'Le titre',
87       'revision' => TRUE,
88     );
89     $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', $edit, t('Save and keep published (this translation)'));
90
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');
104   }
105
106   /**
107    * Tests the translation filtering when navigating trough revisions.
108    */
109   protected function doTestTranslationFilter() {
110     // Create a node in English.
111     $node = $this->drupalCreateNode([
112       'type' => 'article',
113       'title' => 'english_revision_0',
114     ]);
115     $revision1 = $node->getRevisionId();
116
117     // Translate to french.
118     $node->addTranslation('fr', ['title' => 'french_revision_0']);
119     $node->save();
120
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();
127
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();
133
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();
139
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();
145
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');
151
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');
157
158     // There shouldn't be other differences in the current language.
159     $this->assertNoLink('Next change');
160   }
161
162   /**
163    * Tests the undefined translation filtering when navigating trough revisions.
164    */
165   protected function doTestUndefinedTranslationFilter() {
166     // Create a node in with undefined langcode.
167     $node = $this->drupalCreateNode([
168       'type' => 'article',
169       'title' => 'undefined_language_revision_0',
170       'langcode' => 'und',
171     ]);
172     $revision1 = $node->getRevisionId();
173
174     // Create 3 new revisions of the node.
175     $node->setTitle('undefined_language_revision_1');
176     $node->setNewRevision(TRUE);
177     $node->save();
178     $revision2 = $node->getRevisionId();
179
180     $node->setTitle('undefined_language_revision_2');
181     $node->setNewRevision(TRUE);
182     $node->save();
183
184     $node->setTitle('undefined_language_revision_3');
185     $node->setNewRevision(TRUE);
186     $node->save();
187
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);
192
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');
198
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');
204   }
205
206 }