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