View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Core\Url;
use Drupal\Core\Database\Database;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Language\LanguageInterface;
class LocaleTranslationUiTest extends BrowserTestBase {
protected static $modules = [
'locale',
];
protected $defaultTheme = 'stark';
public function testEnglishTranslation() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/regional/language/edit/en');
$this
->submitForm([
'locale_translate_english' => TRUE,
], 'Save language');
$this
->assertSession()
->linkByHrefExists('/admin/config/regional/translate?langcode=en', 0, 'Enabled interface translation to English.');
}
public function testStringTranslation() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$translate_user = $this
->drupalCreateUser([
'translate interface',
'access administration pages',
]);
$langcode = 'xx';
$name = 'cucurbitaceae';
$translation = $this
->randomMachineName(16);
$translation_to_en = $this
->randomMachineName(16);
$this
->drupalLogin($admin_user);
$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');
t($name, [], [
'langcode' => $langcode,
])
->render();
$this->container
->get('string_translation')
->reset();
$this
->assertSession()
->responseContains('"edit-languages-' . $langcode . '-weight"');
$this
->assertSession()
->pageTextContains($name);
$this
->drupalLogout();
$name_ws = $name . " ";
$this
->drupalLogin($translate_user);
$search = [
'string' => $name_ws,
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($name);
$this
->assertSession()
->pageTextContains($name);
$this
->assertSession()
->optionNotExists('edit-langcode', 'en');
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/regional/language/edit/en');
$this
->submitForm([
'locale_translate_english' => TRUE,
], 'Save language');
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($name);
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => $translation,
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$this
->assertSession()
->pageTextContains('The strings have been saved.');
$url_bits = explode('?', $this
->getUrl());
$this
->assertEquals(Url::fromRoute('locale.translate_page', [], [
'absolute' => TRUE,
])
->toString(), $url_bits[0], 'Correct page redirection.');
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'translated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($translation);
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => $translation_to_en,
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'translated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($translation_to_en);
$this
->assertNotEquals($translation, $name);
$this
->assertEquals($translation, t($name, [], [
'langcode' => $langcode,
]), 't() works for non-English.');
$this->container
->get('string_translation')
->reset();
$this
->assertNotEquals($translation_to_en, $name);
$this
->assertEquals($translation_to_en, t($name, [], [
'langcode' => 'en',
]), 't() works for English.');
$this
->assertTrue(t($name, [], [
'langcode' => LanguageInterface::LANGCODE_SYSTEM,
]) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('No strings available.');
$this
->drupalLogout();
$this
->drupalGet('xx/user/login');
$this
->assertSession()
->pageTextContains('Enter the password that accompanies your username.');
$this
->drupalLogin($translate_user);
$search = [
'string' => 'accompanies your username',
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => 'Please enter your Llama username.',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$this
->drupalLogout();
$this
->drupalGet('xx/user/login');
$this
->assertSession()
->pageTextContains('Please enter your Llama username.');
$this
->drupalLogin($admin_user);
$path = 'admin/config/regional/language/delete/' . $langcode;
$this
->drupalGet($path);
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains("The {$name} ({$langcode}) language has been removed.");
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'translated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => '',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$this
->assertSession()
->responseContains($name);
$this
->drupalLogin($translate_user);
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextNotContains('No strings available.');
}
public function testJavaScriptTranslation() {
$user = $this
->drupalCreateUser([
'translate interface',
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($user);
$config = $this
->config('locale.settings');
$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->container
->get('language_manager')
->reset();
$query = Database::getConnection()
->select('locales_source', 's');
$query
->addJoin('INNER', 'locales_location', 'l', '[s].[lid] = [l].[lid]');
$source = $query
->fields('s', [
'source',
])
->condition('l.type', 'javascript')
->range(0, 1)
->execute()
->fetchField();
$search = [
'string' => $source,
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => $this
->randomMachineName(),
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
_locale_rebuild_js($langcode);
$locale_javascripts = \Drupal::state()
->get('locale.translation.javascript', []);
$js_file = 'public://' . $config
->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
$this
->assertFileExists($js_file);
\Drupal::service('file_system')
->delete($js_file);
$this
->assertFileDoesNotExist($js_file);
_locale_rebuild_js($langcode);
$this
->assertFileExists($js_file);
}
public function testStringValidation() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
'translate interface',
]);
$this
->drupalLogin($admin_user);
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$key = $this
->randomMachineName(16);
$bad_translations[$key] = "<script>alert('xss');</script>" . $key;
$key = $this
->randomMachineName(16);
$bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key;
$key = $this
->randomMachineName(16);
$bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key;
$key = $this
->randomMachineName(16);
$bad_translations[$key] = "<BODY ONLOAD=alert('xss')>" . $key;
$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');
t($name, [], [
'langcode' => $langcode,
])
->render();
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
foreach ($bad_translations as $translation) {
$edit = [
$lid => $translation,
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$this
->assertSession()
->elementAttributeContains('xpath', '//form[@id="locale-translate-edit-form"]//textarea', 'class', 'error');
$this
->assertSession()
->pageTextNotContains('The string has been saved.');
}
}
public function testStringSearch() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$translate_user = $this
->drupalCreateUser([
'translate interface',
'access administration pages',
]);
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$translation = $this
->randomMachineName(16);
$this
->drupalLogin($admin_user);
$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');
$edit = [
'predefined_langcode' => 'custom',
'langcode' => 'yy',
'label' => $this
->randomMachineName(16),
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
t($name, [], [
'langcode' => $langcode,
])
->render();
$this->container
->get('string_translation')
->reset();
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($name);
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'translated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('No strings available.');
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextNotContains('No strings available.');
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => $translation,
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$search = [
'string' => $translation,
'langcode' => $langcode,
'translation' => 'translated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextNotContains('No strings available.');
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('No strings available.');
$search = [
'string' => $translation,
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('No strings available.');
$search = [
'string' => $translation,
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextNotContains('No strings available.');
$search = [
'string' => $translation,
'langcode' => 'yy',
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('No strings available.');
$unavailable_string = $this
->randomMachineName(16);
$search = [
'string' => $unavailable_string,
'langcode' => $langcode,
'translation' => 'all',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('No strings available.');
}
public function testUICustomizedStrings() {
$user = $this
->drupalCreateUser([
'translate interface',
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($user);
ConfigurableLanguage::createFromLangcode('de')
->save();
$string = $this->container
->get('locale.storage')
->createString([
'source' => $this
->randomMachineName(100),
'context' => $this
->randomMachineName(20),
])
->save();
$translation = $this->container
->get('locale.storage')
->createTranslation([
'lid' => $string->lid,
'language' => 'de',
'translation' => $this
->randomMachineName(100),
'customized' => 0,
])
->save();
$this->container
->get('string_translation')
->reset();
$search = [
'string' => $string
->getString(),
'langcode' => 'de',
'translation' => 'translated',
'customized' => '0',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($translation
->getString());
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => $translation
->getString(),
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$search = [
'string' => $string
->getString(),
'langcode' => 'de',
'translation' => 'translated',
'customized' => '0',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($string
->getString());
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea
->getAttribute('name');
$edit = [
$lid => $this
->randomMachineName(100),
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$search = [
'string' => $string
->getString(),
'langcode' => 'de',
'translation' => 'translated',
'customized' => '1',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains($string
->getString());
}
}