4 * @group ContentHandler
6 * ^--- needed, because we do need the database to test link updates
8 class TextContentTest
extends MediaWikiLangTestCase
{
11 protected function setUp() {
16 $user->setName( '127.0.0.1' );
18 $this->setMwGlobals( array(
20 'wgTextModelsToParse' => array(
21 CONTENT_MODEL_WIKITEXT
,
23 CONTENT_MODEL_JAVASCRIPT
,
26 'wgAlwaysUseTidy' => false,
29 $this->context
= new RequestContext( new FauxRequest() );
30 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
31 $this->context
->setUser( $user );
34 public function newContent( $text ) {
35 return new TextContent( $text );
38 public static function dataGetParserOutput() {
41 'TextContentTest_testGetParserOutput',
43 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
53 * @dataProvider dataGetParserOutput
54 * @covers TextContent::getParserOutput
56 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
57 $expectedFields = null
59 $title = Title
::newFromText( $title );
60 $content = ContentHandler
::makeContent( $text, $title, $model );
62 $po = $content->getParserOutput( $title );
64 $html = $po->getText();
65 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
67 $this->assertEquals( $expectedHtml, trim( $html ) );
69 if ( $expectedFields ) {
70 foreach ( $expectedFields as $field => $exp ) {
71 $f = 'get' . ucfirst( $field );
72 $v = call_user_func( array( $po, $f ) );
74 if ( is_array( $exp ) ) {
75 $this->assertArrayEquals( $exp, $v );
77 $this->assertEquals( $exp, $v );
82 // TODO: assert more properties
85 public static function dataPreSaveTransform() {
88 #0: no signature resolution
101 * @dataProvider dataPreSaveTransform
102 * @covers TextContent::preSaveTransform
104 public function testPreSaveTransform( $text, $expected ) {
107 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
109 $content = $this->newContent( $text );
110 $content = $content->preSaveTransform(
111 $this->context
->getTitle(),
112 $this->context
->getUser(),
116 $this->assertEquals( $expected, $content->getNativeData() );
119 public static function dataPreloadTransform() {
129 * @dataProvider dataPreloadTransform
130 * @covers TextContent::preloadTransform
132 public function testPreloadTransform( $text, $expected ) {
134 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
136 $content = $this->newContent( $text );
137 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
139 $this->assertEquals( $expected, $content->getNativeData() );
142 public static function dataGetRedirectTarget() {
144 array( '#REDIRECT [[Test]]',
151 * @dataProvider dataGetRedirectTarget
152 * @covers TextContent::getRedirectTarget
154 public function testGetRedirectTarget( $text, $expected ) {
155 $content = $this->newContent( $text );
156 $t = $content->getRedirectTarget();
158 if ( is_null( $expected ) ) {
159 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
161 $this->assertEquals( $expected, $t->getPrefixedText() );
166 * @dataProvider dataGetRedirectTarget
167 * @covers TextContent::isRedirect
169 public function testIsRedirect( $text, $expected ) {
170 $content = $this->newContent( $text );
172 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
176 * @todo Test needs database! Should be done by a test class in the Database group.
179 public function getRedirectChain() {
180 $text = $this->getNativeData();
181 return Title::newFromRedirectArray( $text );
186 * @todo Test needs database! Should be done by a test class in the Database group.
189 public function getUltimateRedirectTarget() {
190 $text = $this->getNativeData();
191 return Title::newFromRedirectRecurse( $text );
195 public static function dataIsCountable() {
221 * @dataProvider dataIsCountable
223 * @covers TextContent::isCountable
225 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
226 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
228 $content = $this->newContent( $text );
230 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
235 'isCountable() returned unexpected value ' . var_export( $v, true )
236 . ' instead of ' . var_export( $expected, true )
237 . " in mode `$mode` for text \"$text\""
241 public static function dataGetTextForSummary() {
243 array( "hello\nworld.",
247 array( 'hello world.',
251 array( '[[hello world]].',
259 * @dataProvider dataGetTextForSummary
260 * @covers TextContent::getTextForSummary
262 public function testGetTextForSummary( $text, $maxlength, $expected ) {
263 $content = $this->newContent( $text );
265 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
269 * @covers TextContent::getTextForSearchIndex
271 public function testGetTextForSearchIndex() {
272 $content = $this->newContent( 'hello world.' );
274 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
278 * @covers TextContent::copy
280 public function testCopy() {
281 $content = $this->newContent( 'hello world.' );
282 $copy = $content->copy();
284 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
285 $this->assertEquals( 'hello world.', $copy->getNativeData() );
289 * @covers TextContent::getSize
291 public function testGetSize() {
292 $content = $this->newContent( 'hello world.' );
294 $this->assertEquals( 12, $content->getSize() );
298 * @covers TextContent::getNativeData
300 public function testGetNativeData() {
301 $content = $this->newContent( 'hello world.' );
303 $this->assertEquals( 'hello world.', $content->getNativeData() );
307 * @covers TextContent::getWikitextForTransclusion
309 public function testGetWikitextForTransclusion() {
310 $content = $this->newContent( 'hello world.' );
312 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
316 * @covers TextContent::getModel
318 public function testGetModel() {
319 $content = $this->newContent( "hello world." );
321 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
325 * @covers TextContent::getContentHandler
327 public function testGetContentHandler() {
328 $content = $this->newContent( "hello world." );
330 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
333 public static function dataIsEmpty() {
338 array( 'hallo welt.', false ),
343 * @dataProvider dataIsEmpty
344 * @covers TextContent::isEmpty
346 public function testIsEmpty( $text, $empty ) {
347 $content = $this->newContent( $text );
349 $this->assertEquals( $empty, $content->isEmpty() );
352 public static function dataEquals() {
354 array( new TextContent( "hallo" ), null, false ),
355 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
356 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
357 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
358 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
363 * @dataProvider dataEquals
364 * @covers TextContent::equals
366 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
367 $this->assertEquals( $equal, $a->equals( $b ) );
370 public static function dataGetDeletionUpdates() {
372 array( "TextContentTest_testGetSecondaryDataUpdates_1",
373 CONTENT_MODEL_TEXT
, "hello ''world''\n",
376 array( "TextContentTest_testGetSecondaryDataUpdates_2",
377 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
385 * @dataProvider dataGetDeletionUpdates
386 * @covers TextContent::getDeletionUpdates
388 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
389 $ns = $this->getDefaultWikitextNS();
390 $title = Title
::newFromText( $title, $ns );
392 $content = ContentHandler
::makeContent( $text, $title, $model );
394 $page = WikiPage
::factory( $title );
395 $page->doEditContent( $content, '' );
397 $updates = $content->getDeletionUpdates( $page );
399 // make updates accessible by class name
400 foreach ( $updates as $update ) {
401 $class = get_class( $update );
402 $updates[$class] = $update;
405 if ( !$expectedStuff ) {
406 $this->assertTrue( true ); // make phpunit happy
410 foreach ( $expectedStuff as $class => $fieldValues ) {
411 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
413 $update = $updates[$class];
415 foreach ( $fieldValues as $field => $value ) {
416 $v = $update->$field; #if the field doesn't exist, just crash and burn
417 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
421 $page->doDeleteArticle( '' );
424 public static function provideConvert() {
428 CONTENT_MODEL_WIKITEXT
,
434 CONTENT_MODEL_WIKITEXT
,
446 CONTENT_MODEL_JAVASCRIPT
,
454 * @dataProvider provideConvert
455 * @covers TextContent::convert
457 public function testConvert( $text, $model, $lossy, $expectedNative ) {
458 $content = $this->newContent( $text );
460 $converted = $content->convert( $model, $lossy );
462 if ( $expectedNative === false ) {
463 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
465 $this->assertInstanceOf( 'Content', $converted );
466 $this->assertEquals( $expectedNative, $converted->getNativeData() );