function TwigExtensionTest::testSafeJoin
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php \Drupal\Tests\Core\Template\TwigExtensionTest::testSafeJoin()
- 8.9.x core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php \Drupal\Tests\Core\Template\TwigExtensionTest::testSafeJoin()
- 10 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php \Drupal\Tests\Core\Template\TwigExtensionTest::testSafeJoin()
@covers ::safeJoin
File
-
core/
tests/ Drupal/ Tests/ Core/ Template/ TwigExtensionTest.php, line 252
Class
- TwigExtensionTest
- Tests the twig extension.
Namespace
Drupal\Tests\Core\TemplateCode
public function testSafeJoin() : void {
$this->renderer
->expects($this->any())
->method('render')
->with([
'#markup' => '<strong>will be rendered</strong>',
'#printed' => FALSE,
])
->willReturn('<strong>will be rendered</strong>');
$twig_environment = $this->prophesize(TwigEnvironment::class)
->reveal();
// Simulate t().
$markup = $this->prophesize(TranslatableMarkup::class);
$markup->__toString()
->willReturn('<em>will be markup</em>');
$markup = $markup->reveal();
$items = [
'<em>will be escaped</em>',
$markup,
[
'#markup' => '<strong>will be rendered</strong>',
],
];
$result = $this->systemUnderTest
->safeJoin($twig_environment, $items, '<br/>');
$this->assertEquals('<em>will be escaped</em><br/><em>will be markup</em><br/><strong>will be rendered</strong>', $result);
// Ensure safe_join Twig filter supports Traversable variables.
$items = new \ArrayObject([
'<em>will be escaped</em>',
$markup,
[
'#markup' => '<strong>will be rendered</strong>',
],
]);
$result = $this->systemUnderTest
->safeJoin($twig_environment, $items, ', ');
$this->assertEquals('<em>will be escaped</em>, <em>will be markup</em>, <strong>will be rendered</strong>', $result);
// Ensure safe_join Twig filter supports empty variables.
$items = NULL;
$result = $this->systemUnderTest
->safeJoin($twig_environment, $items, '<br>');
$this->assertEmpty($result);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.