You are here

TwigSanitizeTest.php in Twig Tools 8

File

tests/src/Unit/TwigSanitizeTest.php
View source
<?php

namespace Drupal\Tests\twig_tools\Unit;

use Drupal\Core\Template\Loader\StringLoader;
use Drupal\Tests\UnitTestCase;
use Drupal\twig_tools\TwigExtension\TwigSanitize;

/**
 * Tests to ensure sanitization filters work correctly.
 *
 * @group twig_tools
 *
 * @coversDefaultClass \Drupal\twig_tools\TwigExtension\TwigSanitize
 */
class TwigSanitizeTest extends UnitTestCase {

  /**
   * Create a new TwigExtension object.
   */
  public function setUp() {
    parent::setUp();
    $loader = new StringLoader();
    $this->twig = new \Twig_Environment($loader);
    $twigTools = new TwigSanitize();
    $this->twig
      ->addExtension($twigTools);
  }

  /**
   * @covers ::cleanClassArray
   *
   * @dataProvider providerTestCleanClassArrayValues
   */
  public function testCleanClassArray($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for providerTestCleanClassArrayValues.
   *
   * @return array
   *   An array of test data their cleaned class values.
   */
  public function providerTestCleanClassArrayValues() {
    return [
      [
        "{{-\n         [\n        'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789',\n        '¡¢£¤¥',\n        'css__identifier__with__double__underscores',\n        'invalid !\"#\$%&\\'()*+,./:;<=>?@[\\]^`{|}~ identifier',\n        'block__element--modifier',\n      ]|clean_class_array|join(' ') -}}",
        "abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-0123456789 ¡¢£¤¥ css__identifier__with__double__underscores invalid---identifier block__element--modifier",
      ],
    ];
  }

  /**
   * @covers ::arrayUnique
   *
   * @dataProvider providerTestArrayUniqueValues
   */
  public function testArrayUnique($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testArrayUnique.
   *
   * @return array
   *   An array of test data and their unique values.
   */
  public function providerTestArrayUniqueValues() {
    return [
      [
        "{{- [\n        '0',\n        '1',\n        '2',\n        '3',\n        '0',\n        '1',\n        NULL,\n        FALSE,\n        TRUE,\n        'Unique',\n        'Not Unique',\n        'Not Unique',\n        0,\n        1,\n        2,\n        0.0,\n        1.0,\n        2.0,\n      ]|array_unique|join(', ') -}}",
        "0, 1, 2, 3, , Unique, Not Unique",
      ],
    ];
  }

  /**
   * @covers ::removeEmpty
   *
   * @dataProvider providerTestRemoveEmptyValues
   */
  public function testRemoveEmpty($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for providerTestRemoveEmptyValues.
   *
   * @return array
   *   An array of test data and their falsy values.
   */
  public function providerTestRemoveEmptyValues() {
    return [
      [
        "{{-\n         [\n        '0',\n        '1',\n        '2',\n        '3',\n        '0',\n        '1',\n        0,\n        1,\n        2,\n        0.0,\n        1.0,\n        2.0,\n        FALSE,\n        TRUE,\n        NULL,\n        'Unique',\n        [],\n      ]|remove_empty|join(', ') -}}",
        "1, 2, 3, 1, 1, 2, 1, 2, 1, Unique",
      ],
    ];
  }

  /**
   * @covers ::scrubClassArray
   *
   * @dataProvider providerTestScrubClassArrayValues
   */
  public function testScrubClassArray($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testScrubClassArray.
   *
   * @return array
   *   An array of test data and their sanitized values.
   */
  public function providerTestScrubClassArrayValues() {
    return [
      [
        "{{-\n         [\n        'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789',\n        '¡¢£¤¥',\n        'css__identifier__with__double__underscores',\n        'invalid !\"#\$%&\\'()*+,./:;<=>?@[\\]^`{|}~ identifier',\n        'block__element--modifier',\n        'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789',\n        '¡¢£¤¥',\n        'css__identifier__with__double__underscores',\n        'invalid !\"#\$%&\\'()*+,./:;<=>?@[\\]^`{|}~ identifier',\n        'block__element--modifier',\n        '0',\n        '1',\n        '2',\n        '3',\n        '0',\n        '1',\n        NULL,\n        FALSE,\n        TRUE,\n        'Unique',\n        'Not Unique',\n        'Not Unique',\n        0,\n        1,\n        2,\n        0.0,\n        1.0,\n        2.0,\n        '0',\n        '1',\n        '2',\n        '3',\n        '0',\n        '1',\n        0,\n        1,\n        2,\n        0.0,\n        1.0,\n        2.0,\n        FALSE,\n        TRUE,\n        NULL,\n        'Unique',\n      ]|scrub_class_array|join(' ') -}}",
        "abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-0123456789 ¡¢£¤¥ css__identifier__with__double__underscores invalid---identifier block__element--modifier _ unique not-unique",
      ],
    ];
  }

  /**
   * Unset the test object.
   */
  public function tearDown() {
    unset($this->twigTools, $this->twig);
  }

}

Classes

Namesort descending Description
TwigSanitizeTest Tests to ensure sanitization filters work correctly.