View source
<?php
namespace Drupal\Tests\xbbcode\Kernel;
use Drupal\Component\Utility\Html;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
use Drupal\xbbcode\Entity\Tag;
use Drupal\xbbcode\Entity\TagSet;
class XBBCodeFilterTest extends KernelTestBase {
public static $modules = [
'system',
'filter',
'xbbcode',
'xbbcode_test_plugin',
'user',
];
private $renderer;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'system',
'filter',
'xbbcode',
'xbbcode_test_plugin',
]);
$tag = Tag::create([
'id' => 'bad_tag',
'label' => 'Bad Tag',
'description' => 'Renders the outer source of itself.',
'default_name' => 'bad_tag',
'sample' => '[{{ name }}]Content[/{{ name }}]',
'template_code' => '<{{ tag.name }}>{{ tag.outerSource }}</{{ tag.name }}>',
]);
$tag
->save();
$tag_set = TagSet::create([
'id' => 'test_set',
'label' => 'Test Set',
'tags' => [
'test_plugin' => [
'id' => 'test_plugin_id',
],
'test_tag' => [
'id' => 'xbbcode_tag:test_tag_id',
],
'test_template' => [
'id' => 'xbbcode_tag:test_tag_external',
],
'bad_tag' => [
'id' => 'xbbcode_tag:bad_tag',
],
],
]);
$tag_set
->save();
$xbbcode_format = FilterFormat::create([
'format' => 'xbbcode_test',
'name' => 'XBBCode Test',
'filters' => [
'filter_html_escape' => [
'status' => 1,
'weight' => 0,
],
'xbbcode' => [
'status' => 1,
'weight' => 1,
'settings' => [
'tags' => 'test_set',
'linebreaks' => FALSE,
],
],
],
]);
$xbbcode_format
->save();
$this->renderer = \Drupal::service('renderer');
}
public function testAttributes() : void {
$values = [
$this
->randomString() . '\'"\'" \\\\',
'\'"\'" \\\\' . $this
->randomString(),
$this
->randomString() . '\'"\'" ]\\\\' . $this
->randomString(),
];
$keys = [
$this
->randomMachineName(),
$this
->randomMachineName(),
$this
->randomMachineName(),
];
$string = $keys[0] . "='" . preg_replace('/[\\\\\']/', '\\\\\\0', $values[0]) . "' " . $keys[1] . '=' . preg_replace('/[\\\\\\"\'\\s\\[\\]]/', '\\\\\\0', $values[1]) . ' ' . $keys[2] . '="' . preg_replace('/[\\\\\\"]/', '\\\\\\0', $values[2]) . '"';
$content = $this
->randomString() . '[v=';
$text = "[test_plugin {$string}]{$content}[/test_plugin]";
$markup = check_markup($text, 'xbbcode_test');
$expected_markup = '<span data-' . $keys[0] . '="' . Html::escape($values[0]) . '" ' . 'data-' . $keys[1] . '="' . Html::escape($values[1]) . '" ' . 'data-' . $keys[2] . '="' . Html::escape($values[2]) . '">' . '{prepared:' . Html::escape($content) . '}</span>';
self::assertEquals($expected_markup, $markup);
}
public function testFilter() : void {
$string = [
$this
->randomString(),
$this
->randomString(),
$this
->randomString(),
$this
->randomString(),
$this
->randomString(),
];
$escaped = array_map(static function ($x) {
return Html::escape($x);
}, $string);
$key = [
$this
->randomMachineName(),
$this
->randomMachineName(),
];
$text = "{$string[0]}[test_plugin {$key[0]}={$key[1]}]{$string[1]}" . "[TEST_plugin {$key[1]}={$key[0]}]{$string[2]}[/test_PLUGIN]" . "{$string[3]}[/test_plugin]{$string[4]}";
$expected = "{$escaped[0]}<span data-{$key[0]}=\"{$key[1]}\">{prepared:{$escaped[1]}" . "<span data-{$key[1]}=\"{$key[0]}\">{prepared:{$escaped[2]}}</span>" . "{$escaped[3]}}</span>{$escaped[4]}";
self::assertEquals($expected, check_markup($text, 'xbbcode_test'));
$text = "{$string[0]}[test_plugin {$key[0]}={$key[1]}]{$string[1]}" . "[BAD_tag {$key[1]}={$key[0]}]{$string[2]}[/bad_TAG]" . "{$string[3]}[/test_plugin]{$string[4]}";
$expected = "{$escaped[0]}<span data-{$key[0]}=\"{$key[1]}\">{prepared:{$escaped[1]}" . "<bad_tag>[BAD_tag {$key[1]}={$key[0]}]{$escaped[2]}[/bad_TAG]</bad_tag>" . "{$escaped[3]}}</span>{$escaped[4]}";
self::assertEquals($expected, check_markup($text, 'xbbcode_test'));
$val = preg_replace('/[\\\\\\"]/', '\\\\\\0', $string[2]);
$text = "[test_tag]{$string[0]}[test_template]{$string[1]}" . "[test_plugin {$key[0]}=\"{$val}\"]{$string[2]}[/test_plugin]" . "{$string[3]}[/test_template]{$string[4]}[/test_tag]";
$expected = "<strong>{$escaped[0]}<em>{$escaped[1]}" . "<span data-{$key[0]}=\"{$escaped[2]}\">{prepared:{$escaped[2]}}</span>" . "{$escaped[3]}</em>\n{$escaped[4]}</strong>";
$output = $this
->checkMarkup($text, 'xbbcode_test');
self::assertEquals($expected, $output['#markup']);
self::assertEquals([
'library' => [
'xbbcode_test_plugin/library-template',
'xbbcode_test_plugin/library-plugin',
],
], $output['#attached']);
}
private function checkMarkup(string $text, string $format_id = NULL) : array {
$build = [
'#type' => 'processed_text',
'#text' => $text,
'#format' => $format_id,
];
try {
$this->renderer
->renderPlain($build);
} catch (\Exception $e) {
$build['#markup'] = '';
}
return $build;
}
}