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( [
24 'wgTextModelsToParse' => [
25 CONTENT_MODEL_WIKITEXT
,
27 CONTENT_MODEL_JAVASCRIPT
,
30 'wgCapitalLinks' => true,
31 'wgHooks' => [], // 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( [ $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 [ '#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 getUltimateRedirectTarget() {
188 $text = $this->getNativeData();
189 return Title::newFromRedirectRecurse( $text );
193 public static function dataIsCountable() {
219 * @dataProvider dataIsCountable
221 * @covers TextContent::isCountable
223 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
224 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
226 $content = $this->newContent( $text );
228 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
233 'isCountable() returned unexpected value ' . var_export( $v, true )
234 . ' instead of ' . var_export( $expected, true )
235 . " in mode `$mode` for text \"$text\""
239 public static function dataGetTextForSummary() {
249 [ '[[hello world]].',
257 * @dataProvider dataGetTextForSummary
258 * @covers TextContent::getTextForSummary
260 public function testGetTextForSummary( $text, $maxlength, $expected ) {
261 $content = $this->newContent( $text );
263 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
267 * @covers TextContent::getTextForSearchIndex
269 public function testGetTextForSearchIndex() {
270 $content = $this->newContent( 'hello world.' );
272 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
276 * @covers TextContent::copy
278 public function testCopy() {
279 $content = $this->newContent( 'hello world.' );
280 $copy = $content->copy();
282 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
283 $this->assertEquals( 'hello world.', $copy->getNativeData() );
287 * @covers TextContent::getSize
289 public function testGetSize() {
290 $content = $this->newContent( 'hello world.' );
292 $this->assertEquals( 12, $content->getSize() );
296 * @covers TextContent::getNativeData
298 public function testGetNativeData() {
299 $content = $this->newContent( 'hello world.' );
301 $this->assertEquals( 'hello world.', $content->getNativeData() );
305 * @covers TextContent::getWikitextForTransclusion
307 public function testGetWikitextForTransclusion() {
308 $content = $this->newContent( 'hello world.' );
310 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
314 * @covers TextContent::getModel
316 public function testGetModel() {
317 $content = $this->newContent( "hello world." );
319 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
323 * @covers TextContent::getContentHandler
325 public function testGetContentHandler() {
326 $content = $this->newContent( "hello world." );
328 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
331 public static function dataIsEmpty() {
336 [ 'hallo welt.', false ],
341 * @dataProvider dataIsEmpty
342 * @covers TextContent::isEmpty
344 public function testIsEmpty( $text, $empty ) {
345 $content = $this->newContent( $text );
347 $this->assertEquals( $empty, $content->isEmpty() );
350 public static function dataEquals() {
352 [ new TextContent( "hallo" ), null, false ],
353 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
354 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
355 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
356 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
361 * @dataProvider dataEquals
362 * @covers TextContent::equals
364 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
365 $this->assertEquals( $equal, $a->equals( $b ) );
368 public static function dataGetDeletionUpdates() {
370 [ "TextContentTest_testGetSecondaryDataUpdates_1",
371 CONTENT_MODEL_TEXT
, "hello ''world''\n",
374 [ "TextContentTest_testGetSecondaryDataUpdates_2",
375 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
383 * @dataProvider dataGetDeletionUpdates
384 * @covers TextContent::getDeletionUpdates
386 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
387 $ns = $this->getDefaultWikitextNS();
388 $title = Title
::newFromText( $title, $ns );
390 $content = ContentHandler
::makeContent( $text, $title, $model );
392 $page = WikiPage
::factory( $title );
393 $page->doEditContent( $content, '' );
395 $updates = $content->getDeletionUpdates( $page );
397 // make updates accessible by class name
398 foreach ( $updates as $update ) {
399 $class = get_class( $update );
400 $updates[$class] = $update;
403 if ( !$expectedStuff ) {
404 $this->assertTrue( true ); // make phpunit happy
408 foreach ( $expectedStuff as $class => $fieldValues ) {
409 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
411 $update = $updates[$class];
413 foreach ( $fieldValues as $field => $value ) {
414 $v = $update->$field; # if the field doesn't exist, just crash and burn
415 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
419 $page->doDeleteArticle( '' );
422 public static function provideConvert() {
426 CONTENT_MODEL_WIKITEXT
,
432 CONTENT_MODEL_WIKITEXT
,
444 CONTENT_MODEL_JAVASCRIPT
,
452 * @dataProvider provideConvert
453 * @covers TextContent::convert
455 public function testConvert( $text, $model, $lossy, $expectedNative ) {
456 $content = $this->newContent( $text );
458 $converted = $content->convert( $model, $lossy );
460 if ( $expectedNative === false ) {
461 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
463 $this->assertInstanceOf( 'Content', $converted );
464 $this->assertEquals( $expectedNative, $converted->getNativeData() );