View source
<?php
namespace Drupal\Tests\locale\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\locale\StringInterface;
class LocaleStringTest extends KernelTestBase {
protected static $modules = [
'language',
'locale',
];
protected $storage;
protected function setUp() {
parent::setUp();
$this->storage = $this->container
->get('locale.storage');
foreach ([
'es',
'de',
] as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$this
->installSchema('locale', [
'locales_location',
'locales_source',
'locales_target',
]);
}
public function testStringCrudApi() {
$source = $this
->buildSourceString()
->save();
$this
->assertNotEmpty($source->lid);
$string1 = $this->storage
->findString([
'lid' => $source->lid,
]);
$this
->assertEquals($source, $string1);
$string2 = $this->storage
->findString([
'source' => $source->source,
'context' => $source->context,
]);
$this
->assertEquals($source, $string2);
$string3 = $this->storage
->findString([
'source' => $source->source,
'context' => '',
]);
$this
->assertNull($string3);
$this
->assertEquals('none', $source->version);
$string = $this->storage
->findTranslation([
'lid' => $source->lid,
]);
$this
->assertEquals(\Drupal::VERSION, $string->version);
$langcode = 'es';
$translation = $this
->createTranslation($source, $langcode);
$this
->assertEquals(LOCALE_NOT_CUSTOMIZED, $translation->customized);
$string1 = $this->storage
->findTranslation([
'language' => $langcode,
'lid' => $source->lid,
]);
$this
->assertEquals($translation->translation, $string1->translation);
$string2 = $this->storage
->findTranslation([
'language' => $langcode,
'source' => $source->source,
'context' => $source->context,
]);
$this
->assertEquals($translation->translation, $string2->translation);
$translation
->setCustomized()
->save();
$translation = $this->storage
->findTranslation([
'language' => $langcode,
'lid' => $source->lid,
]);
$this
->assertEquals(LOCALE_CUSTOMIZED, $translation->customized);
$translation
->delete();
$deleted = $this->storage
->findTranslation([
'language' => $langcode,
'lid' => $source->lid,
]);
$this
->assertNull($deleted->translation);
$lid = $source->lid;
$this
->createAllTranslations($source);
$search = $this->storage
->getTranslations([
'lid' => $source->lid,
]);
$this
->assertCount(3, $search);
$source
->delete();
$string = $this->storage
->findString([
'lid' => $lid,
]);
$this
->assertNull($string);
$deleted = $search = $this->storage
->getTranslations([
'lid' => $lid,
]);
$this
->assertEmpty($deleted);
$source_string = $this
->buildSourceString();
$source_string
->addLocation('javascript', $this
->randomString(8));
$source_string
->addLocation('configuration', $this
->randomString(50));
$source_string
->addLocation('code', $this
->randomString(100));
$source_string
->addLocation('path', $location = $this
->randomString(300));
$source_string
->save();
$rows = $this->container
->get('database')
->select('locales_location')
->fields('locales_location')
->condition('sid', $source_string->lid)
->execute()
->fetchAllAssoc('type');
$this
->assertCount(4, $rows);
$this
->assertEquals(substr($location, 0, 255), $rows['path']->name);
}
public function testStringSearchApi() {
$language_count = 3;
$prefix = $this
->randomMachineName(100);
$source1 = $this
->buildSourceString([
'source' => $prefix . $this
->randomMachineName(100),
])
->save();
$source2 = $this
->buildSourceString([
'source' => $prefix . $this
->randomMachineName(100),
])
->save();
$source3 = $this
->buildSourceString()
->save();
$strings = $this->storage
->getStrings([]);
$this
->assertCount(3, $strings);
$filter_options['filters'] = [
'source' => $prefix,
];
$strings = $this->storage
->getStrings([], $filter_options);
$this
->assertCount(2, $strings);
$translate1 = $this
->createAllTranslations($source1);
$this
->createAllTranslations($source2, [
'customized' => LOCALE_CUSTOMIZED,
]);
$langcode = 'es';
$found = $this->storage
->findTranslation([
'language' => $langcode,
'source' => $source1->source,
'context' => $source1->context,
]);
$this
->assertTrue($found && isset($found->language) && isset($found->translation) && !$found
->isNew(), 'Translation not found searching by source and context.');
$this
->assertEquals($translate1[$langcode]->translation, $found->translation);
$found = $this->storage
->findTranslation([
'language' => $langcode,
'source' => $source3->source,
'context' => $source3->context,
]);
$this
->assertTrue($found && $found->lid == $source3->lid && !isset($found->translation) && $found
->isNew());
$translations = $this->storage
->getTranslations([
'translated' => TRUE,
]);
$this
->assertCount(2 * $language_count, $translations);
$translations = $this->storage
->getTranslations([
'customized' => LOCALE_CUSTOMIZED,
'translated' => TRUE,
]);
$this
->assertCount($language_count, $translations);
$translations = $this->storage
->getTranslations([
'language' => 'es',
'customized' => LOCALE_CUSTOMIZED,
'translated' => TRUE,
]);
$this
->assertCount(1, $translations);
$translations = $this->storage
->getStrings([
'translated' => FALSE,
]);
$this
->assertCount(1, $translations);
$filter_options['filters'] = [
'source' => $prefix,
];
$translations = $this->storage
->getTranslations([
'language' => 'es',
], $filter_options);
$this
->assertCount(2, $translations);
}
protected function buildSourceString(array $values = []) {
return $this->storage
->createString($values += [
'source' => $this
->randomMachineName(100),
'context' => $this
->randomMachineName(20),
]);
}
protected function createAllTranslations(StringInterface $source, array $values = []) {
$list = [];
$language_manager = $this->container
->get('language_manager');
foreach ($language_manager
->getLanguages() as $language) {
$list[$language
->getId()] = $this
->createTranslation($source, $language
->getId(), $values);
}
return $list;
}
protected function createTranslation(StringInterface $source, $langcode, array $values = []) {
return $this->storage
->createTranslation($values + [
'lid' => $source->lid,
'language' => $langcode,
'translation' => $this
->randomMachineName(100),
])
->save();
}
}