View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Core\Database\Database;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\file\Entity\File;
use Drupal\Tests\BrowserTestBase;
use Drupal\Component\Render\FormattableMarkup;
abstract class LocaleUpdateBase extends BrowserTestBase {
protected $timestampOld;
protected $timestampMedium;
protected $timestampNew;
protected $timestampNow;
protected static $modules = [
'locale',
'locale_test',
];
protected function setUp() : void {
parent::setUp();
$this->timestampOld = REQUEST_TIME - 300;
$this->timestampMedium = REQUEST_TIME - 200;
$this->timestampNew = REQUEST_TIME - 100;
$this->timestampNow = REQUEST_TIME;
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
->save();
}
protected function setTranslationsDirectory($path) {
\Drupal::service('file_system')
->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
$this
->config('locale.settings')
->set('translation.path', $path)
->save();
}
protected function addLanguage($langcode) {
$edit = [
'predefined_langcode' => $langcode,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this->container
->get('language_manager')
->reset();
$this
->assertNotEmpty(\Drupal::languageManager()
->getLanguage($langcode), new FormattableMarkup('Language %langcode added.', [
'%langcode' => $langcode,
]));
}
protected function makePoFile($path, $filename, $timestamp = NULL, array $translations = []) {
$timestamp = $timestamp ? $timestamp : REQUEST_TIME;
$path = 'public://' . $path;
$text = '';
$po_header = <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"
EOF;
if ($translations) {
foreach ($translations as $source => $target) {
$text .= 'msgid "' . $source . '"' . "\n";
$text .= 'msgstr "' . $target . '"' . "\n";
}
}
\Drupal::service('file_system')
->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
$file = File::create([
'uid' => 1,
'filename' => $filename,
'uri' => $path . '/' . $filename,
'filemime' => 'text/x-gettext-translation',
'timestamp' => $timestamp,
]);
$file
->setPermanent();
file_put_contents($file
->getFileUri(), $po_header . $text);
touch(\Drupal::service('file_system')
->realpath($file
->getFileUri()), $timestamp);
$file
->save();
}
protected function setTranslationFiles() {
$config = $this
->config('locale.settings');
\Drupal::state()
->set('locale.test_projects_alter', TRUE);
\Drupal::state()
->set('locale.remove_core_project', FALSE);
$public_path = PublicStream::basePath();
$this
->setTranslationsDirectory($public_path . '/local');
$config
->set('translation.default_filename', '%project-%version.%language._po')
->save();
$translations_one = [
'January' => 'Januar_1',
'February' => 'Februar_1',
'March' => 'Marz_1',
];
$translations_two = [
'February' => 'Februar_2',
'March' => 'Marz_2',
'April' => 'April_2',
];
$translations_three = [
'April' => 'April_3',
'May' => 'Mai_3',
'June' => 'Juni_3',
];
$this
->makePoFile('remote/all/contrib_module_one', 'contrib_module_one-8.x-1.1.de._po', $this->timestampNew, $translations_one);
$this
->makePoFile('remote/all/contrib_module_two', 'contrib_module_two-8.x-2.0-beta4.de._po', $this->timestampOld, $translations_two);
$this
->makePoFile('remote/all/contrib_module_three', 'contrib_module_three-8.x-1.0.de._po', $this->timestampOld, $translations_three);
$this
->makePoFile('local', 'contrib_module_one-8.x-1.1.de._po', $this->timestampOld, $translations_one);
$this
->makePoFile('local', 'contrib_module_two-8.x-2.0-beta4.de._po', $this->timestampNew, $translations_two);
$this
->makePoFile('local', 'contrib_module_three-8.x-1.0.de._po', $this->timestampOld, $translations_three);
$this
->makePoFile('local', 'custom_module_one.de.po', $this->timestampNew);
}
protected function setCurrentTranslations() {
$langcode = 'de';
$context = '';
$non_customized_translations = [
'March' => 'Marz',
'June' => 'Juni',
];
foreach ($non_customized_translations as $source => $translation) {
$string = $this->container
->get('locale.storage')
->createString([
'source' => $source,
'context' => $context,
])
->save();
$this->container
->get('locale.storage')
->createTranslation([
'lid' => $string
->getId(),
'language' => $langcode,
'translation' => $translation,
'customized' => LOCALE_NOT_CUSTOMIZED,
])
->save();
}
$customized_translations = [
'January' => 'Januar_customized',
'February' => 'Februar_customized',
'May' => 'Mai_customized',
];
foreach ($customized_translations as $source => $translation) {
$string = $this->container
->get('locale.storage')
->createString([
'source' => $source,
'context' => $context,
])
->save();
$this->container
->get('locale.storage')
->createTranslation([
'lid' => $string
->getId(),
'language' => $langcode,
'translation' => $translation,
'customized' => LOCALE_CUSTOMIZED,
])
->save();
}
$default = [
'langcode' => $langcode,
'uri' => '',
'timestamp' => $this->timestampMedium,
'last_checked' => $this->timestampMedium,
];
$data[] = [
'project' => 'contrib_module_one',
'filename' => 'contrib_module_one-8.x-1.1.de._po',
'version' => '8.x-1.1',
];
$data[] = [
'project' => 'contrib_module_two',
'filename' => 'contrib_module_two-8.x-2.0-beta4.de._po',
'version' => '8.x-2.0-beta4',
];
$data[] = [
'project' => 'contrib_module_three',
'filename' => 'contrib_module_three-8.x-1.0.de._po',
'version' => '8.x-1.0',
];
$data[] = [
'project' => 'custom_module_one',
'filename' => 'custom_module_one.de.po',
'version' => '',
];
$connection = Database::getConnection();
foreach ($data as $file) {
$file = array_merge($default, $file);
$connection
->insert('locale_file')
->fields($file)
->execute();
}
}
protected function assertTranslation($source, $translation, $langcode, $message = '') {
$query = Database::getConnection()
->select('locales_target', 'lt');
$query
->innerJoin('locales_source', 'ls', '[ls].[lid] = [lt].[lid]');
$db_translation = $query
->fields('lt', [
'translation',
])
->condition('ls.source', $source)
->condition('lt.language', $langcode)
->execute()
->fetchField();
$db_translation = $db_translation == FALSE ? '' : $db_translation;
$this
->assertEquals($translation, $db_translation, $message ? $message : new FormattableMarkup('Correct translation of %source (%language)', [
'%source' => $source,
'%language' => $langcode,
]));
}
}