/
Type_Page.test.php
261 lines (220 loc) · 8.23 KB
/
Type_Page.test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace dokuwiki\plugin\struct\test;
use dokuwiki\plugin\struct\meta\Value;
use dokuwiki\plugin\struct\test\mock\Search;
use dokuwiki\plugin\struct\types\Page;
/**
* Testing the Page Type
*
* @group plugin_struct
* @group plugins
*/
class Type_Page_struct_test extends StructTest {
public function setUp() {
parent::setUp();
saveWikiText('syntax', 'dummy', 'test');
// make sure the search index is initialized
idx_addPage('wiki:syntax');
idx_addPage('syntax');
idx_addPage('wiki:welcome');
idx_addPage('wiki:dokuwiki');
}
public function test_sort() {
saveWikiText('title1', 'test', 'test');
$pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('title1');
$pageMeta->setTitle('This is a title');
saveWikiText('title2', 'test', 'test');
$pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('title2');
$pageMeta->setTitle('This is a title');
saveWikiText('title3', 'test', 'test');
$pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('title3');
$pageMeta->setTitle('Another Title');
$this->loadSchemaJSON('pageschema');
$this->saveData('test1', 'pageschema', array('singletitle' => 'title1'));
$this->saveData('test2', 'pageschema', array('singletitle' => 'title2'));
$this->saveData('test3', 'pageschema', array('singletitle' => 'title3'));
$search = new Search();
$search->addSchema('pageschema');
$search->addColumn('%pageid%');
$search->addColumn('singletitle');
$search->addSort('singletitle', true);
/** @var Value[][] $result */
$result = $search->execute();
$this->assertEquals(3, count($result));
$this->assertEquals('test3', $result[0][0]->getValue());
$this->assertEquals('test1', $result[1][0]->getValue());
$this->assertEquals('test2', $result[2][0]->getValue());
}
public function test_search() {
// prepare some data
$this->loadSchemaJSON('pageschema');
$this->saveData(
'syntax',
'pageschema',
array(
'singlepage' => 'wiki:dokuwiki',
'multipage' => array('wiki:dokuwiki', 'wiki:syntax', 'wiki:welcome'),
'singletitle' => 'wiki:dokuwiki',
'multititle' => array('wiki:dokuwiki', 'wiki:syntax', 'wiki:welcome'),
)
);
// make sure titles for some pages are known (not for wiki:welcome)
$pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('wiki:dokuwiki');
$pageMeta->setTitle('DokuWiki Overview');
$pageMeta = new \dokuwiki\plugin\struct\meta\PageMeta('wiki:syntax');
$pageMeta->setTitle('DokuWiki Foobar Syntax');
$pageMeta->savePageData();
// search
$search = new Search();
$search->addSchema('pageschema');
$search->addColumn('singlepage');
$search->addColumn('multipage');
$search->addColumn('singletitle');
$search->addColumn('multititle');
/** @var Value[][] $result */
$result = $search->execute();
// no titles:
$this->assertEquals('wiki:dokuwiki', $result[0][0]->getValue());
$this->assertEquals(array('wiki:dokuwiki', 'wiki:syntax', 'wiki:welcome'), $result[0][1]->getValue());
// titles as JSON:
$this->assertEquals('["wiki:dokuwiki","DokuWiki Overview"]', $result[0][2]->getValue());
$this->assertEquals(
array(
'["wiki:dokuwiki","DokuWiki Overview"]',
'["wiki:syntax","DokuWiki Foobar Syntax"]',
'["wiki:welcome",null]' // no title for this
),
$result[0][3]->getValue()
);
// if there is no title in the database display the pageid
$this->assertEquals(
array(
'DokuWiki Overview',
'DokuWiki Foobar Syntax',
'wiki:welcome'
),
$result[0][3]->getDisplayValue()
);
// search single with title
$single = clone $search;
$single->addFilter('singletitle', 'Overview', '*~', 'AND');
$result = $single->execute();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));
// search multi with title
$multi = clone $search;
$multi->addFilter('multititle', 'Foobar', '*~', 'AND');
$result = $multi->execute();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));
// search single with page
$single = clone $search;
$single->addFilter('singletitle', 'wiki:dokuwiki', '*~', 'AND');
$result = $single->execute();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));
// search multi with page
$multi = clone $search;
$multi->addFilter('multititle', 'welcome', '*~', 'AND');
$result = $multi->execute();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));
}
/**
* This provides the testdata for @see Type_Page_struct_test::test_validate
*/
public static function validate_testdata() {
return array(
array(
'namespace:page',
'namespace:page',
'do not change clean valid page'
),
array(
'namespace:page#headline',
'namespace:page#headline',
'keep fragments'
),
array(
'namespace:page#headline#second',
'namespace:page#headline_second',
'keep fragments, but only the first one'
),
array(
'namespace:page?do=something',
'namespace:page_do_something',
'clean query strings'
)
);
}
/**
* @param string $rawvalue
* @param string $validatedValue
* @param string $msg
*
* @dataProvider validate_testdata
*/
public function test_validate($rawvalue, $validatedValue, $msg) {
$page = new Page();
$this->assertEquals($validatedValue, $page->validate($rawvalue), $msg);
}
public function test_ajax_default() {
global $INPUT;
$page = new Page(
array(
'autocomplete' => array(
'mininput' => 2,
'maxresult' => 5,
'namespace' => '',
'postfix' => '',
),
)
);
$INPUT->set('search', 'syntax');
$this->assertEquals(
array(
array('label' => 'syntax', 'value' => 'syntax'),
array('label' => 'syntax (wiki)', 'value' => 'wiki:syntax')
), $page->handleAjax()
);
$INPUT->set('search', 'ynt');
$this->assertEquals(
array(
array('label' => 'syntax', 'value' => 'syntax'),
array('label' => 'syntax (wiki)', 'value' => 'wiki:syntax')
), $page->handleAjax()
);
$INPUT->set('search', 's'); // under mininput
$this->assertEquals(array(), $page->handleAjax());
}
public function test_ajax_namespace() {
global $INPUT;
$page = new Page(
array(
'autocomplete' => array(
'mininput' => 2,
'maxresult' => 5,
'namespace' => 'wiki',
'postfix' => '',
),
)
);
$INPUT->set('search', 'ynt');
$this->assertEquals(array(array('label' => 'syntax (wiki)', 'value' => 'wiki:syntax')), $page->handleAjax());
}
public function test_ajax_postfix() {
global $INPUT;
$page = new Page(
array(
'autocomplete' => array(
'mininput' => 2,
'maxresult' => 5,
'namespace' => '',
'postfix' => 'iki',
),
)
);
$INPUT->set('search', 'oku');
$this->assertEquals(array(array('label' => 'dokuwiki (wiki)', 'value' => 'wiki:dokuwiki')), $page->handleAjax());
}
}