1<?php
2
3if (!defined('DOKU_INC')) die();
4
5/**
6 * Test namespace includes
7 *
8 * @group plugin_include
9 * @group plugins
10 */
11class plugin_include_namespaces_includes_test extends DokuWikiTest {
12    /**
13     * @var helper_plugin_include $helper
14     */
15    private $helper;
16
17    /**
18     * Setup - enable and load the include plugin and create the test pages
19     */
20    public function setUp() : void
21    {
22        $this->pluginsEnabled[] = 'include';
23        parent::setUp(); // this enables the include plugin
24        $this->helper = plugin_load('helper', 'include');
25
26        global $conf;
27        $conf['hidepages'] = 'inclhidden:hidden';
28
29        // for testing hidden pages
30        saveWikiText('inclhidden:hidden', 'Hidden page', 'Created hidden page');
31        saveWikiText('inclhidden:visible', 'Visible page', 'Created visible page');
32
33        // pages on different levels
34        saveWikiText('incltest:level1', 'Page on level 1', 'Created page on level 1');
35        saveWikiText('incltest:ns:level2', 'Page on level 2', 'Created page on level 2');
36        saveWikiText('incltest:ns:ns:level3', 'Page on level 3', 'Created page on level 3');
37
38        // for page ordering
39        saveWikiText('inclorder:page1', 'Page 1', 'Created page 1');
40        saveWikiText('inclorder:page2', 'Page 2', 'Created page 2');
41        saveWikiText('inclorder:page3', '{{include_n>10}} Page 3/10', 'created page 3/1');
42        saveWikiText('inclorder:page4', '{{include_n>2}} Page 4/2', 'created page 4/0');
43    }
44
45    /**
46     * Helper function to read dir content
47     */
48    protected function getDirContent ($dir) {
49        if (is_dir($dir)) {
50            $pages = array();
51            if ($handle = opendir($dir)) {
52                while (($file = readdir($handle)) !== false) {
53                    if ($file != '.' && $file != '..') {
54                        $pages [] = $file;
55                    }
56                }
57                closedir($handle);
58                return $pages;
59            }
60        }
61        return null;
62    }
63
64    /**
65     * Test hiding of hidden pages in namespace includes
66     */
67    public function test_hidden() {
68        $flags = $this->helper->get_flags(array());
69        $pages = $this->helper->_get_included_pages('namespace', 'inclhidden:', '', '', $flags);
70        $this->assertEquals(array(
71                                 array('id' => 'inclhidden:visible', 'exists' => true, 'parent_id' => ''),
72                            ), $pages);
73    }
74
75    /**
76     * Test include depth limit
77     */
78    public function test_depth() {
79        $flags = $this->helper->get_flags(array());
80        $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags);
81        $this->assertEquals(array(
82                                 array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
83                            ), $pages);
84        $flags = $this->helper->get_flags(array('depth=2'));
85        $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags);
86        $this->assertEquals(array(
87                                 array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
88                                 array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''),
89                            ), $pages);
90        $flags = $this->helper->get_flags(array('depth=2'));
91        $pages = $this->helper->_get_included_pages('namespace', 'incltest:ns', '', '', $flags);
92        $this->assertEquals(array(
93                                 array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''),
94                                 array('id' => 'incltest:ns:ns:level3', 'exists' => true, 'parent_id' => ''),
95                            ), $pages);
96        $flags = $this->helper->get_flags(array('depth=0'));
97        $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags);
98        $this->assertEquals(array(
99                                 array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
100                                 array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''),
101                                 array('id' => 'incltest:ns:ns:level3', 'exists' => true, 'parent_id' => ''),
102                            ), $pages);
103
104        // test include of the root namespace
105        $flags = $this->helper->get_flags(array());
106        $pages = $this->helper->_get_included_pages('namespace', ':', '', '', $flags);
107        $this->assertEquals(array(array('id' => 'mailinglist', 'exists' => true, 'parent_id' => '')), $pages);
108        $flags = $this->helper->get_flags(array('depth=2'));
109        $pages = $this->helper->_get_included_pages('namespace', ':', '', '', $flags);
110        $expected = array(
111                                 array('id' => 'inclhidden:visible', 'exists' => true, 'parent_id' => ''),
112                                 array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
113                                 array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
114                                 array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
115                                 array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
116                                 array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
117                                 array('id' => 'mailinglist', 'exists' => true, 'parent_id' => ''),
118                                 //array('id' => 'wiki:dokuwiki', 'exists' => true, 'parent_id' => ''),
119                                 //array('id' => 'wiki:syntax', 'exists' => true, 'parent_id' => ''),
120                                 //$wikiPages,
121                            );
122
123        // page int:editandsavetest exists in DokuWiki after September 2017
124        if (page_exists('int:editandsavetest')) {
125            $expected [] = array('id' => 'int:editandsavetest', 'exists' => true, 'parent_id' => '');
126        }
127
128        // Add pages in namespace wiki
129        $dir = $this->getDirContent(dirname(__FILE__).'/../../../../_test/data/pages/wiki');
130        $this->assertTrue($dir !== null);
131        foreach ($dir as $page) {
132            $page = substr($page, 0, -4);
133            $expected [] = array('id' => 'wiki:'.$page, 'exists' => true, 'parent_id' => '');
134        }
135
136        array_multisort($expected);
137        array_multisort($pages);
138        $this->assertEquals($expected, $pages);
139    }
140
141    /**
142     * Test ordering of namespace includes
143     */
144    public function test_order() {
145
146        $flags = $this->helper->get_flags(array());
147        $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
148
149        $this->assertEquals(array(
150                                 array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
151                                 array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
152                                 array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
153                                 array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
154                            ), $pages);
155
156        $flags = $this->helper->get_flags(array('rsort'));
157        $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
158        $this->assertEquals(array(
159                                 array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
160                                 array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
161                                 array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
162                                 array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
163                            ), $pages);
164        $flags = $this->helper->get_flags(array('order=custom'));
165        $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
166        $this->assertEquals(array(
167                                 array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
168                                 array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
169                                 array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
170                                 array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
171                            ), $pages);
172
173        $flags = $this->helper->get_flags(array('order=custom', 'rsort'));
174        $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
175        $this->assertEquals(array(
176                                 array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
177                                 array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
178                                 array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
179                                 array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
180                            ), $pages);
181    }
182}
183