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() {
parent::setUp();
module_load_include('compare.inc', 'locale');
module_load_include('fetch.inc', 'locale');
$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
->assertText(t('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
->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
$this
->drupalGet('admin/reports/translations/check');
$result = locale_translation_get_status();
$this
->assertEqual($result['contrib_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_one found');
$this
->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestampOld, 'Translation timestamp found');
$this
->assertEqual($result['contrib_module_two']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_two found');
$this
->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
$this
->assertEqual($result['locale_test']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of locale_test found');
$this
->assertEqual($result['custom_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of custom_module_one found');
$edit = [
'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
];
$this
->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
$this
->drupalGet('admin/reports/translations/check');
$result = locale_translation_get_status();
$this
->assertEqual($result['contrib_module_one']['de']->type, LOCALE_TRANSLATION_REMOTE, 'Translation of contrib_module_one found');
$this
->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
$this
->assertEqual($result['contrib_module_two']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_two found');
$this
->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
$this
->assertEqual($result['contrib_module_three']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_three found');
$this
->assertEqual($result['contrib_module_three']['de']->timestamp, $this->timestampOld, 'Translation timestamp found');
$this
->assertEqual($result['locale_test']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of locale_test found');
$this
->assertEqual($result['custom_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, '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
->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
$this
->drupalGet('admin/reports/translations/check');
$this
->assertRaw('<label for="edit-langcodes-de" class="visually-hidden">Update German</label>', 'German language found');
$this
->assertText('Updates for: Contributed module one, Contributed module two, Custom module one, Locale test', 'Updates found');
$date_formatter = $this->container
->get('date.formatter');
$this
->assertText('Contributed module one (' . $date_formatter
->format($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module one');
$this
->assertText('Contributed module two (' . $date_formatter
->format($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module two');
$this
->drupalPostForm('admin/reports/translations', [], t('Update translations'));
$status = locale_translation_get_status();
$this
->assertEqual($status['contrib_module_one']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_one found');
$this
->assertEqual($status['contrib_module_two']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_two found');
$this
->assertEqual($status['contrib_module_three']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_three found');
drupal_static_reset('locale_translation_get_file_history');
$history = locale_translation_get_file_history();
$this
->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestampNow, 'Translation of contrib_module_one is imported');
$this
->assertTrue($history['contrib_module_one']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_one is updated');
$this
->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation of contrib_module_two is imported');
$this
->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_two is updated');
$this
->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestampMedium, 'Translation of contrib_module_three is not imported');
$this
->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_three is not updated');
$this
->assertEqual(t('January', [], [
'langcode' => 'de',
]), 'Januar_1', 'Translation of January');
$this
->assertEqual(t('February', [], [
'langcode' => 'de',
]), 'Februar_2', 'Translation of February');
$this
->assertEqual(t('March', [], [
'langcode' => 'de',
]), 'Marz_2', 'Translation of March');
$this
->assertEqual(t('April', [], [
'langcode' => 'de',
]), 'April_2', 'Translation of April');
$this
->assertEqual(t('May', [], [
'langcode' => 'de',
]), 'Mai_customized', 'Translation of May');
$this
->assertEqual(t('June', [], [
'langcode' => 'de',
]), 'Juni', 'Translation of June');
$this
->assertEqual(t('Monday', [], [
'langcode' => 'de',
]), 'Montag', '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
->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
$this
->drupalGet('admin/reports/translations/check');
$this
->drupalPostForm('admin/reports/translations', [], t('Update translations'));
$status = locale_translation_get_status();
$this
->assertEqual($status['contrib_module_one']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_one found');
$this
->assertEqual($status['contrib_module_two']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_two found');
$this
->assertEqual($status['contrib_module_three']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_three found');
drupal_static_reset('locale_translation_get_file_history');
$history = locale_translation_get_file_history();
$this
->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestampMedium, 'Translation of contrib_module_one is imported');
$this
->assertEqual($history['contrib_module_one']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_one is updated');
$this
->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation of contrib_module_two is imported');
$this
->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_two is updated');
$this
->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestampMedium, 'Translation of contrib_module_three is not imported');
$this
->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_three is not updated');
$this
->assertEqual(t('January', [], [
'langcode' => 'de',
]), 'Januar_customized', 'Translation of January');
$this
->assertEqual(t('February', [], [
'langcode' => 'de',
]), 'Februar_2', 'Translation of February');
$this
->assertEqual(t('March', [], [
'langcode' => 'de',
]), 'Marz_2', 'Translation of March');
$this
->assertEqual(t('April', [], [
'langcode' => 'de',
]), 'April_2', 'Translation of April');
$this
->assertEqual(t('May', [], [
'langcode' => 'de',
]), 'Mai_customized', 'Translation of May');
$this
->assertEqual(t('June', [], [
'langcode' => 'de',
]), 'Juni', 'Translation of June');
$this
->assertEqual(t('Monday', [], [
'langcode' => 'de',
]), 'Montag', '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
->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
$this
->drupalGet('admin/reports/translations/check');
$this
->drupalPostForm('admin/reports/translations', [], t('Update translations'));
$this
->assertEqual(t('January', [], [
'langcode' => 'de',
]), 'Januar_customized', 'Translation of January');
$this
->assertEqual(t('February', [], [
'langcode' => 'de',
]), 'Februar_customized', 'Translation of February');
$this
->assertEqual(t('March', [], [
'langcode' => 'de',
]), 'Marz_2', 'Translation of March');
$this
->assertEqual(t('April', [], [
'langcode' => 'de',
]), 'April_2', 'Translation of April');
$this
->assertEqual(t('May', [], [
'langcode' => 'de',
]), 'Mai_customized', 'Translation of May');
$this
->assertEqual(t('June', [], [
'langcode' => 'de',
]), 'Juni', 'Translation of June');
$this
->assertEqual(t('Monday', [], [
'langcode' => 'de',
]), 'Montag', '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
->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
$this
->drupalGet('admin/reports/translations/check');
$this
->drupalPostForm('admin/reports/translations', [], t('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
->drupalPostForm('admin/modules', $edit, t('Install'));
$this
->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
'%number' => 7,
'%update' => 0,
'%delete' => 0,
]), 'One translation file imported.');
$this
->assertTranslation('Tuesday', 'Dienstag', 'de');
$edit = [
'uninstall[locale_test_translate]' => 1,
];
$this
->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
$this
->drupalPostForm(NULL, [], t('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
->drupalPostForm('admin/modules', $edit, t('Install'));
$this
->assertTranslation('Extraday', '', 'nl');
$this
->assertTranslation('Tuesday', 'Dienstag', 'de');
$edit = [
'predefined_langcode' => 'nl',
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$this
->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
'%number' => 8,
'%update' => 0,
'%delete' => 0,
]), 'One language added.');
$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
->drupalPostForm('admin/config/regional/language/delete/nl', [], t('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
->drupalPostForm('admin/modules', $edit, t('Install'));
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
$this
->assertText(t('One translation file imported.'), 'Language file automatically imported.');
$this
->assertText(t('One translation string was skipped because of disallowed or malformed HTML'), 'Language file automatically imported.');
$search = [
'string' => 'lundi',
'langcode' => $langcode,
'translation' => 'translated',
];
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertNoText(t('No strings available.'), 'String successfully imported.');
$search = [
'string' => 'Source string for multiline translation',
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText('Multiline translation string to make sure that import works with it.', 'String successfully imported.');
$search = [
'string' => 'HTML source string',
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText('Allowed HTML source string', 'String successfully imported.');
$this
->assertNoText('Another allowed HTML source string', 'String with disallowed translation not imported.');
}
}