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