xref: /plugin/simplenavi/_test/SimplenaviTest.php (revision b1fcfa9f85d776273177fea2f3e6ee67513b4ab2)
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    {
23d8ce5486SAndreas Gohr        parent::setUp();
24d8ce5486SAndreas Gohr        saveWikiText('sidebar', '{{simplenavi>}}', 'create test sidebar');
25e75a33bfSAndreas Gohr
26e75a33bfSAndreas Gohr        $pages = [
27e58e2f72SAndreas Gohr            ['foo', 'Foo Page'],
28d418c031SAndreas Gohr            ['simplenavi', 'Self Start'],
29e75a33bfSAndreas Gohr            ['namespace1:start', 'ZZZ Namespace 1 Start'],
30e75a33bfSAndreas Gohr            ['namespace2:foo', 'Namespace 2 Foo'],
31e75a33bfSAndreas Gohr            ['namespace2', 'Namespace 2 Start'],
32e75a33bfSAndreas Gohr            ['namespace12:foo', 'Namespace 12 Foo'],
33e75a33bfSAndreas Gohr            ['namespace12:start', 'Namespace 12 Start'],
34e75a33bfSAndreas Gohr            ['namespace123:namespace123', 'AAA Namespace 123 Start'],
35e75a33bfSAndreas Gohr            ['namespace123:foo', 'Namespace 123 Foo'],
36e75a33bfSAndreas Gohr            ['namespace123:deep:start', 'Namespace 123 Deep Start'],
37e75a33bfSAndreas Gohr            ['namespace123:deep:foo', 'Namespace 123 Deep Foo'],
38e75a33bfSAndreas Gohr            ['namespace21:foo', 'Namespace 21 Foo'],
39e75a33bfSAndreas Gohr            ['namespace21:start', 'Namespace 21 Start'],
40e75a33bfSAndreas Gohr        ];
41e75a33bfSAndreas Gohr
42e75a33bfSAndreas Gohr        foreach ($pages as $page) {
43e75a33bfSAndreas Gohr            saveWikiText('simplenavi:' . $page[0], '====== ' . $page[1] . ' ======', 'create test page');
44e75a33bfSAndreas Gohr        }
45e75a33bfSAndreas Gohr
46e75a33bfSAndreas Gohr    }
47e75a33bfSAndreas Gohr
48d418c031SAndreas Gohr    public function dataProvider()
49d418c031SAndreas Gohr    {
50e75a33bfSAndreas Gohr
51e75a33bfSAndreas Gohr        yield [
52e75a33bfSAndreas Gohr            'set' => 'by ID, all branches closed',
534c5e7fe5SAndreas Gohr            'sort' => 'id',
544c5e7fe5SAndreas Gohr            'usetitle' => false,
55d418c031SAndreas Gohr            'home' => false,
56e58e2f72SAndreas Gohr            'current' => 'simplenavi:page',
57e75a33bfSAndreas Gohr            'expect' => [
584c5e7fe5SAndreas Gohr                '+simplenavi:foo',
594c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
604c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
614c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
624c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
634c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
64e75a33bfSAndreas Gohr            ]
65e75a33bfSAndreas Gohr        ];
66e75a33bfSAndreas Gohr
67e75a33bfSAndreas Gohr        yield [
68e75a33bfSAndreas Gohr            'set' => 'by ID, branch open',
694c5e7fe5SAndreas Gohr            'sort' => 'id',
704c5e7fe5SAndreas Gohr            'usetitle' => false,
71d418c031SAndreas Gohr            'home' => false,
72e75a33bfSAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
73e75a33bfSAndreas Gohr            'expect' => [
744c5e7fe5SAndreas Gohr                '+simplenavi:foo',
754c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
764c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
774c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
784c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:deep:start',
794c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:foo',
804c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:foo',
814c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
824c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
83e75a33bfSAndreas Gohr            ]
84e75a33bfSAndreas Gohr        ];
85e75a33bfSAndreas Gohr
86e75a33bfSAndreas Gohr
87e75a33bfSAndreas Gohr        yield [
88e75a33bfSAndreas Gohr            'set' => 'by Title, Natural Search, all branches closed',
894c5e7fe5SAndreas Gohr            'sort' => 'title',
904c5e7fe5SAndreas Gohr            'usetitle' => true,
91d418c031SAndreas Gohr            'home' => false,
92e58e2f72SAndreas Gohr            'current' => 'simplenavi:page',
93e75a33bfSAndreas Gohr            'expect' => [
944c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
954c5e7fe5SAndreas Gohr                '+simplenavi:foo',
964c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
974c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
984c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
994c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
100e75a33bfSAndreas Gohr            ]
101e75a33bfSAndreas Gohr        ];
102e75a33bfSAndreas Gohr
103e75a33bfSAndreas Gohr        yield [
104e75a33bfSAndreas Gohr            'set' => 'by Title, Natural Sort, branch open',
1054c5e7fe5SAndreas Gohr            'sort' => 'title',
1064c5e7fe5SAndreas Gohr            'usetitle' => true,
107d418c031SAndreas Gohr            'home' => false,
108e58e2f72SAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
109e58e2f72SAndreas Gohr            'expect' => [
1104c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
1114c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:deep:start',
1124c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:foo',
1134c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:foo',
1144c5e7fe5SAndreas Gohr                '+simplenavi:foo',
1154c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
1164c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
1174c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
1184c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
119e58e2f72SAndreas Gohr            ]
120e58e2f72SAndreas Gohr        ];
121e58e2f72SAndreas Gohr
122e58e2f72SAndreas Gohr        yield [
123e58e2f72SAndreas Gohr            'set' => 'by Title, Natural Sort, NS first, branch open',
1244c5e7fe5SAndreas Gohr            'sort' => 'ns_title',
1254c5e7fe5SAndreas Gohr            'usetitle' => true,
126d418c031SAndreas Gohr            'home' => false,
127e75a33bfSAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
128e75a33bfSAndreas Gohr            'expect' => [
1294c5e7fe5SAndreas Gohr                '+simplenavi:namespace123:namespace123',
1304c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:deep:start',
1314c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:foo',
1324c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:foo',
1334c5e7fe5SAndreas Gohr                '+simplenavi:namespace2',
1344c5e7fe5SAndreas Gohr                '+simplenavi:namespace12:start',
1354c5e7fe5SAndreas Gohr                '+simplenavi:namespace21:start',
1364c5e7fe5SAndreas Gohr                '+simplenavi:namespace1:start',
1374c5e7fe5SAndreas Gohr                '+simplenavi:foo',
138e75a33bfSAndreas Gohr            ]
139e75a33bfSAndreas Gohr        ];
140d418c031SAndreas Gohr
141d418c031SAndreas Gohr        yield [
142d418c031SAndreas Gohr            'set' => 'by ID, branch open with home level',
1434c5e7fe5SAndreas Gohr            'sort' => 'id',
1444c5e7fe5SAndreas Gohr            'usetitle' => false,
145d418c031SAndreas Gohr            'home' => true,
146d418c031SAndreas Gohr            'current' => 'simplenavi:namespace123:deep:foo',
147d418c031SAndreas Gohr            'expect' => [
1484c5e7fe5SAndreas Gohr                '+simplenavi:simplenavi',
1494c5e7fe5SAndreas Gohr                '++simplenavi:foo',
1504c5e7fe5SAndreas Gohr                '++simplenavi:namespace1:start',
1514c5e7fe5SAndreas Gohr                '++simplenavi:namespace12:start',
1524c5e7fe5SAndreas Gohr                '++simplenavi:namespace123:namespace123',
1534c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:deep:start',
1544c5e7fe5SAndreas Gohr                '++++simplenavi:namespace123:deep:foo',
1554c5e7fe5SAndreas Gohr                '+++simplenavi:namespace123:foo',
1564c5e7fe5SAndreas Gohr                '++simplenavi:namespace2',
1574c5e7fe5SAndreas Gohr                '++simplenavi:namespace21:start',
158d418c031SAndreas Gohr            ]
159d418c031SAndreas Gohr        ];
160d8ce5486SAndreas Gohr    }
161d8ce5486SAndreas Gohr
162d8ce5486SAndreas Gohr    /**
163e75a33bfSAndreas Gohr     * @dataProvider dataProvider
164d8ce5486SAndreas Gohr     */
1654c5e7fe5SAndreas Gohr    public function testSorting($set, $sort, $usetitle, $home, $current, $expect)
166d8ce5486SAndreas Gohr    {
167e75a33bfSAndreas Gohr        $simpleNavi = new \syntax_plugin_simplenavi();
1684c5e7fe5SAndreas Gohr
1694c5e7fe5SAndreas Gohr        $simpleNavi->initState('simplenavi', $current, $usetitle, $sort, $home);
1704c5e7fe5SAndreas Gohr
1714c5e7fe5SAndreas Gohr        /** @var PageTreeBuilder $tree */
1724c5e7fe5SAndreas Gohr        $tree = $this->callInaccessibleMethod($simpleNavi, 'getTree', []);
1734c5e7fe5SAndreas Gohr
1744c5e7fe5SAndreas Gohr
1754c5e7fe5SAndreas Gohr        $this->assertSame(join("\n", $expect), (string) $tree, $set);
176d8ce5486SAndreas Gohr    }
177d8ce5486SAndreas Gohr
178*b1fcfa9fSAndreas Gohr (aider)    /**
179*b1fcfa9fSAndreas Gohr (aider)     * Test the isParent method
180*b1fcfa9fSAndreas Gohr (aider)     */
181*b1fcfa9fSAndreas Gohr (aider)    public function testIsParent(): void
182*b1fcfa9fSAndreas Gohr (aider)    {
183*b1fcfa9fSAndreas Gohr (aider)        $simpleNavi = new \syntax_plugin_simplenavi();
184*b1fcfa9fSAndreas Gohr (aider)
185*b1fcfa9fSAndreas Gohr (aider)        // Test cases where parent is a parent of child
186*b1fcfa9fSAndreas Gohr (aider)        $this->assertTrue($this->callInaccessibleMethod($simpleNavi, 'isParent', ['namespace1:namespace2:page', 'namespace1']));
187*b1fcfa9fSAndreas Gohr (aider)        $this->assertTrue($this->callInaccessibleMethod($simpleNavi, 'isParent', ['namespace1:namespace2:page', 'namespace1:namespace2']));
188*b1fcfa9fSAndreas Gohr (aider)        $this->assertTrue($this->callInaccessibleMethod($simpleNavi, 'isParent', ['namespace1:page', 'namespace1']));
189*b1fcfa9fSAndreas Gohr (aider)
190*b1fcfa9fSAndreas Gohr (aider)        // Test cases where parent is not a parent of child
191*b1fcfa9fSAndreas Gohr (aider)        $this->assertFalse($this->callInaccessibleMethod($simpleNavi, 'isParent', ['namespace1:page', 'namespace2']));
192*b1fcfa9fSAndreas Gohr (aider)        $this->assertFalse($this->callInaccessibleMethod($simpleNavi, 'isParent', ['namespace1:namespace2:page', 'namespace1:namespace3']));
193*b1fcfa9fSAndreas Gohr (aider)
194*b1fcfa9fSAndreas Gohr (aider)        // Test edge cases
195*b1fcfa9fSAndreas Gohr (aider)        $this->assertTrue($this->callInaccessibleMethod($simpleNavi, 'isParent', ['page', ''])); // Empty parent is parent of all
196*b1fcfa9fSAndreas Gohr (aider)        $this->assertTrue($this->callInaccessibleMethod($simpleNavi, 'isParent', ['page', 'page'])); // Page is parent of itself
197*b1fcfa9fSAndreas Gohr (aider)    }
198*b1fcfa9fSAndreas Gohr (aider)
199d8ce5486SAndreas Gohr}
200d8ce5486SAndreas Gohr
201d8ce5486SAndreas Gohr
202d8ce5486SAndreas Gohr
203