Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / diff / src / Tests / DiffLocaleTest.php
1 <?php
2
3 namespace Drupal\diff\Tests;
4 use Drupal\Tests\diff\Functional\CoreVersionUiTestTrait;
5
6 /**
7  * Test diff functionality with localization and translation.
8  *
9  * @group diff
10  */
11 class DiffLocaleTest extends DiffTestBase {
12
13   use CoreVersionUiTestTrait;
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'locale',
22     'content_translation',
23   ];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     $this->drupalLogin($this->rootUser);
32
33     // Add French language.
34     $edit = array(
35       'predefined_langcode' => 'fr',
36     );
37     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
38
39     // Enable content translation on articles.
40     $this->drupalGet('admin/config/regional/content-language');
41     $edit = array(
42       'entity_types[node]' => TRUE,
43       'settings[node][article][translatable]' => TRUE,
44       'settings[node][article][settings][language][language_alterable]' => TRUE,
45     );
46     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
47   }
48
49   /**
50    * Run all independent tests.
51    */
52   public function testAll() {
53     $this->doTestTranslationRevisions();
54     $this->doTestUndefinedTranslationFilter();
55     $this->doTestTranslationFilter();
56   }
57
58   /**
59    * Test Diff functionality for the revisions of a translated node.
60    */
61   protected function doTestTranslationRevisions() {
62
63     // Create an article and its translation. Assert aliases.
64     $edit = array(
65       'title[0][value]' => 'English node',
66       'langcode[0][value]' => 'en',
67     );
68     $this->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
69     $english_node = $this->drupalGetNodeByTitle('English node');
70
71     $this->drupalGet('node/' . $english_node->id() . '/translations');
72     $this->clickLink(t('Add'));
73     $edit = array(
74       'title[0][value]' => 'French node',
75       'revision' => FALSE,
76     );
77     $this->drupalPostNodeForm(NULL, $edit, t('Save and keep published (this translation)'));
78     $this->rebuildContainer();
79     $english_node = $this->drupalGetNodeByTitle('English node');
80     $french_node = $english_node->getTranslation('fr');
81
82     // Create a new revision on both languages.
83     $edit = array(
84       'title[0][value]' => 'Updated title',
85       'revision' => TRUE,
86     );
87     $this->drupalPostNodeForm('node/' . $english_node->id() . '/edit', $edit, t('Save and keep published (this translation)'));
88     $edit = array(
89       'title[0][value]' => 'Le titre',
90       'revision' => TRUE,
91     );
92     $this->drupalPostNodeForm('fr/node/' . $english_node->id() . '/edit', $edit, t('Save and keep published (this translation)'));
93
94     // View differences between revisions. Check that they don't mix up.
95     $this->drupalGet('node/' . $english_node->id() . '/revisions');
96     $this->drupalGet('node/' . $english_node->id() . '/revisions/view/1/2/split_fields');
97     $this->assertText('Title');
98     $this->assertText('English node');
99     $this->assertText('Updated title');
100     $this->drupalGet('fr/node/' . $english_node->id() . '/revisions');
101     $this->drupalGet('fr/node/' . $english_node->id() . '/revisions/view/1/3/split_fields');
102     $this->assertText('Title');
103     $this->assertNoText('English node');
104     $this->assertNoText('Updated title');
105     $this->assertText('French node');
106     $this->assertText('Le titre');
107   }
108
109   /**
110    * Tests the translation filtering when navigating trough revisions.
111    */
112   protected function doTestTranslationFilter() {
113     // Create a node in English.
114     $node = $this->drupalCreateNode([
115       'type' => 'article',
116       'title' => 'english_revision_0',
117     ]);
118     $revision1 = $node->getRevisionId();
119
120     // Translate to french.
121     $node->addTranslation('fr', ['title' => 'french_revision_0']);
122     $node->save();
123
124     // Create a revision in English.
125     $english_node = $node->getTranslation('en');
126     $english_node->setTitle('english_revision_1');
127     $english_node->setNewRevision(TRUE);
128     $english_node->save();
129     $revision2 = $node->getRevisionId();
130
131     // Create a revision in French.
132     $french_node = $node->getTranslation('fr');
133     $french_node->setTitle('french_revision_1');
134     $french_node->setNewRevision(TRUE);
135     $french_node->save();
136
137     // Create a new revision in English.
138     $english_node = $node->getTranslation('en');
139     $english_node->setTitle('english_revision_2');
140     $english_node->setNewRevision(TRUE);
141     $english_node->save();
142
143     // Create a new revision in French.
144     $french_node = $node->getTranslation('fr');
145     $french_node->setTitle('french_revision_2');
146     $french_node->setNewRevision(TRUE);
147     $french_node->save();
148
149     // Compare first two revisions.
150     $this->drupalGet('node/' . $node->id() . '/revisions/view/' . $revision1 . '/' . $revision2 . '/split_fields');
151     $diffs = $this->xpath('//span[@class="diffchange"]');
152     $this->assertEqual($diffs[0], 'english_revision_0');
153     $this->assertEqual($diffs[1], 'english_revision_1');
154
155     // Check next difference.
156     $this->clickLink('Next change');
157     $diffs = $this->xpath('//span[@class="diffchange"]');
158     $this->assertEqual($diffs[0], 'english_revision_1');
159     $this->assertEqual($diffs[1], 'english_revision_2');
160
161     // There shouldn't be other differences in the current language.
162     $this->assertNoLink('Next change');
163   }
164
165   /**
166    * Tests the undefined translation filtering when navigating trough revisions.
167    */
168   protected function doTestUndefinedTranslationFilter() {
169     // Create a node in with undefined langcode.
170     $node = $this->drupalCreateNode([
171       'type' => 'article',
172       'title' => 'undefined_language_revision_0',
173       'langcode' => 'und',
174     ]);
175     $revision1 = $node->getRevisionId();
176
177     // Create 3 new revisions of the node.
178     $node->setTitle('undefined_language_revision_1');
179     $node->setNewRevision(TRUE);
180     $node->save();
181     $revision2 = $node->getRevisionId();
182
183     $node->setTitle('undefined_language_revision_2');
184     $node->setNewRevision(TRUE);
185     $node->save();
186
187     $node->setTitle('undefined_language_revision_3');
188     $node->setNewRevision(TRUE);
189     $node->save();
190
191     // Check the amount of revisions displayed.
192     $this->drupalGet('node/' . $node->id() . '/revisions');
193     $element = $this->xpath('//*[@id="edit-node-revisions-table"]/tbody/tr');
194     $this->assertEqual(count($element), 4);
195
196     // Compare the first two revisions.
197     $this->drupalGet('node/' . $node->id() . '/revisions/view/' . $revision1 . '/' . $revision2 . '/split_fields');
198     $diffs = $this->xpath('//span[@class="diffchange"]');
199     $this->assertEqual($diffs[0], 'undefined_language_revision_0');
200     $this->assertEqual($diffs[1], 'undefined_language_revision_1');
201
202     // Compare the next two revisions.
203     $this->clickLink('Next change');
204     $diffs = $this->xpath('//span[@class="diffchange"]');
205     $this->assertEqual($diffs[0], 'undefined_language_revision_1');
206     $this->assertEqual($diffs[1], 'undefined_language_revision_2');
207   }
208
209 }