Skip to content

Commit 92dd1f9

Browse files
committed
Merge pull request #164 from micgro42/TitleFieldsUseHeadings
Title fields use headings
2 parents 1e92136 + 15b9cc3 commit 92dd1f9

File tree

4 files changed

+290
-14
lines changed

4 files changed

+290
-14
lines changed

_test/db.test.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
4+
/**
5+
* @group plugin_data
6+
* @group plugins
7+
* @group slow
8+
*/
9+
class db_data_entry_test extends DokuWikiTest {
10+
11+
protected $pluginsEnabled = array('data', 'sqlite',);
12+
13+
public static function setUpBeforeClass() {
14+
parent::setUpBeforeClass();
15+
}
16+
17+
18+
public function setUp() {
19+
parent::setUp();
20+
21+
saveWikiText('foo',"====== Page-Heading ======",'summary');
22+
$req = new TestRequest();
23+
$req->get(array(),'/doku.php?id=foo');
24+
25+
26+
saveWikiText('testpage',"---- dataentry Testentry ----\n"
27+
. "test1_title: foo|bar\n"
28+
. "----\n",'summary');
29+
//trigger save to db
30+
$req = new TestRequest();
31+
$req->get(array(),'/doku.php?id=testpage');
32+
}
33+
34+
function test_title_input_id () {
35+
36+
$test_table = "---- datatable Testtable ----\n"
37+
. "cols: %pageid%, test1\n"
38+
. "filter: test1~ *foo*\n";
39+
40+
/** @var syntax_plugin_data_entry $plugin */
41+
$plugin = plugin_load('syntax','data_table');
42+
43+
$handler = new Doku_Handler();
44+
$data = $plugin->handle($test_table, 0, 0, $handler);
45+
$renderer = new Doku_Renderer_xhtml();
46+
$plugin->render('xhtml',$renderer,$data);
47+
48+
$result = $renderer->doc;
49+
50+
$actual_value = substr($result,strpos($result,'<td class="align test1">')+24);
51+
$actual_value = substr($actual_value,0,strpos($actual_value,'</td>'));
52+
$expected_value = 'foo|bar';
53+
$this->assertSame($expected_value,$actual_value);
54+
55+
$actual_link = substr($result,strpos($result,'<td class="align pageid">')+25);
56+
$actual_link = substr($actual_link,strpos($actual_link,'doku.php'));
57+
$actual_link = substr($actual_link,0,strpos($actual_link,'</a>'));
58+
59+
$this->assertSame('doku.php?id=testpage" class="wikilink1" title="testpage">testpage',$actual_link);
60+
61+
}
62+
63+
function test_title_input_title () {
64+
65+
$test_table = "---- datatable Testtable ----\n"
66+
. "cols: %pageid%, test1\n"
67+
. "filter: test1~ *bar*\n";
68+
69+
/** @var syntax_plugin_data_entry $plugin */
70+
$plugin = plugin_load('syntax','data_table');
71+
72+
$handler = new Doku_Handler();
73+
$data = $plugin->handle($test_table, 0, 0, $handler);
74+
$renderer = new Doku_Renderer_xhtml();
75+
$plugin->render('xhtml',$renderer,$data);
76+
77+
$result = $renderer->doc;
78+
79+
$actual_value = substr($result,strpos($result,'<td class="align test1">')+24);
80+
$actual_value = substr($actual_value,0,strpos($actual_value,'</td>'));
81+
$expected_value = 'foo|bar';
82+
$this->assertSame($expected_value,$actual_value);
83+
84+
$actual_link = substr($result,strpos($result,'<td class="align pageid">')+25);
85+
$actual_link = substr($actual_link,strpos($actual_link,'doku.php'));
86+
$actual_link = substr($actual_link,0,strpos($actual_link,'</a>'));
87+
88+
$this->assertSame('doku.php?id=testpage" class="wikilink1" title="testpage">testpage',$actual_link);
89+
}
90+
91+
function test_title_input_Heading () {
92+
93+
$test_table = "---- datatable Testtable ----\n"
94+
. "cols: %pageid%, test1\n"
95+
. "filter: test1_title~ *Heading*\n";
96+
97+
/** @var syntax_plugin_data_entry $plugin */
98+
$plugin = plugin_load('syntax','data_table');
99+
100+
$handler = new Doku_Handler();
101+
$data = $plugin->handle($test_table, 0, 0, $handler);
102+
$renderer = new Doku_Renderer_xhtml();
103+
$plugin->render('xhtml',$renderer,$data);
104+
105+
$result = $renderer->doc;
106+
107+
$actual_value = substr($result,strpos($result,'<td class="align test1">')+24);
108+
$actual_value = substr($actual_value,0,strpos($actual_value,'</td>'));
109+
$expected_value = 'foo|bar';
110+
$this->assertSame($expected_value,$actual_value);
111+
112+
$actual_link = substr($result,strpos($result,'<td class="align pageid">')+25);
113+
$actual_link = substr($actual_link,strpos($actual_link,'doku.php'));
114+
$actual_link = substr($actual_link,0,strpos($actual_link,'</a>'));
115+
116+
$this->assertSame('doku.php?id=testpage" class="wikilink1" title="testpage">testpage',$actual_link);
117+
}
118+
119+
function test_title_input_stackns () {
120+
121+
$test_table = "---- datatable Testtable ----\n"
122+
. "cols: %pageid%, test1\n";
123+
124+
global $ID;
125+
$ID = 'foo:bar:start';
126+
127+
/** @var syntax_plugin_data_entry $plugin */
128+
$plugin = plugin_load('syntax','data_table');
129+
130+
$handler = new Doku_Handler();
131+
$data = $plugin->handle($test_table, 0, 0, $handler);
132+
$renderer = new Doku_Renderer_xhtml();
133+
$plugin->render('xhtml',$renderer,$data);
134+
135+
$result = $renderer->doc;
136+
137+
$actual_value = substr($result,strpos($result,'<td class="align test1">')+24);
138+
$actual_value = substr($actual_value,0,strpos($actual_value,'</td>'));
139+
$expected_value = 'foo|bar';
140+
$this->assertSame($expected_value,$actual_value);
141+
142+
$actual_link = substr($result,strpos($result,'<td class="align pageid">')+25);
143+
$actual_link = substr($actual_link,strpos($actual_link,'doku.php'));
144+
$actual_link = substr($actual_link,0,strpos($actual_link,'</a>'));
145+
146+
$this->assertSame('doku.php?id=testpage" class="wikilink1" title="testpage">testpage',$actual_link);
147+
}
148+
149+
}

