xref: /plugin/simplenavi/_test/SimplenaviTest.php (revision d8ce5486268fd1c02234429a87c5de7aaa6425c7)
1<?php
2
3namespace dokuwiki\plugin\simplenavi\test;
4
5use DokuWikiTest;
6use TestRequest;
7
8/**
9 * General tests for the simplenavi plugin
10 *
11 * @author  Michael Große <grosse@cosmocode.de>
12 *
13 * @group plugin_simplenavi
14 * @group plugins
15 */
16class SimplenaviTest extends DokuWikiTest
17{
18
19    protected $pluginsEnabled = array('simplenavi');
20
21    public function setUp(): void
22    {
23        parent::setUp();
24        saveWikiText('sidebar', '{{simplenavi>}}', 'create test sidebar');
25        saveWikiText('namespace1:foo', 'bar', 'foobar');
26        saveWikiText('namespace2:foo', 'bar', 'foobar');
27        saveWikiText('namespace12:foo', 'bar', 'foobar');
28        saveWikiText('namespace123:foo', 'bar', 'foobar');
29        saveWikiText('namespace21:foo', 'bar', 'foobar');
30
31    }
32
33    /**
34     * @covers syntax_plugin_simplenavi
35     */
36    public function testOutputNatural()
37    {
38        global $ID, $conf;
39        $conf['plugin']['simplenavi']['sort'] = 'natural';
40
41        $ID = 'wiki:start';
42        $request = new TestRequest();
43        $input = [
44            'id' => 'namespace1:foo',
45        ];
46        saveWikiText('wiki:start', 'some text', 'Test init');
47        $response = $request->post($input);
48        $naviBegin = strpos($response->getContent(), '<!-- ********** ASIDE ********** -->') + 36;
49        $naviEnd = strpos($response->getContent(), '<!-- /aside -->');
50        $navi = substr($response->getContent(), $naviBegin, $naviEnd - $naviBegin);
51        $navilines = explode("\n", $navi);
52        $listlines = [];
53        foreach ($navilines as $line) {
54            if (substr($line, 0, 4) != '<li ') continue;
55            if (strpos($line, 'namespace') === false) continue;
56            $listlines[] = $line;
57        }
58
59        $this->assertTrue(
60            strpos($listlines[0], 'href="/./doku.php?id=namespace1:start"') !== false,
61            'namespace1 should be before other namespaces and espacially before its subpages and namespaces'
62        );
63        $this->assertTrue(
64            strpos($listlines[1], 'href="/./doku.php?id=namespace1:foo"') !== false,
65            'level2 should follow open level1'
66        );
67        $this->assertTrue(
68            strpos($listlines[2], 'href="/./doku.php?id=namespace2:start"') !== false,
69            'namespace2 should be after namespace1 and its pages.'
70        );
71        $this->assertTrue(
72            strpos($listlines[3], 'href="/./doku.php?id=namespace12:start"') !== false,
73            'namespace12 should be after namespace2.'
74        );
75        $this->assertTrue(
76            strpos($listlines[4], 'href="/./doku.php?id=namespace21:start"') !== false,
77            'namespace21 should be after namespace12.'
78        );
79        $this->assertTrue(
80            strpos($listlines[5], 'href="/./doku.php?id=namespace123:start"') !== false,
81            'namespace123 should be after namespace21.'
82        );
83    }
84
85    /**
86     * @covers syntax_plugin_simplenavi
87     */
88    public function testOutputAscii()
89    {
90        global $ID, $conf;
91        $conf['plugin']['simplenavi']['sort'] = 'ascii';
92
93        $ID = 'wiki:start';
94        $request = new TestRequest();
95        $input = [
96            'id' => 'namespace1:foo',
97        ];
98        saveWikiText('wiki:start', 'some text', 'Test init');
99        $response = $request->post($input);
100        $naviBegin = strpos($response->getContent(), '<!-- ********** ASIDE ********** -->') + 36;
101        $naviEnd = strpos($response->getContent(), '<!-- /aside -->');
102        $navi = substr($response->getContent(), $naviBegin, $naviEnd - $naviBegin);
103        $navilines = explode("\n", $navi);
104        $listlines = [];
105        foreach ($navilines as $line) {
106            if (substr($line, 0, 4) != '<li ') continue;
107            if (strpos($line, 'namespace') === false) continue;
108            $listlines[] = $line;
109        }
110
111        $this->assertTrue(
112            strpos($listlines[0], 'href="/./doku.php?id=namespace1:start"') !== false,
113            'namespace1 should be before other namespaces and espacially before its subpages and namespaces'
114        );
115        $this->assertTrue(
116            strpos($listlines[1], 'href="/./doku.php?id=namespace1:foo"') !== false,
117            'level2 should follow open level1.'
118        );
119        $this->assertTrue(
120            strpos($listlines[2], 'href="/./doku.php?id=namespace12:start"') !== false,
121            'namespace12 should be after namespace1 and its pages.'
122        );
123        $this->assertTrue(
124            strpos($listlines[3], 'href="/./doku.php?id=namespace123:start"') !== false,
125            'namespace123 should be after namespace12.'
126        );
127        $this->assertTrue(strpos(
128                $listlines[4], 'href="/./doku.php?id=namespace2:start"') !== false,
129            'namespace2 should be after namespace123.'
130        );
131        $this->assertTrue(
132            strpos($listlines[5], 'href="/./doku.php?id=namespace21:start"') !== false,
133            'namespace21 should be after namespace2.'
134        );
135    }
136
137}
138
139
140
141