DisallowedRawHtmlRendererTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the league/commonmark package.
  4. *
  5. * (c) Colin O'Dell <colinodell@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\CommonMark\Tests\Unit\Extension\DisallowedRawHtml;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
  14. use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlRenderer;
  15. use League\CommonMark\Node\Node;
  16. use League\CommonMark\Renderer\NodeRendererInterface;
  17. use League\CommonMark\Tests\Unit\Renderer\FakeChildNodeRenderer;
  18. use League\Config\ConfigurationInterface;
  19. use PHPUnit\Framework\TestCase;
  20. final class DisallowedRawHtmlRendererTest extends TestCase
  21. {
  22. public function testWithEmptyHtml(): void
  23. {
  24. $mockRenderer = $this->createMock(NodeRendererInterface::class);
  25. $mockRenderer->method('render')->willReturn('');
  26. $renderer = new DisallowedRawHtmlRenderer($mockRenderer);
  27. $renderer->setConfiguration($this->createConfiguration());
  28. $this->assertSame('', $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer()));
  29. }
  30. /**
  31. * @dataProvider dataProviderForTestWithDefaultSettings
  32. */
  33. public function testWithDefaultSettings(string $input, string $expectedOutput): void
  34. {
  35. $mockRenderer = $this->createMock(NodeRendererInterface::class);
  36. $mockRenderer->method('render')->willReturn($input);
  37. $renderer = new DisallowedRawHtmlRenderer($mockRenderer);
  38. $renderer->setConfiguration($this->createConfiguration());
  39. $this->assertSame($expectedOutput, $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer()));
  40. }
  41. /**
  42. * @return iterable<mixed>
  43. */
  44. public function dataProviderForTestWithDefaultSettings(): iterable
  45. {
  46. // Different tag variants
  47. yield ['<title>', '&lt;title>'];
  48. yield ['</title>', '&lt;/title>'];
  49. yield ['<title x="sdf">', '&lt;title x="sdf">'];
  50. yield ['<title/>', '&lt;title/>'];
  51. yield ['<title />', '&lt;title />'];
  52. // Other tags escaped by default
  53. yield ['<textarea>', '&lt;textarea>'];
  54. yield ['<style>', '&lt;style>'];
  55. yield ['<xmp>', '&lt;xmp>'];
  56. yield ['<iframe>', '&lt;iframe>'];
  57. yield ['<noembed>', '&lt;noembed>'];
  58. yield ['<noframes>', '&lt;noframes>'];
  59. yield ['<script>', '&lt;script>'];
  60. yield ['<plaintext>', '&lt;plaintext>'];
  61. // Tags not escaped by default
  62. yield ['<strong>', '<strong>'];
  63. }
  64. /**
  65. * @dataProvider dataProviderForTestWithCustomSettings
  66. */
  67. public function testWithCustomSettings(string $input, string $expectedOutput): void
  68. {
  69. $mockRenderer = $this->createMock(NodeRendererInterface::class);
  70. $mockRenderer->method('render')->willReturn($input);
  71. $renderer = new DisallowedRawHtmlRenderer($mockRenderer);
  72. $renderer->setConfiguration($this->createConfiguration([
  73. 'disallowed_raw_html' => [
  74. 'disallowed_tags' => [
  75. 'strong',
  76. ],
  77. ],
  78. ]));
  79. $this->assertSame($expectedOutput, $renderer->render($this->createMock(Node::class), new FakeChildNodeRenderer()));
  80. }
  81. /**
  82. * @return iterable<mixed>
  83. */
  84. public function dataProviderForTestWithCustomSettings(): iterable
  85. {
  86. // Tags that I've configured to escape
  87. yield ['<strong>', '&lt;strong>'];
  88. yield ['</strong>', '&lt;/strong>'];
  89. yield ['<strong x="sdf">', '&lt;strong x="sdf">'];
  90. yield ['<strong/>', '&lt;strong/>'];
  91. yield ['<strong />', '&lt;strong />'];
  92. // Defaults that I didn't include in my custom config
  93. yield ['<title>', '<title>'];
  94. yield ['<textarea>', '<textarea>'];
  95. yield ['<style>', '<style>'];
  96. yield ['<xmp>', '<xmp>'];
  97. yield ['<iframe>', '<iframe>'];
  98. yield ['<noembed>', '<noembed>'];
  99. yield ['<noframes>', '<noframes>'];
  100. yield ['<script>', '<script>'];
  101. yield ['<plaintext>', '<plaintext>'];
  102. }
  103. /**
  104. * @param array<string, mixed> $values
  105. */
  106. private function createConfiguration(array $values = []): ConfigurationInterface
  107. {
  108. $config = Environment::createDefaultConfiguration();
  109. (new DisallowedRawHtmlExtension())->configureSchema($config);
  110. $config->merge($values);
  111. return $config->reader();
  112. }
  113. }