class TwigSanitizeTest in Twig Tools 8
Tests to ensure sanitization filters work correctly.
@group twig_tools
@coversDefaultClass \Drupal\twig_tools\TwigExtension\TwigSanitize
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\twig_tools\Unit\TwigSanitizeTest
Expanded class hierarchy of TwigSanitizeTest
File
- tests/
src/ Unit/ TwigSanitizeTest.php, line 16
Namespace
Drupal\Tests\twig_tools\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
TwigSanitizeTest:: |
public | function | Provides test data for testArrayUnique. | |
TwigSanitizeTest:: |
public | function | Provides test data for providerTestCleanClassArrayValues. | |
TwigSanitizeTest:: |
public | function | Provides test data for providerTestRemoveEmptyValues. | |
TwigSanitizeTest:: |
public | function | Provides test data for testScrubClassArray. | |
TwigSanitizeTest:: |
public | function |
Create a new TwigExtension object. Overrides UnitTestCase:: |
|
TwigSanitizeTest:: |
public | function | Unset the test object. | |
TwigSanitizeTest:: |
public | function | @covers ::arrayUnique | |
TwigSanitizeTest:: |
public | function | @covers ::cleanClassArray | |
TwigSanitizeTest:: |
public | function | @covers ::removeEmpty | |
TwigSanitizeTest:: |
public | function | @covers ::scrubClassArray | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |