/**
  * Tests the escaping of objects implementing SafeStringInterface.
  *
  * @covers ::escapeFilter
  */
 public function testSafeStringEscaping()
 {
     $renderer = $this->getMock('\\Drupal\\Core\\Render\\RendererInterface');
     $twig = new \Twig_Environment(NULL, array('debug' => TRUE, 'cache' => FALSE, 'autoescape' => TRUE, 'optimizations' => 0));
     $twig_extension = new TwigExtension($renderer);
     // By default, TwigExtension will attempt to cast objects to strings.
     // Ensure objects that implement SafeStringInterface are unchanged.
     $safe_string = $this->getMock('\\Drupal\\Component\\Utility\\SafeStringInterface');
     $this->assertSame($safe_string, $twig_extension->escapeFilter($twig, $safe_string, 'html', 'UTF-8', TRUE));
     // Ensure objects that do not implement SafeStringInterface are escaped.
     $string_object = new TwigExtensionTestString("<script>alert('here');</script>");
     $this->assertSame('&lt;script&gt;alert(&#039;here&#039;);&lt;/script&gt;', $twig_extension->escapeFilter($twig, $string_object, 'html', 'UTF-8', TRUE));
 }
Esempio n. 2
0
 /**
  * @covers ::escapeFilter
  * @covers ::bubbleArgMetadata
  */
 public function testEscapeWithGeneratedLink()
 {
     $renderer = $this->prophesize(RendererInterface::class);
     $twig = new \Twig_Environment(NULL, ['debug' => TRUE, 'cache' => FALSE, 'autoescape' => 'html', 'optimizations' => 0]);
     $twig_extension = new TwigExtension($renderer->reveal());
     $twig->addExtension($twig_extension->setUrlGenerator($this->prophesize(UrlGeneratorInterface::class)->reveal()));
     $link = new GeneratedLink();
     $link->setGeneratedLink('<a href="http://example.com"></a>');
     $link->addCacheTags(['foo']);
     $link->addAttachments(['library' => ['system/base']]);
     $result = $twig_extension->escapeFilter($twig, $link, 'html', NULL, TRUE);
     $renderer->render(["#cache" => ["contexts" => [], "tags" => ["foo"], "max-age" => -1], "#attached" => ['library' => ['system/base']]])->shouldHaveBeenCalled();
     $this->assertEquals('<a href="http://example.com"></a>', $result);
 }