View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\locale\Locale;
use Drupal\Tests\BrowserTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
class LocaleConfigTranslationImportTest extends BrowserTestBase {
protected static $modules = [
'language',
'locale_test_translate',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
}
public function testConfigTranslationImport() {
$admin_user = $this
->drupalCreateUser([
'administer modules',
'administer site configuration',
'administer languages',
'access administration pages',
'administer permissions',
]);
$this
->drupalLogin($admin_user);
ConfigurableLanguage::createFromLangcode('af')
->save();
$this->container
->get('module_installer')
->install([
'locale',
]);
$this
->resetAll();
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
->save();
$edit = [
'authenticated[translate interface]' => 'translate interface',
];
$this
->drupalGet('admin/people/permissions');
$this
->submitForm($edit, 'Save permissions');
$this
->drupalGet('admin/reports/translations/check');
$status = locale_translation_get_status();
$status['drupal']['af']->type = 'current';
\Drupal::state()
->set('locale.translation_status', $status);
$this
->drupalGet('admin/reports/translations');
$this
->submitForm([], 'Update translations');
$override = \Drupal::languageManager()
->getLanguageConfigOverride('af', 'system.maintenance');
$this
->assertEquals('Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.', $override
->get('message'));
}
public function testConfigTranslationModuleInstall() {
$this->container
->get('module_installer')
->install([
'block',
'config_translation',
]);
$this
->resetAll();
$this
->assertFalse($this
->config('locale.settings')
->get('translation.import_enabled'), 'Translations imports are disabled by default in the Testing profile.');
$admin_user = $this
->drupalCreateUser([
'administer modules',
'administer site configuration',
'administer languages',
'access administration pages',
'administer permissions',
'translate configuration',
]);
$this
->drupalLogin($admin_user);
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
->save();
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm([
'predefined_langcode' => 'af',
], 'Add language');
$this
->drupalPlaceBlock('system_branding_block', [
'region' => 'header',
'id' => 'site-branding',
]);
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm([
'site_slogan' => 'Test site slogan',
], 'Save configuration');
$this
->drupalGet('admin/config/system/site-information/translate/af/edit');
$this
->submitForm([
'translation[config_names][system.site][slogan]' => 'Test site slogan in Afrikaans',
], 'Save translation');
$this
->drupalGet('af');
$this
->assertSession()
->pageTextContains('Test site slogan in Afrikaans');
$override = \Drupal::languageManager()
->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$this
->assertEquals('Locale can translate Afrikaans', $override
->get('translatable_default_with_translation'));
$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();
$this
->drupalGet('admin/modules');
$this
->submitForm([
'modules[dblog][enable]' => 'dblog',
], 'Install');
$this
->assertSession()
->pageTextContains('Module Database Logging has been enabled.');
$this
->drupalGet('af');
$this
->assertSession()
->pageTextContains('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
->assertEquals($expected, $override
->get());
}
public function testLocaleRemovalAndConfigOverrideDelete() {
$this->container
->get('module_installer')
->install([
'locale',
]);
$this
->resetAll();
$admin_user = $this
->drupalCreateUser([
'administer modules',
'administer site configuration',
'administer languages',
'access administration pages',
'administer permissions',
'translate interface',
]);
$this
->drupalLogin($admin_user);
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
->save();
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm([
'predefined_langcode' => 'af',
], 'Add language');
$override = \Drupal::languageManager()
->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$this
->assertEquals([
'translatable_default_with_translation' => 'Locale can translate Afrikaans',
], $override
->get());
$locale_storage = \Drupal::service('locale.storage');
$string = $locale_storage
->findString([
'source' => 'Locale can translate',
]);
\Drupal::service('locale.storage')
->delete($string);
$count = Locale::config()
->updateConfigTranslations([
'locale_test_translate.settings',
], [
'af',
]);
$this
->assertEquals(1, $count, 'Correct count of updated translations');
$override = \Drupal::languageManager()
->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$this
->assertEquals([], $override
->get());
$this
->assertTrue($override
->isNew(), 'The configuration override was deleted when the Locale string was deleted.');
}
public function testLocaleRemovalAndConfigOverridePreserve() {
$this->container
->get('module_installer')
->install([
'locale',
]);
$this
->resetAll();
$admin_user = $this
->drupalCreateUser([
'administer modules',
'administer site configuration',
'administer languages',
'access administration pages',
'administer permissions',
'translate interface',
]);
$this
->drupalLogin($admin_user);
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
->save();
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm([
'predefined_langcode' => 'af',
], 'Add language');
$override = \Drupal::languageManager()
->getLanguageConfigOverride('af', 'locale_test_translate.settings');
$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
->assertEquals($expected, $override
->get());
$search = [
'string' => 'Locale can translate',
'langcode' => 'af',
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => '',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, '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
->assertEquals($expected, $override
->get());
}
}