xref: /dokuwiki/_test/tests/Parsing/ParserMode/GfmHtmlEntityTest.php (revision 75364f13219a5af44f52c564ea0a62df64c3a17f)
1<?php
2
3namespace dokuwiki\test\Parsing\ParserMode;
4
5use dokuwiki\Parsing\ParserMode\GfmHtmlEntity;
6
7/**
8 * Sanity tests for the GfmHtmlEntity lexer mode.
9 *
10 * Decoding semantics are tested exhaustively in
11 * {@see \dokuwiki\test\Parsing\Helpers\HtmlEntityTest}; this class
12 * covers only that the mode wires up to the lexer correctly and emits
13 * cdata at the right positions. Consecutive cdata calls are coalesced
14 * by Handler\Block::addCall during finalize(), so a successful match
15 * shows up as a single cdata containing the decoded character spliced
16 * into the surrounding text.
17 */
18class GfmHtmlEntityTest extends ParserTestBase
19{
20    private function assertParsedCdata(string $input, string $expectedCdata): void
21    {
22        $this->P->addMode('gfm_html_entity', new GfmHtmlEntity());
23        $this->P->parse($input);
24        $this->assertCalls([
25            ['document_start', []],
26            ['p_open', []],
27            ['cdata', [$expectedCdata]],
28            ['p_close', []],
29            ['document_end', []],
30        ], $this->H->calls);
31    }
32
33    public function testNumericDecodes()
34    {
35        $this->assertParsedCdata('x &#35; y', "\nx # y");
36    }
37
38    public function testNamedDecodes()
39    {
40        $this->assertParsedCdata('a&copy;b', "\na\u{00A9}b");
41    }
42
43    public function testUnknownNameStaysLiteral()
44    {
45        $this->assertParsedCdata('a&MadeUpEntity;b', "\na&MadeUpEntity;b");
46    }
47
48    public function testNonMatchingInputStaysLiteral()
49    {
50        $this->assertParsedCdata('a&#abcdef0;b', "\na&#abcdef0;b");
51    }
52}
53