1<?php
2
3require_once DOKU_INC.'inc/parser/xhtml.php';
4
5/**
6 * @group plugin_indexmenu
7 */
8class indexmenu_syntax_indexmenu_test extends DokuWikiTest {
9
10    private $exampleIndex;
11
12    public function setup() {
13        global $conf;
14        $this->pluginsEnabled[] = 'indexmenu';
15        parent::setup();
16
17        //$conf['plugin']['indexmenu']['headpage'] = '';
18        //$conf['plugin']['indexmenu']['hide_headpage'] = false;
19
20        //saveWikiText('titleonly:sub:test', "====== Title ====== \n content", 'created');
21        //saveWikiText('test', "====== Title ====== \n content", 'created');
22        //idx_addPage('titleonly:sub:test');
23        //idx_addPage('test');
24    }
25
26    function __construct() {
27        $this->exampleIndex = "{{indexmenu>:}}";
28    }
29
30    /**
31     * Create from list of values the output array of handle()
32     *
33     * @param array $values
34     * @return array aligned similar to output of handle()
35     */
36    function createData($values) {
37
38        list($ns, $theme, $identifier, $nocookie, $navbar, $noscroll, $maxjs, $notoc, $jsajax, $context, $nomenu,
39            $sort, $msort, $rsort, $nsort, $level, $nons, $nopg, $nss, $max, $js, $skipns, $skipfile, $hsort,
40            $headpage, $hide_headpage) = $values;
41
42        return array(
43            $ns,
44            Array(
45                'theme'      => $theme,
46                'identifier' => $identifier,
47                'nocookie'   => $nocookie,
48                'navbar'     => $navbar,
49                'noscroll'   => $noscroll,
50                'maxjs'      => $maxjs,
51                'notoc'      => $notoc,
52                'jsajax'     => $jsajax,
53                'context'    => $context,
54                'nomenu'     => $nomenu,
55            ),
56            $sort,
57            $msort,
58            $rsort,
59            $nsort,
60            array(
61                'level'         => $level,
62                'nons'          => $nons,
63                'nopg'          => $nopg,
64                'nss'           => $nss,
65                'max'           => $max,
66                'js'            => $js,
67                'skip_index'    => $skipns,
68                'skip_file'     => $skipfile,
69                'headpage'      => $headpage,
70                'hide_headpage' => $hide_headpage
71            ),
72            $hsort
73        );
74    }
75
76    /**
77     * Parse the syntax to options
78     * expect: different combinations with or without js option, covers recognizing all syntax options
79     */
80    function testHandle() {
81        global $conf;
82
83        $plugin = new syntax_plugin_indexmenu_indexmenu();
84
85        $null   = new Doku_Handler();
86        $result = $plugin->handle($this->exampleIndex, 0, 40, $null);
87
88        $idcalculatedfromns = sprintf("%u", crc32(''));
89        $tests              = array(
90            //root ns (empty is not recognized..)
91            array(
92                'syntax'=> "{{indexmenu>:}}",
93                'data'  => array(
94                    '', 'default', 'random', false, false, false, 0, false, '', false, false,
95                    0, false, false, false, -1, false, false, array(), 0, false, array(''), array(''), false,
96                    ":start:,:same:,:inside:", 1
97                )
98            ),
99            //root ns, #levels=1, js renderer
100            array(
101                'syntax'=> "{{indexmenu>#1|js}}",
102                'data'  => array(
103                    '', 'default', 'random', false, false, false, 0, false, '', false, false,
104                    0, false, false, false, 1, false, false, array(), 0, true, array(''), array(''), false,
105                    ":start:,:same:,:inside:", 1
106                )
107            ),
108            //root ns, #levels=2, all not js specific options  (nocookie is from context)
109            array(
110                'syntax'=> "{{indexmenu>#2 test#6|navbar context tsort dsort msort hsort rsort nsort nons nopg}}",
111                'data'  => array(
112                    '', 'default', 'random', true, true, false, 0, false, '&sort=t&msort=indexmenu_n&rsort=1&nsort=1&hsort=1&nopg=1', true, false,
113                    't', 'indexmenu_n', true, true, 2, true, true, array(array('test', 6)), 0, false, array(''), array(''), true,
114                    ":start:,:same:,:inside:", 1
115                )
116            ),
117            //root ns, #levels=2, js renderer, all not js specific options
118            array(
119                'syntax'=> "{{indexmenu>#2 test#6|navbar js#bj_ubuntu.png context tsort dsort msort hsort rsort nsort nons nopg}}",
120                'data'  => array(
121                    '', 'bj_ubuntu.png', 'random', true, true, false, 0, false, '&sort=t&msort=indexmenu_n&rsort=1&nsort=1&hsort=1&nopg=1', true, false,
122                    't', 'indexmenu_n', true, true, 2, true, true, array(array('test', 6)), 0, true, array(''), array(''), true,
123                    ":start:,:same:,:inside:", 1
124                ),
125            ),
126            //root ns, #levels=1, all options
127            array(
128                'syntax'=> "{{indexmenu>#1|navbar context nocookie noscroll notoc nomenu dsort msort#date:modified hsort rsort nsort nons nopg max#2#4 maxjs#3 id#54321}}",
129                'data'  => array(
130                    '', 'default', 'random', true, true, true, 0, true, '&sort=d&msort=date modified&rsort=1&nsort=1&hsort=1&nopg=1', true, true,
131                    'd', 'date modified', true, true, 1, true, true, array(), 0, false, array(''), array(''), true,
132                    ":start:,:same:,:inside:", 1
133                )
134            ),
135            //root ns, #levels=1, js renderer, all options
136            array(
137                'syntax'=> "{{indexmenu>#1|js#bj_ubuntu.png navbar context nocookie noscroll notoc nomenu dsort msort#date:modified hsort rsort nsort nons nopg max#2#4 maxjs#3 id#54321}}",
138                'data'  => array(
139                    '', 'bj_ubuntu.png', 54321, true, true, true, 3, true, '&sort=d&msort=date modified&rsort=1&nsort=1&hsort=1&nopg=1&max=4', true, true,
140                    'd', 'date modified', true, true, 1, true, true, array(), 2, true, array(''), array(''), true,
141                    ":start:,:same:,:inside:", 1
142                )
143            ),
144            //root ns, #levels=1, skipfile and ns
145
146            array(
147                'syntax'=> "{{indexmenu>#1 test|skipfile+/(^myusers:spaces$|privatens:userss)/ skipns=/(^myusers:spaces$|privatens:users)/ id#ns}}",
148                'data'  => array(
149                    '', 'default', 'random', false, false, false, 0, false, '&skipns=%3D/%28%5Emyusers%3Aspaces%24%7Cprivatens%3Ausers%29/&skipfile=%2B/%28%5Emyusers%3Aspaces%24%7Cprivatens%3Auserss%29/', false, false,
150                    0, false, false, false, 1, false, false, array(array('test', -1)), 0, false, array('/(^myusers:spaces$|privatens:users)/'), array('', '/(^myusers:spaces$|privatens:userss)/'), false,
151                    ":start:,:same:,:inside:", 1
152                )
153            ),
154            //root ns, #levels=1, js renderer, skipfile and ns
155            array(
156                'syntax'=> "{{indexmenu>#1 test|js skipfile=/(^myusers:spaces$|privatens:userss)/ skipns+/(^myusers:spaces$|privatens:userssss)/ id#ns}}",
157                'data'  => array(
158                    '', 'default', 0, false, false, false, 0, false, '&skipns=%2B/%28%5Emyusers%3Aspaces%24%7Cprivatens%3Auserssss%29/&skipfile=%3D/%28%5Emyusers%3Aspaces%24%7Cprivatens%3Auserss%29/', false, false,
159                    0, false, false, false, 1, false, false, array(array('test', -1)), 0, true, array('', '/(^myusers:spaces$|privatens:userssss)/'), array('/(^myusers:spaces$|privatens:userss)/'), false,
160                    ":start:,:same:,:inside:", 1
161                )
162            )
163        );
164
165        foreach($tests as $test) {
166            $null   = new Doku_Handler();
167            $result = $plugin->handle($test['syntax'], 0, 40, $null);
168
169            //copy unique generated number, which is about 23 characters
170            $len_id = strlen($result[1]['identifier']);
171            if(!is_numeric($test['data'][2]) && ($len_id > 20||$len_id<=23)) {
172                $test['data'][2] = $result[1]['identifier'];
173            }
174            $data = $this->createData($test['data']);
175
176            $this->assertEquals($data, $result, 'Data array corrupted');
177        }
178    }
179
180    /**
181     * Rendering for nonexisting namespace
182     * expect: no paragraph due to no message set
183     * expect: one paragraph, since message set
184     * expect: contains namespace which replaced {{ns}}
185     * expect: message contained rendered italic syntax
186     */
187    function testRenderEmptymsg() {
188        global $conf;
189
190        $noexistns        = 'nonexisting:namespace';
191        $emptyindexsyntax = "{{indexmenu>$noexistns}}";
192
193        $xhtml  = new Doku_Renderer_xhtml();
194        $plugin = new syntax_plugin_indexmenu_indexmenu();
195
196        $null   = new Doku_Handler();
197        $result = $plugin->handle($emptyindexsyntax, 0, 10, $null);
198
199        //no empty message
200        $plugin->render('xhtml', $xhtml, $result);
201        $doc = phpQuery::newDocument($xhtml->doc);
202        $this->assertEquals(0, pq('p', $doc)->length);
203
204        // Fill in empty message
205        $conf['plugin']['indexmenu']['empty_msg'] = 'This namespace is //empty//: {{ns}}';
206        $plugin->render('xhtml', $xhtml, $result);
207        $doc = phpQuery::newDocument($xhtml->doc);
208
209        $this->assertEquals(1, pq('p', $doc)->length);
210        $this->assertEquals(1, pq("p:contains($noexistns)")->length);
211        $this->assertEquals(1, pq("p em")->length);
212    }
213
214}
215