View source
<?php
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
use Twig\Error\SyntaxError;
class TwigTransTest extends BrowserTestBase {
protected static $modules = [
'theme_test',
'twig_theme_test',
'locale',
'language',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $languages = [
'xx' => 'Lolspeak',
'zz' => 'Lolspeak2',
];
protected function setUp() : void {
parent::setUp();
\Drupal::service('theme_installer')
->install([
'test_theme',
]);
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
'administer site configuration',
'translate interface',
]);
$this
->drupalLogin($this->adminUser);
$this
->installLanguages();
$this
->config('system.site')
->set('default_langcode', 'xx')
->save();
$this
->rebuildContainer();
$this
->assertEquals('xx', \Drupal::languageManager()
->getDefaultLanguage()
->getId(), 'Lolspeak is the default language');
}
public function testTwigTransTags() {
$this
->drupalGet('twig-theme-test/trans', [
'language' => \Drupal::languageManager()
->getLanguage('xx'),
]);
$this
->assertTwigTransTags();
$parameters = $this->container
->getParameter('twig.config');
$parameters['debug'] = TRUE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->resetAll();
$this
->drupalGet('twig-theme-test/trans', [
'language' => \Drupal::languageManager()
->getLanguage('xx'),
]);
$this
->assertTwigTransTags();
}
public function testEmptyTwigTransTags() {
$elements = [
'#type' => 'inline_template',
'#template' => '{% trans %}{% endtrans %}',
];
$renderer = \Drupal::service('renderer');
try {
$renderer
->renderPlain($elements);
$this
->fail('{% trans %}{% endtrans %} did not throw an exception.');
} catch (SyntaxError $e) {
$this
->assertStringContainsString('{% trans %} tag cannot be empty', $e
->getMessage());
} catch (\Exception $e) {
$this
->fail('{% trans %}{% endtrans %} threw an unexpected exception.');
}
}
protected function assertTwigTransTags() {
$this
->assertSession()
->pageTextContains('OH HAI SUNZ');
$this
->assertSession()
->pageTextContains('O HAI SUNZZZZZZZ');
$this
->assertSession()
->pageTextContains('O HERRO ERRRF.');
$this
->assertSession()
->pageTextContains('OH HAI TEH MUUN');
$this
->assertSession()
->pageTextContains('O HAI STARRRRR');
$this
->assertSession()
->pageTextContains('O HAI 2 STARZZZZ');
$this
->assertSession()
->responseContains('ESCAPEE: &"<>');
$this
->assertSession()
->responseContains('PLAYSHOLDR: <em class="placeholder">&"<></em>');
$this
->assertSession()
->responseContains('DIS complex token HAZ LENGTH OV: 3. IT CONTAYNZ: <em class="placeholder">12345</em> AN &"<>.');
$this
->assertSession()
->pageTextContains('I have context.');
$this
->assertSession()
->pageTextContains('I HAZ KONTEX.');
$this
->assertSession()
->pageTextContains('O HAI NU TXT.');
$this
->assertSession()
->pageTextContains('O HAI NU TXTZZZZ.');
$this
->assertSession()
->pageTextNotContains(pi());
}
protected function installLanguages() {
$file_system = \Drupal::service('file_system');
foreach ($this->languages as $langcode => $name) {
$contents = $this
->poFileContents($langcode);
if ($contents) {
$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()
->responseContains('"edit-languages-' . $langcode . '-weight"');
$filename = $file_system
->tempnam('temporary://', "po_") . '.po';
file_put_contents($filename, $contents);
$options = [
'files[file]' => $filename,
'langcode' => $langcode,
'customized' => TRUE,
];
$this
->drupalGet('admin/config/regional/translate/import');
$this
->submitForm($options, 'Import');
$file_system
->unlink($filename);
}
}
$this->container
->get('language_manager')
->reset();
}
protected function poFileContents($langcode) {
if ($langcode === 'xx') {
return <<<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"
msgid "Hello sun."
msgstr "OH HAI SUNZ"
msgctxt "Lolspeak"
msgid "Hello sun."
msgstr "O HAI SUNZZZZZZZ"
msgid "Hello Earth."
msgstr "O HERRO ERRRF."
msgid "Hello moon."
msgstr "OH HAI TEH MUUN"
msgid "Hello star."
msgid_plural "Hello @count stars."
msgstr[0] "O HAI STARRRRR"
msgstr[1] "O HAI @count STARZZZZ"
msgid "Escaped: @string"
msgstr "ESCAPEE: @string"
msgid "Placeholder: %string"
msgstr "PLAYSHOLDR: %string"
msgid "This @token.name has a length of: @count. It contains: %token.numbers and @token.bad_text."
msgstr "DIS @token.name HAZ LENGTH OV: @count. IT CONTAYNZ: %token.numbers AN @token.bad_text."
msgctxt "Lolspeak"
msgid "I have context."
msgstr "I HAZ KONTEX."
EOF;
}
elseif ($langcode === 'zz') {
return <<<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"
msgid "Hello new text."
msgstr "O HAI NU TXT."
msgctxt "Lolspeak"
msgid "Hello new text."
msgstr "O HAI NU TXTZZZZ."
EOF;
}
return FALSE;
}
}