Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / content / TextContentTest.php
blob03cbbc0153016132d050908d947b9620d6f4a926
1 <?php
3 /**
4 * @group ContentHandler
5 * @group Database
6 * ^--- needed, because we do need the database to test link updates
7 */
8 class TextContentTest extends MediaWikiLangTestCase {
9 protected $context;
10 protected $savedContentGetParserOutput;
12 protected function setUp() {
13 global $wgHooks;
15 parent::setUp();
17 // Anon user
18 $user = new User();
19 $user->setName( '127.0.0.1' );
21 $this->context = new RequestContext( new FauxRequest() );
22 $this->context->setTitle( Title::newFromText( 'Test' ) );
23 $this->context->setUser( $user );
25 $this->setMwGlobals( array(
26 'wgUser' => $user,
27 'wgTextModelsToParse' => array(
28 CONTENT_MODEL_WIKITEXT,
29 CONTENT_MODEL_CSS,
30 CONTENT_MODEL_JAVASCRIPT,
32 'wgUseTidy' => false,
33 'wgAlwaysUseTidy' => false,
34 ) );
36 // bypass hooks that force custom rendering
37 if ( isset( $wgHooks['ContentGetParserOutput'] ) ) {
38 $this->savedContentGetParserOutput = $wgHooks['ContentGetParserOutput'];
39 unset( $wgHooks['ContentGetParserOutput'] );
43 public function teardown() {
44 global $wgHooks;
46 // restore hooks that force custom rendering
47 if ( $this->savedContentGetParserOutput !== null ) {
48 $wgHooks['ContentGetParserOutput'] = $this->savedContentGetParserOutput;
51 parent::teardown();
54 public function newContent( $text ) {
55 return new TextContent( $text );
58 public static function dataGetParserOutput() {
59 return array(
60 array(
61 'TextContentTest_testGetParserOutput',
62 CONTENT_MODEL_TEXT,
63 "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
64 array(
65 'Links' => array()
68 // TODO: more...?
72 /**
73 * @dataProvider dataGetParserOutput
74 * @covers TextContent::getParserOutput
76 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
77 $expectedFields = null
78 ) {
79 $title = Title::newFromText( $title );
80 $content = ContentHandler::makeContent( $text, $title, $model );
82 $po = $content->getParserOutput( $title );
84 $html = $po->getText();
85 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
87 $this->assertEquals( $expectedHtml, trim( $html ) );
89 if ( $expectedFields ) {
90 foreach ( $expectedFields as $field => $exp ) {
91 $f = 'get' . ucfirst( $field );
92 $v = call_user_func( array( $po, $f ) );
94 if ( is_array( $exp ) ) {
95 $this->assertArrayEquals( $exp, $v );
96 } else {
97 $this->assertEquals( $exp, $v );
102 // TODO: assert more properties
105 public static function dataPreSaveTransform() {
106 return array(
107 array(
108 #0: no signature resolution
109 'hello this is ~~~',
110 'hello this is ~~~',
112 array(
113 #1: rtrim
114 " Foo \n ",
115 ' Foo',
121 * @dataProvider dataPreSaveTransform
122 * @covers TextContent::preSaveTransform
124 public function testPreSaveTransform( $text, $expected ) {
125 global $wgContLang;
127 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
129 $content = $this->newContent( $text );
130 $content = $content->preSaveTransform(
131 $this->context->getTitle(),
132 $this->context->getUser(),
133 $options
136 $this->assertEquals( $expected, $content->getNativeData() );
139 public static function dataPreloadTransform() {
140 return array(
141 array(
142 'hello this is ~~~',
143 'hello this is ~~~',
149 * @dataProvider dataPreloadTransform
150 * @covers TextContent::preloadTransform
152 public function testPreloadTransform( $text, $expected ) {
153 global $wgContLang;
154 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
156 $content = $this->newContent( $text );
157 $content = $content->preloadTransform( $this->context->getTitle(), $options );
159 $this->assertEquals( $expected, $content->getNativeData() );
162 public static function dataGetRedirectTarget() {
163 return array(
164 array( '#REDIRECT [[Test]]',
165 null,
171 * @dataProvider dataGetRedirectTarget
172 * @covers TextContent::getRedirectTarget
174 public function testGetRedirectTarget( $text, $expected ) {
175 $content = $this->newContent( $text );
176 $t = $content->getRedirectTarget();
178 if ( is_null( $expected ) ) {
179 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
180 } else {
181 $this->assertEquals( $expected, $t->getPrefixedText() );
186 * @dataProvider dataGetRedirectTarget
187 * @covers TextContent::isRedirect
189 public function testIsRedirect( $text, $expected ) {
190 $content = $this->newContent( $text );
192 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
196 * @todo Test needs database! Should be done by a test class in the Database group.
199 public function getRedirectChain() {
200 $text = $this->getNativeData();
201 return Title::newFromRedirectArray( $text );
206 * @todo Test needs database! Should be done by a test class in the Database group.
209 public function getUltimateRedirectTarget() {
210 $text = $this->getNativeData();
211 return Title::newFromRedirectRecurse( $text );
215 public static function dataIsCountable() {
216 return array(
217 array( '',
218 null,
219 'any',
220 true
222 array( 'Foo',
223 null,
224 'any',
225 true
227 array( 'Foo',
228 null,
229 'comma',
230 false
232 array( 'Foo, bar',
233 null,
234 'comma',
235 false
241 * @dataProvider dataIsCountable
242 * @group Database
243 * @covers TextContent::isCountable
245 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
246 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
248 $content = $this->newContent( $text );
250 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
252 $this->assertEquals(
253 $expected,
255 'isCountable() returned unexpected value ' . var_export( $v, true )
256 . ' instead of ' . var_export( $expected, true )
257 . " in mode `$mode` for text \"$text\""
261 public static function dataGetTextForSummary() {
262 return array(
263 array( "hello\nworld.",
265 'hello world.',
267 array( 'hello world.',
269 'hello...',
271 array( '[[hello world]].',
273 '[[hel...',
279 * @dataProvider dataGetTextForSummary
280 * @covers TextContent::getTextForSummary
282 public function testGetTextForSummary( $text, $maxlength, $expected ) {
283 $content = $this->newContent( $text );
285 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
289 * @covers TextContent::getTextForSearchIndex
291 public function testGetTextForSearchIndex() {
292 $content = $this->newContent( 'hello world.' );
294 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
298 * @covers TextContent::copy
300 public function testCopy() {
301 $content = $this->newContent( 'hello world.' );
302 $copy = $content->copy();
304 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
305 $this->assertEquals( 'hello world.', $copy->getNativeData() );
309 * @covers TextContent::getSize
311 public function testGetSize() {
312 $content = $this->newContent( 'hello world.' );
314 $this->assertEquals( 12, $content->getSize() );
318 * @covers TextContent::getNativeData
320 public function testGetNativeData() {
321 $content = $this->newContent( 'hello world.' );
323 $this->assertEquals( 'hello world.', $content->getNativeData() );
327 * @covers TextContent::getWikitextForTransclusion
329 public function testGetWikitextForTransclusion() {
330 $content = $this->newContent( 'hello world.' );
332 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
336 * @covers TextContent::getModel
338 public function testGetModel() {
339 $content = $this->newContent( "hello world." );
341 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
345 * @covers TextContent::getContentHandler
347 public function testGetContentHandler() {
348 $content = $this->newContent( "hello world." );
350 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
353 public static function dataIsEmpty() {
354 return array(
355 array( '', true ),
356 array( ' ', false ),
357 array( '0', false ),
358 array( 'hallo welt.', false ),
363 * @dataProvider dataIsEmpty
364 * @covers TextContent::isEmpty
366 public function testIsEmpty( $text, $empty ) {
367 $content = $this->newContent( $text );
369 $this->assertEquals( $empty, $content->isEmpty() );
372 public static function dataEquals() {
373 return array(
374 array( new TextContent( "hallo" ), null, false ),
375 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
376 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
377 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
378 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
383 * @dataProvider dataEquals
384 * @covers TextContent::equals
386 public function testEquals( Content $a, Content $b = null, $equal = false ) {
387 $this->assertEquals( $equal, $a->equals( $b ) );
390 public static function dataGetDeletionUpdates() {
391 return array(
392 array( "TextContentTest_testGetSecondaryDataUpdates_1",
393 CONTENT_MODEL_TEXT, "hello ''world''\n",
394 array()
396 array( "TextContentTest_testGetSecondaryDataUpdates_2",
397 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
398 array()
400 // TODO: more...?
405 * @dataProvider dataGetDeletionUpdates
406 * @covers TextContent::getDeletionUpdates
408 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
409 $ns = $this->getDefaultWikitextNS();
410 $title = Title::newFromText( $title, $ns );
412 $content = ContentHandler::makeContent( $text, $title, $model );
414 $page = WikiPage::factory( $title );
415 $page->doEditContent( $content, '' );
417 $updates = $content->getDeletionUpdates( $page );
419 // make updates accessible by class name
420 foreach ( $updates as $update ) {
421 $class = get_class( $update );
422 $updates[$class] = $update;
425 if ( !$expectedStuff ) {
426 $this->assertTrue( true ); // make phpunit happy
427 return;
430 foreach ( $expectedStuff as $class => $fieldValues ) {
431 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
433 $update = $updates[$class];
435 foreach ( $fieldValues as $field => $value ) {
436 $v = $update->$field; #if the field doesn't exist, just crash and burn
437 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
441 $page->doDeleteArticle( '' );
444 public static function provideConvert() {
445 return array(
446 array( // #0
447 'Hallo Welt',
448 CONTENT_MODEL_WIKITEXT,
449 'lossless',
450 'Hallo Welt'
452 array( // #1
453 'Hallo Welt',
454 CONTENT_MODEL_WIKITEXT,
455 'lossless',
456 'Hallo Welt'
458 array( // #1
459 'Hallo Welt',
460 CONTENT_MODEL_CSS,
461 'lossless',
462 'Hallo Welt'
464 array( // #1
465 'Hallo Welt',
466 CONTENT_MODEL_JAVASCRIPT,
467 'lossless',
468 'Hallo Welt'
474 * @dataProvider provideConvert
475 * @covers TextContent::convert
477 public function testConvert( $text, $model, $lossy, $expectedNative ) {
478 $content = $this->newContent( $text );
480 $converted = $content->convert( $model, $lossy );
482 if ( $expectedNative === false ) {
483 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
484 } else {
485 $this->assertInstanceOf( 'Content', $converted );
486 $this->assertEquals( $expectedNative, $converted->getNativeData() );