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 'wgAlwaysUseTidy' => false,
31 'wgCapitalLinks' => true,
32 'wgHooks' => array(), // bypass hook ContentGetParserOutput that force custom rendering
36 public function newContent( $text ) {
37 return new TextContent( $text );
40 public static function dataGetParserOutput() {
43 'TextContentTest_testGetParserOutput',
45 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
55 * @dataProvider dataGetParserOutput
56 * @covers TextContent::getParserOutput
58 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
59 $expectedFields = null
61 $title = Title
::newFromText( $title );
62 $content = ContentHandler
::makeContent( $text, $title, $model );
64 $po = $content->getParserOutput( $title );
66 $html = $po->getText();
67 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
69 $this->assertEquals( $expectedHtml, trim( $html ) );
71 if ( $expectedFields ) {
72 foreach ( $expectedFields as $field => $exp ) {
73 $f = 'get' . ucfirst( $field );
74 $v = call_user_func( array( $po, $f ) );
76 if ( is_array( $exp ) ) {
77 $this->assertArrayEquals( $exp, $v );
79 $this->assertEquals( $exp, $v );
84 // TODO: assert more properties
87 public static function dataPreSaveTransform() {
90 #0: no signature resolution
103 * @dataProvider dataPreSaveTransform
104 * @covers TextContent::preSaveTransform
106 public function testPreSaveTransform( $text, $expected ) {
109 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
111 $content = $this->newContent( $text );
112 $content = $content->preSaveTransform(
113 $this->context
->getTitle(),
114 $this->context
->getUser(),
118 $this->assertEquals( $expected, $content->getNativeData() );
121 public static function dataPreloadTransform() {
131 * @dataProvider dataPreloadTransform
132 * @covers TextContent::preloadTransform
134 public function testPreloadTransform( $text, $expected ) {
136 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
138 $content = $this->newContent( $text );
139 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
141 $this->assertEquals( $expected, $content->getNativeData() );
144 public static function dataGetRedirectTarget() {
146 array( '#REDIRECT [[Test]]',
153 * @dataProvider dataGetRedirectTarget
154 * @covers TextContent::getRedirectTarget
156 public function testGetRedirectTarget( $text, $expected ) {
157 $content = $this->newContent( $text );
158 $t = $content->getRedirectTarget();
160 if ( is_null( $expected ) ) {
161 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
163 $this->assertEquals( $expected, $t->getPrefixedText() );
168 * @dataProvider dataGetRedirectTarget
169 * @covers TextContent::isRedirect
171 public function testIsRedirect( $text, $expected ) {
172 $content = $this->newContent( $text );
174 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
178 * @todo Test needs database! Should be done by a test class in the Database group.
181 public function getRedirectChain() {
182 $text = $this->getNativeData();
183 return Title::newFromRedirectArray( $text );
188 * @todo Test needs database! Should be done by a test class in the Database group.
191 public function getUltimateRedirectTarget() {
192 $text = $this->getNativeData();
193 return Title::newFromRedirectRecurse( $text );
197 public static function dataIsCountable() {
223 * @dataProvider dataIsCountable
225 * @covers TextContent::isCountable
227 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
228 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
230 $content = $this->newContent( $text );
232 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
237 'isCountable() returned unexpected value ' . var_export( $v, true )
238 . ' instead of ' . var_export( $expected, true )
239 . " in mode `$mode` for text \"$text\""
243 public static function dataGetTextForSummary() {
245 array( "hello\nworld.",
249 array( 'hello world.',
253 array( '[[hello world]].',
261 * @dataProvider dataGetTextForSummary
262 * @covers TextContent::getTextForSummary
264 public function testGetTextForSummary( $text, $maxlength, $expected ) {
265 $content = $this->newContent( $text );
267 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
271 * @covers TextContent::getTextForSearchIndex
273 public function testGetTextForSearchIndex() {
274 $content = $this->newContent( 'hello world.' );
276 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
280 * @covers TextContent::copy
282 public function testCopy() {
283 $content = $this->newContent( 'hello world.' );
284 $copy = $content->copy();
286 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
287 $this->assertEquals( 'hello world.', $copy->getNativeData() );
291 * @covers TextContent::getSize
293 public function testGetSize() {
294 $content = $this->newContent( 'hello world.' );
296 $this->assertEquals( 12, $content->getSize() );
300 * @covers TextContent::getNativeData
302 public function testGetNativeData() {
303 $content = $this->newContent( 'hello world.' );
305 $this->assertEquals( 'hello world.', $content->getNativeData() );
309 * @covers TextContent::getWikitextForTransclusion
311 public function testGetWikitextForTransclusion() {
312 $content = $this->newContent( 'hello world.' );
314 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
318 * @covers TextContent::getModel
320 public function testGetModel() {
321 $content = $this->newContent( "hello world." );
323 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
327 * @covers TextContent::getContentHandler
329 public function testGetContentHandler() {
330 $content = $this->newContent( "hello world." );
332 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
335 public static function dataIsEmpty() {
340 array( 'hallo welt.', false ),
345 * @dataProvider dataIsEmpty
346 * @covers TextContent::isEmpty
348 public function testIsEmpty( $text, $empty ) {
349 $content = $this->newContent( $text );
351 $this->assertEquals( $empty, $content->isEmpty() );
354 public static function dataEquals() {
356 array( new TextContent( "hallo" ), null, false ),
357 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
358 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
359 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
360 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
365 * @dataProvider dataEquals
366 * @covers TextContent::equals
368 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
369 $this->assertEquals( $equal, $a->equals( $b ) );
372 public static function dataGetDeletionUpdates() {
374 array( "TextContentTest_testGetSecondaryDataUpdates_1",
375 CONTENT_MODEL_TEXT
, "hello ''world''\n",
378 array( "TextContentTest_testGetSecondaryDataUpdates_2",
379 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
387 * @dataProvider dataGetDeletionUpdates
388 * @covers TextContent::getDeletionUpdates
390 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
391 $ns = $this->getDefaultWikitextNS();
392 $title = Title
::newFromText( $title, $ns );
394 $content = ContentHandler
::makeContent( $text, $title, $model );
396 $page = WikiPage
::factory( $title );
397 $page->doEditContent( $content, '' );
399 $updates = $content->getDeletionUpdates( $page );
401 // make updates accessible by class name
402 foreach ( $updates as $update ) {
403 $class = get_class( $update );
404 $updates[$class] = $update;
407 if ( !$expectedStuff ) {
408 $this->assertTrue( true ); // make phpunit happy
412 foreach ( $expectedStuff as $class => $fieldValues ) {
413 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
415 $update = $updates[$class];
417 foreach ( $fieldValues as $field => $value ) {
418 $v = $update->$field; #if the field doesn't exist, just crash and burn
419 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
423 $page->doDeleteArticle( '' );
426 public static function provideConvert() {
430 CONTENT_MODEL_WIKITEXT
,
436 CONTENT_MODEL_WIKITEXT
,
448 CONTENT_MODEL_JAVASCRIPT
,
456 * @dataProvider provideConvert
457 * @covers TextContent::convert
459 public function testConvert( $text, $model, $lossy, $expectedNative ) {
460 $content = $this->newContent( $text );
462 $converted = $content->convert( $model, $lossy );
464 if ( $expectedNative === false ) {
465 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
467 $this->assertInstanceOf( 'Content', $converted );
468 $this->assertEquals( $expectedNative, $converted->getNativeData() );