xref: /dokuwiki/_test/tests/Parsing/SyntaxOverrideTest.php (revision 4b31eadfd0dd82e519dd953a4cb0ad079114879d)
1<?php
2
3namespace dokuwiki\test\Parsing;
4
5use dokuwiki\Parsing\ModeRegistry;
6
7/**
8 * The per-parse syntax override: p_get_instructions($text, $syntax) parses
9 * under the requested flavour regardless of the configured $conf['syntax'].
10 */
11class SyntaxOverrideTest extends \DokuWikiTest
12{
13    /** Names of all instruction calls produced for $text under $syntax. */
14    private function callNames(string $text, ?string $syntax): array
15    {
16        return array_column(p_get_instructions($text, $syntax), 0);
17    }
18
19    public function testDwOverrideUnderMarkdownConfig()
20    {
21        global $conf;
22        $conf['syntax'] = 'md';
23
24        // [[wiki]] is DokuWiki link syntax; it only parses as a link when the
25        // DW internallink mode is loaded, which the 'dw' override forces on.
26        $names = $this->callNames('[[wiki:syntax]]', 'dw');
27        $this->assertContains('internallink', $names);
28    }
29
30    public function testMarkdownOverrideUnderDwConfig()
31    {
32        global $conf;
33        $conf['syntax'] = 'dw';
34
35        // *foo* is GFM emphasis; it only fires when gfm_emphasis is loaded,
36        // which the 'md' override forces on even though the wiki prefers DW.
37        $names = $this->callNames('a *foo* b', 'md');
38        $this->assertContains('emphasis_open', $names);
39    }
40
41    public function testNullHonoursConfiguredSyntax()
42    {
43        global $conf;
44        $conf['syntax'] = 'md';
45
46        // Passing null means "use the configured syntax" — so [[wiki]] stays
47        // literal because DW internallink is not loaded under 'md'.
48        $names = $this->callNames('[[wiki:syntax]]', null);
49        $this->assertNotContains('internallink', $names);
50    }
51
52    public function testTwoRegistriesProduceDifferentModeLists()
53    {
54        // Catches a singleton regression: building two registries with
55        // different syntaxes in one request must yield different mode sets.
56        $dw = array_column((new ModeRegistry('dw'))->getModes(), 'mode');
57        $md = array_column((new ModeRegistry('md'))->getModes(), 'mode');
58
59        $this->assertNotEquals($dw, $md);
60        $this->assertContains('internallink', $dw);
61        $this->assertContains('gfm_emphasis', $md);
62    }
63}
64