View source
<?php
namespace Drupal\Tests\language_hierarchy\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
class StringDatabaseStorageDecoratorTest extends KernelTestBase {
public static $modules = [
'config_translation',
'language',
'language_hierarchy',
'locale',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('language_hierarchy', [
'language_hierarchy_priority',
]);
$languages = [
'en' => NULL,
'es' => 'en',
'es-MX' => NULL,
'de' => 'en',
'de-DE' => 'de',
];
foreach ($languages as $langcode => $fallback) {
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language
->setThirdPartySetting('language_hierarchy', 'fallback_langcode', $fallback);
$language
->save();
}
$this
->installSchema('locale', [
'locales_location',
'locales_source',
'locales_target',
]);
$this->storage = $this->container
->get('locale.storage');
}
public function testStringFallback() {
$strings = [
'en' => 'Read the article',
'de' => 'Artikel lesen',
];
$source = $this->storage
->createString([
'source' => $strings['en'],
'context' => $this
->randomMachineName(20),
])
->save();
$translation = $this->storage
->createTranslation([
'lid' => $source->lid,
'language' => 'en',
'translation' => $strings['en'],
])
->save();
$translation = $this->storage
->createTranslation([
'lid' => $source->lid,
'language' => 'de',
'translation' => $strings['de'],
])
->save();
$expected_strings = [
'en' => $strings['en'],
'es' => $strings['en'],
'es-MX' => '',
'de' => $strings['de'],
'de-DE' => $strings['de'],
];
foreach ($expected_strings as $langcode => $expected_string) {
$translation = $this->storage
->findTranslation([
'language' => $langcode,
'lid' => $source->lid,
]);
$this
->assertEquals($expected_string, $translation
->getString());
}
}
public function testStringFallbackPriority() {
$strings = [
'en' => 'Read the article',
'de' => 'Artikel lesen',
'de-DE' => 'Artikel lesen (Germany)',
];
$source = $this->storage
->createString([
'source' => $strings['en'],
'context' => $this
->randomMachineName(20),
])
->save();
$translation = $this->storage
->createTranslation([
'lid' => $source->lid,
'language' => 'en',
'translation' => $strings['en'],
])
->save();
$translation = $this->storage
->createTranslation([
'lid' => $source->lid,
'language' => 'de-DE',
'translation' => $strings['de-DE'],
])
->save();
$translation = $this->storage
->createTranslation([
'lid' => $source->lid,
'language' => 'de',
'translation' => $strings['de'],
])
->save();
$expected_strings = [
'en' => $strings['en'],
'de' => $strings['de'],
'de-DE' => $strings['de-DE'],
];
foreach ($expected_strings as $langcode => $expected_string) {
$translation = $this->storage
->findTranslation([
'language' => $langcode,
'lid' => $source->lid,
]);
$this
->assertEquals($expected_string, $translation
->getString());
}
}
}