3 namespace Drupal\Tests\locale\Kernel;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\locale\Locale;
7 use Drupal\locale\StringInterface;
8 use Drupal\locale\TranslationString;
9 use Drupal\KernelTests\KernelTestBase;
12 * Tests that shipped configuration translations are updated correctly.
16 class LocaleConfigSubscriberTest extends KernelTestBase {
21 public static $modules = ['language', 'locale', 'system', 'locale_test'];
24 * The configurable language manager used in this test.
26 * @var \Drupal\language\ConfigurableLanguageManagerInterface
28 protected $languageManager;
31 * The configuration factory used in this test.
33 * @var \Drupal\Core\Config\ConfigFactoryInterface
35 protected $configFactory;
38 * The string storage used in this test.
40 * @var \Drupal\locale\StringStorageInterface
42 protected $stringStorage;
45 * The locale configuration manager used in this test.
47 * @var \Drupal\locale\LocaleConfigManager
49 protected $localeConfigManager;
54 protected function setUp() {
57 $this->setUpDefaultLanguage();
59 $this->installSchema('locale', ['locales_source', 'locales_target', 'locales_location']);
61 $this->setupLanguages();
63 $this->installConfig(['locale_test']);
64 // Simulate this hook invoked which would happen if in a non-kernel test
65 // or normal environment.
66 // @see locale_modules_installed()
67 // @see locale_system_update()
68 locale_system_set_config_langcodes();
69 $langcodes = array_keys(\Drupal::languageManager()->getLanguages());
70 $names = Locale::config()->getComponentNames();
71 Locale::config()->updateConfigTranslations($names, $langcodes);
73 $this->configFactory = $this->container->get('config.factory');
74 $this->stringStorage = $this->container->get('locale.storage');
75 $this->localeConfigManager = $this->container->get('locale.config_manager');
76 $this->languageManager = $this->container->get('language_manager');
82 * Sets up default language for this test.
84 protected function setUpDefaultLanguage() {
85 // Keep the default English.
89 * Sets up languages needed for this test.
91 protected function setUpLanguages() {
92 ConfigurableLanguage::createFromLangcode('de')->save();
96 * Sets up the locale storage strings to be in line with configuration.
98 protected function setUpLocale() {
99 // Set up the locale database the same way we have in the config samples.
100 $this->setUpNoTranslation('locale_test.no_translation', 'test', 'Test', 'de');
101 $this->setUpTranslation('locale_test.translation', 'test', 'English test', 'German test', 'de');
105 * Tests creating translations of shipped configuration.
107 public function testCreateTranslation() {
108 $config_name = 'locale_test.no_translation';
110 $this->saveLanguageOverride($config_name, 'test', 'Test (German)', 'de');
111 $this->assertTranslation($config_name, 'Test (German)', 'de');
115 * Tests importing community translations of shipped configuration.
117 public function testLocaleCreateTranslation() {
118 $config_name = 'locale_test.no_translation';
120 $this->saveLocaleTranslationData($config_name, 'test', 'Test', 'Test (German)', 'de');
121 $this->assertTranslation($config_name, 'Test (German)', 'de', FALSE);
125 * Tests updating translations of shipped configuration.
127 public function testUpdateTranslation() {
128 $config_name = 'locale_test.translation';
130 $this->saveLanguageOverride($config_name, 'test', 'Updated German test', 'de');
131 $this->assertTranslation($config_name, 'Updated German test', 'de');
135 * Tests updating community translations of shipped configuration.
137 public function testLocaleUpdateTranslation() {
138 $config_name = 'locale_test.translation';
140 $this->saveLocaleTranslationData($config_name, 'test', 'English test', 'Updated German test', 'de');
141 $this->assertTranslation($config_name, 'Updated German test', 'de', FALSE);
145 * Tests deleting translations of shipped configuration.
147 public function testDeleteTranslation() {
148 $config_name = 'locale_test.translation';
150 $this->deleteLanguageOverride($config_name, 'test', 'English test', 'de');
151 // Instead of deleting the translation, we need to keep a translation with
152 // the source value and mark it as customized to prevent the deletion being
153 // reverted by importing community translations.
154 $this->assertTranslation($config_name, 'English test', 'de');
158 * Tests deleting community translations of shipped configuration.
160 public function testLocaleDeleteTranslation() {
161 $config_name = 'locale_test.translation';
163 $this->deleteLocaleTranslationData($config_name, 'test', 'English test', 'de');
164 $this->assertNoTranslation($config_name, 'de');
168 * Sets up a configuration string without a translation.
170 * The actual configuration is already available by installing locale_test
171 * module, as it is done in LocaleConfigSubscriberTest::setUp(). This sets up
172 * the necessary source string and verifies that everything is as expected to
173 * avoid false positives.
175 * @param string $config_name
176 * The configuration name.
178 * The configuration key.
179 * @param string $source
181 * @param string $langcode
184 protected function setUpNoTranslation($config_name, $key, $source, $langcode) {
185 $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
186 $this->assertNoConfigOverride($config_name, $key, $source, $langcode);
187 $this->assertNoTranslation($config_name, $langcode);
191 * Sets up a configuration string with a translation.
193 * The actual configuration is already available by installing locale_test
194 * module, as it is done in LocaleConfigSubscriberTest::setUp(). This sets up
195 * the necessary source and translation strings and verifies that everything
196 * is as expected to avoid false positives.
198 * @param string $config_name
199 * The configuration name.
201 * The configuration key.
202 * @param string $source
204 * @param string $translation
205 * The translation string.
206 * @param string $langcode
208 * @param bool $is_active
209 * Whether the update will affect the active configuration.
211 protected function setUpTranslation($config_name, $key, $source, $translation, $langcode, $is_active = FALSE) {
212 // Create source and translation strings for the configuration value and add
213 // the configuration name as a location. This would be performed by
214 // locale_translate_batch_import() invoking
215 // LocaleConfigManager::updateConfigTranslations() normally.
216 $this->localeConfigManager->reset();
217 $this->localeConfigManager
218 ->getStringTranslation($config_name, $langcode, $source, '')
219 ->setString($translation)
220 ->setCustomized(FALSE)
222 $this->configFactory->reset($config_name);
223 $this->localeConfigManager->reset();
224 $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
227 $this->assertActiveConfig($config_name, $key, $translation, $langcode);
230 $this->assertConfigOverride($config_name, $key, $translation, $langcode);
232 $this->assertTranslation($config_name, $translation, $langcode, FALSE);
236 * Saves a language override.
238 * This will invoke LocaleConfigSubscriber through the event dispatcher. To
239 * make sure the configuration was persisted correctly, the configuration
240 * value is checked. Because LocaleConfigSubscriber temporarily disables the
241 * override state of the configuration factory we check that the correct value
242 * is restored afterwards.
244 * @param string $config_name
245 * The configuration name.
247 * The configuration key.
248 * @param string $value
249 * The configuration value to save.
250 * @param string $langcode
253 protected function saveLanguageOverride($config_name, $key, $value, $langcode) {
254 $translation_override = $this->languageManager
255 ->getLanguageConfigOverride($langcode, $config_name);
256 $translation_override
259 $this->configFactory->reset($config_name);
261 $this->assertConfigOverride($config_name, $key, $value, $langcode);
265 * Saves translation data from locale module.
267 * This will invoke LocaleConfigSubscriber through the event dispatcher. To
268 * make sure the configuration was persisted correctly, the configuration
269 * value is checked. Because LocaleConfigSubscriber temporarily disables the
270 * override state of the configuration factory we check that the correct value
271 * is restored afterwards.
273 * @param string $config_name
274 * The configuration name.
276 * The configuration key.
277 * @param string $source
279 * @param string $translation
280 * The translation string to save.
281 * @param string $langcode
283 * @param bool $is_active
284 * Whether the update will affect the active configuration.
286 protected function saveLocaleTranslationData($config_name, $key, $source, $translation, $langcode, $is_active = FALSE) {
287 $this->localeConfigManager->reset();
288 $this->localeConfigManager
289 ->getStringTranslation($config_name, $langcode, $source, '')
290 ->setString($translation)
292 $this->localeConfigManager->reset();
293 $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
294 $this->configFactory->reset($config_name);
297 $this->assertActiveConfig($config_name, $key, $translation, $langcode);
300 $this->assertConfigOverride($config_name, $key, $translation, $langcode);
305 * Deletes a language override.
307 * This will invoke LocaleConfigSubscriber through the event dispatcher. To
308 * make sure the configuration was persisted correctly, the configuration
309 * value is checked. Because LocaleConfigSubscriber temporarily disables the
310 * override state of the configuration factory we check that the correct value
311 * is restored afterwards.
313 * @param string $config_name
314 * The configuration name.
316 * The configuration key.
317 * @param string $source_value
318 * The source configuration value to verify the correct value is returned
319 * from the configuration factory after the deletion.
320 * @param string $langcode
323 protected function deleteLanguageOverride($config_name, $key, $source_value, $langcode) {
324 $translation_override = $this->languageManager
325 ->getLanguageConfigOverride($langcode, $config_name);
326 $translation_override
329 $this->configFactory->reset($config_name);
331 $this->assertNoConfigOverride($config_name, $key, $source_value, $langcode);
335 * Deletes translation data from locale module.
337 * This will invoke LocaleConfigSubscriber through the event dispatcher. To
338 * make sure the configuration was persisted correctly, the configuration
339 * value is checked. Because LocaleConfigSubscriber temporarily disables the
340 * override state of the configuration factory we check that the correct value
341 * is restored afterwards.
343 * @param string $config_name
344 * The configuration name.
346 * The configuration key.
347 * @param string $source_value
348 * The source configuration value to verify the correct value is returned
349 * from the configuration factory after the deletion.
350 * @param string $langcode
353 protected function deleteLocaleTranslationData($config_name, $key, $source_value, $langcode) {
354 $this->localeConfigManager
355 ->getStringTranslation($config_name, $langcode, $source_value, '')
357 $this->localeConfigManager->reset();
358 $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
359 $this->configFactory->reset($config_name);
361 $this->assertNoConfigOverride($config_name, $key, $source_value, $langcode);
365 * Ensures configuration override is not present anymore.
367 * @param string $config_name
368 * The configuration name.
369 * @param string $langcode
373 * TRUE if the assertion succeeded, FALSE otherwise.
375 protected function assertNoConfigOverride($config_name, $langcode) {
376 $config_langcode = $this->configFactory->getEditable($config_name)->get('langcode');
377 $override = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
378 return $this->assertNotEqual($config_langcode, $langcode) && $this->assertEqual($override->isNew(), TRUE);
382 * Ensures configuration was saved correctly.
384 * @param string $config_name
385 * The configuration name.
387 * The configuration key.
388 * @param string $value
389 * The configuration value.
390 * @param string $langcode
394 * TRUE if the assertion succeeded, FALSE otherwise.
396 protected function assertConfigOverride($config_name, $key, $value, $langcode) {
397 $config_langcode = $this->configFactory->getEditable($config_name)->get('langcode');
398 $override = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
399 return $this->assertNotEqual($config_langcode, $langcode) && $this->assertEqual($override->get($key), $value);
403 * Ensures configuration was saved correctly.
405 * @param string $config_name
406 * The configuration name.
408 * The configuration key.
409 * @param string $value
410 * The configuration value.
411 * @param string $langcode
415 * TRUE if the assertion succeeded, FALSE otherwise.
417 protected function assertActiveConfig($config_name, $key, $value, $langcode) {
418 $config = $this->configFactory->getEditable($config_name);
420 $this->assertEqual($config->get('langcode'), $langcode) &&
421 $this->assertIdentical($config->get($key), $value);
425 * Ensures no translation exists.
427 * @param string $config_name
428 * The configuration name.
429 * @param string $langcode
433 * TRUE if the assertion succeeded, FALSE otherwise.
435 protected function assertNoTranslation($config_name, $langcode) {
436 $strings = $this->stringStorage->getTranslations([
437 'type' => 'configuration',
438 'name' => $config_name,
439 'language' => $langcode,
440 'translated' => TRUE,
442 return $this->assertIdentical([], $strings);
446 * Ensures a translation exists and is marked as customized.
448 * @param string $config_name
449 * The configuration name.
450 * @param string $translation
452 * @param string $langcode
454 * @param bool $customized
455 * Whether or not the string should be asserted to be customized or not
459 * TRUE if the assertion succeeded, FALSE otherwise.
461 protected function assertTranslation($config_name, $translation, $langcode, $customized = TRUE) {
462 // Make sure a string exists.
463 $strings = $this->stringStorage->getTranslations([
464 'type' => 'configuration',
465 'name' => $config_name,
466 'language' => $langcode,
467 'translated' => TRUE,
469 $pass = $this->assertIdentical(1, count($strings));
470 $string = reset($strings);
471 if ($this->assertTrue($string instanceof StringInterface)) {
472 /** @var \Drupal\locale\StringInterface $string */
473 $pass = $pass && $this->assertIdentical($translation, $string->getString());
474 $pass = $pass && $this->assertTrue($string->isTranslation());
475 if ($this->assertTrue($string instanceof TranslationString)) {
476 /** @var \Drupal\locale\TranslationString $string */
477 // Make sure the string is marked as customized so that it does not get
478 // overridden when the string translations are updated.
479 return $pass && $this->assertEqual($customized, $string->customized);