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;
class TwigSanitizeTest extends UnitTestCase {
public function setUp() {
parent::setUp();
$loader = new StringLoader();
$this->twig = new \Twig_Environment($loader);
$twigTools = new TwigSanitize();
$this->twig
->addExtension($twigTools);
}
public function testCleanClassArray($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
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",
],
];
}
public function testArrayUnique($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
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",
],
];
}
public function testRemoveEmpty($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
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",
],
];
}
public function testScrubClassArray($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
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",
],
];
}
public function tearDown() {
unset($this->twigTools, $this->twig);
}
}