class TwigMarkupInterfaceTest in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest
- 9 core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest
Tests Twig with MarkupInterface objects.
@group Theme
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \PHPUnit\Framework\TestCase implements ServiceProviderInterface uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, AssertContentTrait, ConfigTestTrait, ExtensionListTestTrait, RandomGeneratorTrait, TestRequirementsTrait, PhpUnitWarnings
- class \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest
Expanded class hierarchy of TwigMarkupInterfaceTest
File
- core/
tests/ Drupal/ KernelTests/ Core/ Theme/ TwigMarkupInterfaceTest.php, line 25 - Contains \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest.
Namespace
Drupal\KernelTests\Core\ThemeView source
class TwigMarkupInterfaceTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'language',
];
/**
* @dataProvider providerTestMarkupInterfaceEmpty
*/
public function testMarkupInterfaceEmpty($expected, $variable) {
$this
->assertEquals($expected, $this
->renderObjectWithTwig($variable));
}
/**
* Provide test examples.
*/
public function providerTestMarkupInterfaceEmpty() {
return [
// The first argument to \Drupal\Core\StringTranslation\TranslatableMarkup
// is not supposed to be an empty string.
// phpcs:ignore Drupal.Semantics.FunctionT.EmptyString
'empty TranslatableMarkup' => [
'',
new TranslatableMarkup(''),
],
'non-empty TranslatableMarkup' => [
'<span>test</span>',
new TranslatableMarkup('test'),
],
'empty FormattableMarkup' => [
'',
new FormattableMarkup('', [
'@foo' => 'bar',
]),
],
'non-empty FormattableMarkup' => [
'<span>bar</span>',
new FormattableMarkup('@foo', [
'@foo' => 'bar',
]),
],
'non-empty Markup' => [
'<span>test</span>',
Markup::create('test'),
],
'empty GeneratedLink' => [
'',
new GeneratedLink(),
],
'non-empty GeneratedLink' => [
'<span><a hef="http://www.example.com">test</a></span>',
(new GeneratedLink())
->setGeneratedLink('<a hef="http://www.example.com">test</a>'),
],
// Test objects that do not implement \Countable.
'empty SafeMarkupTestMarkup' => [
'',
SafeMarkupTestMarkup::create(''),
],
'non-empty SafeMarkupTestMarkup' => [
'<span>test</span>',
SafeMarkupTestMarkup::create('test'),
],
];
}
/**
* Tests behavior if a string is translated to become an empty string.
*/
public function testEmptyTranslation() {
$settings = Settings::getAll();
$settings['locale_custom_strings_en'] = [
'' => [
'test' => '',
],
];
// Recreate the settings static.
new Settings($settings);
$variable = new TranslatableMarkup('test');
$this
->assertEquals('', $this
->renderObjectWithTwig($variable));
$variable = new TranslatableMarkup('test', [], [
'langcode' => 'de',
]);
$this
->assertEquals('<span>test</span>', $this
->renderObjectWithTwig($variable));
}
/**
* @return \Drupal\Component\Render\MarkupInterface
* The rendered HTML.
*/
protected function renderObjectWithTwig($variable) {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$context = new RenderContext();
return $renderer
->executeInRenderContext($context, function () use ($renderer, $variable) {
$elements = [
'#type' => 'inline_template',
'#template' => '{%- if variable is not empty -%}<span>{{ variable }}</span>{%- endif -%}',
'#context' => [
'variable' => $variable,
],
];
return $renderer
->render($elements);
});
}
}