Browse Source

Issue #3040238 by Peter Törnstrand: Add a test for langcode option

merge-requests/4/head
Chi 6 years ago
parent
commit
5df42502d6
  1. 4
      src/TwigExtension.php
  2. 12
      tests/src/Functional/TwigTweakTest.php
  3. 5
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

4
src/TwigExtension.php

@ -509,8 +509,8 @@ class TwigExtension extends \Twig_Extension {
*/
public function drupalUrl($user_input, array $options = [], $check_access = FALSE) {
if (isset($options['langcode'])) {
$lang_manager = \Drupal::languageManager();
if ($language = $lang_manager->getLanguage($options['langcode'])) {
$language_manager = \Drupal::languageManager();
if ($language = $language_manager->getLanguage($options['langcode'])) {
$options['language'] = $language;
}
}

12
tests/src/Functional/TwigTweakTest.php

@ -5,6 +5,7 @@ namespace Drupal\Tests\twig_tweak\Functional;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Drupal\Core\Render\Markup;
use Drupal\Tests\BrowserTestBase;
@ -28,6 +29,7 @@ class TwigTweakTest extends BrowserTestBase {
'block',
'image',
'responsive_image',
'language',
];
/**
@ -55,6 +57,9 @@ class TwigTweakTest extends BrowserTestBase {
'breakpoint_group' => 'responsive_image',
])->save();
// Setup Russian.
ConfigurableLanguage::createFromLangcode('ru')->save();
}
/**
@ -214,7 +219,12 @@ class TwigTweakTest extends BrowserTestBase {
// Test URL.
$url = Url::fromUserInput('/node/1', ['absolute' => TRUE])->toString();
$xpath = sprintf('//div[@class = "tt-url" and text() = "%s"]', $url);
$xpath = sprintf('//div[@class = "tt-url"]/div[@data-case="default" and text() = "%s"]', $url);
$this->assertByXpath($xpath);
// Test URL (with langcode).
$url = str_replace('node/1', 'ru/node/1', $url);
$xpath = sprintf('//div[@class = "tt-url"]/div[@data-case="with-langcode" and text() = "%s"]', $url);
$this->assertByXpath($xpath);
// Test link.

5
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -43,7 +43,10 @@
<div class="tt-token-data">{{ drupal_token('node:title', {'node': node}) }}</div>
<div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div>
<div class="tt-title">{{ drupal_title() }}</div>
<div class="tt-url">{{ drupal_url('node/1', {absolute: true}) }}</div>
<div class="tt-url">
<div data-case="default">{{ drupal_url('node/1', {absolute: true}) }}</div>
<div data-case="with-langcode">{{ drupal_url('node/1', {absolute: true, langcode: 'ru'}) }}</div>
</div>
<div class="tt-link">{{ drupal_link('Edit', 'node/1/edit', {absolute: true}) }}</div>
<div class="tt-link-html">{% set link_text %}<b>Edit</b>{% endset %}{{ drupal_link(link_text, 'node/1/edit', {absolute: true}) }}</div>
<div class="tt-messages">{{ drupal_messages() }}</div>

Loading…
Cancel
Save