HtmlInputTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
  9. * - (c) John MacFarlane
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace League\CommonMark\Tests\Functional;
  15. use League\CommonMark\CommonMarkConverter;
  16. use League\CommonMark\Util\HtmlFilter;
  17. use PHPUnit\Framework\TestCase;
  18. final class HtmlInputTest extends TestCase
  19. {
  20. public function testDefaultConfig(): void
  21. {
  22. $input = \file_get_contents(__DIR__ . '/data/html_input/input.md');
  23. $expectedOutput = \trim(\file_get_contents(__DIR__ . '/data/html_input/unsafe_output.html'));
  24. $converter = new CommonMarkConverter();
  25. $actualOutput = \trim((string) $converter->convert($input));
  26. $this->assertEquals($expectedOutput, $actualOutput);
  27. }
  28. public function testAllowHtmlInputConfig(): void
  29. {
  30. $input = \file_get_contents(__DIR__ . '/data/html_input/input.md');
  31. $expectedOutput = \trim(\file_get_contents(__DIR__ . '/data/html_input/unsafe_output.html'));
  32. $converter = new CommonMarkConverter(['html_input' => HtmlFilter::ALLOW]);
  33. $actualOutput = \trim((string) $converter->convert($input));
  34. $this->assertEquals($expectedOutput, $actualOutput);
  35. }
  36. public function testEscapeHtmlInputConfig(): void
  37. {
  38. $input = \file_get_contents(__DIR__ . '/data/html_input/input.md');
  39. $expectedOutput = \trim(\file_get_contents(__DIR__ . '/data/html_input/escaped_output.html'));
  40. $converter = new CommonMarkConverter(['html_input' => HtmlFilter::ESCAPE]);
  41. $actualOutput = \trim((string) $converter->convert($input));
  42. $this->assertEquals($expectedOutput, $actualOutput);
  43. }
  44. public function testStripHtmlInputConfig(): void
  45. {
  46. $input = \file_get_contents(__DIR__ . '/data/html_input/input.md');
  47. $expectedOutput = \trim(\file_get_contents(__DIR__ . '/data/html_input/safe_output.html'));
  48. $converter = new CommonMarkConverter(['html_input' => HtmlFilter::STRIP]);
  49. $actualOutput = \trim((string) $converter->convert($input));
  50. $this->assertEquals($expectedOutput, $actualOutput);
  51. }
  52. }