FootnoteExtensionTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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> and uAfrica.com (http://uafrica.com)
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Functional\Extension\Footnote;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
  14. use League\CommonMark\Extension\Footnote\FootnoteExtension;
  15. use League\CommonMark\MarkdownConverter;
  16. use PHPUnit\Framework\TestCase;
  17. final class FootnoteExtensionTest extends TestCase
  18. {
  19. /**
  20. * @dataProvider dataForIntegrationTest
  21. *
  22. * @param array<string, mixed> $config
  23. */
  24. public function testFootnote(string $string, string $expected, array $config = []): void
  25. {
  26. $environment = new Environment(['footnote' => $config]);
  27. $environment->addExtension(new CommonMarkCoreExtension());
  28. $environment->addExtension(new FootnoteExtension());
  29. $converter = new MarkdownConverter($environment);
  30. $html = \trim((string) $converter->convert($string));
  31. $this->assertSame($expected, $html);
  32. }
  33. /**
  34. * @return array<array<string>>
  35. */
  36. public function dataForIntegrationTest(): array
  37. {
  38. return [
  39. [
  40. "Here[^note1]\n\n[^note1]: There",
  41. '<p>Here<sup id="fnref:note1"><a class="footnote-ref" href="#fn:note1" role="doc-noteref">1</a></sup></p>
  42. <div class="footnotes" role="doc-endnotes"><hr /><ol><li class="footnote" id="fn:note1" role="doc-endnote"><p>There&nbsp;<a class="footnote-backref" rev="footnote" href="#fnref:note1" role="doc-backlink">↩</a></p></li></ol></div>',
  43. ],
  44. [
  45. "Here[^note1]\n\n[^note1]: There",
  46. '<p>Here<sup id="customfnref:note1"><a class="footnote-ref" href="#customfn:note1" role="doc-noteref">1</a></sup></p>
  47. <div class="footnotes" role="doc-endnotes"><hr /><ol><li class="footnote" id="customfn:note1" role="doc-endnote"><p>There&nbsp;<a class="footnote-backref" rev="footnote" href="#customfnref:note1" role="doc-backlink">↩</a></p></li></ol></div>',
  48. ['ref_id_prefix' => 'customfnref:', 'footnote_id_prefix' => 'customfn:'],
  49. ],
  50. ];
  51. }
  52. /**
  53. * @dataProvider dataProviderForTestFootnotesWithCustomOptions
  54. */
  55. public function testFootnotesWithCustomOptions(string $input, string $expected): void
  56. {
  57. $environment = new Environment([
  58. 'footnote' => [
  59. 'backref_class' => 'custom-backref',
  60. // Ensure multiple characters are allowed (including multibyte) and special HTML characters are escaped.
  61. 'backref_symbol' => '↩ 🦄️ <3 You',
  62. 'container_add_hr' => false,
  63. 'container_class' => 'custom-notes',
  64. 'ref_class' => 'custom-ref',
  65. 'ref_id_prefix' => 'fnref:',
  66. 'footnote_class' => 'custom-footnote',
  67. 'footnote_id_prefix' => 'fn:',
  68. ],
  69. ]);
  70. $environment->addExtension(new CommonMarkCoreExtension());
  71. $environment->addExtension(new FootnoteExtension());
  72. $converter = new MarkdownConverter($environment);
  73. $this->assertEquals($expected, \trim((string) $converter->convert($input)));
  74. }
  75. public function dataProviderForTestFootnotesWithCustomOptions(): \Generator
  76. {
  77. yield ["Here[^note1]\n\n[^note1]: There", '<p>Here<sup id="fnref:note1"><a class="custom-ref" href="#fn:note1" role="doc-noteref">1</a></sup></p>' . "\n" . '<div class="custom-notes" role="doc-endnotes"><ol><li class="custom-footnote" id="fn:note1" role="doc-endnote"><p>There&nbsp;<a class="custom-backref" rev="footnote" href="#fnref:note1" role="doc-backlink">↩ 🦄️ &lt;3 You</a></p></li></ol></div>'];
  78. yield ["_Here_[^note1]\n\n[^note1]: **There**", '<p><em>Here</em><sup id="fnref:note1"><a class="custom-ref" href="#fn:note1" role="doc-noteref">1</a></sup></p>' . "\n" . '<div class="custom-notes" role="doc-endnotes"><ol><li class="custom-footnote" id="fn:note1" role="doc-endnote"><p><strong>There</strong>&nbsp;<a class="custom-backref" rev="footnote" href="#fnref:note1" role="doc-backlink">↩ 🦄️ &lt;3 You</a></p></li></ol></div>'];
  79. }
  80. public function testFootnotesWithEmptySymbol(): void
  81. {
  82. $environment = new Environment([
  83. 'footnote' => [
  84. 'backref_symbol' => '',
  85. ],
  86. ]);
  87. $environment->addExtension(new CommonMarkCoreExtension());
  88. $environment->addExtension(new FootnoteExtension());
  89. $converter = new MarkdownConverter($environment);
  90. $input = "Here[^note1]\n\n[^note1]: There";
  91. $expected = '<p>Here<sup id="fnref:note1"><a class="footnote-ref" href="#fn:note1" role="doc-noteref">1</a></sup></p>' . "\n" . '<div class="footnotes" role="doc-endnotes"><hr /><ol><li class="footnote" id="fn:note1" role="doc-endnote"><p>There&nbsp;<a class="footnote-backref" rev="footnote" href="#fnref:note1" role="doc-backlink" /></p></li></ol></div>';
  92. $this->assertEquals($expected, \trim((string) $converter->convert($input)));
  93. }
  94. }