3 namespace Drupal\locale\Tests;
5 use Drupal\locale\Locale;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
10 * Tests translation update's effects on configuration translations.
14 class LocaleConfigTranslationImportTest extends WebTestBase {
21 public static $modules = ['language', 'locale_test_translate'];
24 * Test update changes configuration translations if enabled after language.
26 public function testConfigTranslationImport() {
27 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions']);
28 $this->drupalLogin($admin_user);
30 // Add a language. The Afrikaans translation file of locale_test_translate
31 // (test.af.po) has been prepared with a configuration translation.
32 ConfigurableLanguage::createFromLangcode('af')->save();
34 // Enable locale module.
35 $this->container->get('module_installer')->install(['locale']);
38 // Enable import of translations. By default this is disabled for automated
40 $this->config('locale.settings')
41 ->set('translation.import_enabled', TRUE)
44 // Add translation permissions now that the locale module has been enabled.
46 'authenticated[translate interface]' => 'translate interface',
48 $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
50 // Check and update the translation status. This will import the Afrikaans
51 // translations of locale_test_translate module.
52 $this->drupalGet('admin/reports/translations/check');
54 // Override the Drupal core translation status to be up to date.
55 // Drupal core should not be a subject in this test.
56 $status = locale_translation_get_status();
57 $status['drupal']['af']->type = 'current';
58 \Drupal::state()->set('locale.translation_status', $status);
60 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
62 // Check if configuration translations have been imported.
63 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'system.maintenance');
64 $this->assertEqual($override->get('message'), 'Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.');
68 * Test update changes configuration translations if enabled after language.
70 public function testConfigTranslationModuleInstall() {
72 // Enable locale, block and config_translation modules.
73 $this->container->get('module_installer')->install(['block', 'config_translation']);
76 // The testing profile overrides locale.settings to disable translation
77 // import. Test that this override is in place.
78 $this->assertFalse($this->config('locale.settings')->get('translation.import_enabled'), 'Translations imports are disabled by default in the Testing profile.');
80 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate configuration']);
81 $this->drupalLogin($admin_user);
83 // Enable import of translations. By default this is disabled for automated
85 $this->config('locale.settings')
86 ->set('translation.import_enabled', TRUE)
89 // Add predefined language.
90 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
92 // Add the system branding block to the page.
93 $this->drupalPlaceBlock('system_branding_block', ['region' => 'header', 'id' => 'site-branding']);
94 $this->drupalPostForm('admin/config/system/site-information', ['site_slogan' => 'Test site slogan'], 'Save configuration');
95 $this->drupalPostForm('admin/config/system/site-information/translate/af/edit', ['translation[config_names][system.site][slogan]' => 'Test site slogan in Afrikaans'], 'Save translation');
97 // Get the front page and ensure that the translated configuration appears.
98 $this->drupalGet('af');
99 $this->assertText('Test site slogan in Afrikaans');
101 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
102 $this->assertEqual('Locale can translate Afrikaans', $override->get('translatable_default_with_translation'));
104 // Update test configuration.
106 ->set('translatable_no_default', 'This translation is preserved')
107 ->set('translatable_default_with_translation', 'This translation is preserved')
108 ->set('translatable_default_with_no_translation', 'This translation is preserved')
111 // Install any module.
112 $this->drupalPostForm('admin/modules', ['modules[dblog][enable]' => 'dblog'], t('Install'));
113 $this->assertText('Module Database Logging has been enabled.');
115 // Get the front page and ensure that the translated configuration still
117 $this->drupalGet('af');
118 $this->assertText('Test site slogan in Afrikaans');
120 $this->rebuildContainer();
121 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
123 'translatable_no_default' => 'This translation is preserved',
124 'translatable_default_with_translation' => 'This translation is preserved',
125 'translatable_default_with_no_translation' => 'This translation is preserved'
127 $this->assertEqual($expected, $override->get());
131 * Test removing a string from Locale deletes configuration translations.
133 public function testLocaleRemovalAndConfigOverrideDelete() {
134 // Enable the locale module.
135 $this->container->get('module_installer')->install(['locale']);
138 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
139 $this->drupalLogin($admin_user);
141 // Enable import of translations. By default this is disabled for automated
143 $this->config('locale.settings')
144 ->set('translation.import_enabled', TRUE)
147 // Add predefined language.
148 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
150 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
151 $this->assertEqual(['translatable_default_with_translation' => 'Locale can translate Afrikaans'], $override->get());
153 // Remove the string from translation to simulate a Locale removal. Note
154 // that is no current way of doing this in the UI.
155 $locale_storage = \Drupal::service('locale.storage');
156 $string = $locale_storage->findString(['source' => 'Locale can translate']);
157 \Drupal::service('locale.storage')->delete($string);
158 // Force a rebuild of config translations.
159 $count = Locale::config()->updateConfigTranslations(['locale_test_translate.settings'], ['af']);
160 $this->assertEqual($count, 1, 'Correct count of updated translations');
162 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
163 $this->assertEqual([], $override->get());
164 $this->assertTrue($override->isNew(), 'The configuration override was deleted when the Locale string was deleted.');
168 * Test removing a string from Locale changes configuration translations.
170 public function testLocaleRemovalAndConfigOverridePreserve() {
171 // Enable the locale module.
172 $this->container->get('module_installer')->install(['locale']);
175 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
176 $this->drupalLogin($admin_user);
178 // Enable import of translations. By default this is disabled for automated
180 $this->config('locale.settings')
181 ->set('translation.import_enabled', TRUE)
184 // Add predefined language.
185 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
187 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
188 // Update test configuration.
190 ->set('translatable_no_default', 'This translation is preserved')
191 ->set('translatable_default_with_no_translation', 'This translation is preserved')
194 'translatable_default_with_translation' => 'Locale can translate Afrikaans',
195 'translatable_no_default' => 'This translation is preserved',
196 'translatable_default_with_no_translation' => 'This translation is preserved'
198 $this->assertEqual($expected, $override->get());
200 // Set the translated string to empty.
202 'string' => 'Locale can translate',
204 'translation' => 'all',
206 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
207 $textareas = $this->xpath('//textarea');
208 $textarea = current($textareas);
209 $lid = (string) $textarea[0]['name'];
213 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
215 $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
217 'translatable_no_default' => 'This translation is preserved',
218 'translatable_default_with_no_translation' => 'This translation is preserved'
220 $this->assertEqual($expected, $override->get());