3 namespace Drupal\locale\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\Core\Language\LanguageInterface;
9 * Tests translation of configuration strings.
13 class LocaleConfigTranslationTest extends WebTestBase {
16 * The language code used.
27 public static $modules = ['locale', 'contact', 'contact_test'];
32 protected function setUp() {
34 // Add a default locale storage for all these tests.
35 $this->storage = $this->container->get('locale.storage');
37 // Enable import of translations. By default this is disabled for automated
39 $this->config('locale.settings')
40 ->set('translation.import_enabled', TRUE)
43 // Add custom language.
44 $this->langcode = 'xx';
45 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface', 'administer modules', 'access site-wide contact form', 'administer contact forms', 'administer site configuration']);
46 $this->drupalLogin($admin_user);
47 $name = $this->randomMachineName(16);
49 'predefined_langcode' => 'custom',
50 'langcode' => $this->langcode,
52 'direction' => LanguageInterface::DIRECTION_LTR,
54 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
56 $edit = ["prefix[$this->langcode]" => $this->langcode];
57 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
61 * Tests basic configuration translation.
63 public function testConfigTranslation() {
64 // Check that the maintenance message exists and create translation for it.
65 $source = '@site is currently under maintenance. We should be back shortly. Thank you for your patience.';
66 $string = $this->storage->findString(['source' => $source, 'context' => '', 'type' => 'configuration']);
67 $this->assertTrue($string, 'Configuration strings have been created upon installation.');
69 // Translate using the UI so configuration is refreshed.
70 $message = $this->randomMachineName(20);
72 'string' => $string->source,
73 'langcode' => $this->langcode,
74 'translation' => 'all',
76 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
77 $textareas = $this->xpath('//textarea');
78 $textarea = current($textareas);
79 $lid = (string) $textarea[0]['name'];
83 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
85 // Get translation and check we've only got the message.
86 $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'system.maintenance')->get();
87 $this->assertEqual(count($translation), 1, 'Got the right number of properties after translation.');
88 $this->assertEqual($translation['message'], $message);
90 // Check default medium date format exists and create a translation for it.
91 $string = $this->storage->findString(['source' => 'D, m/d/Y - H:i', 'context' => 'PHP date format', 'type' => 'configuration']);
92 $this->assertTrue($string, 'Configuration date formats have been created upon installation.');
94 // Translate using the UI so configuration is refreshed.
96 'string' => $string->source,
97 'langcode' => $this->langcode,
98 'translation' => 'all',
100 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
101 $textareas = $this->xpath('//textarea');
102 $textarea = current($textareas);
103 $lid = (string) $textarea[0]['name'];
107 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
109 $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'core.date_format.medium')->get();
110 $this->assertEqual($translation['pattern'], 'D', 'Got the right date format pattern after translation.');
112 // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
114 $formatted_date = format_date(494015820, $type = 'medium', NULL, 'America/New_York', $this->langcode);
115 $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
117 // Assert strings from image module config are not available.
118 $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
119 $this->assertFalse($string, 'Configuration strings have been created upon installation.');
121 // Enable the image module.
122 $this->drupalPostForm('admin/modules', ['modules[image][enable]' => "1"], t('Install'));
123 $this->rebuildContainer();
125 $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
126 $this->assertTrue($string, 'Configuration strings have been created upon installation.');
127 $locations = $string->getLocations();
128 $this->assertTrue(isset($locations['configuration']) && isset($locations['configuration']['image.style.medium']), 'Configuration string has been created with the right location');
130 // Check the string is unique and has no translation yet.
131 $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
132 $this->assertEqual(count($translations), 1);
133 $translation = reset($translations);
134 $this->assertEqual($translation->source, $string->source);
135 $this->assertTrue(empty($translation->translation));
137 // Translate using the UI so configuration is refreshed.
138 $image_style_label = $this->randomMachineName(20);
140 'string' => $string->source,
141 'langcode' => $this->langcode,
142 'translation' => 'all',
144 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
145 $textarea = current($this->xpath('//textarea'));
146 $lid = (string) $textarea[0]['name'];
148 $lid => $image_style_label,
150 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
152 // Check the right single translation has been created.
153 $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
154 $translation = reset($translations);
155 $this->assertTrue(count($translations) == 1 && $translation->source == $string->source && $translation->translation == $image_style_label, 'Got only one translation for image configuration.');
157 // Try more complex configuration data.
158 $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'image.style.medium')->get();
159 $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation');
161 // Uninstall the module.
162 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[image]' => "image"], t('Uninstall'));
163 $this->drupalPostForm(NULL, [], t('Uninstall'));
165 // Ensure that the translated configuration has been removed.
166 $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
167 $this->assertTrue($override->isNew(), 'Translated configuration for image module removed.');
169 // Translate default category using the UI so configuration is refreshed.
170 $category_label = $this->randomMachineName(20);
172 'string' => 'Website feedback',
173 'langcode' => $this->langcode,
174 'translation' => 'all',
176 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
177 $textarea = current($this->xpath('//textarea'));
178 $lid = (string) $textarea[0]['name'];
180 $lid => $category_label,
182 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
184 // Check if this category displayed in this language will use the
185 // translation. This test ensures the entity loaded from the request
186 // upcasting will already work.
187 $this->drupalGet($this->langcode . '/contact/feedback');
188 $this->assertText($category_label);
190 // Check if the UI does not show the translated String.
191 $this->drupalGet('admin/structure/contact/manage/feedback');
192 $this->assertFieldById('edit-label', 'Website feedback', 'Translation is not loaded for Edit Form.');
196 * Test translatability of optional configuration in locale.
198 public function testOptionalConfiguration() {
199 $this->assertNodeConfig(FALSE, FALSE);
200 // Enable the node module.
201 $this->drupalPostForm('admin/modules', ['modules[node][enable]' => "1"], t('Install'));
202 $this->drupalPostForm(NULL, [], t('Continue'));
203 $this->rebuildContainer();
204 $this->assertNodeConfig(TRUE, FALSE);
205 // Enable the views module (which node provides some optional config for).
206 $this->drupalPostForm('admin/modules', ['modules[views][enable]' => "1"], t('Install'));
207 $this->rebuildContainer();
208 $this->assertNodeConfig(TRUE, TRUE);
212 * Check that node configuration source strings are made available in locale.
214 * @param bool $required
215 * Whether to assume a sample of the required default configuration is
217 * @param bool $optional
218 * Whether to assume a sample of the optional default configuration is
221 protected function assertNodeConfig($required, $optional) {
222 // Check the required default configuration in node module.
223 $string = $this->storage->findString(['source' => 'Make content sticky', 'context' => '', 'type' => 'configuration']);
225 $this->assertFalse($this->config('system.action.node_make_sticky_action')->isNew());
226 $this->assertTrue($string, 'Node action text can be found with node module.');
229 $this->assertTrue($this->config('system.action.node_make_sticky_action')->isNew());
230 $this->assertFalse($string, 'Node action text can not be found without node module.');
233 // Check the optional default configuration in node module.
234 $string = $this->storage->findString(['source' => 'No front page content has been created yet.', 'context' => '', 'type' => 'configuration']);
236 $this->assertFalse($this->config('views.view.frontpage')->isNew());
237 $this->assertTrue($string, 'Node view text can be found with node and views modules.');
240 $this->assertTrue($this->config('views.view.frontpage')->isNew());
241 $this->assertFalse($string, 'Node view text can not be found without node and/or views modules.');