Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\node\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
6 use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
|
Chris@0
|
7 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\node\Entity\Node;
|
Chris@0
|
10 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Tests the Node Translation UI.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group node
|
Chris@0
|
16 */
|
Chris@0
|
17 class NodeTranslationUITest extends ContentTranslationUITestBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * {inheritdoc}
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $defaultCacheContexts = [
|
Chris@0
|
23 'languages:language_interface',
|
Chris@0
|
24 'theme',
|
Chris@0
|
25 'route',
|
Chris@0
|
26 'timezone',
|
Chris@0
|
27 'url.path.parent',
|
Chris@0
|
28 'url.query_args:_wrapper_format',
|
Chris@14
|
29 'user.roles',
|
Chris@14
|
30 // These two cache contexts are added by BigPipe.
|
Chris@14
|
31 'cookies:big_pipe_nojs',
|
Chris@14
|
32 'session.exists',
|
Chris@0
|
33 ];
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Modules to enable.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var array
|
Chris@0
|
39 */
|
Chris@0
|
40 public static $modules = ['block', 'language', 'content_translation', 'node', 'datetime', 'field_ui', 'help'];
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * The profile to install as a basis for testing.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @var string
|
Chris@0
|
46 */
|
Chris@0
|
47 protected $profile = 'standard';
|
Chris@0
|
48
|
Chris@0
|
49 protected function setUp() {
|
Chris@0
|
50 $this->entityTypeId = 'node';
|
Chris@0
|
51 $this->bundle = 'article';
|
Chris@0
|
52 parent::setUp();
|
Chris@0
|
53
|
Chris@0
|
54 // Ensure the help message is shown even with prefixed paths.
|
Chris@0
|
55 $this->drupalPlaceBlock('help_block', ['region' => 'content']);
|
Chris@0
|
56
|
Chris@0
|
57 // Display the language selector.
|
Chris@0
|
58 $this->drupalLogin($this->administrator);
|
Chris@0
|
59 $edit = ['language_configuration[language_alterable]' => TRUE];
|
Chris@0
|
60 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
|
Chris@0
|
61 $this->drupalLogin($this->translator);
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Tests the basic translation UI.
|
Chris@0
|
66 */
|
Chris@0
|
67 public function testTranslationUI() {
|
Chris@0
|
68 parent::testTranslationUI();
|
Chris@0
|
69 $this->doUninstallTest();
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Tests changing the published status on a node without fields.
|
Chris@0
|
74 */
|
Chris@0
|
75 public function testPublishedStatusNoFields() {
|
Chris@0
|
76 // Test changing the published status of an article without fields.
|
Chris@0
|
77 $this->drupalLogin($this->administrator);
|
Chris@0
|
78 // Delete all fields.
|
Chris@0
|
79 $this->drupalGet('admin/structure/types/manage/article/fields');
|
Chris@0
|
80 $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $this->fieldName . '/delete', [], t('Delete'));
|
Chris@0
|
81 $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.field_tags/delete', [], t('Delete'));
|
Chris@0
|
82 $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.field_image/delete', [], t('Delete'));
|
Chris@0
|
83
|
Chris@0
|
84 // Add a node.
|
Chris@0
|
85 $default_langcode = $this->langcodes[0];
|
Chris@0
|
86 $values[$default_langcode] = ['title' => [['value' => $this->randomMachineName()]]];
|
Chris@0
|
87 $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
|
Chris@0
|
88 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
89 ->getStorage($this->entityTypeId);
|
Chris@0
|
90 $storage->resetCache([$this->entityId]);
|
Chris@0
|
91 $entity = $storage->load($this->entityId);
|
Chris@0
|
92
|
Chris@0
|
93 // Add a content translation.
|
Chris@0
|
94 $langcode = 'fr';
|
Chris@0
|
95 $language = ConfigurableLanguage::load($langcode);
|
Chris@0
|
96 $values[$langcode] = ['title' => [['value' => $this->randomMachineName()]]];
|
Chris@0
|
97
|
Chris@0
|
98 $entity_type_id = $entity->getEntityTypeId();
|
Chris@0
|
99 $add_url = Url::fromRoute("entity.$entity_type_id.content_translation_add", [
|
Chris@0
|
100 $entity->getEntityTypeId() => $entity->id(),
|
Chris@0
|
101 'source' => $default_langcode,
|
Chris@0
|
102 'target' => $langcode
|
Chris@0
|
103 ], ['language' => $language]);
|
Chris@0
|
104 $edit = $this->getEditValues($values, $langcode);
|
Chris@0
|
105 $edit['status[value]'] = FALSE;
|
Chris@0
|
106 $this->drupalPostForm($add_url, $edit, t('Save (this translation)'));
|
Chris@0
|
107
|
Chris@0
|
108 $storage->resetCache([$this->entityId]);
|
Chris@0
|
109 $entity = $storage->load($this->entityId);
|
Chris@0
|
110 $translation = $entity->getTranslation($langcode);
|
Chris@0
|
111 // Make sure we unpublished the node correctly.
|
Chris@0
|
112 $this->assertFalse($this->manager->getTranslationMetadata($translation)->isPublished(), 'The translation has been correctly unpublished.');
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * {@inheritdoc}
|
Chris@0
|
117 */
|
Chris@0
|
118 protected function getTranslatorPermissions() {
|
Chris@0
|
119 return array_merge(parent::getTranslatorPermissions(), ['administer nodes', "edit any $this->bundle content"]);
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * {@inheritdoc}
|
Chris@0
|
124 */
|
Chris@0
|
125 protected function getEditorPermissions() {
|
Chris@0
|
126 return ['administer nodes', 'create article content'];
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 /**
|
Chris@0
|
130 * {@inheritdoc}
|
Chris@0
|
131 */
|
Chris@0
|
132 protected function getAdministratorPermissions() {
|
Chris@0
|
133 return array_merge(parent::getAdministratorPermissions(), ['access administration pages', 'administer content types', 'administer node fields', 'access content overview', 'bypass node access', 'administer languages', 'administer themes', 'view the administration theme']);
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * {@inheritdoc}
|
Chris@0
|
138 */
|
Chris@0
|
139 protected function getNewEntityValues($langcode) {
|
Chris@0
|
140 return ['title' => [['value' => $this->randomMachineName()]]] + parent::getNewEntityValues($langcode);
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * {@inheritdoc}
|
Chris@0
|
145 */
|
Chris@0
|
146 protected function doTestPublishedStatus() {
|
Chris@0
|
147 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
148 ->getStorage($this->entityTypeId);
|
Chris@0
|
149 $storage->resetCache([$this->entityId]);
|
Chris@0
|
150 $entity = $storage->load($this->entityId);
|
Chris@0
|
151 $languages = $this->container->get('language_manager')->getLanguages();
|
Chris@0
|
152
|
Chris@0
|
153 $statuses = [
|
Chris@0
|
154 TRUE,
|
Chris@0
|
155 FALSE,
|
Chris@0
|
156 ];
|
Chris@0
|
157
|
Chris@0
|
158 foreach ($statuses as $index => $value) {
|
Chris@0
|
159 // (Un)publish the node translations and check that the translation
|
Chris@0
|
160 // statuses are (un)published accordingly.
|
Chris@0
|
161 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
162 $options = ['language' => $languages[$langcode]];
|
Chris@0
|
163 $url = $entity->urlInfo('edit-form', $options);
|
Chris@0
|
164 $this->drupalPostForm($url, ['status[value]' => $value], t('Save') . $this->getFormSubmitSuffix($entity, $langcode), $options);
|
Chris@0
|
165 }
|
Chris@0
|
166 $storage->resetCache([$this->entityId]);
|
Chris@0
|
167 $entity = $storage->load($this->entityId);
|
Chris@0
|
168 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
169 // The node is created as unpublished thus we switch to the published
|
Chris@0
|
170 // status first.
|
Chris@0
|
171 $status = !$index;
|
Chris@0
|
172 $translation = $entity->getTranslation($langcode);
|
Chris@0
|
173 $this->assertEqual($status, $this->manager->getTranslationMetadata($translation)->isPublished(), 'The translation has been correctly unpublished.');
|
Chris@0
|
174 }
|
Chris@0
|
175 }
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 /**
|
Chris@0
|
179 * {@inheritdoc}
|
Chris@0
|
180 */
|
Chris@0
|
181 protected function doTestAuthoringInfo() {
|
Chris@0
|
182 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
183 ->getStorage($this->entityTypeId);
|
Chris@0
|
184 $storage->resetCache([$this->entityId]);
|
Chris@0
|
185 $entity = $storage->load($this->entityId);
|
Chris@0
|
186 $languages = $this->container->get('language_manager')->getLanguages();
|
Chris@0
|
187 $values = [];
|
Chris@0
|
188
|
Chris@0
|
189 // Post different base field information for each translation.
|
Chris@0
|
190 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
191 $user = $this->drupalCreateUser();
|
Chris@0
|
192 $values[$langcode] = [
|
Chris@0
|
193 'uid' => $user->id(),
|
Chris@0
|
194 'created' => REQUEST_TIME - mt_rand(0, 1000),
|
Chris@0
|
195 'sticky' => (bool) mt_rand(0, 1),
|
Chris@0
|
196 'promote' => (bool) mt_rand(0, 1),
|
Chris@0
|
197 ];
|
Chris@0
|
198 $edit = [
|
Chris@0
|
199 'uid[0][target_id]' => $user->getUsername(),
|
Chris@0
|
200 'created[0][value][date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
|
Chris@0
|
201 'created[0][value][time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
|
Chris@0
|
202 'sticky[value]' => $values[$langcode]['sticky'],
|
Chris@0
|
203 'promote[value]' => $values[$langcode]['promote'],
|
Chris@0
|
204 ];
|
Chris@0
|
205 $options = ['language' => $languages[$langcode]];
|
Chris@0
|
206 $url = $entity->urlInfo('edit-form', $options);
|
Chris@0
|
207 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode), $options);
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 $storage->resetCache([$this->entityId]);
|
Chris@0
|
211 $entity = $storage->load($this->entityId);
|
Chris@0
|
212 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
213 $translation = $entity->getTranslation($langcode);
|
Chris@0
|
214 $metadata = $this->manager->getTranslationMetadata($translation);
|
Chris@0
|
215 $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
|
Chris@0
|
216 $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
|
Chris@0
|
217 $this->assertEqual($translation->isSticky(), $values[$langcode]['sticky'], 'Sticky of Translation correctly stored.');
|
Chris@0
|
218 $this->assertEqual($translation->isPromoted(), $values[$langcode]['promote'], 'Promoted of Translation correctly stored.');
|
Chris@0
|
219 }
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 /**
|
Chris@0
|
223 * Tests that translation page inherits admin status of edit page.
|
Chris@0
|
224 */
|
Chris@0
|
225 public function testTranslationLinkTheme() {
|
Chris@0
|
226 $this->drupalLogin($this->administrator);
|
Chris@0
|
227 $article = $this->drupalCreateNode(['type' => 'article', 'langcode' => $this->langcodes[0]]);
|
Chris@0
|
228
|
Chris@0
|
229 // Set up Seven as the admin theme and use it for node editing.
|
Chris@0
|
230 $this->container->get('theme_handler')->install(['seven']);
|
Chris@0
|
231 $edit = [];
|
Chris@0
|
232 $edit['admin_theme'] = 'seven';
|
Chris@0
|
233 $edit['use_admin_theme'] = TRUE;
|
Chris@0
|
234 $this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
|
Chris@0
|
235 $this->drupalGet('node/' . $article->id() . '/translations');
|
Chris@0
|
236 $this->assertRaw('core/themes/seven/css/base/elements.css', 'Translation uses admin theme if edit is admin.');
|
Chris@0
|
237
|
Chris@0
|
238 // Turn off admin theme for editing, assert inheritance to translations.
|
Chris@0
|
239 $edit['use_admin_theme'] = FALSE;
|
Chris@0
|
240 $this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
|
Chris@0
|
241 $this->drupalGet('node/' . $article->id() . '/translations');
|
Chris@0
|
242 $this->assertNoRaw('core/themes/seven/css/base/elements.css', 'Translation uses frontend theme if edit is frontend.');
|
Chris@0
|
243
|
Chris@0
|
244 // Assert presence of translation page itself (vs. DisabledBundle below).
|
Chris@0
|
245 $this->assertResponse(200);
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 /**
|
Chris@0
|
249 * Tests that no metadata is stored for a disabled bundle.
|
Chris@0
|
250 */
|
Chris@0
|
251 public function testDisabledBundle() {
|
Chris@0
|
252 // Create a bundle that does not have translation enabled.
|
Chris@0
|
253 $disabledBundle = $this->randomMachineName();
|
Chris@0
|
254 $this->drupalCreateContentType(['type' => $disabledBundle, 'name' => $disabledBundle]);
|
Chris@0
|
255
|
Chris@0
|
256 // Create a node for each bundle.
|
Chris@0
|
257 $node = $this->drupalCreateNode([
|
Chris@0
|
258 'type' => $this->bundle,
|
Chris@0
|
259 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
Chris@0
|
260 ]);
|
Chris@0
|
261
|
Chris@0
|
262 // Make sure that nothing was inserted into the {content_translation} table.
|
Chris@0
|
263 $rows = db_query('SELECT nid, count(nid) AS count FROM {node_field_data} WHERE type <> :type GROUP BY nid HAVING count(nid) >= 2', [':type' => $this->bundle])->fetchAll();
|
Chris@0
|
264 $this->assertEqual(0, count($rows));
|
Chris@0
|
265
|
Chris@0
|
266 // Ensure the translation tab is not accessible.
|
Chris@0
|
267 $this->drupalGet('node/' . $node->id() . '/translations');
|
Chris@0
|
268 $this->assertResponse(403);
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 /**
|
Chris@0
|
272 * Tests that translations are rendered properly.
|
Chris@0
|
273 */
|
Chris@0
|
274 public function testTranslationRendering() {
|
Chris@0
|
275 $default_langcode = $this->langcodes[0];
|
Chris@0
|
276 $values[$default_langcode] = $this->getNewEntityValues($default_langcode);
|
Chris@0
|
277 $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
|
Chris@0
|
278 $node = \Drupal::entityManager()->getStorage($this->entityTypeId)->load($this->entityId);
|
Chris@0
|
279 $node->setPromoted(TRUE);
|
Chris@0
|
280
|
Chris@0
|
281 // Create translations.
|
Chris@0
|
282 foreach (array_diff($this->langcodes, [$default_langcode]) as $langcode) {
|
Chris@0
|
283 $values[$langcode] = $this->getNewEntityValues($langcode);
|
Chris@0
|
284 $translation = $node->addTranslation($langcode, $values[$langcode]);
|
Chris@0
|
285 // Publish and promote the translation to frontpage.
|
Chris@0
|
286 $translation->setPromoted(TRUE);
|
Chris@0
|
287 $translation->setPublished(TRUE);
|
Chris@0
|
288 }
|
Chris@0
|
289 $node->save();
|
Chris@0
|
290
|
Chris@0
|
291 // Test that the frontpage view displays the correct translations.
|
Chris@0
|
292 \Drupal::service('module_installer')->install(['views'], TRUE);
|
Chris@0
|
293 $this->rebuildContainer();
|
Chris@0
|
294 $this->doTestTranslations('node', $values);
|
Chris@0
|
295
|
Chris@0
|
296 // Enable the translation language renderer.
|
Chris@0
|
297 $view = \Drupal::entityManager()->getStorage('view')->load('frontpage');
|
Chris@0
|
298 $display = &$view->getDisplay('default');
|
Chris@0
|
299 $display['display_options']['rendering_language'] = '***LANGUAGE_entity_translation***';
|
Chris@0
|
300 $view->save();
|
Chris@0
|
301
|
Chris@0
|
302 // Need to check from the beginning, including the base_path, in the url
|
Chris@0
|
303 // since the pattern for the default language might be a substring of
|
Chris@0
|
304 // the strings for other languages.
|
Chris@0
|
305 $base_path = base_path();
|
Chris@0
|
306
|
Chris@0
|
307 // Check the frontpage for 'Read more' links to each translation.
|
Chris@0
|
308 // See also assertTaxonomyPage() in NodeAccessBaseTableTest.
|
Chris@0
|
309 $node_href = 'node/' . $node->id();
|
Chris@0
|
310 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
311 $this->drupalGet('node', ['language' => \Drupal::languageManager()->getLanguage($langcode)]);
|
Chris@0
|
312 $num_match_found = 0;
|
Chris@0
|
313 if ($langcode == 'en') {
|
Chris@0
|
314 // Site default language does not have langcode prefix in the URL.
|
Chris@0
|
315 $expected_href = $base_path . $node_href;
|
Chris@0
|
316 }
|
Chris@0
|
317 else {
|
Chris@0
|
318 $expected_href = $base_path . $langcode . '/' . $node_href;
|
Chris@0
|
319 }
|
Chris@0
|
320 $pattern = '|^' . $expected_href . '$|';
|
Chris@0
|
321 foreach ($this->xpath("//a[text()='Read more']") as $link) {
|
Chris@0
|
322 if (preg_match($pattern, $link->getAttribute('href'), $matches) == TRUE) {
|
Chris@0
|
323 $num_match_found++;
|
Chris@0
|
324 }
|
Chris@0
|
325 }
|
Chris@0
|
326 $this->assertTrue($num_match_found == 1, 'There is 1 Read more link, ' . $expected_href . ', for the ' . $langcode . ' translation of a node on the frontpage. (Found ' . $num_match_found . '.)');
|
Chris@0
|
327 }
|
Chris@0
|
328
|
Chris@0
|
329 // Check the frontpage for 'Add new comment' links that include the
|
Chris@0
|
330 // language.
|
Chris@0
|
331 $comment_form_href = 'node/' . $node->id() . '#comment-form';
|
Chris@0
|
332 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
333 $this->drupalGet('node', ['language' => \Drupal::languageManager()->getLanguage($langcode)]);
|
Chris@0
|
334 $num_match_found = 0;
|
Chris@0
|
335 if ($langcode == 'en') {
|
Chris@0
|
336 // Site default language does not have langcode prefix in the URL.
|
Chris@0
|
337 $expected_href = $base_path . $comment_form_href;
|
Chris@0
|
338 }
|
Chris@0
|
339 else {
|
Chris@0
|
340 $expected_href = $base_path . $langcode . '/' . $comment_form_href;
|
Chris@0
|
341 }
|
Chris@0
|
342 $pattern = '|^' . $expected_href . '$|';
|
Chris@0
|
343 foreach ($this->xpath("//a[text()='Add new comment']") as $link) {
|
Chris@0
|
344 if (preg_match($pattern, $link->getAttribute('href'), $matches) == TRUE) {
|
Chris@0
|
345 $num_match_found++;
|
Chris@0
|
346 }
|
Chris@0
|
347 }
|
Chris@0
|
348 $this->assertTrue($num_match_found == 1, 'There is 1 Add new comment link, ' . $expected_href . ', for the ' . $langcode . ' translation of a node on the frontpage. (Found ' . $num_match_found . '.)');
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 // Test that the node page displays the correct translations.
|
Chris@0
|
352 $this->doTestTranslations('node/' . $node->id(), $values);
|
Chris@0
|
353
|
Chris@0
|
354 // Test that the node page has the correct alternate hreflang links.
|
Chris@0
|
355 $this->doTestAlternateHreflangLinks($node->urlInfo());
|
Chris@0
|
356 }
|
Chris@0
|
357
|
Chris@0
|
358 /**
|
Chris@0
|
359 * Tests that the given path displays the correct translation values.
|
Chris@0
|
360 *
|
Chris@0
|
361 * @param string $path
|
Chris@0
|
362 * The path to be tested.
|
Chris@0
|
363 * @param array $values
|
Chris@0
|
364 * The translation values to be found.
|
Chris@0
|
365 */
|
Chris@0
|
366 protected function doTestTranslations($path, array $values) {
|
Chris@0
|
367 $languages = $this->container->get('language_manager')->getLanguages();
|
Chris@0
|
368 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
369 $this->drupalGet($path, ['language' => $languages[$langcode]]);
|
Chris@0
|
370 $this->assertText($values[$langcode]['title'][0]['value'], format_string('The %langcode node translation is correctly displayed.', ['%langcode' => $langcode]));
|
Chris@0
|
371 }
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 /**
|
Chris@0
|
375 * Tests that the given path provides the correct alternate hreflang links.
|
Chris@0
|
376 *
|
Chris@0
|
377 * @param \Drupal\Core\Url $url
|
Chris@0
|
378 * The path to be tested.
|
Chris@0
|
379 */
|
Chris@0
|
380 protected function doTestAlternateHreflangLinks(Url $url) {
|
Chris@0
|
381 $languages = $this->container->get('language_manager')->getLanguages();
|
Chris@0
|
382 $url->setAbsolute();
|
Chris@0
|
383 $urls = [];
|
Chris@0
|
384 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
385 $language_url = clone $url;
|
Chris@0
|
386 $urls[$langcode] = $language_url->setOption('language', $languages[$langcode]);
|
Chris@0
|
387 }
|
Chris@0
|
388 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
389 $this->drupalGet($urls[$langcode]);
|
Chris@0
|
390 foreach ($urls as $alternate_langcode => $language_url) {
|
Chris@0
|
391 // Retrieve desired link elements from the HTML head.
|
Chris@0
|
392 $links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
|
Chris@0
|
393 [':href' => $language_url->toString(), ':hreflang' => $alternate_langcode]);
|
Chris@0
|
394 $this->assert(isset($links[0]), format_string('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', ['%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString()]));
|
Chris@0
|
395 }
|
Chris@0
|
396 }
|
Chris@0
|
397 }
|
Chris@0
|
398
|
Chris@0
|
399 /**
|
Chris@0
|
400 * {@inheritdoc}
|
Chris@0
|
401 */
|
Chris@0
|
402 protected function getFormSubmitSuffix(EntityInterface $entity, $langcode) {
|
Chris@0
|
403 if (!$entity->isNew() && $entity->isTranslatable()) {
|
Chris@0
|
404 $translations = $entity->getTranslationLanguages();
|
Chris@0
|
405 if ((count($translations) > 1 || !isset($translations[$langcode])) && ($field = $entity->getFieldDefinition('status'))) {
|
Chris@0
|
406 return ' ' . ($field->isTranslatable() ? t('(this translation)') : t('(all translations)'));
|
Chris@0
|
407 }
|
Chris@0
|
408 }
|
Chris@0
|
409 return '';
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@0
|
412 /**
|
Chris@0
|
413 * Tests uninstalling content_translation.
|
Chris@0
|
414 */
|
Chris@0
|
415 protected function doUninstallTest() {
|
Chris@0
|
416 // Delete all the nodes so there is no data.
|
Chris@0
|
417 $nodes = Node::loadMultiple();
|
Chris@0
|
418 foreach ($nodes as $node) {
|
Chris@0
|
419 $node->delete();
|
Chris@0
|
420 }
|
Chris@0
|
421 $language_count = count(\Drupal::configFactory()->listAll('language.content_settings.'));
|
Chris@0
|
422 \Drupal::service('module_installer')->uninstall(['content_translation']);
|
Chris@0
|
423 $this->rebuildContainer();
|
Chris@0
|
424 $this->assertEqual($language_count, count(\Drupal::configFactory()->listAll('language.content_settings.')), 'Languages have been fixed rather than deleted during content_translation uninstall.');
|
Chris@0
|
425 }
|
Chris@0
|
426
|
Chris@0
|
427 /**
|
Chris@0
|
428 * {@inheritdoc}
|
Chris@0
|
429 */
|
Chris@0
|
430 protected function doTestTranslationEdit() {
|
Chris@0
|
431 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
432 ->getStorage($this->entityTypeId);
|
Chris@0
|
433 $storage->resetCache([$this->entityId]);
|
Chris@0
|
434 $entity = $storage->load($this->entityId);
|
Chris@0
|
435 $languages = $this->container->get('language_manager')->getLanguages();
|
Chris@0
|
436 $type_name = node_get_type_label($entity);
|
Chris@0
|
437
|
Chris@0
|
438 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
439 // We only want to test the title for non-english translations.
|
Chris@0
|
440 if ($langcode != 'en') {
|
Chris@0
|
441 $options = ['language' => $languages[$langcode]];
|
Chris@0
|
442 $url = $entity->urlInfo('edit-form', $options);
|
Chris@0
|
443 $this->drupalGet($url);
|
Chris@0
|
444
|
Chris@0
|
445 $title = t('<em>Edit @type</em> @title [%language translation]', [
|
Chris@0
|
446 '@type' => $type_name,
|
Chris@0
|
447 '@title' => $entity->getTranslation($langcode)->label(),
|
Chris@0
|
448 '%language' => $languages[$langcode]->getName(),
|
Chris@0
|
449 ]);
|
Chris@0
|
450 $this->assertRaw($title);
|
Chris@0
|
451 }
|
Chris@0
|
452 }
|
Chris@0
|
453 }
|
Chris@0
|
454
|
Chris@0
|
455 /**
|
Chris@0
|
456 * Tests that revision translations are rendered properly.
|
Chris@0
|
457 */
|
Chris@0
|
458 public function testRevisionTranslationRendering() {
|
Chris@0
|
459 $storage = \Drupal::entityTypeManager()->getStorage('node');
|
Chris@0
|
460
|
Chris@0
|
461 // Create a node.
|
Chris@0
|
462 $nid = $this->createEntity(['title' => 'First rev en title'], 'en');
|
Chris@0
|
463 $node = $storage->load($nid);
|
Chris@0
|
464 $original_revision_id = $node->getRevisionId();
|
Chris@0
|
465
|
Chris@0
|
466 // Add a French translation.
|
Chris@0
|
467 $translation = $node->addTranslation('fr');
|
Chris@0
|
468 $translation->title = 'First rev fr title';
|
Chris@0
|
469 $translation->setNewRevision(FALSE);
|
Chris@0
|
470 $translation->save();
|
Chris@0
|
471
|
Chris@0
|
472 // Create a new revision.
|
Chris@0
|
473 $node->title = 'Second rev en title';
|
Chris@0
|
474 $node->setNewRevision(TRUE);
|
Chris@0
|
475 $node->save();
|
Chris@0
|
476
|
Chris@0
|
477 // Get an English view of this revision.
|
Chris@0
|
478 $original_revision = $storage->loadRevision($original_revision_id);
|
Chris@0
|
479 $original_revision_url = $original_revision->toUrl('revision')->toString();
|
Chris@0
|
480
|
Chris@0
|
481 // Should be different from regular node URL.
|
Chris@0
|
482 $this->assertNotIdentical($original_revision_url, $original_revision->toUrl()->toString());
|
Chris@0
|
483 $this->drupalGet($original_revision_url);
|
Chris@0
|
484 $this->assertResponse(200);
|
Chris@0
|
485
|
Chris@0
|
486 // Contents should be in English, of correct revision.
|
Chris@0
|
487 $this->assertText('First rev en title');
|
Chris@0
|
488 $this->assertNoText('First rev fr title');
|
Chris@0
|
489
|
Chris@0
|
490 // Get a French view.
|
Chris@0
|
491 $url_fr = $original_revision->getTranslation('fr')->toUrl('revision')->toString();
|
Chris@0
|
492
|
Chris@0
|
493 // Should have different URL from English.
|
Chris@0
|
494 $this->assertNotIdentical($url_fr, $original_revision->toUrl()->toString());
|
Chris@0
|
495 $this->assertNotIdentical($url_fr, $original_revision_url);
|
Chris@0
|
496 $this->drupalGet($url_fr);
|
Chris@0
|
497 $this->assertResponse(200);
|
Chris@0
|
498
|
Chris@0
|
499 // Contents should be in French, of correct revision.
|
Chris@0
|
500 $this->assertText('First rev fr title');
|
Chris@0
|
501 $this->assertNoText('First rev en title');
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 }
|