View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
class LocaleUpdateTest extends LocaleUpdateBase {
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$module_handler = \Drupal::moduleHandler();
$module_handler
->loadInclude('locale', 'inc', 'locale.compare');
$module_handler
->loadInclude('locale', 'inc', 'locale.fetch');
$admin_user = $this
->drupalCreateUser([
'administer modules',
'administer site configuration',
'administer languages',
'access administration pages',
'translate interface',
]);
$this
->drupalLogin($admin_user);
$this
->addLanguage('de');
}
public function testUpdateCheckStatus() {
$this
->drupalGet('admin/reports/translations');
$this
->assertSession()
->pageTextContains('Missing translations for one project');
$config = $this
->config('locale.settings');
\Drupal::state()
->set('locale.test_projects_alter', TRUE);
$this
->setTranslationFiles();
$config
->set('translation.default_filename', '%project-%version.%language._po')
->save();
$edit = [
'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
];
$this
->drupalGet('admin/config/regional/translate/settings');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/reports/translations/check');
$result = locale_translation_get_status();
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['contrib_module_one']['de']->type, 'Translation of contrib_module_one found');
$this
->assertEquals($this->timestampOld, $result['contrib_module_one']['de']->timestamp, 'Translation timestamp found');
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['contrib_module_two']['de']->type, 'Translation of contrib_module_two found');
$this
->assertEquals($this->timestampNew, $result['contrib_module_two']['de']->timestamp, 'Translation timestamp found');
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['locale_test']['de']->type, 'Translation of locale_test found');
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['custom_module_one']['de']->type, 'Translation of custom_module_one found');
$edit = [
'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
];
$this
->drupalGet('admin/config/regional/translate/settings');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/reports/translations/check');
$result = locale_translation_get_status();
$this
->assertEquals(LOCALE_TRANSLATION_REMOTE, $result['contrib_module_one']['de']->type, 'Translation of contrib_module_one found');
$this
->assertEquals($this->timestampNew, $result['contrib_module_one']['de']->timestamp, 'Translation timestamp found');
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['contrib_module_two']['de']->type, 'Translation of contrib_module_two found');
$this
->assertEquals($this->timestampNew, $result['contrib_module_two']['de']->timestamp, 'Translation timestamp found');
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['contrib_module_three']['de']->type, 'Translation of contrib_module_three found');
$this
->assertEquals($this->timestampOld, $result['contrib_module_three']['de']->timestamp, 'Translation timestamp found');
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['locale_test']['de']->type, 'Translation of locale_test found');
$this
->assertEquals(LOCALE_TRANSLATION_LOCAL, $result['custom_module_one']['de']->type, 'Translation of custom_module_one found');
}
public function testUpdateImportSourceRemote() {
$config = $this
->config('locale.settings');
$this
->setTranslationFiles();
$this
->setCurrentTranslations();
$config
->set('translation.default_filename', '%project-%version.%language._po');
$edit = [
'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
];
$this
->drupalGet('admin/config/regional/translate/settings');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/reports/translations/check');
$this
->assertSession()
->responseContains('<label for="edit-langcodes-de" class="visually-hidden">Update German</label>');
$this
->assertSession()
->pageTextContains('Updates for: Contributed module one, Contributed module two, Custom module one, Locale test');
$date_formatter = $this->container
->get('date.formatter');
$this
->assertSession()
->pageTextContains('Contributed module one (' . $date_formatter
->format($this->timestampNew, 'html_date') . ')');
$this
->assertSession()
->pageTextContains('Contributed module two (' . $date_formatter
->format($this->timestampNew, 'html_date') . ')');
$this
->drupalGet('admin/reports/translations');
$this
->submitForm([], 'Update translations');
$status = locale_translation_get_status();
$this
->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_one']['de']->type, 'Translation of contrib_module_one found');
$this
->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_two']['de']->type, 'Translation of contrib_module_two found');
$this
->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_three']['de']->type, 'Translation of contrib_module_three found');
drupal_static_reset('locale_translation_get_file_history');
$history = locale_translation_get_file_history();
$this
->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_one']['de']->timestamp);
$this
->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_one']['de']->last_checked);
$this
->assertEquals($this->timestampNew, $history['contrib_module_two']['de']->timestamp, 'Translation of contrib_module_two is imported');
$this
->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_two']['de']->last_checked);
$this
->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->timestamp, 'Translation of contrib_module_three is not imported');
$this
->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->last_checked, 'Translation of contrib_module_three is not updated');
$this
->assertEquals('Januar_1', t('January', [], [
'langcode' => 'de',
]), 'Translation of January');
$this
->assertEquals('Februar_2', t('February', [], [
'langcode' => 'de',
]), 'Translation of February');
$this
->assertEquals('Marz_2', t('March', [], [
'langcode' => 'de',
]), 'Translation of March');
$this
->assertEquals('April_2', t('April', [], [
'langcode' => 'de',
]), 'Translation of April');
$this
->assertEquals('Mai_customized', t('May', [], [
'langcode' => 'de',
]), 'Translation of May');
$this
->assertEquals('Juni', t('June', [], [
'langcode' => 'de',
]), 'Translation of June');
$this
->assertEquals('Montag', t('Monday', [], [
'langcode' => 'de',
]), 'Translation of Monday');
}
public function testUpdateImportSourceLocal() {
$config = $this
->config('locale.settings');
$this
->setTranslationFiles();
$this
->setCurrentTranslations();
$config
->set('translation.default_filename', '%project-%version.%language._po');
$edit = [
'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
];
$this
->drupalGet('admin/config/regional/translate/settings');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/reports/translations/check');
$this
->drupalGet('admin/reports/translations');
$this
->submitForm([], 'Update translations');
$status = locale_translation_get_status();
$this
->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_one']['de']->type, 'Translation of contrib_module_one found');
$this
->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_two']['de']->type, 'Translation of contrib_module_two found');
$this
->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_three']['de']->type, 'Translation of contrib_module_three found');
drupal_static_reset('locale_translation_get_file_history');
$history = locale_translation_get_file_history();
$this
->assertGreaterThanOrEqual($this->timestampMedium, $history['contrib_module_one']['de']->timestamp);
$this
->assertEquals($this->timestampMedium, $history['contrib_module_one']['de']->last_checked, 'Translation of contrib_module_one is updated');
$this
->assertEquals($this->timestampNew, $history['contrib_module_two']['de']->timestamp, 'Translation of contrib_module_two is imported');
$this
->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_two']['de']->last_checked);
$this
->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->timestamp, 'Translation of contrib_module_three is not imported');
$this
->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->last_checked, 'Translation of contrib_module_three is not updated');
$this
->assertEquals('Januar_customized', t('January', [], [
'langcode' => 'de',
]), 'Translation of January');
$this
->assertEquals('Februar_2', t('February', [], [
'langcode' => 'de',
]), 'Translation of February');
$this
->assertEquals('Marz_2', t('March', [], [
'langcode' => 'de',
]), 'Translation of March');
$this
->assertEquals('April_2', t('April', [], [
'langcode' => 'de',
]), 'Translation of April');
$this
->assertEquals('Mai_customized', t('May', [], [
'langcode' => 'de',
]), 'Translation of May');
$this
->assertEquals('Juni', t('June', [], [
'langcode' => 'de',
]), 'Translation of June');
$this
->assertEquals('Montag', t('Monday', [], [
'langcode' => 'de',
]), 'Translation of Monday');
}
public function testUpdateImportModeNonCustomized() {
$config = $this
->config('locale.settings');
$this
->setTranslationFiles();
$this
->setCurrentTranslations();
$config
->set('translation.default_filename', '%project-%version.%language._po');
$edit = [
'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED,
];
$this
->drupalGet('admin/config/regional/translate/settings');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/reports/translations/check');
$this
->drupalGet('admin/reports/translations');
$this
->submitForm([], 'Update translations');
$this
->assertEquals('Januar_customized', t('January', [], [
'langcode' => 'de',
]), 'Translation of January');
$this
->assertEquals('Februar_customized', t('February', [], [
'langcode' => 'de',
]), 'Translation of February');
$this
->assertEquals('Marz_2', t('March', [], [
'langcode' => 'de',
]), 'Translation of March');
$this
->assertEquals('April_2', t('April', [], [
'langcode' => 'de',
]), 'Translation of April');
$this
->assertEquals('Mai_customized', t('May', [], [
'langcode' => 'de',
]), 'Translation of May');
$this
->assertEquals('Juni', t('June', [], [
'langcode' => 'de',
]), 'Translation of June');
$this
->assertEquals('Montag', t('Monday', [], [
'langcode' => 'de',
]), 'Translation of Monday');
}
public function testUpdateImportModeNone() {
$config = $this
->config('locale.settings');
$this
->setTranslationFiles();
$this
->setCurrentTranslations();
$config
->set('translation.default_filename', '%project-%version.%language._po');
$edit = [
'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NONE,
];
$this
->drupalGet('admin/config/regional/translate/settings');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/reports/translations/check');
$this
->drupalGet('admin/reports/translations');
$this
->submitForm([], 'Update translations');
$this
->assertTranslation('January', 'Januar_customized', 'de');
$this
->assertTranslation('February', 'Februar_customized', 'de');
$this
->assertTranslation('March', 'Marz', 'de');
$this
->assertTranslation('April', 'April_2', 'de');
$this
->assertTranslation('May', 'Mai_customized', 'de');
$this
->assertTranslation('June', 'Juni', 'de');
$this
->assertTranslation('Monday', 'Montag', 'de');
}
public function testEnableUninstallModule() {
\Drupal::state()
->set('locale.test_system_info_alter', TRUE);
$this
->assertTranslation('Tuesday', '', 'de');
$edit = [
'modules[locale_test_translate][enable]' => 'locale_test_translate',
];
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$this
->assertSession()
->pageTextContains("One translation file imported. 7 translations were added, 0 translations were updated and 0 translations were removed.");
$this
->assertTranslation('Tuesday', 'Dienstag', 'de');
$edit = [
'uninstall[locale_test_translate]' => 1,
];
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->submitForm([], 'Uninstall');
$history = locale_translation_get_file_history();
$this
->assertFalse(isset($history['locale_test_translate']), 'Project removed from the file history');
$projects = locale_translation_get_projects();
$this
->assertFalse(isset($projects['locale_test_translate']), 'Project removed from the project list');
}
public function testEnableLanguage() {
\Drupal::state()
->set('locale.test_system_info_alter', TRUE);
$edit = [
'modules[locale_test_translate][enable]' => 'locale_test_translate',
];
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$this
->assertTranslation('Extraday', '', 'nl');
$this
->assertTranslation('Tuesday', 'Dienstag', 'de');
$edit = [
'predefined_langcode' => 'nl',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->assertSession()
->pageTextContains("One translation file imported. 8 translations were added, 0 translations were updated and 0 translations were removed.");
$this
->assertTranslation('Extraday', 'extra dag', 'nl');
$connection = Database::getConnection();
$result = $connection
->select('locale_file', 'lf')
->fields('lf', [
'project',
])
->condition('langcode', 'nl')
->execute()
->fetchField();
$this
->assertNotEmpty($result, 'Files added to file history');
$this
->drupalGet('admin/config/regional/language/delete/nl');
$this
->submitForm([], 'Delete');
$result = $connection
->select('locale_file', 'lf')
->fields('lf', [
'project',
])
->condition('langcode', 'nl')
->execute()
->fetchField();
$this
->assertFalse($result, 'Files removed from file history');
$this
->assertTranslation('Extraday', '', 'nl');
$this
->assertTranslation('Tuesday', 'Dienstag', 'de');
}
public function testEnableCustomLanguage() {
\Drupal::state()
->set('locale.test_system_info_alter', TRUE);
$edit = [
'modules[locale_test_translate][enable]' => 'locale_test_translate',
];
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
$this
->assertSession()
->pageTextContains('One translation file imported.');
$this
->assertSession()
->pageTextContains('One translation string was skipped because of disallowed or malformed HTML');
$search = [
'string' => 'lundi',
'langcode' => $langcode,
'translation' => 'translated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextNotContains('No strings available.');
$search = [
'string' => 'Source string for multiline translation',
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('Multiline translation string to make sure that import works with it.');
$search = [
'string' => 'HTML source string',
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('Allowed HTML source string');
$this
->assertSession()
->pageTextNotContains('Another allowed HTML source string');
}
}