/** * 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('<script>alert('here');</script>', $twig_extension->escapeFilter($twig, $string_object, 'html', 'UTF-8', TRUE)); }
/** * @covers ::renderVar * @covers ::bubbleArgMetadata */ public function testRenderVarWithGeneratedLink() { $renderer = $this->prophesize(RendererInterface::class); $twig_extension = new TwigExtension($renderer->reveal()); $link = new GeneratedLink(); $link->setGeneratedLink('<a href="http://example.com"></a>'); $link->addCacheTags(['foo']); $link->addAttachments(['library' => ['system/base']]); $result = $twig_extension->renderVar($link); $renderer->render(["#cache" => ["contexts" => [], "tags" => ["foo"], "max-age" => -1], "#attached" => ['library' => ['system/base']]])->shouldHaveBeenCalled(); $this->assertEquals('<a href="http://example.com"></a>', $result); }
/** * @dataProvider providerTestRenderVar */ public function testRenderVar($result, $input) { $renderer = $this->prophesize(RendererInterface::class); $renderer->render($result += ['#printed' => FALSE])->willReturn('Rendered output'); $renderer = $renderer->reveal(); $twig_extension = new TwigExtension($renderer); $this->assertEquals('Rendered output', $twig_extension->renderVar($input)); }
/** * @covers ::safeJoin */ 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(). $string = '<em>will be markup</em>'; SafeMarkup::setMultiple([$string => ['html' => TRUE]]); $items = ['<em>will be escaped</em>', $string, ['#markup' => '<strong>will be rendered</strong>']]; $result = $twig_extension->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); }