Commit 1ed01c54 authored by catch's avatar catch

Issue #2580575 by alexpott, YesCT, Gábor Hojtsy: Installing a module can delete config translations

parent b75c57ff
......@@ -122,13 +122,14 @@ public function __construct(StorageInterface $config_storage, StringStorageInter
}
/**
* Gets array of translated strings for translatable configuration.
* Gets array of translated strings for Locale translatable configuration.
*
* @param string $name
* Configuration object name.
*
* @return array
* Array of translatable elements of the default configuration in $name.
* Array of Locale translatable elements of the default configuration in
* $name.
*/
public function getTranslatableDefaultConfig($name) {
if ($this->isSupported($name)) {
......@@ -167,13 +168,16 @@ protected function getTranslatableData(TypedDataInterface $element) {
}
}
else {
// Something is only translatable by Locale if there is a string in the
// first place.
$value = $element->getValue();
$definition = $element->getDataDefinition();
if (!empty($definition['translatable'])) {
if (!empty($definition['translatable']) && $value !== '' && $value !== NULL) {
$options = array();
if (isset($definition['translation context'])) {
$options['context'] = $definition['translation context'];
}
return new TranslatableMarkup($element->getValue(), array(), $options);
return new TranslatableMarkup($value, array(), $options);
}
}
return $translatable;
......@@ -559,33 +563,27 @@ public function updateConfigTranslations(array $names, array $langcodes = array(
foreach ($langcodes as $langcode) {
$processed = $this->processTranslatableData($name, $active, $translatable, $langcode);
// If the language code is not the same as the active storage
// language, we should update the configuration override.
if ($langcode != $active_langcode) {
// If the language code is not the same as the active storage
// language, we should update a configuration override.
$override = $this->languageManager->getLanguageConfigOverride($langcode, $name);
// Filter out locale managed configuration keys so that translations
// removed from Locale will be reflected in the config override.
$data = $this->filterOverride($override->get(), $translatable);
if (!empty($processed)) {
// Update translation data in configuration override.
$this->saveTranslationOverride($name, $langcode, $processed);
// Merge in the Locale managed translations with existing data.
$data = NestedArray::mergeDeepArray(array($data, $processed), TRUE);
}
if (empty($data) && !$override->isNew()) {
// The configuration override contains Locale overrides that no
// longer exist.
$this->deleteTranslationOverride($name, $langcode);
$count++;
}
else {
$override = $this->languageManager->getLanguageConfigOverride($langcode, $name);
if (!$override->isNew()) {
$data = $this->filterOverride($override->get(), $translatable);
if (empty($data)) {
// Delete language override if there is no data left at all.
// This means all prior translations in the override were locale
// managed.
$this->deleteTranslationOverride($name, $langcode);
$count++;
}
else {
// If there were translatable elements besides locale managed
// items, save with only those, and remove the ones managed
// by locale only.
$this->saveTranslationOverride($name, $langcode, $data);
$count++;
}
}
elseif (!empty($data)) {
// Update translation data in configuration override.
$this->saveTranslationOverride($name, $langcode, $data);
$count++;
}
}
elseif (locale_is_translatable($langcode)) {
......
......@@ -7,6 +7,7 @@
namespace Drupal\locale\Tests;
use Drupal\locale\Locale;
use Drupal\simpletest\WebTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Core\Url;
......@@ -25,20 +26,12 @@ class LocaleConfigTranslationImportTest extends WebTestBase {
*/
public static $modules = array('language', 'locale_test_translate');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$admin_user = $this->drupalCreateUser(array('administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions'));
$this->drupalLogin($admin_user);
}
/**
* Test update changes configuration translations if enabled after language.
*/
public function testConfigTranslationImport() {
$admin_user = $this->drupalCreateUser(array('administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions'));
$this->drupalLogin($admin_user);
// Add a language. The Afrikaans translation file of locale_test_translate
// (test.af.po) has been prepared with a configuration translation.
......@@ -77,4 +70,156 @@ public function testConfigTranslationImport() {
$this->assertEqual($override->get('message'), 'Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.');
}
/**
* Test update changes configuration translations if enabled after language.
*/
public function testConfigTranslationModuleInstall() {
// Enable locale, block and config_translation modules.
$this->container->get('module_installer')->install(['block', 'config_translation']);
$this->resetAll();
$admin_user = $this->drupalCreateUser(array('administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate configuration'));
$this->drupalLogin($admin_user);
// Enable import of translations. By default this is disabled for automated
// tests.
$this->config('locale.settings')
->set('translation.import_enabled', TRUE)
->save();
// Add predefined language.
$this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
// Add the system branding block to the page.
$this->drupalPlaceBlock('system_branding_block', array('region' => 'header', 'id' => 'site-branding'));
$this->drupalPostForm('admin/config/system/site-information', ['site_slogan' => 'Test site slogan'], 'Save configuration');
$this->drupalPostForm('admin/config/system/site-information/translate/af/edit', ['translation[config_names][system.site][slogan]' => 'Test site slogan in Afrikaans'], 'Save translation');
// Get the front page and ensure that the translated configuration appears.
$this->drupalGet('af');
$this->assertText('Test site slogan in Afrikaans');
$override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$this->assertEqual('Locale can translate Afrikaans', $override->get('translatable_default_with_translation'));
// Update test configuration.
$override
->set('translatable_no_default', 'This translation is preserved')
->set('translatable_default_with_translation', 'This translation is preserved')
->set('translatable_default_with_no_translation', 'This translation is preserved')
->save();
// Install any module.
$this->drupalPostForm('admin/modules', ['modules[Core][dblog][enable]' => 'dblog'], t('Install'));
$this->assertText('Module Database Logging has been enabled.');
// Get the front page and ensure that the translated configuration still
// appears.
$this->drupalGet('af');
$this->assertText('Test site slogan in Afrikaans');
$this->rebuildContainer();
$override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$expected = [
'translatable_no_default' => 'This translation is preserved',
'translatable_default_with_translation' => 'This translation is preserved',
'translatable_default_with_no_translation' => 'This translation is preserved'
];
$this->assertEqual($expected, $override->get());
}
/**
* Test removing a string from Locale deletes configuration translations.
*/
function testLocaleRemovalAndConfigOverrideDelete() {
// Enable the locale module.
$this->container->get('module_installer')->install(['locale']);
$this->resetAll();
$admin_user = $this->drupalCreateUser(array('administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface'));
$this->drupalLogin($admin_user);
// Enable import of translations. By default this is disabled for automated
// tests.
$this->config('locale.settings')
->set('translation.import_enabled', TRUE)
->save();
// Add predefined language.
$this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
$override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$this->assertEqual(['translatable_default_with_translation' => 'Locale can translate Afrikaans'], $override->get());
// Remove the string from translation to simulate a Locale removal. Note
// that is no current way of doing this in the UI.
$locale_storage = \Drupal::service('locale.storage');
$string = $locale_storage->findString(array('source' => 'Locale can translate'));
\Drupal::service('locale.storage')->delete($string);
// Force a rebuild of config translations.
$count = Locale::config()->updateConfigTranslations(['locale_test_translate.settings'], ['af']);
$this->assertEqual($count, 1, 'Correct count of updated translations');
$override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$this->assertEqual([], $override->get());
$this->assertTrue($override->isNew(), 'The configuration override was deleted when the Locale string was deleted.');
}
/**
* Test removing a string from Locale changes configuration translations.
*/
function testLocaleRemovalAndConfigOverridePreserve() {
// Enable the locale module.
$this->container->get('module_installer')->install(['locale']);
$this->resetAll();
$admin_user = $this->drupalCreateUser(array('administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface'));
$this->drupalLogin($admin_user);
// Enable import of translations. By default this is disabled for automated
// tests.
$this->config('locale.settings')
->set('translation.import_enabled', TRUE)
->save();
// Add predefined language.
$this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
$override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
// Update test configuration.
$override
->set('translatable_no_default', 'This translation is preserved')
->set('translatable_default_with_no_translation', 'This translation is preserved')
->save();
$expected = [
'translatable_default_with_translation' => 'Locale can translate Afrikaans',
'translatable_no_default' => 'This translation is preserved',
'translatable_default_with_no_translation' => 'This translation is preserved'
];
$this->assertEqual($expected, $override->get());
// Set the translated string to empty.
$search = array(
'string' => 'Locale can translate',
'langcode' => 'af',
'translation' => 'all',
);
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textareas = $this->xpath('//textarea');
$textarea = current($textareas);
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => '',
);
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$expected = [
'translatable_no_default' => 'This translation is preserved',
'translatable_default_with_no_translation' => 'This translation is preserved'
];
$this->assertEqual($expected, $override->get());
}
}
translatable_no_default: ''
translatable_default_with_translation: 'Locale can translate'
translatable_default_with_no_translation: 'Locale can not translate'
locale_test_translate.settings:
type: config_object
label: 'Test for locale translations'
mapping:
translatable_no_default:
type: label
translatable_default_with_translation:
type: label
translatable_default_with_no_translation:
type: label
......@@ -8,3 +8,6 @@ msgstr ""
msgid "@site is currently under maintenance. We should be back shortly. Thank you for your patience."
msgstr "Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees."
msgid "Locale can translate"
msgstr "Locale can translate Afrikaans"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment