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,
34 'wgCapitalLinks' => true,
37 // bypass hooks that force custom rendering
38 if ( isset( $wgHooks['ContentGetParserOutput'] ) ) {
39 $this->savedContentGetParserOutput
= $wgHooks['ContentGetParserOutput'];
40 unset( $wgHooks['ContentGetParserOutput'] );
44 public function teardown() {
47 // restore hooks that force custom rendering
48 if ( $this->savedContentGetParserOutput
!== null ) {
49 $wgHooks['ContentGetParserOutput'] = $this->savedContentGetParserOutput
;
55 public function newContent( $text ) {
56 return new TextContent( $text );
59 public static function dataGetParserOutput() {
62 'TextContentTest_testGetParserOutput',
64 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
74 * @dataProvider dataGetParserOutput
75 * @covers TextContent::getParserOutput
77 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
78 $expectedFields = null
80 $title = Title
::newFromText( $title );
81 $content = ContentHandler
::makeContent( $text, $title, $model );
83 $po = $content->getParserOutput( $title );
85 $html = $po->getText();
86 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
88 $this->assertEquals( $expectedHtml, trim( $html ) );
90 if ( $expectedFields ) {
91 foreach ( $expectedFields as $field => $exp ) {
92 $f = 'get' . ucfirst( $field );
93 $v = call_user_func( array( $po, $f ) );
95 if ( is_array( $exp ) ) {
96 $this->assertArrayEquals( $exp, $v );
98 $this->assertEquals( $exp, $v );
103 // TODO: assert more properties
106 public static function dataPreSaveTransform() {
109 #0: no signature resolution
122 * @dataProvider dataPreSaveTransform
123 * @covers TextContent::preSaveTransform
125 public function testPreSaveTransform( $text, $expected ) {
128 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
130 $content = $this->newContent( $text );
131 $content = $content->preSaveTransform(
132 $this->context
->getTitle(),
133 $this->context
->getUser(),
137 $this->assertEquals( $expected, $content->getNativeData() );
140 public static function dataPreloadTransform() {
150 * @dataProvider dataPreloadTransform
151 * @covers TextContent::preloadTransform
153 public function testPreloadTransform( $text, $expected ) {
155 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
157 $content = $this->newContent( $text );
158 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
160 $this->assertEquals( $expected, $content->getNativeData() );
163 public static function dataGetRedirectTarget() {
165 array( '#REDIRECT [[Test]]',
172 * @dataProvider dataGetRedirectTarget
173 * @covers TextContent::getRedirectTarget
175 public function testGetRedirectTarget( $text, $expected ) {
176 $content = $this->newContent( $text );
177 $t = $content->getRedirectTarget();
179 if ( is_null( $expected ) ) {
180 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
182 $this->assertEquals( $expected, $t->getPrefixedText() );
187 * @dataProvider dataGetRedirectTarget
188 * @covers TextContent::isRedirect
190 public function testIsRedirect( $text, $expected ) {
191 $content = $this->newContent( $text );
193 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
197 * @todo Test needs database! Should be done by a test class in the Database group.
200 public function getRedirectChain() {
201 $text = $this->getNativeData();
202 return Title::newFromRedirectArray( $text );
207 * @todo Test needs database! Should be done by a test class in the Database group.
210 public function getUltimateRedirectTarget() {
211 $text = $this->getNativeData();
212 return Title::newFromRedirectRecurse( $text );
216 public static function dataIsCountable() {
242 * @dataProvider dataIsCountable
244 * @covers TextContent::isCountable
246 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
247 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
249 $content = $this->newContent( $text );
251 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
256 'isCountable() returned unexpected value ' . var_export( $v, true )
257 . ' instead of ' . var_export( $expected, true )
258 . " in mode `$mode` for text \"$text\""
262 public static function dataGetTextForSummary() {
264 array( "hello\nworld.",
268 array( 'hello world.',
272 array( '[[hello world]].',
280 * @dataProvider dataGetTextForSummary
281 * @covers TextContent::getTextForSummary
283 public function testGetTextForSummary( $text, $maxlength, $expected ) {
284 $content = $this->newContent( $text );
286 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
290 * @covers TextContent::getTextForSearchIndex
292 public function testGetTextForSearchIndex() {
293 $content = $this->newContent( 'hello world.' );
295 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
299 * @covers TextContent::copy
301 public function testCopy() {
302 $content = $this->newContent( 'hello world.' );
303 $copy = $content->copy();
305 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
306 $this->assertEquals( 'hello world.', $copy->getNativeData() );
310 * @covers TextContent::getSize
312 public function testGetSize() {
313 $content = $this->newContent( 'hello world.' );
315 $this->assertEquals( 12, $content->getSize() );
319 * @covers TextContent::getNativeData
321 public function testGetNativeData() {
322 $content = $this->newContent( 'hello world.' );
324 $this->assertEquals( 'hello world.', $content->getNativeData() );
328 * @covers TextContent::getWikitextForTransclusion
330 public function testGetWikitextForTransclusion() {
331 $content = $this->newContent( 'hello world.' );
333 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
337 * @covers TextContent::getModel
339 public function testGetModel() {
340 $content = $this->newContent( "hello world." );
342 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
346 * @covers TextContent::getContentHandler
348 public function testGetContentHandler() {
349 $content = $this->newContent( "hello world." );
351 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
354 public static function dataIsEmpty() {
359 array( 'hallo welt.', false ),
364 * @dataProvider dataIsEmpty
365 * @covers TextContent::isEmpty
367 public function testIsEmpty( $text, $empty ) {
368 $content = $this->newContent( $text );
370 $this->assertEquals( $empty, $content->isEmpty() );
373 public static function dataEquals() {
375 array( new TextContent( "hallo" ), null, false ),
376 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
377 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
378 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
379 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
384 * @dataProvider dataEquals
385 * @covers TextContent::equals
387 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
388 $this->assertEquals( $equal, $a->equals( $b ) );
391 public static function dataGetDeletionUpdates() {
393 array( "TextContentTest_testGetSecondaryDataUpdates_1",
394 CONTENT_MODEL_TEXT
, "hello ''world''\n",
397 array( "TextContentTest_testGetSecondaryDataUpdates_2",
398 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
406 * @dataProvider dataGetDeletionUpdates
407 * @covers TextContent::getDeletionUpdates
409 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
410 $ns = $this->getDefaultWikitextNS();
411 $title = Title
::newFromText( $title, $ns );
413 $content = ContentHandler
::makeContent( $text, $title, $model );
415 $page = WikiPage
::factory( $title );
416 $page->doEditContent( $content, '' );
418 $updates = $content->getDeletionUpdates( $page );
420 // make updates accessible by class name
421 foreach ( $updates as $update ) {
422 $class = get_class( $update );
423 $updates[$class] = $update;
426 if ( !$expectedStuff ) {
427 $this->assertTrue( true ); // make phpunit happy
431 foreach ( $expectedStuff as $class => $fieldValues ) {
432 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
434 $update = $updates[$class];
436 foreach ( $fieldValues as $field => $value ) {
437 $v = $update->$field; #if the field doesn't exist, just crash and burn
438 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
442 $page->doDeleteArticle( '' );
445 public static function provideConvert() {
449 CONTENT_MODEL_WIKITEXT
,
455 CONTENT_MODEL_WIKITEXT
,
467 CONTENT_MODEL_JAVASCRIPT
,
475 * @dataProvider provideConvert
476 * @covers TextContent::convert
478 public function testConvert( $text, $model, $lossy, $expectedNative ) {
479 $content = $this->newContent( $text );
481 $converted = $content->convert( $model, $lossy );
483 if ( $expectedNative === false ) {
484 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
486 $this->assertInstanceOf( 'Content', $converted );
487 $this->assertEquals( $expectedNative, $converted->getNativeData() );