1<?php
2
3class Doku_Renderer_xhtml_mock extends Doku_Renderer_xhtml
4{
5
6    function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content')
7    {
8        $inputvalues = array(
9            'id' => $id,
10            'name' => $name,
11            'search' => $search,
12            'returnonly' => $returnonly,
13            'linktype' => $linktype
14        );
15        return "<internallink>" . serialize($inputvalues) . "</internallink>";
16    }
17}
18
19/**
20 * @group plugin_data
21 * @group plugins
22 */
23class syntax_plugin_data_entry_test extends DokuWikiTest
24{
25
26    protected $pluginsEnabled = array('data', 'sqlite');
27
28    private $exampleEntry = "---- dataentry projects ----\n"
29    . "type          : web development\n"
30    . "volume        : 1 Mrd # how much do they pay?\n"
31    . "employees     : Joe, Jane, Jim\n"
32    . "customer_page : customers:microsoft\n"
33    . "deadline_dt   : 2009-08-17\n"
34    . "server_pages  : servers:devel01, extern:microsoft\n"
35    . "Website_url   : http://www.microsoft.com\n"
36    . "task_tags     : programming, coding, design, html\n"
37    . "tests_        : \\#5 done\n"
38    . "----\n";
39
40    function testHandle()
41    {
42        $plugin = new syntax_plugin_data_entry();
43
44        $handler = new Doku_Handler();
45        $result = $plugin->handle($this->exampleEntry, 0, 10, $handler);
46
47        $this->assertEquals(10, $result['pos'], 'Position has changed');
48        $this->assertEquals('projects', $result['classes'], 'wrong class name detected');
49
50        $data = array(
51            'type' => 'web development',
52            'volume' => '1 Mrd',
53            'employee' => array('Joe', 'Jane', 'Jim'),
54            'customer' => 'customers:microsoft',
55            'deadline' => '2009-08-17',
56            'server' => array('servers:devel01', 'extern:microsoft'),
57            'website' => 'http://www.microsoft.com',
58            'task' => array('programming', 'coding', 'design', 'html'),
59            'tests' => '#5 done',
60            '----' => ''
61        );
62        $this->assertEquals($data, $result['data'], 'Data array corrupted');
63
64        $cols = array(
65            'type' => $this->createColumnEntry('type', false, 'type', 'type', 'type', false),
66            'volume' => $this->createColumnEntry('volume', false, 'volume', 'volume', 'volume', false),
67            'employee' => $this->createColumnEntry('employees', 1, 'employee', 'employee', 'employee', false),
68            'customer' => $this->createColumnEntry('customer_page', false, 'customer', 'customer', 'customer',
69                'page'),
70            'deadline' => $this->createColumnEntry('deadline_dt', false, 'deadline', 'deadline', 'deadline', 'dt'),
71            'server' => $this->createColumnEntry('server_pages', 1, 'server', 'server', 'server', 'page'),
72            'website' => $this->createColumnEntry('Website_url', false, 'website', 'Website', 'Website', 'url'),
73            'task' => $this->createColumnEntry('task_tags', 1, 'task', 'task', 'task', 'tag'),
74            'tests' => $this->createColumnEntry('tests_', 0, 'tests', 'tests', 'tests', false),
75            '----' => $this->createColumnEntry('----', false, '----', '----', '----', false)
76        );
77        $cols['volume']['comment'] = ' how much do they pay?';
78        $this->assertEquals($cols, $result['cols'], 'Cols array corrupted');
79    }
80
81    function test_pageEntry_noTitle()
82    {
83        $test_entry = '---- dataentry ----
84        test1_page: foo
85        ----';
86
87        /** @var syntax_plugin_data_entry $plugin */
88        $plugin = plugin_load('syntax', 'data_entry');
89
90        $handler = new Doku_Handler();
91        $data = $plugin->handle($test_entry, 0, 10, $handler);
92        $renderer = new Doku_Renderer_xhtml_mock();
93        $plugin->render('xhtml', $renderer, $data);
94        $result = $renderer->doc;
95        $result = substr($result, 0, strpos($result, '</internallink>'));
96        $result = substr($result, strpos($result, '<internallink>') + 14);
97        $result = unserialize($result);
98
99        $this->assertSame(':foo', $result['id']);
100        $this->assertSame(null, $result['name'], 'page does not accept a title. useheading decides');
101    }
102
103    function test_pageEntry_withTitle()
104    {
105        $test_entry = '---- dataentry ----
106        test1_page: foo|bar
107        ----';
108
109        /** @var syntax_plugin_data_entry $plugin */
110        $plugin = plugin_load('syntax', 'data_entry');
111
112        $handler = new Doku_Handler();
113        $data = $plugin->handle($test_entry, 0, 10, $handler);
114        $renderer = new Doku_Renderer_xhtml_mock();
115        $plugin->render('xhtml', $renderer, $data);
116        $result = $renderer->doc;
117        $result = substr($result, 0, strpos($result, '</internallink>'));
118        $result = substr($result, strpos($result, '<internallink>') + 14);
119        $result = unserialize($result);
120
121        $this->assertSame(':foo_bar', $result['id'], 'for type page a title becomes part of the id');
122        $this->assertSame(null, $result['name'], 'page never accepts a title. useheading decides');
123    }
124
125    function test_pageidEntry_noTitle()
126    {
127        $test_entry = '---- dataentry ----
128        test1_pageid: foo
129        ----';
130
131        /** @var syntax_plugin_data_entry $plugin */
132        $plugin = plugin_load('syntax', 'data_entry');
133
134        $handler = new Doku_Handler();
135        $data = $plugin->handle($test_entry, 0, 10, $handler);
136        $renderer = new Doku_Renderer_xhtml_mock();
137        $plugin->render('xhtml', $renderer, $data);
138        $result = $renderer->doc;
139        $result = substr($result, 0, strpos($result, '</internallink>'));
140        $result = substr($result, strpos($result, '<internallink>') + 14);
141        $result = unserialize($result);
142
143        $this->assertSame('foo', $result['id']);
144        $this->assertSame('foo', $result['name'], 'pageid: use the pageid as title if no title is provided.');
145    }
146
147    function test_pageidEntry_withTitle()
148    {
149        $test_entry = '---- dataentry ----
150        test1_pageid: foo|bar
151        ----';
152
153        /** @var syntax_plugin_data_entry $plugin */
154        $plugin = plugin_load('syntax', 'data_entry');
155
156        $handler = new Doku_Handler();
157        $data = $plugin->handle($test_entry, 0, 10, $handler);
158        $renderer = new Doku_Renderer_xhtml_mock();
159        $plugin->render('xhtml', $renderer, $data);
160        $result = $renderer->doc;
161        $result = substr($result, 0, strpos($result, '</internallink>'));
162        $result = substr($result, strpos($result, '<internallink>') + 14);
163        $result = unserialize($result);
164
165        $this->assertSame('foo', $result['id'], "wrong id handed to internal link");
166        $this->assertSame('bar', $result['name'], 'pageid: use the provided title');
167    }
168
169    function test_titleEntry_noTitle()
170    {
171        $test_entry = '---- dataentry ----
172        test1_title: foo
173        ----';
174
175        /** @var syntax_plugin_data_entry $plugin */
176        $plugin = plugin_load('syntax', 'data_entry');
177
178        $handler = new Doku_Handler();
179        $data = $plugin->handle($test_entry, 0, 10, $handler);
180        $renderer = new Doku_Renderer_xhtml_mock();
181        $plugin->render('xhtml', $renderer, $data);
182        $result = $renderer->doc;
183        $result = substr($result, 0, strpos($result, '</internallink>'));
184        $result = substr($result, strpos($result, '<internallink>') + 14);
185        $result = unserialize($result);
186
187        $this->assertSame(':foo', $result['id']);
188        $this->assertSame(null, $result['name'], 'no title should be given to internal link. Let useheading decide.');
189    }
190
191
192    function test_titleEntry_withTitle()
193    {
194        $test_entry = '---- dataentry ----
195        test3_title: link:to:page|TitleOfPage
196        ----';
197
198        /** @var syntax_plugin_data_entry $plugin */
199        $plugin = plugin_load('syntax', 'data_entry');
200
201        $handler = new Doku_Handler();
202        $data = $plugin->handle($test_entry, 0, 10, $handler);
203        $renderer = new Doku_Renderer_xhtml_mock();
204        $plugin->render('xhtml', $renderer, $data);
205        $result = $renderer->doc;
206        $result = substr($result, 0, strpos($result, '</internallink>'));
207        $result = substr($result, strpos($result, '<internallink>') + 14);
208        $result = unserialize($result);
209
210        $this->assertSame(':link:to:page', $result['id']);
211        $this->assertSame('TitleOfPage', $result['name'], 'The Title provided should be the title shown.');
212    }
213
214    function test_editToWiki()
215    {
216        $data = array(
217            'classes' => 'projects',
218            'data' => array(
219                array(
220                    'title' => 'type',
221                    'type' => '',
222                    'multi' => '',
223                    'value' => 'web development',
224                    'comment' => '',
225                ),
226                array(
227                    'title' => 'volume',
228                    'type' => '',
229                    'multi' => '',
230                    'value' => '1 Mrd',
231                    'comment' => 'how much do they pay?',
232                ),
233                array(
234                    'title' => 'employee',
235                    'type' => '',
236                    'multi' => '1',
237                    'value' => 'Joe, Jane, Jim',
238                    'comment' => '',
239                ),
240                array(
241                    'title' => 'customer',
242                    'type' => 'page',
243                    'multi' => '',
244                    'value' => 'customers:microsoft',
245                    'comment' => '',
246                ),
247                array(
248                    'title' => 'deadline',
249                    'type' => 'dt',
250                    'multi' => '',
251                    'value' => '2009-08-17',
252                    'comment' => '',
253                ),
254                array(
255                    'title' => 'server',
256                    'type' => 'page',
257                    'multi' => '1',
258                    'value' => 'servers:devel01, extern:microsoft',
259                    'comment' => '',
260                ),
261                array(
262                    'title' => 'Website',
263                    'type' => 'url',
264                    'multi' => '',
265                    'value' => 'http://www.microsoft.com',
266                    'comment' => '',
267                ),
268                array(
269                    'title' => 'task',
270                    'type' => 'tag',
271                    'multi' => '1',
272                    'value' => 'programming, coding, design, html',
273                    'comment' => '',
274                ),
275                array(
276                    'title' => 'tests',
277                    'type' => '',
278                    'multi' => '',
279                    'value' => '#5 done',
280                    'comment' => '',
281                ),
282                //empty row
283                array(
284                    'title' => '',
285                    'type' => '',
286                    'multi' => '',
287                    'value' => '',
288                    'comment' => '',
289                )
290            )
291        );
292
293        $plugin = new syntax_plugin_data_entry();
294        $this->assertEquals($this->exampleEntry, $plugin->editToWiki($data));
295    }
296
297
298    function testHandleEmpty()
299    {
300        $plugin = new syntax_plugin_data_entry();
301
302        $entry = "---- dataentry projects ----\n"
303            . "\n"
304            . "----\n";
305
306        $handler = new Doku_Handler();
307        $result = $plugin->handle($entry, 0, 10, $handler);
308
309        $this->assertEquals(10, $result['pos'], 'Position has changed');
310        $this->assertEquals(35, $result['len'], 'wrong entry length');
311        $this->assertEquals('projects', $result['classes'], 'wrong class name detected');
312
313        $data = array(
314            '----' => ''
315        );
316        $this->assertEquals($data, $result['data'], 'Data array corrupted');
317
318        $cols = array(
319            '----' => $this->createColumnEntry('----', false, '----', '----', '----', false)
320        );
321        $this->assertEquals($cols, $result['cols'], 'Cols array corrupted');
322    }
323
324    protected function createColumnEntry($name, $multi, $key, $origkey, $title, $type)
325    {
326        return array(
327            'colname' => $name,
328            'multi' => $multi,
329            'key' => $key,
330            'origkey' => $origkey,
331            'title' => $title,
332            'type' => $type
333        );
334    }
335
336    function testShowData()
337    {
338        $handler = new Doku_Handler();
339        $xhtml = new Doku_Renderer_xhtml();
340        $plugin = new syntax_plugin_data_entry();
341
342        $result = $plugin->handle($this->exampleEntry, 0, 10, $handler);
343
344        $plugin->showData($result, $xhtml);
345        $doc = (new DOMWrap\Document())->html($xhtml->doc);
346
347        $this->assertEquals(1, $doc->find('div.inline.dataplugin_entry.projects')->count());
348        $this->assertEquals(1, $doc->find('dl dt.type')->count());
349        $this->assertEquals(1, $doc->find('dl dd.type')->count());
350        $this->assertEquals(1, $doc->find('dl dt.volume')->count());
351        $this->assertEquals(1, $doc->find('dl dd.volume')->count());
352        $this->assertEquals(1, $doc->find('dl dt.employee')->count());
353        $this->assertEquals(1, $doc->find('dl dd.employee')->count());
354        $this->assertEquals(1, $doc->find('dl dt.customer')->count());
355        $this->assertEquals(1, $doc->find('dl dd.customer')->count());
356        $this->assertEquals(1, $doc->find('dl dt.deadline')->count());
357        $this->assertEquals(1, $doc->find('dl dd.deadline')->count());
358        $this->assertEquals(1, $doc->find('dl dt.server')->count());
359        $this->assertEquals(1, $doc->find('dl dd.server')->count());
360        $this->assertEquals(1, $doc->find('dl dt.website')->count());
361        $this->assertEquals(1, $doc->find('dl dd.website')->count());
362        $this->assertEquals(1, $doc->find('dl dt.task')->count());
363        $this->assertEquals(1, $doc->find('dl dd.task')->count());
364        $this->assertEquals(1, $doc->find('dl dt.tests')->count());
365        $this->assertEquals(1, $doc->find('dl dd.tests')->count());
366    }
367
368    function testComments()
369    {
370        $entry = "---- dataentry projects ----\n"
371            . "volume        : 1 Mrd # how much do they pay?\n"
372            . "server        : http://www.microsoft.com      # Comment\n"
373            . "Website_url   : http://www.microsoft.com\#test # Comment\n"
374            . "Site_url      : https://www.microsoft.com/page\#test\n"
375            . "tests_        : \\#5 done\n"
376            . "----\n";
377
378        $plugin = new syntax_plugin_data_entry();
379
380        $handler = new Doku_Handler();
381        $result = $plugin->handle($entry, 0, 10, $handler);
382
383        $this->assertEquals(10, $result['pos'], 'Position has changed');
384        $this->assertEquals('projects', $result['classes'], 'wrong class name detected');
385
386        $data = array(
387            'volume' => '1 Mrd',
388            'server' => 'http://www.microsoft.com',
389            'website' => 'http://www.microsoft.com#test',
390            'site' => 'https://www.microsoft.com/page#test',
391            'tests' => '#5 done',
392            '----' => ''
393        );
394        $this->assertEquals($data, $result['data'], 'Data array corrupted');
395
396    }
397}
398
399
400