4 * @group ContentHandler
6 * ^--- needed, because we do need the database to test link updates
8 class TextContentTest
extends MediaWikiLangTestCase
{
10 protected $savedContentGetParserOutput;
12 protected function setUp() {
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(
27 'wgTextModelsToParse' => array(
28 CONTENT_MODEL_WIKITEXT
,
30 CONTENT_MODEL_JAVASCRIPT
,
33 'wgAlwaysUseTidy' => false,
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() {
46 // restore hooks that force custom rendering
47 if ( $this->savedContentGetParserOutput
!== null ) {
48 $wgHooks['ContentGetParserOutput'] = $this->savedContentGetParserOutput
;
54 public function newContent( $text ) {
55 return new TextContent( $text );
58 public static function dataGetParserOutput() {
61 'TextContentTest_testGetParserOutput',
63 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
73 * @dataProvider dataGetParserOutput
74 * @covers TextContent::getParserOutput
76 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
77 $expectedFields = null
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 );
97 $this->assertEquals( $exp, $v );
102 // TODO: assert more properties
105 public static function dataPreSaveTransform() {
108 #0: no signature resolution
121 * @dataProvider dataPreSaveTransform
122 * @covers TextContent::preSaveTransform
124 public function testPreSaveTransform( $text, $expected ) {
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(),
136 $this->assertEquals( $expected, $content->getNativeData() );
139 public static function dataPreloadTransform() {
149 * @dataProvider dataPreloadTransform
150 * @covers TextContent::preloadTransform
152 public function testPreloadTransform( $text, $expected ) {
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() {
164 array( '#REDIRECT [[Test]]',
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" );
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() {
241 * @dataProvider dataIsCountable
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() );
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() {
263 array( "hello\nworld.",
267 array( 'hello world.',
271 array( '[[hello world]].',
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() {
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() {
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() {
392 array( "TextContentTest_testGetSecondaryDataUpdates_1",
393 CONTENT_MODEL_TEXT
, "hello ''world''\n",
396 array( "TextContentTest_testGetSecondaryDataUpdates_2",
397 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
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
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() {
448 CONTENT_MODEL_WIKITEXT
,
454 CONTENT_MODEL_WIKITEXT
,
466 CONTENT_MODEL_JAVASCRIPT
,
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!" );
485 $this->assertInstanceOf( 'Content', $converted );
486 $this->assertEquals( $expectedNative, $converted->getNativeData() );