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