xref: /dokuwiki/_test/tests/Parsing/ParserMode/AbstractModeTest.php (revision 9a38b8db3f243daf610e443c21cb76e9753358a9)
1<?php
2
3namespace dokuwiki\test\Parsing\ParserMode;
4
5use dokuwiki\Parsing\ModeRegistry;
6use dokuwiki\Parsing\ParserMode\Eol;
7use dokuwiki\Parsing\ParserMode\Table;
8
9class AbstractModeTest extends \DokuWikiTest
10{
11    /** @var ModeRegistry */
12    private $registry;
13
14    function setUp(): void
15    {
16        parent::setUp();
17        global $conf;
18        $this->registry = new ModeRegistry($conf['syntax']);
19    }
20
21    /**
22     * A mode that both declares accepted categories and assigns individual mode
23     * names to $allowedModes directly (the sibling-component pattern) must keep
24     * both after setModeRegistry(): the category-derived modes and its own direct
25     * entries. Regression: setModeRegistry() used to replace the list with the
26     * category-derived modes, dropping the direct entries.
27     */
28    function testSetModeRegistryMergesDirectlyAssignedModesWithCategories()
29    {
30        // Table declares FORMATTING/SUBSTITUTION/DISABLED/PROTECTED categories.
31        $mode = new Table();
32        self::setInaccessibleProperty($mode, 'allowedModes', ['plugin_foo_bar']);
33
34        $mode->setModeRegistry($this->registry);
35
36        // the directly-assigned sibling mode survives the merge
37        $this->assertTrue($mode->accepts('plugin_foo_bar'));
38        // the category-derived modes are still present
39        $this->assertTrue($mode->accepts('strong'));
40        $this->assertTrue($mode->accepts('unformatted'));
41    }
42
43    /**
44     * With no categories declared, the directly-assigned $allowedModes are used
45     * as-is (and deduplicated).
46     */
47    function testSetModeRegistryUsesDirectlyAssignedModesWhenNoCategories()
48    {
49        // Eol declares no categories.
50        $mode = new Eol();
51        self::setInaccessibleProperty($mode, 'allowedModes', ['plugin_foo_bar', 'plugin_foo_baz', 'plugin_foo_bar']);
52
53        $mode->setModeRegistry($this->registry);
54
55        $this->assertTrue($mode->accepts('plugin_foo_bar'));
56        $this->assertTrue($mode->accepts('plugin_foo_baz'));
57        $this->assertFalse($mode->accepts('strong'));
58        $this->assertSame(
59            ['plugin_foo_bar', 'plugin_foo_baz'],
60            self::getInaccessibleProperty($mode, 'allowedModes')
61        );
62    }
63}
64