You are here

class TwigSanitizeTest in Twig Tools 8

Tests to ensure sanitization filters work correctly.

@group twig_tools

@coversDefaultClass \Drupal\twig_tools\TwigExtension\TwigSanitize

Hierarchy

Expanded class hierarchy of TwigSanitizeTest

File

tests/src/Unit/TwigSanitizeTest.php, line 16

Namespace

Drupal\Tests\twig_tools\Unit
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TwigSanitizeTest::providerTestArrayUniqueValues public function Provides test data for testArrayUnique.
TwigSanitizeTest::providerTestCleanClassArrayValues public function Provides test data for providerTestCleanClassArrayValues.
TwigSanitizeTest::providerTestRemoveEmptyValues public function Provides test data for providerTestRemoveEmptyValues.
TwigSanitizeTest::providerTestScrubClassArrayValues public function Provides test data for testScrubClassArray.
TwigSanitizeTest::setUp public function Create a new TwigExtension object. Overrides UnitTestCase::setUp
TwigSanitizeTest::tearDown public function Unset the test object.
TwigSanitizeTest::testArrayUnique public function @covers ::arrayUnique
TwigSanitizeTest::testCleanClassArray public function @covers ::cleanClassArray
TwigSanitizeTest::testRemoveEmpty public function @covers ::removeEmpty
TwigSanitizeTest::testScrubClassArray public function @covers ::scrubClassArray
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed 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.