3 namespace Drupal\Tests\content_translation\Functional;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Tests\node\Functional\NodeTestBase;
7 use Drupal\Tests\TestFileCreationTrait;
10 * Tests the content translation language that is set.
12 * @group content_translation
14 class ContentTranslationLanguageChangeTest extends NodeTestBase {
16 use TestFileCreationTrait {
17 getTestFiles as drupalGetTestFiles;
25 public static $modules = ['language', 'content_translation', 'content_translation_test', 'node', 'block', 'field_ui', 'image'];
30 protected function setUp() {
32 $langcodes = ['de', 'fr'];
33 foreach ($langcodes as $langcode) {
34 ConfigurableLanguage::createFromLangcode($langcode)->save();
36 $this->drupalPlaceBlock('local_tasks_block');
37 $user = $this->drupalCreateUser([
38 'administer site configuration',
40 'create article content',
41 'edit any article content',
42 'delete any article content',
43 'administer content translation',
44 'translate any entity',
45 'create content translations',
46 'administer languages',
47 'administer content types',
48 'administer node fields',
50 $this->drupalLogin($user);
52 // Enable translation for article.
54 'entity_types[node]' => TRUE,
55 'settings[node][article][translatable]' => TRUE,
56 'settings[node][article][settings][language][language_alterable]' => TRUE,
58 $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
60 // Add an image field.
61 $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
63 'new_storage_type' => 'image',
64 'field_name' => 'image_field',
65 'label' => 'image_field',
67 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
68 $this->drupalPostForm(NULL, [], t('Save field settings'));
69 $this->drupalPostForm(NULL, [], t('Save settings'));
73 * Test that the source language is properly set when changing.
75 public function testLanguageChange() {
76 // Create a node in English.
77 $this->drupalGet('node/add/article');
79 'title[0][value]' => 'english_title',
81 $this->drupalPostForm(NULL, $edit, t('Save'));
83 // Create a translation in French.
84 $this->clickLink('Translate');
85 $this->clickLink('Add');
86 $this->drupalPostForm(NULL, [], t('Save (this translation)'));
87 $this->clickLink('Translate');
89 // Edit English translation.
90 $this->clickLink('Edit');
91 // Upload and image after changing the node language.
92 $images = $this->drupalGetTestFiles('image')[1];
94 'langcode[0][value]' => 'de',
95 'files[field_image_field_0]' => $images->uri,
97 $this->drupalPostForm(NULL, $edit, t('Upload'));
98 $this->drupalPostForm(NULL, ['field_image_field[0][alt]' => 'alternative_text'], t('Save (this translation)'));
100 // Check that the translation languages are correct.
101 $node = $this->getNodeByTitle('english_title');
102 $translation_languages = array_keys($node->getTranslationLanguages());
103 $this->assertTrue(in_array('fr', $translation_languages));
104 $this->assertTrue(in_array('de', $translation_languages));
108 * Test that title does not change on ajax call with new language value.
110 public function testTitleDoesNotChangesOnChangingLanguageWidgetAndTriggeringAjaxCall() {
111 // Create a node in English.
112 $this->drupalGet('node/add/article', ['query' => ['test_field_only_en_fr' => 1]]);
114 'title[0][value]' => 'english_title',
115 'test_field_only_en_fr' => 'node created',
117 $this->drupalPostForm(NULL, $edit, t('Save'));
118 $this->assertEqual('node created', \Drupal::state()->get('test_field_only_en_fr'));
120 // Create a translation in French.
121 $this->clickLink('Translate');
122 $this->clickLink('Add');
123 $this->drupalPostForm(NULL, [], t('Save (this translation)'));
124 $this->clickLink('Translate');
126 // Edit English translation.
127 $node = $this->getNodeByTitle('english_title');
128 $this->drupalGet('node/' . $node->id() . '/edit');
129 // Test the expected title when loading the form.
130 $this->assertRaw('<title>Edit Article english_title | Drupal</title>');
131 // Upload and image after changing the node language.
132 $images = $this->drupalGetTestFiles('image')[1];
134 'langcode[0][value]' => 'de',
135 'files[field_image_field_0]' => $images->uri,
137 $this->drupalPostForm(NULL, $edit, t('Upload'));
138 // Test the expected title after triggering an ajax call with a new
139 // language selected.
140 $this->assertRaw('<title>Edit Article english_title | Drupal</title>');
142 'langcode[0][value]' => 'en',
143 'field_image_field[0][alt]' => 'alternative_text',
145 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
147 // Check that the translation languages are correct.
148 $node = $this->getNodeByTitle('english_title');
149 $translation_languages = array_keys($node->getTranslationLanguages());
150 $this->assertTrue(in_array('fr', $translation_languages));
151 $this->assertTrue(!in_array('de', $translation_languages));