xref: /plugin/simplenavi/_test/SimplenaviTest.php (revision a2e567776f298dcdd7d4e640b1852bc3062ba4d3)
1d8ce5486SAndreas Gohr<?php
2d8ce5486SAndreas Gohr
3d8ce5486SAndreas Gohrnamespace dokuwiki\plugin\simplenavi\test;
4d8ce5486SAndreas Gohr
54c5e7fe5SAndreas Gohruse dokuwiki\TreeBuilder\PageTreeBuilder;
6d8ce5486SAndreas Gohruse DokuWikiTest;
7d8ce5486SAndreas Gohr
8d8ce5486SAndreas Gohr/**
9d8ce5486SAndreas Gohr * General tests for the simplenavi plugin
10d8ce5486SAndreas Gohr *
11d8ce5486SAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
12d8ce5486SAndreas Gohr *
13d8ce5486SAndreas Gohr * @group plugin_simplenavi
14d8ce5486SAndreas Gohr * @group plugins
15d8ce5486SAndreas Gohr */
16d8ce5486SAndreas Gohrclass SimplenaviTest extends DokuWikiTest
17d8ce5486SAndreas Gohr{
18d8ce5486SAndreas Gohr
19d8ce5486SAndreas Gohr    protected $pluginsEnabled = array('simplenavi');
20d8ce5486SAndreas Gohr
21d8ce5486SAndreas Gohr    public function setUp(): void
22d8ce5486SAndreas Gohr    {
23*a2e56777SAndreas Gohr        global $conf;
24*a2e56777SAndreas Gohr
25d8ce5486SAndreas Gohr        parent::setUp();
26d8ce5486SAndreas Gohr        saveWikiText('sidebar', '{{simplenavi>}}', 'create test sidebar');
27e75a33bfSAndreas Gohr
28*a2e56777SAndreas Gohr        $conf['hidepages'] = ':hidden(:|$)';
29*a2e56777SAndreas Gohr
30e75a33bfSAndreas Gohr        $pages = [
31e58e2f72SAndreas Gohr            ['foo', 'Foo Page'],
32d418c031SAndreas Gohr            ['simplenavi', 'Self Start'],
33e75a33bfSAndreas Gohr            ['namespace1:start', 'ZZZ Namespace 1 Start'],
34e75a33bfSAndreas Gohr            ['namespace2:foo', 'Namespace 2 Foo'],
35e75a33bfSAndreas Gohr            ['namespace2', 'Namespace 2 Start'],
36e75a33bfSAndreas Gohr            ['namespace12:foo', 'Namespace 12 Foo'],
37e75a33bfSAndreas Gohr            ['namespace12:start', 'Namespace 12 Start'],
38e75a33bfSAndreas Gohr            ['namespace123:namespace123', 'AAA Namespace 123 Start'],
39e75a33bfSAndreas Gohr            ['namespace123:foo', 'Namespace 123 Foo'],
40*a2e56777SAndreas Gohr            ['namespace123:hidden', 'A hidden page'],
41e75a33bfSAndreas Gohr            ['namespace123:deep:start', 'Namespace 123 Deep Start'],
42e75a33bfSAndreas Gohr            ['namespace123:deep:foo', 'Namespace 123 Deep Foo'],
43e75a33bfSAndreas Gohr            ['namespace21:foo', 'Namespace 21 Foo'],
44e75a33bfSAndreas Gohr            ['namespace21:start', 'Namespace 21 Start'],
45e75a33bfSAndreas Gohr        ];
46e75a33bfSAndreas Gohr
47e75a33bfSAndreas Gohr        foreach ($pages as $page) {
48e75a33bfSAndreas Gohr            saveWikiText('simplenavi:' . $page[0], '====== ' . $page[1] . ' ======', 'create test page');
49e75a33bfSAndreas Gohr        }
50e75a33bfSAndreas Gohr
51e75a33bfSAndreas Gohr    }
52e75a33bfSAndreas Gohr
53d418c031SAndreas Gohr    public function dataProvider()
54d418c031SAndreas Gohr    {
55e75a33bfSAndreas Gohr
56e75a33bfSAndreas Gohr        yield [
57e75a33bfSAndreas Gohr            'set' => 'by ID, all branches closed',
584c5e7fe5SAndreas Gohr            'sort' => 'id',
594c5e7fe5SAndreas Gohr            'usetitle' => false,
60d418c031SAndreas Gohr            'home' => false,
61e58e2f72SAndreas Gohr            'current' => 'simplenavi:page',
62e75a33bfSAndreas Gohr            'expect' => [
634c5e7fe5SAndreas Gohr                '+simplenavi:foo',
644c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
654c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
664c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
674c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
684c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
69e75a33bfSAndreas Gohr            ]
70e75a33bfSAndreas Gohr        ];
71e75a33bfSAndreas Gohr
72e75a33bfSAndreas Gohr        yield [
73e75a33bfSAndreas Gohr            'set' => 'by ID, branch open',
744c5e7fe5SAndreas Gohr            'sort' => 'id',
754c5e7fe5SAndreas Gohr            'usetitle' => false,
76d418c031SAndreas Gohr            'home' => false,
77e75a33bfSAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
78e75a33bfSAndreas Gohr            'expect' => [
794c5e7fe5SAndreas Gohr                '+simplenavi:foo',
804c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
814c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
824c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
834c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:deep:start',
844c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:foo',
854c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:foo',
864c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
874c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
88e75a33bfSAndreas Gohr            ]
89e75a33bfSAndreas Gohr        ];
90e75a33bfSAndreas Gohr
91e75a33bfSAndreas Gohr
92e75a33bfSAndreas Gohr        yield [
93e75a33bfSAndreas Gohr            'set' => 'by Title, Natural Search, all branches closed',
944c5e7fe5SAndreas Gohr            'sort' => 'title',
954c5e7fe5SAndreas Gohr            'usetitle' => true,
96d418c031SAndreas Gohr            'home' => false,
97e58e2f72SAndreas Gohr            'current' => 'simplenavi:page',
98e75a33bfSAndreas Gohr            'expect' => [
994c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
1004c5e7fe5SAndreas Gohr                '+simplenavi:foo',
1014c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
1024c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
1034c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
1044c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
105e75a33bfSAndreas Gohr            ]
106e75a33bfSAndreas Gohr        ];
107e75a33bfSAndreas Gohr
108e75a33bfSAndreas Gohr        yield [
109e75a33bfSAndreas Gohr            'set' => 'by Title, Natural Sort, branch open',
1104c5e7fe5SAndreas Gohr            'sort' => 'title',
1114c5e7fe5SAndreas Gohr            'usetitle' => true,
112d418c031SAndreas Gohr            'home' => false,
113e58e2f72SAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
114e58e2f72SAndreas Gohr            'expect' => [
1154c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
1164c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:deep:start',
1174c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:foo',
1184c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:foo',
1194c5e7fe5SAndreas Gohr                '+simplenavi:foo',
1204c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
1214c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
1224c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
1234c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
124e58e2f72SAndreas Gohr            ]
125e58e2f72SAndreas Gohr        ];
126e58e2f72SAndreas Gohr
127e58e2f72SAndreas Gohr        yield [
128e58e2f72SAndreas Gohr            'set' => 'by Title, Natural Sort, NS first, branch open',
1294c5e7fe5SAndreas Gohr            'sort' => 'ns_title',
1304c5e7fe5SAndreas Gohr            'usetitle' => true,
131d418c031SAndreas Gohr            'home' => false,
132e75a33bfSAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
133e75a33bfSAndreas Gohr            'expect' => [
1344c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
1354c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:deep:start',
1364c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:foo',
1374c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:foo',
1384c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
1394c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
1404c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
1414c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
1424c5e7fe5SAndreas Gohr                '+simplenavi:foo',
143e75a33bfSAndreas Gohr            ]
144e75a33bfSAndreas Gohr        ];
145d418c031SAndreas Gohr
146d418c031SAndreas Gohr        yield [
147d418c031SAndreas Gohr            'set' => 'by ID, branch open with home level',
1484c5e7fe5SAndreas Gohr            'sort' => 'id',
1494c5e7fe5SAndreas Gohr            'usetitle' => false,
150d418c031SAndreas Gohr            'home' => true,
151d418c031SAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
152d418c031SAndreas Gohr            'expect' => [
1534c5e7fe5SAndreas Gohr                '+simplenavi:simplenavi',
1544c5e7fe5SAndreas Gohr                '++simplenavi:foo',
1554c5e7fe5SAndreas Gohr                '++simplenavi:namespace1:start',
1564c5e7fe5SAndreas Gohr                '++simplenavi:namespace12:start',
1574c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:namespace123',
1584c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:start',
1594c5e7fe5SAndreas Gohr                '++++simplenavi:namespace123:deep:foo',
1604c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:foo',
1614c5e7fe5SAndreas Gohr                '++simplenavi:namespace2',
1624c5e7fe5SAndreas Gohr                '++simplenavi:namespace21:start',
163d418c031SAndreas Gohr            ]
164d418c031SAndreas Gohr        ];
165d8ce5486SAndreas Gohr    }
166d8ce5486SAndreas Gohr
167d8ce5486SAndreas Gohr    /**
168e75a33bfSAndreas Gohr     * @dataProvider dataProvider
169d8ce5486SAndreas Gohr     */
1704c5e7fe5SAndreas Gohr    public function testSorting($set, $sort, $usetitle, $home, $current, $expect)
171d8ce5486SAndreas Gohr    {
172e75a33bfSAndreas Gohr        $simpleNavi = new \syntax_plugin_simplenavi();
1734c5e7fe5SAndreas Gohr
1744c5e7fe5SAndreas Gohr        $simpleNavi->initState('simplenavi', $current, $usetitle, $sort, $home);
1754c5e7fe5SAndreas Gohr
1764c5e7fe5SAndreas Gohr        /** @var PageTreeBuilder $tree */
1774c5e7fe5SAndreas Gohr        $tree = $this->callInaccessibleMethod($simpleNavi, 'getTree', []);
1784c5e7fe5SAndreas Gohr
1794c5e7fe5SAndreas Gohr
1804c5e7fe5SAndreas Gohr        $this->assertSame(join("\n", $expect), (string) $tree, $set);
181d8ce5486SAndreas Gohr    }
182d8ce5486SAndreas Gohr
183b1fcfa9fSAndreas Gohr (aider)    /**
18402d11b9cSAndreas Gohr (aider)     * Data provider for isParent test
185b1fcfa9fSAndreas Gohr (aider)     */
18602d11b9cSAndreas Gohr (aider)    public function isParentDataProvider(): array
187b1fcfa9fSAndreas Gohr (aider)    {
18802d11b9cSAndreas Gohr (aider)        return [
189b1fcfa9fSAndreas Gohr (aider)            // Test cases where parent is a parent of child
19002d11b9cSAndreas Gohr (aider)            'parent of deep child' => [true, 'namespace1:namespace2:page', 'namespace1'],
19102d11b9cSAndreas Gohr (aider)            'direct parent' => [true, 'namespace1:namespace2:page', 'namespace1:namespace2'],
19202d11b9cSAndreas Gohr (aider)            'parent of page' => [true, 'namespace1:page', 'namespace1'],
193b1fcfa9fSAndreas Gohr (aider)
194b1fcfa9fSAndreas Gohr (aider)            // Test cases where parent is not a parent of child
19502d11b9cSAndreas Gohr (aider)            'different namespace' => [false, 'namespace1:page', 'namespace2'],
19602d11b9cSAndreas Gohr (aider)            'sibling namespace' => [false, 'namespace1:namespace2:page', 'namespace1:namespace3'],
197b1fcfa9fSAndreas Gohr (aider)
198b1fcfa9fSAndreas Gohr (aider)            // Test edge cases
19902d11b9cSAndreas Gohr (aider)            'empty parent' => [true, 'page', ''], // Empty parent is parent of all
20002d11b9cSAndreas Gohr (aider)            'self as parent' => [true, 'page', 'page'], // Page is parent of itself
20102d11b9cSAndreas Gohr (aider)        ];
20202d11b9cSAndreas Gohr (aider)    }
20302d11b9cSAndreas Gohr (aider)
20402d11b9cSAndreas Gohr (aider)    /**
20502d11b9cSAndreas Gohr (aider)     * Test the isParent method
20602d11b9cSAndreas Gohr (aider)     * @dataProvider isParentDataProvider
20702d11b9cSAndreas Gohr (aider)     */
20802d11b9cSAndreas Gohr (aider)    public function testIsParent(bool $expected, string $child, string $parent): void
20902d11b9cSAndreas Gohr (aider)    {
21002d11b9cSAndreas Gohr (aider)        $simpleNavi = new \syntax_plugin_simplenavi();
21102d11b9cSAndreas Gohr (aider)        $result = $this->callInaccessibleMethod($simpleNavi, 'isParent', [$child, $parent]);
21202d11b9cSAndreas Gohr (aider)        $this->assertSame($expected, $result);
213b1fcfa9fSAndreas Gohr (aider)    }
214b1fcfa9fSAndreas Gohr (aider)
215d8ce5486SAndreas Gohr}
216d8ce5486SAndreas Gohr
217d8ce5486SAndreas Gohr
218d8ce5486SAndreas Gohr
219