xref: /dokuwiki/_test/tests/TreeBuilder/ControlPageBuilderTest.php (revision 78a26510e6c070c63f2aafa834b153599b5832e0)
1*78a26510SAndreas Gohr<?php
2*78a26510SAndreas Gohr
3*78a26510SAndreas Gohrnamespace dokuwiki\test\Treebuilder;
4*78a26510SAndreas Gohr
5*78a26510SAndreas Gohruse dokuwiki\TreeBuilder\ControlPageBuilder;
6*78a26510SAndreas Gohruse DokuWikiTest;
7*78a26510SAndreas Gohr
8*78a26510SAndreas Gohrclass ControlPageBuilderTest extends DokuWikiTest
9*78a26510SAndreas Gohr{
10*78a26510SAndreas Gohr    public static function setUpBeforeClass(): void
11*78a26510SAndreas Gohr    {
12*78a26510SAndreas Gohr        parent::setUpBeforeClass();
13*78a26510SAndreas Gohr        saveWikiText('simple', file_get_contents(__DIR__ . '/cp/simple.txt'), 'test');
14*78a26510SAndreas Gohr        saveWikiText('foo:complex', file_get_contents(__DIR__ . '/cp/complex.txt'), 'test');
15*78a26510SAndreas Gohr    }
16*78a26510SAndreas Gohr
17*78a26510SAndreas Gohr    public function testSimpleParsing()
18*78a26510SAndreas Gohr    {
19*78a26510SAndreas Gohr        $control = new ControlPageBuilder('simple');
20*78a26510SAndreas Gohr        $control->generate();
21*78a26510SAndreas Gohr
22*78a26510SAndreas Gohr        $expected = [
23*78a26510SAndreas Gohr            '+briefs:start',
24*78a26510SAndreas Gohr            '+qhsr:start',
25*78a26510SAndreas Gohr            '++qhsr:q',
26*78a26510SAndreas Gohr            '++qhsr:cert',
27*78a26510SAndreas Gohr            '++qhsr:hse:start',
28*78a26510SAndreas Gohr            '++qhsr:engsystems',
29*78a26510SAndreas Gohr            '++qhsr:performance',
30*78a26510SAndreas Gohr            '++qhsr:competence',
31*78a26510SAndreas Gohr            '++qhsr:ashford',
32*78a26510SAndreas Gohr            '++qhsr:training',
33*78a26510SAndreas Gohr            '+tech:start',
34*78a26510SAndreas Gohr            '+https://homepage.company.com'
35*78a26510SAndreas Gohr        ];
36*78a26510SAndreas Gohr
37*78a26510SAndreas Gohr        $result = explode("\n", (string)$control);
38*78a26510SAndreas Gohr        sort($expected);
39*78a26510SAndreas Gohr        sort($result);
40*78a26510SAndreas Gohr
41*78a26510SAndreas Gohr        $this->assertEquals($expected, $result);
42*78a26510SAndreas Gohr
43*78a26510SAndreas Gohr        // Additional structure tests
44*78a26510SAndreas Gohr        $top = $control->getTop();
45*78a26510SAndreas Gohr        $this->assertEquals(4, count($top->getChildren()));
46*78a26510SAndreas Gohr        $this->assertEquals(1, count($top->getChildren()[0]->getParents()));
47*78a26510SAndreas Gohr        $this->assertEquals(4, count($top->getChildren()[1]->getSiblings()));
48*78a26510SAndreas Gohr        $this->assertEquals(8, count($top->getChildren()[1]->getChildren()));
49*78a26510SAndreas Gohr
50*78a26510SAndreas Gohr        $this->assertEquals(12, count($control->getAll()));
51*78a26510SAndreas Gohr        $this->assertEquals(11, count($control->getLeaves()));
52*78a26510SAndreas Gohr        $this->assertEquals(1, count($control->getBranches()));
53*78a26510SAndreas Gohr    }
54*78a26510SAndreas Gohr
55*78a26510SAndreas Gohr    /**
56*78a26510SAndreas Gohr     * Parse the complex example with different flags
57*78a26510SAndreas Gohr     *
58*78a26510SAndreas Gohr     * @return array[]
59*78a26510SAndreas Gohr     * @see testComplexParsing
60*78a26510SAndreas Gohr     */
61*78a26510SAndreas Gohr    public function complexProvider()
62*78a26510SAndreas Gohr    {
63*78a26510SAndreas Gohr        return [
64*78a26510SAndreas Gohr            'No flags' => [
65*78a26510SAndreas Gohr                'flags' => 0,
66*78a26510SAndreas Gohr                'expected' => [
67*78a26510SAndreas Gohr                    '+content',
68*78a26510SAndreas Gohr                    '+foo:this',
69*78a26510SAndreas Gohr                    '+foo:bar',
70*78a26510SAndreas Gohr                    '+foo:another_link',
71*78a26510SAndreas Gohr                    '+https://www.google.com',
72*78a26510SAndreas Gohr                    '+relativeup',
73*78a26510SAndreas Gohr                    '+foo2:this',
74*78a26510SAndreas Gohr                    '++foo2:deeper:item',
75*78a26510SAndreas Gohr                    '+++foo2:deeper:evendeeper:item',
76*78a26510SAndreas Gohr                    '+foo:blarg:down',
77*78a26510SAndreas Gohr                    '+toplevel',
78*78a26510SAndreas Gohr                    '+foo:link',
79*78a26510SAndreas Gohr                ]
80*78a26510SAndreas Gohr            ],
81*78a26510SAndreas Gohr            'FLAG_NOEXTERNAL' => [
82*78a26510SAndreas Gohr                'flags' => ControlPageBuilder::FLAG_NOEXTERNAL,
83*78a26510SAndreas Gohr                'expected' => [
84*78a26510SAndreas Gohr                    '+content',
85*78a26510SAndreas Gohr                    '+foo:this',
86*78a26510SAndreas Gohr                    '+foo:bar',
87*78a26510SAndreas Gohr                    '+foo:another_link',
88*78a26510SAndreas Gohr                    '+relativeup',
89*78a26510SAndreas Gohr                    '+foo2:this',
90*78a26510SAndreas Gohr                    '++foo2:deeper:item',
91*78a26510SAndreas Gohr                    '+++foo2:deeper:evendeeper:item',
92*78a26510SAndreas Gohr                    '+foo:blarg:down',
93*78a26510SAndreas Gohr                    '+toplevel',
94*78a26510SAndreas Gohr                    '+foo:link',
95*78a26510SAndreas Gohr                ]
96*78a26510SAndreas Gohr            ],
97*78a26510SAndreas Gohr            'FLAG_NOINTERNAL' => [
98*78a26510SAndreas Gohr                'flags' => ControlPageBuilder::FLAG_NOINTERNAL,
99*78a26510SAndreas Gohr                'expected' => [
100*78a26510SAndreas Gohr                    '+https://www.google.com',
101*78a26510SAndreas Gohr                ]
102*78a26510SAndreas Gohr            ],
103*78a26510SAndreas Gohr        ];
104*78a26510SAndreas Gohr    }
105*78a26510SAndreas Gohr
106*78a26510SAndreas Gohr    /**
107*78a26510SAndreas Gohr     * @dataProvider complexProvider
108*78a26510SAndreas Gohr     * @param int $flags
109*78a26510SAndreas Gohr     * @param array $expected
110*78a26510SAndreas Gohr     * @return void
111*78a26510SAndreas Gohr     */
112*78a26510SAndreas Gohr    public function testComplexParsing(int $flags, array $expected)
113*78a26510SAndreas Gohr    {
114*78a26510SAndreas Gohr        $control = new ControlPageBuilder('foo:complex');
115*78a26510SAndreas Gohr        $control->addFlag($flags);
116*78a26510SAndreas Gohr        $control->generate();
117*78a26510SAndreas Gohr
118*78a26510SAndreas Gohr        $result = explode("\n", (string)$control);
119*78a26510SAndreas Gohr        sort($expected);
120*78a26510SAndreas Gohr        sort($result);
121*78a26510SAndreas Gohr
122*78a26510SAndreas Gohr        $this->assertEquals($expected, $result);
123*78a26510SAndreas Gohr    }
124*78a26510SAndreas Gohr
125*78a26510SAndreas Gohr    public function testNonExisting()
126*78a26510SAndreas Gohr    {
127*78a26510SAndreas Gohr        $this->expectException(\RuntimeException::class);
128*78a26510SAndreas Gohr        $control = new ControlPageBuilder('does:not:exist');
129*78a26510SAndreas Gohr        $control->generate();
130*78a26510SAndreas Gohr        $foo = $control->getAll();
131*78a26510SAndreas Gohr    }
132*78a26510SAndreas Gohr}
133