You are here

public function TwigExtensionTest::testSafeJoin in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 183
Contains \Drupal\Tests\Core\Template\TwigExtensionTest.

Class

TwigExtensionTest
Tests the twig extension.

Namespace

Drupal\Tests\Core\Template

Code

public function testSafeJoin() {
  $renderer = $this
    ->prophesize(RendererInterface::class);
  $renderer
    ->render([
    '#markup' => '<strong>will be rendered</strong>',
    '#printed' => FALSE,
  ])
    ->willReturn('<strong>will be rendered</strong>');
  $renderer = $renderer
    ->reveal();
  $twig_extension = new TwigExtension($renderer);
  $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 = $twig_extension
    ->safeJoin($twig_environment, $items, '<br/>');
  $this
    ->assertEquals('&lt;em&gt;will be escaped&lt;/em&gt;<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 = $twig_extension
    ->safeJoin($twig_environment, $items, ', ');
  $this
    ->assertEquals('&lt;em&gt;will be escaped&lt;/em&gt;, <em>will be markup</em>, <strong>will be rendered</strong>', $result);

  // Ensure safe_join Twig filter supports empty variables.
  $items = NULL;
  $result = $twig_extension
    ->safeJoin($twig_environment, $items, '<br>');
  $this
    ->assertEmpty($result);
}