1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\Value;
6use dokuwiki\plugin\struct\test\mock\Search;
7use dokuwiki\plugin\struct\types\Page;
8
9/**
10 * Testing the Page Type
11 *
12 * @group plugin_struct
13 * @group plugins
14 */
15class Type_Page_struct_test extends StructTest
16{
17
18    public function setUp(): void
19    {
20        parent::setUp();
21
22        saveWikiText('syntax', 'dummy', 'test');
23
24        // make sure the search index is initialized
25        idx_addPage('wiki:syntax');
26        idx_addPage('syntax');
27        idx_addPage('wiki:welcome');
28        idx_addPage('wiki:dokuwiki');
29
30    }
31
32    public function test_sort()
33    {
34
35        saveWikiText('title1', 'test', 'test');
36        $pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('title1');
37        $pageMeta->setTitle('This is a title');
38
39        saveWikiText('title2', 'test', 'test');
40        $pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('title2');
41        $pageMeta->setTitle('This is a title');
42
43        saveWikiText('title3', 'test', 'test');
44        $pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('title3');
45        $pageMeta->setTitle('Another Title');
46
47
48        $this->loadSchemaJSON('pageschema');
49        $this->saveData('test1', 'pageschema', array('singletitle' => 'title1'));
50        $this->saveData('test2', 'pageschema', array('singletitle' => 'title2'));
51        $this->saveData('test3', 'pageschema', array('singletitle' => 'title3'));
52
53        $search = new Search();
54        $search->addSchema('pageschema');
55        $search->addColumn('%pageid%');
56        $search->addColumn('singletitle');
57        $search->addSort('singletitle', true);
58        /** @var Value[][] $result */
59        $result = $search->execute();
60
61        $this->assertEquals(3, count($result));
62        $this->assertEquals('test3', $result[0][0]->getValue());
63        $this->assertEquals('test1', $result[1][0]->getValue());
64        $this->assertEquals('test2', $result[2][0]->getValue());
65    }
66
67
68    public function test_search()
69    {
70        // prepare some data
71        $this->loadSchemaJSON('pageschema');
72        $this->saveData(
73            'syntax',
74            'pageschema',
75            array(
76                'singlepage' => 'wiki:dokuwiki',
77                'multipage' => array('wiki:dokuwiki', 'wiki:syntax', 'wiki:welcome'),
78                'singletitle' => 'wiki:dokuwiki',
79                'multititle' => array('wiki:dokuwiki', 'wiki:syntax', 'wiki:welcome'),
80            )
81        );
82
83        // make sure titles for some pages are known (not for wiki:welcome)
84        $pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('wiki:dokuwiki');
85        $pageMeta->setTitle('DokuWiki Overview');
86        $pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('wiki:syntax');
87        $pageMeta->setTitle('DokuWiki Foobar Syntax');
88        $pageMeta->savePageData();
89
90        // search
91        $search = new Search();
92        $search->addSchema('pageschema');
93        $search->addColumn('singlepage');
94        $search->addColumn('multipage');
95        $search->addColumn('singletitle');
96        $search->addColumn('multititle');
97
98        /** @var Value[][] $result */
99        $result = $search->execute();
100
101        // no titles:
102        $this->assertEquals('wiki:dokuwiki', $result[0][0]->getValue());
103        $this->assertEquals(array('wiki:dokuwiki', 'wiki:syntax', 'wiki:welcome'), $result[0][1]->getValue());
104        // titles as JSON:
105        $this->assertEquals('["wiki:dokuwiki","DokuWiki Overview"]', $result[0][2]->getValue());
106        $this->assertEquals(
107            array(
108                '["wiki:dokuwiki","DokuWiki Overview"]',
109                '["wiki:syntax","DokuWiki Foobar Syntax"]',
110                '["wiki:welcome",null]' // no title for this
111            ),
112            $result[0][3]->getValue()
113        );
114
115        // if there is no title in the database display the pageid
116        $this->assertEquals(
117            array(
118                'DokuWiki Overview',
119                'DokuWiki Foobar Syntax',
120                'wiki:welcome'
121            ),
122            $result[0][3]->getDisplayValue()
123        );
124
125        // search single with title
126        $single = clone $search;
127        $single->addFilter('singletitle', 'Overview', '*~', 'AND');
128        $result = $single->execute();
129        $this->assertTrue(is_array($result));
130        $this->assertEquals(1, count($result));
131
132        // search multi with title
133        $multi = clone $search;
134        $multi->addFilter('multititle', 'Foobar', '*~', 'AND');
135        $result = $multi->execute();
136        $this->assertTrue(is_array($result));
137        $this->assertEquals(1, count($result));
138
139        // search single with page
140        $single = clone $search;
141        $single->addFilter('singletitle', 'wiki:dokuwiki', '*~', 'AND');
142        $result = $single->execute();
143        $this->assertTrue(is_array($result));
144        $this->assertEquals(1, count($result));
145
146        // search multi with page
147        $multi = clone $search;
148        $multi->addFilter('multititle', 'welcome', '*~', 'AND');
149        $result = $multi->execute();
150        $this->assertTrue(is_array($result));
151        $this->assertEquals(1, count($result));
152    }
153
154
155    /**
156     * This provides the testdata for @see Type_Page_struct_test::test_validate
157     */
158    public static function validate_testdata()
159    {
160        return array(
161            array(
162                'namespace:page',
163                'namespace:page',
164                'do not change clean valid page'
165            ),
166            array(
167                'namespace:page#headline',
168                'namespace:page#headline',
169                'keep fragments'
170            ),
171            array(
172                'namespace:page#headline#second',
173                'namespace:page#headline_second',
174                'keep fragments, but only the first one'
175            ),
176            array(
177                'namespace:page?do=something',
178                'namespace:page_do_something',
179                'clean query strings'
180            )
181        );
182    }
183
184    /**
185     * @param string $rawvalue
186     * @param string $validatedValue
187     * @param string $msg
188     *
189     * @dataProvider validate_testdata
190     */
191    public function test_validate($rawvalue, $validatedValue, $msg)
192    {
193        $page = new Page();
194        $this->assertEquals($validatedValue, $page->validate($rawvalue), $msg);
195    }
196
197    public function test_ajax_default()
198    {
199        global $INPUT;
200
201        $page = new Page(
202            array(
203                'autocomplete' => array(
204                    'mininput' => 2,
205                    'maxresult' => 5,
206                    'namespace' => '',
207                    'postfix' => '',
208                ),
209            )
210        );
211
212        $INPUT->set('search', 'syntax');
213        $this->assertEquals(
214            array(
215                array('label' => 'syntax', 'value' => 'syntax'),
216                array('label' => 'syntax (wiki)', 'value' => 'wiki:syntax')
217            ), $page->handleAjax()
218        );
219
220        $INPUT->set('search', 'ynt');
221        $this->assertEquals(
222            array(
223                array('label' => 'syntax', 'value' => 'syntax'),
224                array('label' => 'syntax (wiki)', 'value' => 'wiki:syntax')
225            ), $page->handleAjax()
226        );
227
228        $INPUT->set('search', 's'); // under mininput
229        $this->assertEquals(array(), $page->handleAjax());
230    }
231
232    public function test_ajax_namespace()
233    {
234        global $INPUT;
235
236        $page = new Page(
237            array(
238                'autocomplete' => array(
239                    'mininput' => 2,
240                    'maxresult' => 5,
241                    'namespace' => 'wiki',
242                    'postfix' => '',
243                ),
244            )
245        );
246
247        $INPUT->set('search', 'ynt');
248        $this->assertEquals(array(array('label' => 'syntax (wiki)', 'value' => 'wiki:syntax')), $page->handleAjax());
249    }
250
251    public function test_ajax_postfix()
252    {
253        global $INPUT;
254
255        $page = new Page(
256            array(
257                'autocomplete' => array(
258                    'mininput' => 2,
259                    'maxresult' => 5,
260                    'namespace' => '',
261                    'postfix' => 'iki',
262                ),
263            )
264        );
265
266        $INPUT->set('search', 'oku');
267        $this->assertEquals(array(array('label' => 'dokuwiki (wiki)', 'value' => 'wiki:dokuwiki')), $page->handleAjax());
268    }
269
270}
271