You are here

class TwigExtensionTest in Zircon Profile 8

Same name in this branch
  1. 8 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php \Drupal\Tests\Core\Template\TwigExtensionTest
  2. 8 core/modules/system/src/Tests/Theme/TwigExtensionTest.php \Drupal\system\Tests\Theme\TwigExtensionTest
Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php \Drupal\Tests\Core\Template\TwigExtensionTest

Tests the twig extension.

@group Template

@coversDefaultClass \Drupal\Core\Template\TwigExtension

Hierarchy

Expanded class hierarchy of TwigExtensionTest

File

core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php, line 25
Contains \Drupal\Tests\Core\Template\TwigExtensionTest.

Namespace

Drupal\Tests\Core\Template
View source
class TwigExtensionTest extends UnitTestCase {

  /**
   * Tests the escaping
   *
   * @dataProvider providerTestEscaping
   */
  public function testEscaping($template, $expected) {
    $renderer = $this
      ->getMock('\\Drupal\\Core\\Render\\RendererInterface');
    $twig = new \Twig_Environment(NULL, array(
      'debug' => TRUE,
      'cache' => FALSE,
      'autoescape' => 'html',
      'optimizations' => 0,
    ));
    $twig
      ->addExtension((new TwigExtension($renderer))
      ->setUrlGenerator($this
      ->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface')));
    $nodes = $twig
      ->parse($twig
      ->tokenize($template));
    $this
      ->assertSame($expected, $nodes
      ->getNode('body')
      ->getNode(0)
      ->getNode('expr') instanceof \Twig_Node_Expression_Filter);
  }

  /**
   * Provides tests data for testEscaping
   *
   * @return array
   *   An array of test data each containing of a twig template string and
   *   a boolean expecting whether the path will be safe.
   */
  public function providerTestEscaping() {
    return array(
      array(
        '{{ path("foo") }}',
        FALSE,
      ),
      array(
        '{{ path("foo", {}) }}',
        FALSE,
      ),
      array(
        '{{ path("foo", { foo: "foo" }) }}',
        FALSE,
      ),
      array(
        '{{ path("foo", foo) }}',
        TRUE,
      ),
      array(
        '{{ path("foo", { foo: foo }) }}',
        TRUE,
      ),
      array(
        '{{ path("foo", { foo: ["foo", "bar"] }) }}',
        TRUE,
      ),
      array(
        '{{ path("foo", { foo: "foo", bar: "bar" }) }}',
        TRUE,
      ),
      array(
        '{{ path(name = "foo", parameters = {}) }}',
        FALSE,
      ),
      array(
        '{{ path(name = "foo", parameters = { foo: "foo" }) }}',
        FALSE,
      ),
      array(
        '{{ path(name = "foo", parameters = foo) }}',
        TRUE,
      ),
      array(
        '{{ path(name = "foo", parameters = { foo: ["foo", "bar"] }) }}',
        TRUE,
      ),
      array(
        '{{ path(name = "foo", parameters = { foo: foo }) }}',
        TRUE,
      ),
      array(
        '{{ path(name = "foo", parameters = { foo: "foo", bar: "bar" }) }}',
        TRUE,
      ),
    );
  }

  /**
   * Tests the active_theme function.
   */
  public function testActiveTheme() {
    $renderer = $this
      ->getMock('\\Drupal\\Core\\Render\\RendererInterface');
    $extension = new TwigExtension($renderer);
    $theme_manager = $this
      ->getMock('\\Drupal\\Core\\Theme\\ThemeManagerInterface');
    $active_theme = $this
      ->getMockBuilder('\\Drupal\\Core\\Theme\\ActiveTheme')
      ->disableOriginalConstructor()
      ->getMock();
    $active_theme
      ->expects($this
      ->once())
      ->method('getName')
      ->willReturn('test_theme');
    $theme_manager
      ->expects($this
      ->once())
      ->method('getActiveTheme')
      ->willReturn($active_theme);
    $extension
      ->setThemeManager($theme_manager);
    $loader = new \Twig_Loader_String();
    $twig = new \Twig_Environment($loader);
    $twig
      ->addExtension($extension);
    $result = $twig
      ->render('{{ active_theme() }}');
    $this
      ->assertEquals('test_theme', $result);
  }

  /**
   * Tests the format_date filter.
   */
  public function testFormatDate() {
    $date_formatter = $this
      ->getMockBuilder('\\Drupal\\Core\\Datetime\\DateFormatter')
      ->disableOriginalConstructor()
      ->getMock();
    $date_formatter
      ->expects($this
      ->exactly(2))
      ->method('format')
      ->willReturn('1978-11-19');
    $renderer = $this
      ->getMock('\\Drupal\\Core\\Render\\RendererInterface');
    $extension = new TwigExtension($renderer);
    $extension
      ->setDateFormatter($date_formatter);
    $loader = new StringLoader();
    $twig = new \Twig_Environment($loader);
    $twig
      ->addExtension($extension);
    $result = $twig
      ->render('{{ time|format_date("html_date") }}');
    $this
      ->assertEquals($date_formatter
      ->format('html_date'), $result);
  }

  /**
   * Tests the active_theme_path function.
   */
  public function testActiveThemePath() {
    $renderer = $this
      ->getMock('\\Drupal\\Core\\Render\\RendererInterface');
    $extension = new TwigExtension($renderer);
    $theme_manager = $this
      ->getMock('\\Drupal\\Core\\Theme\\ThemeManagerInterface');
    $active_theme = $this
      ->getMockBuilder('\\Drupal\\Core\\Theme\\ActiveTheme')
      ->disableOriginalConstructor()
      ->getMock();
    $active_theme
      ->expects($this
      ->once())
      ->method('getPath')
      ->willReturn('foo/bar');
    $theme_manager
      ->expects($this
      ->once())
      ->method('getActiveTheme')
      ->willReturn($active_theme);
    $extension
      ->setThemeManager($theme_manager);
    $loader = new \Twig_Loader_String();
    $twig = new \Twig_Environment($loader);
    $twig
      ->addExtension($extension);
    $result = $twig
      ->render('{{ active_theme_path() }}');
    $this
      ->assertEquals('foo/bar', $result);
  }

  /**
   * Tests the escaping of objects implementing MarkupInterface.
   *
   * @covers ::escapeFilter
   */
  public function testSafeStringEscaping() {
    $renderer = $this
      ->getMock('\\Drupal\\Core\\Render\\RendererInterface');
    $twig = new \Twig_Environment(NULL, array(
      'debug' => TRUE,
      'cache' => FALSE,
      'autoescape' => 'html',
      'optimizations' => 0,
    ));
    $twig_extension = new TwigExtension($renderer);

    // By default, TwigExtension will attempt to cast objects to strings.
    // Ensure objects that implement MarkupInterface are unchanged.
    $safe_string = $this
      ->getMock('\\Drupal\\Component\\Render\\MarkupInterface');
    $this
      ->assertSame($safe_string, $twig_extension
      ->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;', $twig_extension
      ->escapeFilter($twig, $string_object, 'html', 'UTF-8', TRUE));
  }

  /**
   * @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().
    $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);
  }

  /**
   * @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));
  }
  public function providerTestRenderVar() {
    $data = [];
    $renderable = $this
      ->prophesize(RenderableInterface::class);
    $render_array = [
      '#type' => 'test',
      '#var' => 'giraffe',
    ];
    $renderable
      ->toRenderable()
      ->willReturn($render_array);
    $data['renderable'] = [
      $render_array,
      $renderable
        ->reveal(),
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwigExtensionTest::providerTestEscaping public function Provides tests data for testEscaping
TwigExtensionTest::providerTestRenderVar public function
TwigExtensionTest::testActiveTheme public function Tests the active_theme function.
TwigExtensionTest::testActiveThemePath public function Tests the active_theme_path function.
TwigExtensionTest::testEscaping public function Tests the escaping
TwigExtensionTest::testFormatDate public function Tests the format_date filter.
TwigExtensionTest::testRenderVar public function @dataProvider providerTestRenderVar
TwigExtensionTest::testSafeJoin public function @covers ::safeJoin
TwigExtensionTest::testSafeStringEscaping public function Tests the escaping of objects implementing MarkupInterface.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 259