1<?php
2
3/**
4 * Tests for the filelist plugin.
5 *
6 * These test assume that the directory filelist has the following content:
7 * - exampledir (directory)
8 *   - example2.txt (text file)
9 * - example.txt (text file)
10 * - exampleimage.png (image file)
11 *
12 * @group plugin_filelist
13 * @group plugins
14 */
15class plugin_filelist_test extends DokuWikiTest {
16    public function setUp() {
17        global $conf;
18
19        $this->pluginsEnabled[] = 'filelist';
20        parent::setUp();
21
22        // Setup config so that access to the TMP directory will be allowed
23        $conf ['plugin']['filelist']['allowed_absolute_paths'] = TMP_DIR.'/filelistdata/';
24        $conf ['plugin']['filelist']['web_paths'] = 'http://localhost/';
25    }
26
27    public static function setUpBeforeClass(){
28        parent::setUpBeforeClass();
29
30        // copy test files to test directory
31        TestUtils::rcopy(TMP_DIR, dirname(__FILE__) . '/filelistdata');
32    }
33
34    /**
35     * This function checks that all files are listed in not recursive mode.
36     */
37    public function test_not_recursive () {
38        global $conf;
39
40        // Render filelist
41        $instructions = p_get_instructions('{{filelist>'.TMP_DIR.'/filelistdata/*&style=list&direct=1}}');
42        $xhtml = p_render('xhtml', $instructions, $info);
43
44        // We should find:
45        // - example.txt
46        // - exampleimage.png
47        $result = strpos ($xhtml, 'example.txt');
48        $this->assertFalse($result===false, '"example.txt" not listed');
49        $result = strpos ($xhtml, 'exampleimage.png');
50        $this->assertFalse($result===false, '"exampleimage.png" not listed');
51    }
52
53    /**
54     * This function checks that all files are listed in recursive mode.
55     */
56    public function test_recursive () {
57        global $conf;
58
59        // Render filelist
60        $instructions = p_get_instructions('{{filelist>'.TMP_DIR.'/filelistdata/*&style=list&direct=1&recursive=1}}');
61        $xhtml = p_render('xhtml', $instructions, $info);
62
63        // We should find:
64        // - exampledir
65        //   - example2.txt
66        // - example.txt
67        // - exampleimage.png
68        $result = strpos ($xhtml, 'exampledir');
69        $this->assertFalse($result===false, '"exampledir" not listed');
70        $result = strpos ($xhtml, 'example2.txt');
71        $this->assertFalse($result===false, '"example2.txt" not listed');
72        $result = strpos ($xhtml, 'example.txt');
73        $this->assertFalse($result===false, '"example.txt" not listed');
74        $result = strpos ($xhtml, 'exampleimage.png');
75        $this->assertFalse($result===false, '"exampleimage.png" not listed');
76    }
77
78    /**
79     * This function checks that the unordered list mode
80     * generates the expected XHTML structure.
81     */
82    public function test_list () {
83        global $conf;
84
85        // Render filelist
86        $instructions = p_get_instructions('{{filelist>'.TMP_DIR.'/filelistdata/*&style=list&direct=1&recursive=1}}');
87        $xhtml = p_render('xhtml', $instructions, $info);
88
89        $doc = new DOMDocument();
90        $doc->loadHTML($xhtml);
91
92        $first = $doc->documentElement;
93        $this->assertEquals('html', $first->tagName);
94
95        $children = $first->childNodes;
96        $this->assertTrue($children->length==1);
97        $this->assertEquals('body', $children[0]->nodeName);
98
99        // We should have 'div' first
100        $children = $children[0]->childNodes;
101        $this->assertTrue($children->length==1);
102        $this->assertEquals('div', $children[0]->nodeName);
103
104        // It should have the childs text, 'ol'
105        $children = $children[0]->childNodes;
106        $this->assertTrue($children->length==2);
107        $this->assertEquals('#text', $children[0]->nodeName);
108        $this->assertEquals('ul', $children[1]->nodeName);
109
110        // The 'ol' element should have 3 'li' childs
111        $children = $children[1]->childNodes;
112        $this->assertTrue($children->length==6);
113        $this->assertEquals('li', $children[0]->nodeName);
114        $this->assertEquals('#text', $children[1]->nodeName);
115        $this->assertEquals('li', $children[2]->nodeName);
116        $this->assertEquals('#text', $children[3]->nodeName);
117        $this->assertEquals('li', $children[4]->nodeName);
118        $this->assertEquals('#text', $children[5]->nodeName);
119
120        // First child of first 'li' should be the link to 'example.txt'
121        $node = $children[0];
122        $node_childs = $node->childNodes;
123        $this->assertEquals('level1', $node->getAttribute('class'));
124        $this->assertTrue($node_childs->length==1);
125        $this->assertEquals('a', $node_childs[0]->nodeName);
126        $this->assertEquals('example.txt', $node_childs[0]->nodeValue);
127
128        // First child of second 'li' is directory 'exampledir' and another 'ol'
129        $node = $children[2];
130        $node_childs = $node->childNodes;
131        $this->assertTrue($node_childs->length==2);
132        $this->assertEquals('#text', $node_childs[0]->nodeName);
133        $this->assertEquals('exampledir', $node_childs[0]->nodeValue);
134        $this->assertEquals('ul', $node_childs[1]->nodeName);
135
136        // That 'ol' should have one 'li' with 'class=level2'
137        $node_childs = $node_childs[1]->childNodes;
138        $this->assertTrue($node_childs->length==2);
139        $this->assertEquals('li', $node_childs[0]->nodeName);
140        $this->assertEquals('level2', $node_childs[0]->getAttribute('class'));
141        $this->assertEquals('#text', $node_childs[1]->nodeName);
142
143        // The link of that 'li' should reference 'example2.txt'
144        $node_childs = $node_childs[0]->childNodes;
145        $this->assertTrue($node_childs->length==1);
146        $this->assertEquals('a', $node_childs[0]->nodeName);
147        $this->assertEquals('example2.txt', $node_childs[0]->nodeValue);
148
149        // First child of third 'li' should be the link to 'exampleimage.png'
150        $node = $children[4];
151        $node_childs = $node->childNodes;
152        $this->assertEquals('level1', $node->getAttribute('class'));
153        $this->assertTrue($node_childs->length==1);
154        $this->assertEquals('a', $node_childs[0]->nodeName);
155        $this->assertEquals('exampleimage.png', $node_childs[0]->nodeValue);
156    }
157
158    /**
159     * This function checks that the ordered list mode
160     * generates the expected XHTML structure.
161     */
162    public function test_olist () {
163        global $conf;
164
165        // Render filelist
166        $instructions = p_get_instructions('{{filelist>'.TMP_DIR.'/filelistdata/*&style=olist&direct=1&recursive=1}}');
167        $xhtml = p_render('xhtml', $instructions, $info);
168
169        $doc = new DOMDocument();
170        $doc->loadHTML($xhtml);
171
172        $first = $doc->documentElement;
173        $this->assertEquals('html', $first->tagName);
174
175        $children = $first->childNodes;
176        $this->assertTrue($children->length==1);
177        $this->assertEquals('body', $children[0]->nodeName);
178
179        // We should have 'div' first
180        $children = $children[0]->childNodes;
181        $this->assertTrue($children->length==1);
182        $this->assertEquals('div', $children[0]->nodeName);
183
184        // It should have the childs text, 'ol'
185        $children = $children[0]->childNodes;
186        $this->assertTrue($children->length==2);
187        $this->assertEquals('#text', $children[0]->nodeName);
188        $this->assertEquals('ol', $children[1]->nodeName);
189
190        // The 'ol' element should have 3 'li' childs
191        $children = $children[1]->childNodes;
192        $this->assertTrue($children->length==6);
193        $this->assertEquals('li', $children[0]->nodeName);
194        $this->assertEquals('#text', $children[1]->nodeName);
195        $this->assertEquals('li', $children[2]->nodeName);
196        $this->assertEquals('#text', $children[3]->nodeName);
197        $this->assertEquals('li', $children[4]->nodeName);
198        $this->assertEquals('#text', $children[5]->nodeName);
199
200        // First child of first 'li' should be the link to 'example.txt'
201        $node = $children[0];
202        $node_childs = $node->childNodes;
203        $this->assertEquals('level1', $node->getAttribute('class'));
204        $this->assertTrue($node_childs->length==1);
205        $this->assertEquals('a', $node_childs[0]->nodeName);
206        $this->assertEquals('example.txt', $node_childs[0]->nodeValue);
207
208        // First child of second 'li' is directory 'exampledir' and another 'ol'
209        $node = $children[2];
210        $node_childs = $node->childNodes;
211        $this->assertTrue($node_childs->length==2);
212        $this->assertEquals('#text', $node_childs[0]->nodeName);
213        $this->assertEquals('exampledir', $node_childs[0]->nodeValue);
214        $this->assertEquals('ol', $node_childs[1]->nodeName);
215
216        // That 'ol' should have one 'li' with 'class=level2'
217        $node_childs = $node_childs[1]->childNodes;
218        $this->assertTrue($node_childs->length==2);
219        $this->assertEquals('li', $node_childs[0]->nodeName);
220        $this->assertEquals('level2', $node_childs[0]->getAttribute('class'));
221        $this->assertEquals('#text', $node_childs[1]->nodeName);
222
223        // The link of that 'li' should reference 'example2.txt'
224        $node_childs = $node_childs[0]->childNodes;
225        $this->assertTrue($node_childs->length==1);
226        $this->assertEquals('a', $node_childs[0]->nodeName);
227        $this->assertEquals('example2.txt', $node_childs[0]->nodeValue);
228
229        // First child of third 'li' should be the link to 'exampleimage.png'
230        $node = $children[4];
231        $node_childs = $node->childNodes;
232        $this->assertEquals('level1', $node->getAttribute('class'));
233        $this->assertTrue($node_childs->length==1);
234        $this->assertEquals('a', $node_childs[0]->nodeName);
235        $this->assertEquals('exampleimage.png', $node_childs[0]->nodeValue);
236    }
237
238    /**
239     * This function checks that the page mode
240     * generates the expected XHTML structure.
241     */
242    public function test_page () {
243        global $conf;
244
245        // Render filelist
246        $instructions = p_get_instructions('{{filelist>'.TMP_DIR.'/filelistdata/*&style=page&direct=1&recursive=1}}');
247        $xhtml = p_render('xhtml', $instructions, $info);
248
249        $doc = new DOMDocument();
250        $doc->loadHTML($xhtml);
251
252        $first = $doc->documentElement;
253        $this->assertEquals('html', $first->tagName);
254
255        $children = $first->childNodes;
256        $this->assertTrue($children->length==1);
257        $this->assertEquals('body', $children[0]->nodeName);
258
259        // We should have a 'ul', 'h1', '#test' and 'div' node
260        $children = $children[0]->childNodes;
261        $this->assertTrue($children->length==4);
262        $this->assertEquals('ul', $children[0]->nodeName);
263        $this->assertEquals('h1', $children[1]->nodeName);
264        $this->assertEquals('#text', $children[2]->nodeName);
265        $this->assertEquals('div', $children[3]->nodeName);
266
267        // 'ul' should have the childs 'li', text, 'li', text
268        //$children = $children[0]->childNodes;
269        $node_childs = $children[0]->childNodes;
270        $this->assertTrue($children->length==4);
271        $this->assertEquals('li', $node_childs[0]->nodeName);
272        $this->assertEquals('#text', $node_childs[1]->nodeName);
273        $this->assertEquals('li', $node_childs[2]->nodeName);
274        $this->assertEquals('#text', $node_childs[3]->nodeName);
275
276        // Check first 'li' contents
277        $node = $node_childs[0];
278        $node_childs = $node->childNodes;
279        $this->assertTrue($node_childs->length==1);
280        $this->assertEquals('a', $node_childs[0]->nodeName);
281        $this->assertEquals('example.txt', $node_childs[0]->nodeValue);
282
283        // Check second 'li' contents
284        $node = $children[0]->childNodes;
285        $node_childs = $node[2]->childNodes;
286        $this->assertTrue($node_childs->length==1);
287        $this->assertEquals('a', $node_childs[0]->nodeName);
288        $this->assertEquals('exampleimage.png', $node_childs[0]->nodeValue);
289
290        // Check 'h1' contents
291        $node = $children[1];
292        $this->assertEquals('h1', $node->nodeName);
293        $this->assertEquals('exampledir', $node->nodeValue);
294
295        // Check 'div' contents
296        $node = $children[3];
297        $this->assertEquals('div', $node->nodeName);
298        $this->assertEquals('level1', $node->getAttribute('class'));
299
300        // Check 'div' childs
301        $node_childs = $node->childNodes;
302        $this->assertTrue($node_childs->length==2);
303        $this->assertEquals('#text', $node_childs[0]->nodeName);
304        $this->assertEquals('ul', $node_childs[1]->nodeName);
305
306        // Check 'ul' childs, we should have 'li'
307        $node_childs = $node_childs[1]->childNodes;
308        $this->assertTrue($node_childs->length==2);
309        $this->assertEquals('li', $node_childs[0]->nodeName);
310        $this->assertEquals('#text', $node_childs[1]->nodeName);
311
312        // The 'li' should have a 'a'
313        $node_childs = $node_childs[0]->childNodes;
314        $this->assertTrue($node_childs->length==1);
315        $this->assertEquals('a', $node_childs[0]->nodeName);
316        $this->assertEquals('example2.txt', $node_childs[0]->nodeValue);
317    }
318
319    /**
320     * This function checks that the table mode
321     * generates the expected XHTML structure.
322     */
323    public function test_table () {
324        global $conf;
325
326        // Render filelist
327        $instructions = p_get_instructions('{{filelist>'.TMP_DIR.'/filelistdata/*&style=table&direct=1&recursive=1}}');
328        $xhtml = p_render('xhtml', $instructions, $info);
329
330        $doc = new DOMDocument();
331        $doc->loadHTML($xhtml);
332
333        $first = $doc->documentElement;
334        $this->assertEquals('html', $first->tagName);
335
336        $children = $first->childNodes;
337        $this->assertTrue($children->length==1);
338        $this->assertEquals('body', $children[0]->nodeName);
339
340        // We should have a 'div' node
341        $children = $children[0]->childNodes;
342        $this->assertTrue($children->length==1);
343        $this->assertEquals('div', $children[0]->nodeName);
344        $this->assertEquals('filelist-plugin', $children[0]->getAttribute('class'));
345
346        // Check 'div' childs
347        $children = $children[0]->childNodes;
348        $this->assertTrue($children->length==3);
349        $this->assertEquals('#text', $children[0]->nodeName);
350        $this->assertEquals('div', $children[1]->nodeName);
351        $this->assertEquals('table sectionedit1', $children[1]->getAttribute('class'));
352        $this->assertEquals('#text', $children[2]->nodeName);
353
354        // Check inner 'div' content
355        $children = $children[1]->childNodes;
356        $this->assertTrue($children->length==1);
357        $this->assertEquals('table', $children[0]->nodeName);
358
359        // Check inner 'table' content
360        $children = $children[0]->childNodes;
361        $this->assertTrue($children->length==3);
362        $this->assertEquals('tr', $children[0]->nodeName);
363        $this->assertEquals('tr', $children[1]->nodeName);
364        $this->assertEquals('tr', $children[2]->nodeName);
365
366        // Check table row 1
367        $node_childs = $children[0]->childNodes;
368        $this->assertTrue($node_childs->length==2);
369        $this->assertEquals('td', $node_childs[0]->nodeName);
370        $this->assertEquals('#text', $node_childs[1]->nodeName);
371
372        // Check table row 1/cell 1 content
373        $node_childs = $node_childs[0]->childNodes;
374        $this->assertTrue($node_childs->length==1);
375        $this->assertEquals('a', $node_childs[0]->nodeName);
376        $this->assertEquals('example.txt', $node_childs[0]->nodeValue);
377
378        // Check table row 2
379        $node_childs = $children[1]->childNodes;
380        $this->assertTrue($node_childs->length==2);
381        $this->assertEquals('td', $node_childs[0]->nodeName);
382        $this->assertEquals('#text', $node_childs[1]->nodeName);
383
384        // Check table row 2/cell 1 content
385        $node_childs = $node_childs[0]->childNodes;
386        $this->assertTrue($node_childs->length==1);
387        $this->assertEquals('a', $node_childs[0]->nodeName);
388        $this->assertEquals('exampledir/example2.txt', $node_childs[0]->nodeValue);
389
390        // Check table row 3
391        $node_childs = $children[2]->childNodes;
392        $this->assertTrue($node_childs->length==2);
393        $this->assertEquals('td', $node_childs[0]->nodeName);
394        $this->assertEquals('#text', $node_childs[1]->nodeName);
395
396        // Check table row 3/cell 1 content
397        $node_childs = $node_childs[0]->childNodes;
398        $this->assertTrue($node_childs->length==1);
399        $this->assertEquals('a', $node_childs[0]->nodeName);
400        $this->assertEquals('exampleimage.png', $node_childs[0]->nodeValue);
401
402        /*print_r ($node_childs->children[1]);
403        foreach ($node_childs as $node) {
404            print ("\nTEST:".$node->nodeName." : ".$node->nodeValue."\n");
405        }*/
406    }
407}
408