Backup of db before drupal security update
[yaffs-website] / web / core / modules / content_translation / src / Tests / ContentTranslationLanguageChangeTest.php
1 <?php
2
3 namespace Drupal\content_translation\Tests;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\node\Tests\NodeTestBase;
7
8 /**
9  * Tests the content translation language that is set.
10  *
11  * @group content_translation
12  */
13 class ContentTranslationLanguageChangeTest extends NodeTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['language', 'content_translation', 'content_translation_test', 'node', 'block', 'field_ui', 'image'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27     $langcodes = ['de', 'fr'];
28     foreach ($langcodes as $langcode) {
29       ConfigurableLanguage::createFromLangcode($langcode)->save();
30     }
31     $this->drupalPlaceBlock('local_tasks_block');
32     $user = $this->drupalCreateUser([
33       'administer site configuration',
34       'administer nodes',
35       'create article content',
36       'edit any article content',
37       'delete any article content',
38       'administer content translation',
39       'translate any entity',
40       'create content translations',
41       'administer languages',
42       'administer content types',
43       'administer node fields',
44     ]);
45     $this->drupalLogin($user);
46
47     // Enable translation for article.
48     $edit = [
49       'entity_types[node]' => TRUE,
50       'settings[node][article][translatable]' => TRUE,
51       'settings[node][article][settings][language][language_alterable]' => TRUE,
52     ];
53     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
54
55     // Add an image field.
56     $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
57     $edit = [
58       'new_storage_type' => 'image',
59       'field_name' => 'image_field',
60       'label' => 'image_field',
61     ];
62     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
63     $this->drupalPostForm(NULL, [], t('Save field settings'));
64     $this->drupalPostForm(NULL, [], t('Save settings'));
65   }
66
67   /**
68    * Test that the source language is properly set when changing.
69    */
70   public function testLanguageChange() {
71     // Create a node in English.
72     $this->drupalGet('node/add/article');
73     $edit = [
74       'title[0][value]' => 'english_title',
75     ];
76     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
77
78     // Create a translation in French.
79     $this->clickLink('Translate');
80     $this->clickLink('Add');
81     $this->drupalPostForm(NULL, [], t('Save and keep published (this translation)'));
82     $this->clickLink('Translate');
83
84     // Edit English translation.
85     $this->clickLink('Edit');
86     // Upload and image after changing the node language.
87     $images = $this->drupalGetTestFiles('image')[1];
88     $edit = [
89       'langcode[0][value]' => 'de',
90       'files[field_image_field_0]' => $images->uri,
91     ];
92     $this->drupalPostForm(NULL, $edit, t('Upload'));
93     $this->drupalPostForm(NULL, ['field_image_field[0][alt]' => 'alternative_text'], t('Save and keep published (this translation)'));
94
95     // Check that the translation languages are correct.
96     $node = $this->getNodeByTitle('english_title');
97     $translation_languages = array_keys($node->getTranslationLanguages());
98     $this->assertTrue(in_array('fr', $translation_languages));
99     $this->assertTrue(in_array('de', $translation_languages));
100   }
101
102   /**
103    * Test that title does not change on ajax call with new language value.
104    */
105   public function testTitleDoesNotChangesOnChangingLanguageWidgetAndTriggeringAjaxCall() {
106     // Create a node in English.
107     $this->drupalGet('node/add/article', ['query' => ['test_field_only_en_fr' => 1]]);
108     $edit = [
109       'title[0][value]' => 'english_title',
110       'test_field_only_en_fr' => 'node created',
111     ];
112     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
113     $this->assertEqual('node created', \Drupal::state()->get('test_field_only_en_fr'));
114
115     // Create a translation in French.
116     $this->clickLink('Translate');
117     $this->clickLink('Add');
118     $this->drupalPostForm(NULL, [], t('Save and keep published (this translation)'));
119     $this->clickLink('Translate');
120
121     // Edit English translation.
122     $node = $this->getNodeByTitle('english_title');
123     $this->drupalGet('node/' . $node->id() . '/edit');
124     // Test the expected title when loading the form.
125     $this->assertRaw('<title>Edit Article english_title | Drupal</title>');
126     // Upload and image after changing the node language.
127     $images = $this->drupalGetTestFiles('image')[1];
128     $edit = [
129       'langcode[0][value]' => 'de',
130       'files[field_image_field_0]' => $images->uri,
131     ];
132     $this->drupalPostForm(NULL, $edit, t('Upload'));
133     // Test the expected title after triggering an ajax call with a new
134     // language selected.
135     $this->assertRaw('<title>Edit Article english_title | Drupal</title>');
136     $edit = [
137       'langcode[0][value]' => 'en',
138       'field_image_field[0][alt]' => 'alternative_text'
139     ];
140     $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
141
142     // Check that the translation languages are correct.
143     $node = $this->getNodeByTitle('english_title');
144     $translation_languages = array_keys($node->getTranslationLanguages());
145     $this->assertTrue(in_array('fr', $translation_languages));
146     $this->assertTrue(!in_array('de', $translation_languages));
147   }
148
149 }