Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / tests / phpunit / includes / WikitextContentTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class WikitextContentTest extends MediaWikiTestCase {
7
8 public function setup() {
9 $this->context = new RequestContext( new FauxRequest() );
10 $this->context->setTitle( Title::newFromText( "Test" ) );
11 }
12
13 public function newContent( $text ) {
14 return new WikitextContent( $text );
15 }
16
17 public function dataGetParserOutput() {
18 return array(
19 array("hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
20 // @todo: more...?
21 );
22 }
23
24 /**
25 * @dataProvider dataGetParserOutput
26 */
27 public function testGetParserOutput( $text, $expectedHtml ) {
28 $content = $this->newContent( $text );
29
30 $po = $content->getParserOutput( $this->context->getTitle() );
31
32 $this->assertEquals( $expectedHtml, $po->getText() );
33 return $po;
34 }
35
36 static $sections =
37
38 "Intro
39
40 == stuff ==
41 hello world
42
43 == test ==
44 just a test
45
46 == foo ==
47 more stuff
48 ";
49
50 public function dataGetSection() {
51 return array(
52 array( WikitextContentTest::$sections,
53 "0",
54 "Intro"
55 ),
56 array( WikitextContentTest::$sections,
57 "2",
58 "== test ==
59 just a test"
60 ),
61 array( WikitextContentTest::$sections,
62 "8",
63 false
64 ),
65 );
66 }
67
68 /**
69 * @dataProvider dataGetSection
70 */
71 public function testGetSection( $text, $sectionId, $expectedText ) {
72 $content = $this->newContent( $text );
73
74 $sectionContent = $content->getSection( $sectionId );
75
76 $this->assertEquals( $expectedText, is_null( $sectionContent ) ? null : $sectionContent->getNativeData() );
77 }
78
79 public function dataReplaceSection() {
80 return array(
81 array( WikitextContentTest::$sections,
82 "0",
83 "No more",
84 null,
85 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
86 ),
87 array( WikitextContentTest::$sections,
88 "",
89 "No more",
90 null,
91 "No more"
92 ),
93 array( WikitextContentTest::$sections,
94 "2",
95 "== TEST ==\nmore fun",
96 null,
97 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
98 ),
99 array( WikitextContentTest::$sections,
100 "8",
101 "No more",
102 null,
103 WikitextContentTest::$sections
104 ),
105 array( WikitextContentTest::$sections,
106 "new",
107 "No more",
108 "New",
109 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
110 ),
111 );
112 }
113
114 /**
115 * @dataProvider dataReplaceSection
116 */
117 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
118 $content = $this->newContent( $text );
119 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
120
121 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
122 }
123
124 public function testAddSectionHeader( ) {
125 $content = $this->newContent( 'hello world' );
126 $content = $content->addSectionHeader( 'test' );
127
128 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
129 }
130
131 public function dataPreSaveTransform() {
132 return array(
133 array( 'hello this is ~~~',
134 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
135 ),
136 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
137 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
138 ),
139 );
140 }
141
142 /**
143 * @dataProvider dataPreSaveTransform
144 */
145 public function testPreSaveTransform( $text, $expected ) {
146 global $wgUser, $wgContLang;
147 $options = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
148
149 $content = $this->newContent( $text );
150 $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser(), $options );
151
152 $this->assertEquals( $expected, $content->getNativeData() );
153 }
154
155 public function dataPreloadTransform() {
156 return array(
157 array( 'hello this is ~~~',
158 "hello this is ~~~",
159 ),
160 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
161 'hello \'\'this\'\' is bar',
162 ),
163 );
164 }
165
166 /**
167 * @dataProvider dataPreloadTransform
168 */
169 public function testPreloadTransform( $text, $expected ) {
170 global $wgUser, $wgContLang;
171 $options = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
172
173 $content = $this->newContent( $text );
174 $content = $content->preloadTransform( $this->context->getTitle(), $options );
175
176 $this->assertEquals( $expected, $content->getNativeData() );
177 }
178
179 public function dataGetRedirectTarget() {
180 return array(
181 array( '#REDIRECT [[Test]]',
182 'Test',
183 ),
184 array( '#REDIRECT Test',
185 null,
186 ),
187 array( '* #REDIRECT [[Test]]',
188 null,
189 ),
190 );
191 }
192
193 /**
194 * @dataProvider dataGetRedirectTarget
195 */
196 public function testGetRedirectTarget( $text, $expected ) {
197 $content = $this->newContent( $text );
198 $t = $content->getRedirectTarget( );
199
200 if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
201 else $this->assertEquals( $expected, $t->getPrefixedText() );
202 }
203
204 /**
205 * @dataProvider dataGetRedirectTarget
206 */
207 public function isRedirect( $text, $expected ) {
208 $content = $this->newContent( $text );
209
210 $this->assertEquals( !is_null($expected), $content->isRedirect() );
211 }
212
213
214 /**
215 * @todo: test needs database!
216 */
217 /*
218 public function getRedirectChain() {
219 $text = $this->getNativeData();
220 return Title::newFromRedirectArray( $text );
221 }
222 */
223
224 /**
225 * @todo: test needs database!
226 */
227 /*
228 public function getUltimateRedirectTarget() {
229 $text = $this->getNativeData();
230 return Title::newFromRedirectRecurse( $text );
231 }
232 */
233
234
235 public function dataIsCountable() {
236 return array(
237 array( '',
238 null,
239 'any',
240 true
241 ),
242 array( 'Foo',
243 null,
244 'any',
245 true
246 ),
247 array( 'Foo',
248 null,
249 'comma',
250 false
251 ),
252 array( 'Foo, bar',
253 null,
254 'comma',
255 true
256 ),
257 array( 'Foo',
258 null,
259 'link',
260 false
261 ),
262 array( 'Foo [[bar]]',
263 null,
264 'link',
265 true
266 ),
267 array( 'Foo',
268 true,
269 'link',
270 true
271 ),
272 array( 'Foo [[bar]]',
273 false,
274 'link',
275 false
276 ),
277 array( '#REDIRECT [[bar]]',
278 true,
279 'any',
280 false
281 ),
282 array( '#REDIRECT [[bar]]',
283 true,
284 'comma',
285 false
286 ),
287 array( '#REDIRECT [[bar]]',
288 true,
289 'link',
290 false
291 ),
292 );
293 }
294
295
296 /**
297 * @dataProvider dataIsCountable
298 */
299 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
300 global $wgArticleCountMethod;
301
302 $old = $wgArticleCountMethod;
303 $wgArticleCountMethod = $mode;
304
305 $content = $this->newContent( $text );
306
307 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
308 $wgArticleCountMethod = $old;
309
310 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
311 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
312 }
313
314 public function dataGetTextForSummary() {
315 return array(
316 array( "hello\nworld.",
317 16,
318 'hello world.',
319 ),
320 array( 'hello world.',
321 8,
322 'hello...',
323 ),
324 array( '[[hello world]].',
325 8,
326 'hel...',
327 ),
328 );
329 }
330
331 /**
332 * @dataProvider dataGetTextForSummary
333 */
334 public function testGetTextForSummary( $text, $maxlength, $expected ) {
335 $content = $this->newContent( $text );
336
337 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
338 }
339
340
341 public function testGetTextForSearchIndex( ) {
342 $content = $this->newContent( "hello world." );
343
344 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
345 }
346
347 public function testCopy() {
348 $content = $this->newContent( "hello world." );
349 $copy = $content->copy();
350
351 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
352 $this->assertEquals( "hello world.", $copy->getNativeData() );
353 }
354
355 public function testGetSize( ) {
356 $content = $this->newContent( "hello world." );
357
358 $this->assertEquals( 12, $content->getSize() );
359 }
360
361 public function testGetNativeData( ) {
362 $content = $this->newContent( "hello world." );
363
364 $this->assertEquals( "hello world.", $content->getNativeData() );
365 }
366
367 public function testGetWikitextForTransclusion( ) {
368 $content = $this->newContent( "hello world." );
369
370 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
371 }
372
373 # =================================================================================================================
374
375 public function testGetModel() {
376 $content = $this->newContent( "hello world." );
377
378 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
379 }
380
381 public function testGetContentHandler() {
382 $content = $this->newContent( "hello world." );
383
384 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
385 }
386
387 public function dataIsEmpty( ) {
388 return array(
389 array( '', true ),
390 array( ' ', false ),
391 array( '0', false ),
392 array( 'hallo welt.', false ),
393 );
394 }
395
396 /**
397 * @dataProvider dataIsEmpty
398 */
399 public function testIsEmpty( $text, $empty ) {
400 $content = $this->newContent( $text );
401
402 $this->assertEquals( $empty, $content->isEmpty() );
403 }
404
405 public function dataEquals( ) {
406 return array(
407 array( new WikitextContent( "hallo" ), null, false ),
408 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
409 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
410 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
411 );
412 }
413
414 /**
415 * @dataProvider dataEquals
416 */
417 public function testEquals( Content $a, Content $b = null, $equal = false ) {
418 $this->assertEquals( $equal, $a->equals( $b ) );
419 }
420
421 }