View source
<?php
namespace Drupal\locale\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Component\Utility\SafeMarkup;
class LocaleTranslationUiTest extends WebTestBase {
public static $modules = array(
'locale',
);
public function testEnglishTranslation() {
$admin_user = $this
->drupalCreateUser(array(
'administer languages',
'access administration pages',
));
$this
->drupalLogin($admin_user);
$this
->drupalPostForm('admin/config/regional/language/edit/en', array(
'locale_translate_english' => TRUE,
), t('Save language'));
$this
->assertLinkByHref('/admin/config/regional/translate?langcode=en', 0, 'Enabled interface translation to English.');
}
public function testStringTranslation() {
$admin_user = $this
->drupalCreateUser(array(
'administer languages',
'access administration pages',
));
$translate_user = $this
->drupalCreateUser(array(
'translate interface',
'access administration pages',
));
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$translation = $this
->randomMachineName(16);
$translation_to_en = $this
->randomMachineName(16);
$this
->drupalLogin($admin_user);
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
t($name, array(), array(
'langcode' => $langcode,
))
->render();
$this->container
->get('string_translation')
->reset();
$this
->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
$this
->assertText(t($name), 'Test language added.');
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$search = array(
'string' => $name,
'langcode' => $langcode,
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText($name, 'Search found the string as untranslated.');
$this
->assertText($name, 'name found on edit screen.');
$this
->assertNoOption('edit-langcode', 'en', 'No way to translate the string to English.');
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
$this
->drupalPostForm('admin/config/regional/language/edit/en', array(
'locale_translate_english' => TRUE,
), t('Save language'));
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText($name, 'Search found the string as untranslated.');
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => $translation,
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this
->assertText(t('The strings have been saved.'), 'The strings have been saved.');
$url_bits = explode('?', $this
->getUrl());
$this
->assertEqual($url_bits[0], \Drupal::url('locale.translate_page', array(), array(
'absolute' => TRUE,
)), 'Correct page redirection.');
$search = array(
'string' => $name,
'langcode' => $langcode,
'translation' => 'translated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertRaw($translation, 'Non-English translation properly saved.');
$search = array(
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => $translation_to_en,
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$search = array(
'string' => $name,
'langcode' => 'en',
'translation' => 'translated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertRaw($translation_to_en, 'English translation properly saved.');
$this
->assertTrue($name != $translation && t($name, array(), array(
'langcode' => $langcode,
)) == $translation, 't() works for non-English.');
$this->container
->get('string_translation')
->reset();
$this
->assertTrue($name != $translation_to_en && t($name, array(), array(
'langcode' => 'en',
)) == $translation_to_en, 't() works for English.');
$this
->assertTrue(t($name, array(), array(
'langcode' => LanguageInterface::LANGCODE_SYSTEM,
)) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
$search = array(
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText(t('No strings available.'), 'String is translated.');
$this
->drupalLogout();
$this
->drupalGet('xx/user/login');
$this
->assertText('Enter the password that accompanies your username.');
$this
->drupalLogin($translate_user);
$search = array(
'string' => 'accompanies your username',
'langcode' => $langcode,
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => 'Please enter your Llama username.',
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this
->drupalLogout();
$this
->drupalGet('xx/user/login');
$this
->assertText('Please enter your Llama username.');
$this
->drupalLogin($admin_user);
$path = 'admin/config/regional/language/delete/' . $langcode;
$this
->drupalPostForm($path, array(), t('Delete'));
$t_args = array(
'%language' => $name,
'%langcode' => $langcode,
);
$this
->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The test language has been removed.');
$this
->drupalGet($path);
$this
->assertResponse(404, 'Language no longer found.');
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$search = array(
'string' => $name,
'langcode' => 'en',
'translation' => 'translated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => '',
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this
->assertRaw($name, 'The strings have been saved.');
$this
->drupalLogin($translate_user);
$search = array(
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertNoText(t('No strings available.'), 'The translation has been removed');
}
public function testJavaScriptTranslation() {
$user = $this
->drupalCreateUser(array(
'translate interface',
'administer languages',
'access administration pages',
));
$this
->drupalLogin($user);
$config = $this
->config('locale.settings');
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
$this->container
->get('language_manager')
->reset();
$query = db_select('locales_source', 's');
$query
->addJoin('INNER', 'locales_location', 'l', 's.lid = l.lid');
$source = $query
->fields('s', array(
'source',
))
->condition('l.type', 'javascript')
->range(0, 1)
->execute()
->fetchField();
$search = array(
'string' => $source,
'langcode' => $langcode,
'translation' => 'all',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => $this
->randomMachineName(),
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
_locale_rebuild_js($langcode);
$locale_javascripts = \Drupal::state()
->get('locale.translation.javascript') ?: array();
$js_file = 'public://' . $config
->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
$this
->assertTrue($result = file_exists($js_file), SafeMarkup::format('JavaScript file created: %file', array(
'%file' => $result ? $js_file : 'not found',
)));
file_unmanaged_delete($js_file);
$this
->assertTrue($result = !file_exists($js_file), SafeMarkup::format('JavaScript file deleted: %file', array(
'%file' => $result ? $js_file : 'found',
)));
_locale_rebuild_js($langcode);
$this
->assertTrue($result = file_exists($js_file), SafeMarkup::format('JavaScript file rebuilt: %file', array(
'%file' => $result ? $js_file : 'not found',
)));
}
public function testStringValidation() {
$admin_user = $this
->drupalCreateUser(array(
'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 = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
t($name, array(), array(
'langcode' => $langcode,
))
->render();
$search = array(
'string' => $name,
'langcode' => $langcode,
'translation' => 'all',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
foreach ($bad_translations as $translation) {
$edit = array(
$lid => $translation,
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$form_class = $this
->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class');
$this
->assertNotIdentical(FALSE, strpos($form_class[0], 'error'), 'The string was rejected as unsafe.');
$this
->assertNoText(t('The string has been saved.'), 'The string was not saved.');
}
}
public function testStringSearch() {
$admin_user = $this
->drupalCreateUser(array(
'administer languages',
'access administration pages',
));
$translate_user = $this
->drupalCreateUser(array(
'translate interface',
'access administration pages',
));
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$translation = $this
->randomMachineName(16);
$this
->drupalLogin($admin_user);
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => 'yy',
'label' => $this
->randomMachineName(16),
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
t($name, array(), array(
'langcode' => $langcode,
))
->render();
$this->container
->get('string_translation')
->reset();
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$search = array(
'string' => $name,
'langcode' => $langcode,
'translation' => 'all',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText($name, 'Search found the string.');
$search = array(
'string' => $name,
'langcode' => $langcode,
'translation' => 'translated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText(t('No strings available.'), "Search didn't find the string.");
$search = array(
'string' => $name,
'langcode' => $langcode,
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertNoText(t('No strings available.'), 'Search found the string.');
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => $translation,
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$search = array(
'string' => $translation,
'langcode' => $langcode,
'translation' => 'translated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertNoText(t('No strings available.'), 'Search found the translation.');
$search = array(
'string' => $name,
'langcode' => $langcode,
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText(t('No strings available.'), "Search didn't find the source string.");
$search = array(
'string' => $translation,
'langcode' => $langcode,
'translation' => 'untranslated',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText(t('No strings available.'), "Search didn't find the translation.");
$search = array(
'string' => $translation,
'langcode' => $langcode,
'translation' => 'all',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertNoText(t('No strings available.'), 'Search found the translation.');
$search = array(
'string' => $translation,
'langcode' => 'yy',
'translation' => 'all',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText(t('No strings available.'), "Search didn't find the translation.");
$unavailable_string = $this
->randomMachineName(16);
$search = array(
'string' => $unavailable_string,
'langcode' => $langcode,
'translation' => 'all',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText(t('No strings available.'), "Search didn't find the invalid string.");
}
public function testUICustomizedStrings() {
$user = $this
->drupalCreateUser(array(
'translate interface',
'administer languages',
'access administration pages',
));
$this
->drupalLogin($user);
ConfigurableLanguage::createFromLangcode('de')
->save();
$string = $this->container
->get('locale.storage')
->createString(array(
'source' => $this
->randomMachineName(100),
'context' => $this
->randomMachineName(20),
))
->save();
$translation = $this->container
->get('locale.storage')
->createTranslation(array(
'lid' => $string->lid,
'language' => 'de',
'translation' => $this
->randomMachineName(100),
'customized' => 0,
))
->save();
$this->container
->get('string_translation')
->reset();
$search = array(
'string' => $string
->getString(),
'langcode' => 'de',
'translation' => 'translated',
'customized' => '0',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText($translation
->getString(), 'Translation is found in search result.');
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => $translation
->getString(),
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$search = array(
'string' => $string
->getString(),
'langcode' => 'de',
'translation' => 'translated',
'customized' => '0',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText($string
->getString(), 'Translation is not marked as customized.');
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => $this
->randomMachineName(100),
);
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$search = array(
'string' => $string
->getString(),
'langcode' => 'de',
'translation' => 'translated',
'customized' => '1',
);
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertText($string
->getString(), "Translation is marked as customized.");
}
}