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