File: FootnoteExtensionTest.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 (109 lines) | stat: -rw-r--r-- 5,403 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
<?php

declare(strict_types=1);

/*
 * This file is part of the league/commonmark package.
 *
 * (c) Colin O'Dell <colinodell@gmail.com> and uAfrica.com (http://uafrica.com)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace League\CommonMark\Tests\Functional\Extension\Footnote;

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Footnote\FootnoteExtension;
use League\CommonMark\MarkdownConverter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class FootnoteExtensionTest extends TestCase
{
    /**
     * @param array<string, mixed> $config
     */
    #[DataProvider('dataForIntegrationTest')]
    public function testFootnote(string $string, string $expected, array $config = []): void
    {
        $environment = new Environment(['footnote' => $config]);
        $environment->addExtension(new CommonMarkCoreExtension());
        $environment->addExtension(new FootnoteExtension());

        $converter = new MarkdownConverter($environment);

        $html = \trim((string) $converter->convert($string));

        $this->assertSame($expected, $html);
    }

    /**
     * @return array<array<string>>
     */
    public static function dataForIntegrationTest(): array
    {
        return [
            [
                "Here[^note1]\n\n[^note1]: There",
                '<p>Here<sup id="fnref:note1"><a class="footnote-ref" href="#fn:note1" role="doc-noteref">1</a></sup></p>
<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>',
            ],
            [
                "Here[^note1]\n\n[^note1]: There",
                '<p>Here<sup id="customfnref:note1"><a class="footnote-ref" href="#customfn:note1" role="doc-noteref">1</a></sup></p>
<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>',
                ['ref_id_prefix' => 'customfnref:', 'footnote_id_prefix' => 'customfn:'],
            ],
        ];
    }

    #[DataProvider('dataProviderForTestFootnotesWithCustomOptions')]
    public function testFootnotesWithCustomOptions(string $input, string $expected): void
    {
        $environment = new Environment([
            'footnote' => [
                'backref_class'      => 'custom-backref',
                // Ensure multiple characters are allowed (including multibyte) and special HTML characters are escaped.
                'backref_symbol'     => '↩ 🦄️ <3 You',
                'container_add_hr'   => false,
                'container_class'    => 'custom-notes',
                'ref_class'          => 'custom-ref',
                'ref_id_prefix'      => 'fnref:',
                'footnote_class'     => 'custom-footnote',
                'footnote_id_prefix' => 'fn:',
            ],
        ]);
        $environment->addExtension(new CommonMarkCoreExtension());
        $environment->addExtension(new FootnoteExtension());

        $converter = new MarkdownConverter($environment);

        $this->assertEquals($expected, \trim((string) $converter->convert($input)));
    }

    public static function dataProviderForTestFootnotesWithCustomOptions(): \Generator
    {
        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>'];
        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>'];
    }

    public function testFootnotesWithEmptySymbol(): void
    {
        $environment = new Environment([
            'footnote' => [
                'backref_symbol' => '',
            ],
        ]);
        $environment->addExtension(new CommonMarkCoreExtension());
        $environment->addExtension(new FootnoteExtension());

        $converter = new MarkdownConverter($environment);

        $input    = "Here[^note1]\n\n[^note1]: There";
        $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>';

        $this->assertEquals($expected, \trim((string) $converter->convert($input)));
    }
}