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