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->context
= new RequestContext( new FauxRequest() );
19 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
20 $this->context
->setUser( $user );
22 $this->setMwGlobals( array(
24 'wgTextModelsToParse' => array(
25 CONTENT_MODEL_WIKITEXT
,
27 CONTENT_MODEL_JAVASCRIPT
,
30 'wgCapitalLinks' => true,
31 'wgHooks' => array(), // bypass hook ContentGetParserOutput that force custom rendering
34 MWTidy
::destroySingleton();
37 protected function tearDown() {
38 MWTidy
::destroySingleton();
42 public function newContent( $text ) {
43 return new TextContent( $text );
46 public static function dataGetParserOutput() {
49 'TextContentTest_testGetParserOutput',
51 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
61 * @dataProvider dataGetParserOutput
62 * @covers TextContent::getParserOutput
64 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
65 $expectedFields = null
67 $title = Title
::newFromText( $title );
68 $content = ContentHandler
::makeContent( $text, $title, $model );
70 $po = $content->getParserOutput( $title );
72 $html = $po->getText();
73 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
75 $this->assertEquals( $expectedHtml, trim( $html ) );
77 if ( $expectedFields ) {
78 foreach ( $expectedFields as $field => $exp ) {
79 $f = 'get' . ucfirst( $field );
80 $v = call_user_func( array( $po, $f ) );
82 if ( is_array( $exp ) ) {
83 $this->assertArrayEquals( $exp, $v );
85 $this->assertEquals( $exp, $v );
90 // TODO: assert more properties
93 public static function dataPreSaveTransform() {
96 # 0: no signature resolution
109 * @dataProvider dataPreSaveTransform
110 * @covers TextContent::preSaveTransform
112 public function testPreSaveTransform( $text, $expected ) {
115 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
117 $content = $this->newContent( $text );
118 $content = $content->preSaveTransform(
119 $this->context
->getTitle(),
120 $this->context
->getUser(),
124 $this->assertEquals( $expected, $content->getNativeData() );
127 public static function dataPreloadTransform() {
137 * @dataProvider dataPreloadTransform
138 * @covers TextContent::preloadTransform
140 public function testPreloadTransform( $text, $expected ) {
142 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
144 $content = $this->newContent( $text );
145 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
147 $this->assertEquals( $expected, $content->getNativeData() );
150 public static function dataGetRedirectTarget() {
152 array( '#REDIRECT [[Test]]',
159 * @dataProvider dataGetRedirectTarget
160 * @covers TextContent::getRedirectTarget
162 public function testGetRedirectTarget( $text, $expected ) {
163 $content = $this->newContent( $text );
164 $t = $content->getRedirectTarget();
166 if ( is_null( $expected ) ) {
167 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
169 $this->assertEquals( $expected, $t->getPrefixedText() );
174 * @dataProvider dataGetRedirectTarget
175 * @covers TextContent::isRedirect
177 public function testIsRedirect( $text, $expected ) {
178 $content = $this->newContent( $text );
180 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
184 * @todo Test needs database! Should be done by a test class in the Database group.
187 public function getRedirectChain() {
188 $text = $this->getNativeData();
189 return Title::newFromRedirectArray( $text );
194 * @todo Test needs database! Should be done by a test class in the Database group.
197 public function getUltimateRedirectTarget() {
198 $text = $this->getNativeData();
199 return Title::newFromRedirectRecurse( $text );
203 public static function dataIsCountable() {
229 * @dataProvider dataIsCountable
231 * @covers TextContent::isCountable
233 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
234 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
236 $content = $this->newContent( $text );
238 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
243 'isCountable() returned unexpected value ' . var_export( $v, true )
244 . ' instead of ' . var_export( $expected, true )
245 . " in mode `$mode` for text \"$text\""
249 public static function dataGetTextForSummary() {
251 array( "hello\nworld.",
255 array( 'hello world.',
259 array( '[[hello world]].',
267 * @dataProvider dataGetTextForSummary
268 * @covers TextContent::getTextForSummary
270 public function testGetTextForSummary( $text, $maxlength, $expected ) {
271 $content = $this->newContent( $text );
273 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
277 * @covers TextContent::getTextForSearchIndex
279 public function testGetTextForSearchIndex() {
280 $content = $this->newContent( 'hello world.' );
282 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
286 * @covers TextContent::copy
288 public function testCopy() {
289 $content = $this->newContent( 'hello world.' );
290 $copy = $content->copy();
292 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
293 $this->assertEquals( 'hello world.', $copy->getNativeData() );
297 * @covers TextContent::getSize
299 public function testGetSize() {
300 $content = $this->newContent( 'hello world.' );
302 $this->assertEquals( 12, $content->getSize() );
306 * @covers TextContent::getNativeData
308 public function testGetNativeData() {
309 $content = $this->newContent( 'hello world.' );
311 $this->assertEquals( 'hello world.', $content->getNativeData() );
315 * @covers TextContent::getWikitextForTransclusion
317 public function testGetWikitextForTransclusion() {
318 $content = $this->newContent( 'hello world.' );
320 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
324 * @covers TextContent::getModel
326 public function testGetModel() {
327 $content = $this->newContent( "hello world." );
329 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
333 * @covers TextContent::getContentHandler
335 public function testGetContentHandler() {
336 $content = $this->newContent( "hello world." );
338 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
341 public static function dataIsEmpty() {
346 array( 'hallo welt.', false ),
351 * @dataProvider dataIsEmpty
352 * @covers TextContent::isEmpty
354 public function testIsEmpty( $text, $empty ) {
355 $content = $this->newContent( $text );
357 $this->assertEquals( $empty, $content->isEmpty() );
360 public static function dataEquals() {
362 array( new TextContent( "hallo" ), null, false ),
363 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
364 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
365 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
366 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
371 * @dataProvider dataEquals
372 * @covers TextContent::equals
374 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
375 $this->assertEquals( $equal, $a->equals( $b ) );
378 public static function dataGetDeletionUpdates() {
380 array( "TextContentTest_testGetSecondaryDataUpdates_1",
381 CONTENT_MODEL_TEXT
, "hello ''world''\n",
384 array( "TextContentTest_testGetSecondaryDataUpdates_2",
385 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
393 * @dataProvider dataGetDeletionUpdates
394 * @covers TextContent::getDeletionUpdates
396 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
397 $ns = $this->getDefaultWikitextNS();
398 $title = Title
::newFromText( $title, $ns );
400 $content = ContentHandler
::makeContent( $text, $title, $model );
402 $page = WikiPage
::factory( $title );
403 $page->doEditContent( $content, '' );
405 $updates = $content->getDeletionUpdates( $page );
407 // make updates accessible by class name
408 foreach ( $updates as $update ) {
409 $class = get_class( $update );
410 $updates[$class] = $update;
413 if ( !$expectedStuff ) {
414 $this->assertTrue( true ); // make phpunit happy
418 foreach ( $expectedStuff as $class => $fieldValues ) {
419 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
421 $update = $updates[$class];
423 foreach ( $fieldValues as $field => $value ) {
424 $v = $update->$field; # if the field doesn't exist, just crash and burn
425 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
429 $page->doDeleteArticle( '' );
432 public static function provideConvert() {
436 CONTENT_MODEL_WIKITEXT
,
442 CONTENT_MODEL_WIKITEXT
,
454 CONTENT_MODEL_JAVASCRIPT
,
462 * @dataProvider provideConvert
463 * @covers TextContent::convert
465 public function testConvert( $text, $model, $lossy, $expectedNative ) {
466 $content = $this->newContent( $text );
468 $converted = $content->convert( $model, $lossy );
470 if ( $expectedNative === false ) {
471 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
473 $this->assertInstanceOf( 'Content', $converted );
474 $this->assertEquals( $expectedNative, $converted->getNativeData() );