1<?php
2
3namespace dokuwiki\plugin\navi\test;
4
5use DokuWikiTest;
6use phpQuery;
7
8
9/**
10 * Tests for functionality of the navi plugin
11 *
12 * @group plugin_navi
13 * @group plugins
14 *
15 */
16class BasicListTest extends DokuWikiTest
17{
18
19    protected $pluginsEnabled = ['navi'];
20
21    public function test_controlpage_simple()
22    {
23        // arrange
24        $controlpage = "  * [[a]]\n  * [[b]]\n    * [[c]]";
25        saveWikiText('controlpage', $controlpage, '');
26        saveWikiText('navi', '{{navi>controlpage}}', '');
27
28        // act
29        global $INFO;
30        $INFO['id'] = 'egal';
31
32        $info = [];
33        $actualHTML = p_render('xhtml', p_get_instructions('{{navi>controlpage}}'), $info);
34
35        // assert
36        $expectedHTML = '<div class="plugin__navi "><ul>
37<li class="level1 "><div class="li"><a href="/./doku.php?id=a" class="wikilink2" title="a" rel="nofollow" data-wiki-id="a">a</a></div>
38</li>
39<li class="level1 close"><div class="li"><a href="/./doku.php?id=b" class="wikilink2" title="b" rel="nofollow" data-wiki-id="b">b</a></div>
40</li>
41</ul>
42</div>';
43        $this->assertEquals($expectedHTML, $actualHTML);
44
45    }
46
47    public function test_controlpage_complex()
48    {
49        // arrange
50        $controlpage = "
51  * [[en:products:a:start|BasePage]]
52    * [[en:products:b:d:start|2nd-level Page with hidden child]]
53      * [[en:products:c:projects|hidden 3rd-level page]]
54    * [[en:products:b:archive:start|2nd-level pape]]
55    * [[en:products:c:start|current 2nd-level page with visible child]]
56      * [[en:products:d:start|visible 3rd-level page]]
57";
58        saveWikiText('controlpage', $controlpage, '');
59        saveWikiText('navi', '{{navi>controlpage}}', '');
60        global $ID, $INFO;
61
62        // act
63        $info = [];
64        $ID = 'en:products:c:start';
65        $INFO['id'] = 'en:products:c:start';
66        $actualHTML = p_render('xhtml', p_get_instructions('{{navi>controlpage}}'), $info);
67
68        if(class_exists('DOMWrap\Document')) {
69            $pq = (new \DOMWrap\Document())->html($actualHTML);
70        } else {
71            // deprecated
72            $pq = \phpQuery::newDocumentHTML($actualHTML);
73        }
74
75        $actualPages = [];
76        foreach ($pq->find('a') as $page) {
77            $actualPages[] = $page->getAttribute('title');
78        }
79
80        $actualLiOpen = [];
81        foreach ($pq->find('li.open > div > a, li.open > div > span > a') as $page) {
82            $actualLiOpen[] = $page->getAttribute('title');
83        }
84
85        $actualLiClose = [];
86        foreach ($pq->find('li.close > div > a, li.close > div > span > a') as $page) {
87            $actualLiClose[] = $page->getAttribute('title');
88        }
89
90        $this->assertEquals([
91            0 => 'en:products:a:start',
92            1 => 'en:products:b:d:start',
93            2 => 'en:products:b:archive:start',
94            3 => 'en:products:c:start',
95            4 => 'en:products:d:start',
96        ], $actualPages, 'the correct pages in the correct order');
97        $this->assertEquals([
98            0 => 'en:products:a:start',
99            1 => 'en:products:c:start',
100        ], $actualLiOpen, 'the pages which have have children and are open should have the "open" class');
101        $this->assertEquals([
102            0 => 'en:products:b:d:start',
103        ], $actualLiClose, 'the pages which have have children, but are closed should have the "close" class');
104
105    }
106}
107