1<?php
2
3class dataau_dummy_renderer extends Doku_Renderer_xhtml{
4
5    function internallink($id, $title = '', $ignored=null, $ignored2=false, $linktype = 'content') {
6        return "link: $id $title";
7    }
8
9}
10
11/**
12 * @group plugin_data
13 * @group plugins
14 */
15class helper_plugin_dataau_test extends DokuWikiTest {
16
17    protected $pluginsEnabled = array('dataau', 'sqlite');
18
19    public static function setUpBeforeClass(){
20        parent::setUpBeforeClass();
21        // copy our own config files to the test directory
22        TestUtils::rcopy(dirname(DOKU_CONF), dirname(__FILE__).'/conf');
23    }
24
25    function testCleanData() {
26
27        $helper = new helper_plugin_dataau();
28
29        $this->assertEquals('', $helper->_cleanData('   ', ''));
30        $this->assertEquals('', $helper->_cleanData('', ''));
31        $this->assertEquals('', $helper->_cleanData(null, ''));
32        $this->assertEquals('', $helper->_cleanData(false, ''));
33
34        $this->assertEquals('', $helper->_cleanData('', 'dt'));
35        $this->assertEquals('', $helper->_cleanData('this is not a date', 'dt'));
36        $this->assertEquals('1234-01-01', $helper->_cleanData('1234-1-1', 'dt'));
37        $this->assertEquals('1234-01-01', $helper->_cleanData('1234-01-01', 'dt'));
38        $this->assertEquals('%now%', $helper->_cleanData('%now%', 'dt'));
39        $this->assertEquals('', $helper->_cleanData('1234-01-011', 'dt'));
40
41        $this->assertEquals('http://bla', $helper->_cleanData('bla', 'url'));
42        $this->assertEquals('http://bla', $helper->_cleanData('http://bla', 'url'));
43        $this->assertEquals('https://bla', $helper->_cleanData('https://bla', 'url'));
44        $this->assertEquals('tell://bla', $helper->_cleanData('tell://bla', 'url'));
45
46        $this->assertEquals('bla@bla.de', $helper->_cleanData('bla@bla.de', 'mail'));
47        $this->assertEquals('bla@bla.de bla', $helper->_cleanData('bla@bla.de bla', 'mail'));
48        $this->assertEquals('bla@bla.de bla word', $helper->_cleanData('bla@bla.de bla word', 'mail'));
49        $this->assertEquals('bla@bla.de bla bla word', $helper->_cleanData('bla bla@bla.de bla word', 'mail'));
50        $this->assertEquals('bla@bla.de bla bla word', $helper->_cleanData(' bla bla@bla.de bla word ', 'mail'));
51
52        $this->assertEquals('123', $helper->_cleanData('123', 'page'));
53        $this->assertEquals('123_123', $helper->_cleanData('123 123', 'page'));
54        $this->assertEquals('123', $helper->_cleanData('123', 'nspage'));
55
56        $this->assertEquals('test', $helper->_cleanData('test', ''));
57
58        $this->assertEquals('test', $helper->_cleanData('test', array('type' => '')));
59        $this->assertEquals('', $helper->_cleanData('test', array('type' => '', 'enum' => 'some other')));
60    }
61
62    function testColumn() {
63        global $conf;
64        $helper = new helper_plugin_dataau();
65
66        $this->assertEquals($this->createColumnEntry('type', false, 'type',  'type', 'type', ''), $helper->_column('type'));
67        $this->assertEquals($this->createColumnEntry('types', true, 'type',  'type', 'type', ''), $helper->_column('types'));
68        $this->assertEquals($this->createColumnEntry('', false, '', '', '', ''), $helper->_column(''));
69        $this->assertEquals($this->createColumnEntry('type_url', false, 'type', 'type', 'type', 'url'), $helper->_column('type_url'));
70        $this->assertEquals($this->createColumnEntry('type_urls', true, 'type', 'type', 'type', 'url'), $helper->_column('type_urls'));
71
72        $this->assertEquals($this->createColumnEntry('type_hidden', false, 'type', 'type', 'type', 'hidden'), $helper->_column('type_hidden'));
73        $this->assertEquals($this->createColumnEntry('type_hiddens', true, 'type', 'type', 'type', 'hidden'), $helper->_column('type_hiddens'));
74
75        $this->assertEquals($this->createColumnEntry('%title%', false, '%title%', '%title%', 'Page', 'title'), $helper->_column('%title%'));
76        $this->assertEquals($this->createColumnEntry('%pageid%', false, '%pageid%', '%pageid%','Title', 'page'), $helper->_column('%pageid%'));
77        $this->assertEquals($this->createColumnEntry('%class%', false, '%class%', '%class%', 'Page Class', ''), $helper->_column('%class%'));
78        $this->assertEquals($this->createColumnEntry('%lastmod%', false, '%lastmod%', '%lastmod%', 'Last Modified', 'timestamp'), $helper->_column('%lastmod%'));
79
80        $this->assertEquals($this->createColumnEntry('Type', false, 'type', 'Type', 'Type', ''), $helper->_column('Type'));
81
82
83        // test translated key name
84        $this->assertEquals($this->createColumnEntry('trans_urls', true, 'trans', 'trans', 'Translated Title', 'url'), $helper->_column('trans_urls'));
85        // retry in different language
86        $conf['lang'] = 'de';
87        $helper = new helper_plugin_dataau();
88        $this->assertEquals($this->createColumnEntry('trans_urls', true, 'trans', 'trans', 'Übersetzter Titel', 'url'), $helper->_column('trans_urls'));
89    }
90
91    function testAddPrePostFixes() {
92        global $conf;
93        $helper = new helper_plugin_dataau();
94
95        $this->assertEquals('value', $helper->_addPrePostFixes('', 'value'));
96        $this->assertEquals('prevaluepost', $helper->_addPrePostFixes('', 'value', 'pre', 'post'));
97        $this->assertEquals('valuepost', $helper->_addPrePostFixes('', 'value', '', 'post'));
98        $this->assertEquals('prevalue', $helper->_addPrePostFixes('', 'value', 'pre'));
99        $this->assertEquals('prevaluepost', $helper->_addPrePostFixes(array('prefix' => 'pre', 'postfix' => 'post'), 'value'));
100
101        $conf['lang'] = 'en';
102        $this->assertEquals('envalue', $helper->_addPrePostFixes(array('prefix' => '%lang%'), 'value'));
103
104        $this->assertEquals('value', $helper->_addPrePostFixes(array('prefix' => '%trans%'), 'value'));
105
106        $plugininstalled = in_array('translation', plugin_list('helper',$all=true));
107        if (!$plugininstalled) $this->markTestSkipped('Pre-condition not satisfied: translation plugin must be installed');
108
109        if ($plugininstalled && plugin_enable('translation')) {
110            global $ID;
111            $conf['plugin']['translation']['translations'] = 'de';
112            $ID = 'de:somepage';
113            $this->assertEquals('de:value', $helper->_addPrePostFixes(array('prefix' => '%trans%:'), 'value'));
114        }
115
116    }
117
118    function testResolveData() {
119        $helper = new helper_plugin_dataau();
120
121        $this->assertEquals('tom', $helper->_resolveData('tom', 'name'));
122        $this->assertEquals('jerry', $helper->_resolveData('jerry', 'name'));
123
124        $this->assertEquals('wiki:syntax Formatting Syntax', $helper->_resolveData('wiki:syntax', 'name_title'));
125        $this->assertEquals('none:existing ', $helper->_resolveData('none:existing', 'name_title'));
126    }
127
128    function testFormatData() {
129        global $conf;
130        global $ID;
131        $ID = '';
132
133        $helper = new helper_plugin_dataau();
134        $renderer = new dataau_dummy_renderer();
135
136        $this->assertEquals('value1, value2, val',
137            $helper->_formatData(array('type' => ''), "value1\n value2\n val", $renderer));
138
139        $this->assertEquals('link: :page ',
140            $helper->_formatData(array('type' => 'page'), "page", $renderer));
141
142        $this->assertEquals('link: :page title',
143            $helper->_formatData(array('type' => 'title'), "page|title", $renderer));
144
145        $this->assertEquals('link: page title',
146            $helper->_formatData(array('type' => 'pageid'), "page|title", $renderer));
147
148        $this->assertEquals('link: :key:page ',
149            $helper->_formatData(array('type' => 'nspage', 'key' => 'key'), "page", $renderer));
150
151        $conf['mailguard'] = '';
152        $this->assertEquals('<a href="mailto:pa:ge" class="mail" title="pa:ge">pa:ge</a>',
153            $helper->_formatData(array('type' => 'mail'), "pa:ge", $renderer));
154
155        $this->assertEquals('<a href="mailto:pa:ge" class="mail" title="pa:ge">some user</a>',
156            $helper->_formatData(array('type' => 'mail'), "pa:ge some user", $renderer));
157
158        $conf['mailguard'] = 'visible';
159        $this->assertEquals('<a href="mailto:pa%3Age" class="mail" title="pa%3Age">pa:ge</a>',
160            $helper->_formatData(array('type' => 'mail'), "pa:ge", $renderer));
161
162        $this->assertEquals('<a href="mailto:pa%3Age" class="mail" title="pa%3Age">some user</a>',
163            $helper->_formatData(array('type' => 'mail'), "pa:ge some user", $renderer));
164
165        $this->assertEquals('<a href=\'url\' class=\'urlextern\' rel="nofollow">url</a>',
166            $helper->_formatData(array('type' => 'url'), "url", $renderer));
167
168        $this->assertEquals('<a href="' . wl('start', array('dataflt[0]'=>'_=value')) . '" title="Show pages matching \'value\'" class="wikilink1">value</a>',
169            $helper->_formatData(array('type' => 'tag'), "value", $renderer));
170
171        $this->assertEquals(strftime('%d/%m/%Y %H:%M', 1234567),
172            $helper->_formatData(array('type' => 'timestamp'), "1234567", $renderer));
173
174        $this->assertEquals('<strong>bla</strong>',
175            $helper->_formatData(array('type' => 'wiki'), '|**bla**', $renderer));
176
177
178        $this->assertEquals('<a rel="lightbox" href="'.ml('wiki:dokuwiki-128.png', array('cache' => null)).'" class="media" title="wiki:dokuwiki-128.png"><img src="'.ml('wiki:dokuwiki-128.png', array('w' => 300, 'cache' => null)).'" class="media" title=": dokuwiki-128.png" alt=": dokuwiki-128.png" width="300" /></a>',
179            $helper->_formatData(array('type' => 'img300'), 'wiki:dokuwiki-128.png', $renderer));
180    }
181
182    function testReplacePlaceholdersInSQL() {
183        global $USERINFO;
184        $helper = new helper_plugin_dataau();
185
186        $dataau = array('sql' => '%user%');
187        $_SERVER['REMOTE_USER'] = 'test';
188        $helper->_replacePlaceholdersInSQL($dataau);
189        $this->assertEquals('test', $dataau['sql']);
190
191        $dataau = array('sql' => '%groups%');
192        $USERINFO['grps'] = array('test','admin');
193        $helper->_replacePlaceholdersInSQL($dataau);
194        $this->assertEquals("test','admin", $dataau['sql']);
195
196        $dataau = array('sql' => '%now%');
197        $helper->_replacePlaceholdersInSQL($dataau);
198        $this->assertRegExp('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $dataau['sql']);
199
200        $dataau = array('sql' => '%lang%');
201        $helper->_replacePlaceholdersInSQL($dataau);
202        $this->assertEquals('en', $dataau['sql']);
203    }
204
205    protected function createColumnEntry($name, $multi, $key, $origkey, $title, $type) {
206        return array(
207            'colname' => $name,
208            'multi' => $multi,
209            'key' => $key,
210            'origkey' => $origkey,
211            'title' => $title,
212            'type' => $type
213        );
214    }
215
216    public function testNoSqlPlugin() {
217        $helper = new helper_plugin_dataau();
218        plugin_disable('sqlite');
219        $this->assertFalse($helper->_getDB());
220    }
221
222    public function testParseFilter() {
223        $helper = new helper_plugin_dataau();
224
225        $this->assertEquals($this->createFilterArray('name', 'tom', '=', 'name_some', 'some')
226            , $helper->_parse_filter('name_some = tom'));
227
228        $this->assertEquals($this->createFilterArray('name', 'tom', '=', 'name', '')
229            , $helper->_parse_filter('name = tom'));
230
231        $this->assertEquals($this->createFilterArray('name', 'tom', '!=', 'name', '')
232            , $helper->_parse_filter('name != tom'));
233
234        $this->assertEquals($this->createFilterArray('name', 'tom', '!=', 'name', '')
235            , $helper->_parse_filter('name <> tom'));
236
237        $this->assertEquals($this->createFilterArray('name', 'tom', '<', 'name', '')
238            , $helper->_parse_filter('name < tom'));
239
240        $this->assertEquals($this->createFilterArray('name', 'tom', '>', 'name', '')
241            , $helper->_parse_filter('name > tom'));
242
243        $this->assertEquals($this->createFilterArray('name', 'tom', '<=', 'name', '')
244            , $helper->_parse_filter('name <= tom'));
245
246        $this->assertEquals($this->createFilterArray('name', 'tom', '>=', 'name', '')
247            , $helper->_parse_filter('name >= tom'));
248
249        $this->assertEquals($this->createFilterArray('name', 'tom', 'LIKE', 'name', '')
250            , $helper->_parse_filter('name ~ tom'));
251
252        $this->assertEquals($this->createFilterArray('name', '%tom%', 'LIKE', 'name', '')
253            , $helper->_parse_filter('name *~ tom'));
254
255        $this->assertEquals($this->createFilterArray('name', 'tom', 'NOT LIKE', 'name', '')
256            , $helper->_parse_filter('name !~ tom'));
257
258        $this->assertEquals($this->createFilterArray('name', '%tom', 'LIKE', 'name', '')
259            , $helper->_parse_filter('name ~ *tom'));
260
261        $this->assertEquals($this->createFilterArray('name', 'tom%', 'LIKE', 'name', '')
262            , $helper->_parse_filter('name ~ tom*'));
263
264        $this->assertEquals($this->createFilterArray('name', '%tom%', 'LIKE', 'name', '')
265            , $helper->_parse_filter('name ~ *tom*'));
266
267        $this->assertEquals($this->createFilterArray('name', 'tom', 'IN(', 'name', '')
268            , $helper->_parse_filter('name ~~ tom'));
269
270        $this->assertEquals($this->createFilterArray('name', "t''om','john*", 'IN(', 'name', '')
271            , $helper->_parse_filter("name ~~ t'om,john*"));
272
273        $this->assertEquals(false, $helper->_parse_filter('name is *tom*'));
274        $this->assertEquals(false, $helper->_parse_filter(''));
275    }
276
277    protected function createFilterArray($key, $value, $compare, $colname, $type) {
278        return array(
279            'key' => $key,
280            'value' => $value,
281            'compare' => $compare,
282            'colname' => $colname,
283            'type' => $type
284        );
285    }
286
287    public function testGetFilters() {
288        $helper = new helper_plugin_dataau();
289
290        $this->assertEquals(array(), $helper->_get_filters());
291
292        $_REQUEST['dataflt'] = 'name = tom';
293        $this->assertEquals(array($this->createFilterArrayListEntry('name', 'tom', '=', 'name', '', 'AND')),
294            $helper->_get_filters());
295
296        $_REQUEST['dataflt'] = array();
297        $_REQUEST['dataflt'][] = 'name = tom';
298        $this->assertEquals(array($this->createFilterArrayListEntry('name', 'tom', '=', 'name', '', 'AND')),
299            $helper->_get_filters());
300
301        $_REQUEST['dataflt'] = array();
302        $_REQUEST['dataflt'][] = 'name = tom';
303        $_REQUEST['dataflt'][] = 'unit_url = dokuwiki.org';
304        $this->assertEquals(
305            array(
306                $this->createFilterArrayListEntry('name', 'tom', '=', 'name', '', 'AND'),
307                $this->createFilterArrayListEntry('unit', 'http://dokuwiki.org', '=', 'unit_url', 'url', 'AND')
308            ),
309            $helper->_get_filters());
310    }
311
312    private function createFilterArrayListEntry($key, $value, $compare, $colname, $type, $logic) {
313        $item =  $this->createFilterArray($key, $value, $compare, $colname, $type);
314        $item['logic'] = $logic;
315        return $item;
316    }
317
318    public function testA2UA() {
319        $helper = new helper_plugin_dataau();
320
321        $array = array(
322            'id' => '1',
323            'name' => 'tom'
324        );
325
326        $result = array(
327            'table[id]' => '1',
328            'table[name]' => 'tom'
329        );
330
331        $this->assertEquals($result, $helper->_a2ua('table', $array));
332    }
333
334    public function testMakeTranslationReplacement() {
335        $helper = new helper_plugin_dataau();
336
337        $this->assertEquals('en', $helper->makeTranslationReplacement('%lang%'));
338        $this->assertEquals('', $helper->makeTranslationReplacement('%trans%'));
339
340        $plugininstalled = in_array('translation', plugin_list('helper',$all=true));
341        if (!$plugininstalled) $this->markTestSkipped('Pre-condition not satisfied: translation plugin must be installed');
342
343        if ($plugininstalled && plugin_enable('translation')) {
344            global $conf;
345            global $ID;
346            $conf['plugin']['translation']['translations'] = 'de';
347            $ID = 'de:somepage';
348            $this->assertEquals('en', $helper->makeTranslationReplacement('%lang%'));
349            $this->assertEquals('de', $helper->makeTranslationReplacement('%trans%'));
350        }
351    }
352}
353