File: DisallowedRawHtmlRendererTest.php

package info (click to toggle)
php-league-commonmark 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,264 kB
  • sloc: php: 20,396; xml: 1,988; ruby: 45; makefile: 21; javascript: 15
file content (130 lines) | stat: -rw-r--r-- 4,662 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?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\Attributes\DataProvider;
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 static function dataProviderForTestWithDefaultSettings(): iterable
    {
        // Different tag variants
        yield ['<title>', '&lt;title>'];
        yield ['</title>', '&lt;/title>'];
        yield ['<title x="sdf">', '&lt;title x="sdf">'];
        yield ['<title/>', '&lt;title/>'];
        yield ['<title />', '&lt;title />'];

        // Other tags escaped by default
        yield ['<textarea>', '&lt;textarea>'];
        yield ['<style>', '&lt;style>'];
        yield ['<xmp>', '&lt;xmp>'];
        yield ['<iframe>', '&lt;iframe>'];
        yield ['<noembed>', '&lt;noembed>'];
        yield ['<noframes>', '&lt;noframes>'];
        yield ['<script>', '&lt;script>'];
        yield ['<plaintext>', '&lt;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 static function dataProviderForTestWithCustomSettings(): iterable
    {
        // Tags that I've configured to escape
        yield ['<strong>', '&lt;strong>'];
        yield ['</strong>', '&lt;/strong>'];
        yield ['<strong x="sdf">', '&lt;strong x="sdf">'];
        yield ['<strong/>', '&lt;strong/>'];
        yield ['<strong />', '&lt;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();
    }
}