1bf836aaca8769fac179c05ee79da586901f0bc3
[yaffs-website] / web / core / modules / content_translation / tests / src / Functional / ContentTranslationLanguageChangeTest.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Functional;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Tests\node\Functional\NodeTestBase;
7 use Drupal\Tests\TestFileCreationTrait;
8
9 /**
10  * Tests the content translation language that is set.
11  *
12  * @group content_translation
13  */
14 class ContentTranslationLanguageChangeTest extends NodeTestBase {
15
16   use TestFileCreationTrait {
17     getTestFiles as drupalGetTestFiles;
18   }
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['language', 'content_translation', 'content_translation_test', 'node', 'block', 'field_ui', 'image'];
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32     $langcodes = ['de', 'fr'];
33     foreach ($langcodes as $langcode) {
34       ConfigurableLanguage::createFromLangcode($langcode)->save();
35     }
36     $this->drupalPlaceBlock('local_tasks_block');
37     $user = $this->drupalCreateUser([
38       'administer site configuration',
39       'administer nodes',
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',
49     ]);
50     $this->drupalLogin($user);
51
52     // Enable translation for article.
53     $edit = [
54       'entity_types[node]' => TRUE,
55       'settings[node][article][translatable]' => TRUE,
56       'settings[node][article][settings][language][language_alterable]' => TRUE,
57     ];
58     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
59
60     // Add an image field.
61     $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
62     $edit = [
63       'new_storage_type' => 'image',
64       'field_name' => 'image_field',
65       'label' => 'image_field',
66     ];
67     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
68     $this->drupalPostForm(NULL, [], t('Save field settings'));
69     $this->drupalPostForm(NULL, [], t('Save settings'));
70   }
71
72   /**
73    * Test that the source language is properly set when changing.
74    */
75   public function testLanguageChange() {
76     // Create a node in English.
77     $this->drupalGet('node/add/article');
78     $edit = [
79       'title[0][value]' => 'english_title',
80     ];
81     $this->drupalPostForm(NULL, $edit, t('Save'));
82
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');
88
89     // Edit English translation.
90     $this->clickLink('Edit');
91     // Upload and image after changing the node language.
92     $images = $this->drupalGetTestFiles('image')[1];
93     $edit = [
94       'langcode[0][value]' => 'de',
95       'files[field_image_field_0]' => $images->uri,
96     ];
97     $this->drupalPostForm(NULL, $edit, t('Upload'));
98     $this->drupalPostForm(NULL, ['field_image_field[0][alt]' => 'alternative_text'], t('Save (this translation)'));
99
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));
105   }
106
107   /**
108    * Test that title does not change on ajax call with new language value.
109    */
110   public function testTitleDoesNotChangesOnChangingLanguageWidgetAndTriggeringAjaxCall() {
111     // Create a node in English.
112     $this->drupalGet('node/add/article', ['query' => ['test_field_only_en_fr' => 1]]);
113     $edit = [
114       'title[0][value]' => 'english_title',
115       'test_field_only_en_fr' => 'node created',
116     ];
117     $this->drupalPostForm(NULL, $edit, t('Save'));
118     $this->assertEqual('node created', \Drupal::state()->get('test_field_only_en_fr'));
119
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');
125
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];
133     $edit = [
134       'langcode[0][value]' => 'de',
135       'files[field_image_field_0]' => $images->uri,
136     ];
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>');
141     $edit = [
142       'langcode[0][value]' => 'en',
143       'field_image_field[0][alt]' => 'alternative_text',
144     ];
145     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
146
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));
152   }
153
154 }