_test/helper.test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ function testFormatData() {
136136
$this->assertEquals('value1, value2, val',
137137
$helper->_formatData(array('type' => ''), "value1\n value2\n val", $renderer));
138138

139-
$this->assertEquals('link: page ',
139+
$this->assertEquals('link: :page ',
140140
$helper->_formatData(array('type' => 'page'), "page", $renderer));
141141

142-
$this->assertEquals('link: page title',
142+
$this->assertEquals('link: :page title',
143143
$helper->_formatData(array('type' => 'title'), "page|title", $renderer));
144144

145145
$this->assertEquals('link: page title',

_test/syntax_plugin_data_entry.test.php

Lines changed: 125 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
require_once DOKU_INC . 'inc/parser/xhtml.php';
44

5+
class 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+
519
/**
620
* @group plugin_data
721
* @group plugins
@@ -12,6 +26,10 @@ class syntax_plugin_data_entry_test extends DokuWikiTest {
1226

1327
private $exampleEntry;
1428

29+
public function setUp() {
30+
parent::setUp();
31+
}
32+
1533
function __construct() {
1634
$this->exampleEntry = "---- dataentry projects ----\n"
1735
. "type : web development\n"
@@ -65,36 +83,131 @@ function testHandle() {
6583
$this->assertEquals($cols, $result['cols'], 'Cols array corrupted');
6684
}
6785

86+
function test_pageEntry_noTitle() {
87+
$test_entry = '---- dataentry ----
88+
test1_page: foo
89+
----';
90+
91+
/** @var syntax_plugin_data_entry $plugin */
92+
$plugin = plugin_load('syntax','data_entry');
93+
94+
$handler = new Doku_Handler();
95+
$data = $plugin->handle($test_entry, 0, 10, $handler);
96+
$renderer = new Doku_Renderer_xhtml_mock();
97+
$plugin->render('xhtml',$renderer,$data);
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_data_entry $plugin */
113+
$plugin = plugin_load('syntax','data_entry');
114+
115+
$handler = new Doku_Handler();
116+
$data = $plugin->handle($test_entry, 0, 10, $handler);
117+
$renderer = new Doku_Renderer_xhtml_mock();
118+
$plugin->render('xhtml',$renderer,$data);
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_data_entry $plugin */
134+
$plugin = plugin_load('syntax','data_entry');
135+
136+
$handler = new Doku_Handler();
137+
$data = $plugin->handle($test_entry, 0, 10, $handler);
138+
$renderer = new Doku_Renderer_xhtml_mock();
139+
$plugin->render('xhtml',$renderer,$data);
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_data_entry $plugin */
155+
$plugin = plugin_load('syntax','data_entry');
156+
157+
$handler = new Doku_Handler();
158+
$data = $plugin->handle($test_entry, 0, 10, $handler);
159+
$renderer = new Doku_Renderer_xhtml_mock();
160+
$plugin->render('xhtml',$renderer,$data);
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+
68170
function test_titleEntry_noTitle() {
69171
$test_entry = '---- dataentry ----
70-
test_title: bar
172+
test1_title: foo
71173
----';
72-
$plugin = new syntax_plugin_data_entry();
174+
175+
/** @var syntax_plugin_data_entry $plugin */
176+
$plugin = plugin_load('syntax','data_entry');
73177

74178
$handler = new Doku_Handler();
75179
$data = $plugin->handle($test_entry, 0, 10, $handler);
76-
$renderer = new Doku_Renderer_xhtml();
180+
$renderer = new Doku_Renderer_xhtml_mock();
77181
$plugin->render('xhtml',$renderer,$data);
78182
$result = $renderer->doc;
79-
$result = substr($result,0,strpos($result,'</a>')+4);
80-
$result = substr($result,strpos($result,'<a'));
81-
$this->assertSame('<a href="/./doku.php?id=bar" class="wikilink2" title="bar" rel="nofollow">bar</a>',$result);
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.');
82189
}
83190

191+
84192
function test_titleEntry_withTitle() {
85193
$test_entry = '---- dataentry ----
86-
test_title: link:to:page|TitleOfPage
194+
test3_title: link:to:page|TitleOfPage
87195
----';
88-
$plugin = new syntax_plugin_data_entry();
196+
197+
/** @var syntax_plugin_data_entry $plugin */
198+
$plugin = plugin_load('syntax','data_entry');
89199

90200
$handler = new Doku_Handler();
91201
$data = $plugin->handle($test_entry, 0, 10, $handler);
92-
$renderer = new Doku_Renderer_xhtml();
202+
$renderer = new Doku_Renderer_xhtml_mock();
93203
$plugin->render('xhtml',$renderer,$data);
94204
$result = $renderer->doc;
95-
$result = substr($result,0,strpos($result,'</a>')+4);
96-
$result = substr($result,strpos($result,'<a'));
97-
$this->assertSame('<a href="/./doku.php?id=link:to:page" class="wikilink2" title="link:to:page" rel="nofollow">TitleOfPage</a>',$result);
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.');
98211
}
99212

100213
function test_editToWiki() {

helper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ function _resolveData($value, $colname) {
205205
return $value;
206206
}
207207

208+
public function ensureAbsoluteId($id) {
209+
if (substr($id,0,1) !== ':') {
210+
$id = ':' . $id;
211+
}
212+
return $id;
213+
}
214+
208215
/**
209216
* Return XHTML formated data, depending on column type
210217
*
@@ -232,9 +239,15 @@ function _formatData($column, $value, Doku_Renderer_xhtml $R) {
232239
switch($type) {
233240
case 'page':
234241
$val = $this->_addPrePostFixes($column['type'], $val);
242+
$val = $this->ensureAbsoluteId($val);
235243
$outs[] = $R->internallink($val, null, null, true);
236244
break;
237245
case 'title':
246+
list($id, $title) = explode('|', $val, 2);
247+
$id = $this->_addPrePostFixes($column['type'], $id);
248+
$id = $this->ensureAbsoluteId($id);
249+
$outs[] = $R->internallink($id, $title, null, true);
250+
break;
238251
case 'pageid':
239252
list($id, $title) = explode('|', $val, 2);
240253

@@ -249,6 +262,7 @@ function _formatData($column, $value, Doku_Renderer_xhtml $R) {
249262
}
250263

251264
$id = $this->_addPrePostFixes($column['type'], $id);
265+
252266
$outs[] = $R->internallink($id, $title, null, true);
253267
break;
254268
case 'nspage':

0 commit comments

Comments
 (0)