123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- /*
- * This file is part of the league/commonmark package.
- *
- * (c) Colin O'Dell <colinodell@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- declare(strict_types=1);
- namespace League\CommonMark\Tests\Unit\Extension\DisallowedRawHtml;
- use League\CommonMark\Environment\Environment;
- use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
- use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlRenderer;
- use League\CommonMark\Node\Node;
- use League\CommonMark\Renderer\NodeRendererInterface;
- use League\CommonMark\Tests\Unit\Renderer\FakeChildNodeRenderer;
- use League\Config\ConfigurationInterface;
- use PHPUnit\Framework\TestCase;
- final class DisallowedRawHtmlRendererTest extends TestCase
- {
- public function testWithEmptyHtml(): void
- {
- $mockRenderer = $this->createMock(NodeRendererInterface::class);
- $mockRenderer->method('render')->willReturn('');
- $renderer = new DisallowedRawHtmlRenderer($mockRenderer);
- $renderer->setConfiguration($this->createConfiguration());
- $this->assertSame('', $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer()));
- }
- /**
- * @dataProvider dataProviderForTestWithDefaultSettings
- */
- public function testWithDefaultSettings(string $input, string $expectedOutput): void
- {
- $mockRenderer = $this->createMock(NodeRendererInterface::class);
- $mockRenderer->method('render')->willReturn($input);
- $renderer = new DisallowedRawHtmlRenderer($mockRenderer);
- $renderer->setConfiguration($this->createConfiguration());
- $this->assertSame($expectedOutput, $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer()));
- }
- /**
- * @return iterable<mixed>
- */
- public function dataProviderForTestWithDefaultSettings(): iterable
- {
- // Different tag variants
- yield ['<title>', '<title>'];
- yield ['</title>', '</title>'];
- yield ['<title x="sdf">', '<title x="sdf">'];
- yield ['<title/>', '<title/>'];
- yield ['<title />', '<title />'];
- // Other tags escaped by default
- yield ['<textarea>', '<textarea>'];
- yield ['<style>', '<style>'];
- yield ['<xmp>', '<xmp>'];
- yield ['<iframe>', '<iframe>'];
- yield ['<noembed>', '<noembed>'];
- yield ['<noframes>', '<noframes>'];
- yield ['<script>', '<script>'];
- yield ['<plaintext>', '<plaintext>'];
- // Tags not escaped by default
- yield ['<strong>', '<strong>'];
- }
- /**
- * @dataProvider dataProviderForTestWithCustomSettings
- */
- public function testWithCustomSettings(string $input, string $expectedOutput): void
- {
- $mockRenderer = $this->createMock(NodeRendererInterface::class);
- $mockRenderer->method('render')->willReturn($input);
- $renderer = new DisallowedRawHtmlRenderer($mockRenderer);
- $renderer->setConfiguration($this->createConfiguration([
- 'disallowed_raw_html' => [
- 'disallowed_tags' => [
- 'strong',
- ],
- ],
- ]));
- $this->assertSame($expectedOutput, $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer()));
- }
- /**
- * @return iterable<mixed>
- */
- public function dataProviderForTestWithCustomSettings(): iterable
- {
- // Tags that I've configured to escape
- yield ['<strong>', '<strong>'];
- yield ['</strong>', '</strong>'];
- yield ['<strong x="sdf">', '<strong x="sdf">'];
- yield ['<strong/>', '<strong/>'];
- yield ['<strong />', '<strong />'];
- // Defaults that I didn't include in my custom config
- yield ['<title>', '<title>'];
- yield ['<textarea>', '<textarea>'];
- yield ['<style>', '<style>'];
- yield ['<xmp>', '<xmp>'];
- yield ['<iframe>', '<iframe>'];
- yield ['<noembed>', '<noembed>'];
- yield ['<noframes>', '<noframes>'];
- yield ['<script>', '<script>'];
- yield ['<plaintext>', '<plaintext>'];
- }
- /**
- * @param array<string, mixed> $values
- */
- private function createConfiguration(array $values = []): ConfigurationInterface
- {
- $config = Environment::createDefaultConfiguration();
- (new DisallowedRawHtmlExtension())->configureSchema($config);
- $config->merge($values);
- return $config->reader();
- }
- }
|