View source
<?php
namespace Drupal\locale\Tests;
use Drupal\locale\Locale;
use Drupal\simpletest\WebTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
class LocaleConfigTranslationImportTest extends WebTestBase {
public static $modules = array(
'language',
'locale_test_translate',
);
public function testConfigTranslationImport() {
$admin_user = $this
->drupalCreateUser(array(
'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(array(
'locale',
));
$this
->resetAll();
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->save();
$edit = array(
'authenticated[translate interface]' => 'translate interface',
);
$this
->drupalPostForm('admin/people/permissions', $edit, t('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
->drupalPostForm('admin/reports/translations', array(), t('Update translations'));
$override = \Drupal::languageManager()
->getLanguageConfigOverride('af', 'system.maintenance');
$this
->assertEqual($override
->get('message'), 'Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.');
}
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(array(
'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)
->save();
$this
->drupalPostForm('admin/config/regional/language/add', [
'predefined_langcode' => 'af',
], t('Add language'));
$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');
$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'));
$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
->drupalPostForm('admin/modules', [
'modules[Core][dblog][enable]' => 'dblog',
], t('Install'));
$this
->assertText('Module Database Logging has been enabled.');
$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());
}
function testLocaleRemovalAndConfigOverrideDelete() {
$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);
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->save();
$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());
$locale_storage = \Drupal::service('locale.storage');
$string = $locale_storage
->findString(array(
'source' => 'Locale can translate',
));
\Drupal::service('locale.storage')
->delete($string);
$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.');
}
function testLocaleRemovalAndConfigOverridePreserve() {
$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);
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->save();
$this
->drupalPostForm('admin/config/regional/language/add', [
'predefined_langcode' => 'af',
], t('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
->assertEqual($expected, $override
->get());
$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());
}
}