You are here

public function TwigExtensionTest::testSafeStringEscaping in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php \Drupal\Tests\Core\Template\TwigExtensionTest::testSafeStringEscaping()
  2. 10 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php \Drupal\Tests\Core\Template\TwigExtensionTest::testSafeStringEscaping()

Tests the escaping of objects implementing MarkupInterface.

@covers ::escapeFilter

File

core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php, line 222

Class

TwigExtensionTest
Tests the twig extension.

Namespace

Drupal\Tests\Core\Template

Code

public function testSafeStringEscaping() {
  $loader = new FilesystemLoader();
  $twig = new Environment($loader, [
    'debug' => TRUE,
    'cache' => FALSE,
    'autoescape' => 'html',
    'optimizations' => 0,
  ]);

  // By default, TwigExtension will attempt to cast objects to strings.
  // Ensure objects that implement MarkupInterface are unchanged.
  $safe_string = $this
    ->createMock('\\Drupal\\Component\\Render\\MarkupInterface');
  $this
    ->assertSame($safe_string, $this->systemUnderTest
    ->escapeFilter($twig, $safe_string, 'html', 'UTF-8', TRUE));

  // Ensure objects that do not implement MarkupInterface are escaped.
  $string_object = new TwigExtensionTestString("<script>alert('here');</script>");
  $this
    ->assertSame('&lt;script&gt;alert(&#039;here&#039;);&lt;/script&gt;', $this->systemUnderTest
    ->escapeFilter($twig, $string_object, 'html', 'UTF-8', TRUE));
}