3 namespace Drupal\Tests\locale\Functional;
5 use Drupal\locale\Locale;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
10 * Tests translation update's effects on configuration translations.
14 class LocaleConfigTranslationImportTest extends BrowserTestBase {
21 public static $modules = ['language', 'locale_test_translate'];
26 protected function setUp() {
29 // @todo Re-enable the test and only skip it when the translation cannot be
30 // downloaded, or provide a translation as a fixture instead. See
31 // https://www.drupal.org/project/drupal/issues/2828143.
32 $this->markTestSkipped();
36 * Test update changes configuration translations if enabled after language.
38 public function testConfigTranslationImport() {
39 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions']);
40 $this->drupalLogin($admin_user);
42 // Add a language. The Afrikaans translation file of locale_test_translate
43 // (test.af.po) has been prepared with a configuration translation.
44 ConfigurableLanguage::createFromLangcode('af')->save();
46 // Enable locale module.
47 $this->container->get('module_installer')->install(['locale']);
50 // Enable import of translations. By default this is disabled for automated
52 $this->config('locale.settings')
53 ->set('translation.import_enabled', TRUE)
56 // Add translation permissions now that the locale module has been enabled.
58 'authenticated[translate interface]' => 'translate interface',
60 $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
62 // Check and update the translation status. This will import the Afrikaans
63 // translations of locale_test_translate module.
64 $this->drupalGet('admin/reports/translations/check');
66 // Override the Drupal core translation status to be up to date.
67 // Drupal core should not be a subject in this test.
68 $status = locale_translation_get_status();
69 $status['drupal']['af']->type = 'current';
70 \Drupal::state()->set('locale.translation_status', $status);
72 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
74 // Check if configuration translations have been imported.
75 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'system.maintenance');
76 $this->assertEqual($override->get('message'), 'Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.');
80 * Test update changes configuration translations if enabled after language.
82 public function testConfigTranslationModuleInstall() {
84 // Enable locale, block and config_translation modules.
85 $this->container->get('module_installer')->install(['block', 'config_translation']);
88 // The testing profile overrides locale.settings to disable translation
89 // import. Test that this override is in place.
90 $this->assertFalse($this->config('locale.settings')->get('translation.import_enabled'), 'Translations imports are disabled by default in the Testing profile.');
92 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate configuration']);
93 $this->drupalLogin($admin_user);
95 // Enable import of translations. By default this is disabled for automated
97 $this->config('locale.settings')
98 ->set('translation.import_enabled', TRUE)
101 // Add predefined language.
102 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
104 // Add the system branding block to the page.
105 $this->drupalPlaceBlock('system_branding_block', ['region' => 'header', 'id' => 'site-branding']);
106 $this->drupalPostForm('admin/config/system/site-information', ['site_slogan' => 'Test site slogan'], 'Save configuration');
107 $this->drupalPostForm('admin/config/system/site-information/translate/af/edit', ['translation[config_names][system.site][slogan]' => 'Test site slogan in Afrikaans'], 'Save translation');
109 // Get the front page and ensure that the translated configuration appears.
110 $this->drupalGet('af');
111 $this->assertText('Test site slogan in Afrikaans');
113 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
114 $this->assertEqual('Locale can translate Afrikaans', $override->get('translatable_default_with_translation'));
116 // Update test configuration.
118 ->set('translatable_no_default', 'This translation is preserved')
119 ->set('translatable_default_with_translation', 'This translation is preserved')
120 ->set('translatable_default_with_no_translation', 'This translation is preserved')
123 // Install any module.
124 $this->drupalPostForm('admin/modules', ['modules[dblog][enable]' => 'dblog'], t('Install'));
125 $this->assertText('Module Database Logging has been enabled.');
127 // Get the front page and ensure that the translated configuration still
129 $this->drupalGet('af');
130 $this->assertText('Test site slogan in Afrikaans');
132 $this->rebuildContainer();
133 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
135 'translatable_no_default' => 'This translation is preserved',
136 'translatable_default_with_translation' => 'This translation is preserved',
137 'translatable_default_with_no_translation' => 'This translation is preserved'
139 $this->assertEqual($expected, $override->get());
143 * Test removing a string from Locale deletes configuration translations.
145 public function testLocaleRemovalAndConfigOverrideDelete() {
146 // Enable the locale module.
147 $this->container->get('module_installer')->install(['locale']);
150 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
151 $this->drupalLogin($admin_user);
153 // Enable import of translations. By default this is disabled for automated
155 $this->config('locale.settings')
156 ->set('translation.import_enabled', TRUE)
159 // Add predefined language.
160 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
162 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
163 $this->assertEqual(['translatable_default_with_translation' => 'Locale can translate Afrikaans'], $override->get());
165 // Remove the string from translation to simulate a Locale removal. Note
166 // that is no current way of doing this in the UI.
167 $locale_storage = \Drupal::service('locale.storage');
168 $string = $locale_storage->findString(['source' => 'Locale can translate']);
169 \Drupal::service('locale.storage')->delete($string);
170 // Force a rebuild of config translations.
171 $count = Locale::config()->updateConfigTranslations(['locale_test_translate.settings'], ['af']);
172 $this->assertEqual($count, 1, 'Correct count of updated translations');
174 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
175 $this->assertEqual([], $override->get());
176 $this->assertTrue($override->isNew(), 'The configuration override was deleted when the Locale string was deleted.');
180 * Test removing a string from Locale changes configuration translations.
182 public function testLocaleRemovalAndConfigOverridePreserve() {
183 // Enable the locale module.
184 $this->container->get('module_installer')->install(['locale']);
187 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
188 $this->drupalLogin($admin_user);
190 // Enable import of translations. By default this is disabled for automated
192 $this->config('locale.settings')
193 ->set('translation.import_enabled', TRUE)
196 // Add predefined language.
197 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
199 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
200 // Update test configuration.
202 ->set('translatable_no_default', 'This translation is preserved')
203 ->set('translatable_default_with_no_translation', 'This translation is preserved')
206 'translatable_default_with_translation' => 'Locale can translate Afrikaans',
207 'translatable_no_default' => 'This translation is preserved',
208 'translatable_default_with_no_translation' => 'This translation is preserved'
210 $this->assertEqual($expected, $override->get());
212 // Set the translated string to empty.
214 'string' => 'Locale can translate',
216 'translation' => 'all',
218 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
219 $textareas = $this->xpath('//textarea');
220 $textarea = current($textareas);
221 $lid = $textarea->getAttribute('name');
225 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
227 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
229 'translatable_no_default' => 'This translation is preserved',
230 'translatable_default_with_no_translation' => 'This translation is preserved'
232 $this->assertEqual($expected, $override->get());