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
106 # 2: newline normalization
107 "LF\n\nCRLF\r\n\r\nCR\r\rEND",
108 "LF\n\nCRLF\n\nCR\n\nEND",
114 * @dataProvider dataPreSaveTransform
115 * @covers TextContent::preSaveTransform
117 public function testPreSaveTransform( $text, $expected ) {
120 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
122 $content = $this->newContent( $text );
123 $content = $content->preSaveTransform(
124 $this->context
->getTitle(),
125 $this->context
->getUser(),
129 $this->assertEquals( $expected, $content->getNativeData() );
132 public static function dataPreloadTransform() {
142 * @dataProvider dataPreloadTransform
143 * @covers TextContent::preloadTransform
145 public function testPreloadTransform( $text, $expected ) {
147 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
149 $content = $this->newContent( $text );
150 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
152 $this->assertEquals( $expected, $content->getNativeData() );
155 public static function dataGetRedirectTarget() {
157 [ '#REDIRECT [[Test]]',
164 * @dataProvider dataGetRedirectTarget
165 * @covers TextContent::getRedirectTarget
167 public function testGetRedirectTarget( $text, $expected ) {
168 $content = $this->newContent( $text );
169 $t = $content->getRedirectTarget();
171 if ( is_null( $expected ) ) {
172 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
174 $this->assertEquals( $expected, $t->getPrefixedText() );
179 * @dataProvider dataGetRedirectTarget
180 * @covers TextContent::isRedirect
182 public function testIsRedirect( $text, $expected ) {
183 $content = $this->newContent( $text );
185 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
188 public static function dataIsCountable() {
214 * @dataProvider dataIsCountable
215 * @covers TextContent::isCountable
217 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
218 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
220 $content = $this->newContent( $text );
222 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
227 'isCountable() returned unexpected value ' . var_export( $v, true )
228 . ' instead of ' . var_export( $expected, true )
229 . " in mode `$mode` for text \"$text\""
233 public static function dataGetTextForSummary() {
243 [ '[[hello world]].',
251 * @dataProvider dataGetTextForSummary
252 * @covers TextContent::getTextForSummary
254 public function testGetTextForSummary( $text, $maxlength, $expected ) {
255 $content = $this->newContent( $text );
257 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
261 * @covers TextContent::getTextForSearchIndex
263 public function testGetTextForSearchIndex() {
264 $content = $this->newContent( 'hello world.' );
266 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
270 * @covers TextContent::copy
272 public function testCopy() {
273 $content = $this->newContent( 'hello world.' );
274 $copy = $content->copy();
276 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
277 $this->assertEquals( 'hello world.', $copy->getNativeData() );
281 * @covers TextContent::getSize
283 public function testGetSize() {
284 $content = $this->newContent( 'hello world.' );
286 $this->assertEquals( 12, $content->getSize() );
290 * @covers TextContent::getNativeData
292 public function testGetNativeData() {
293 $content = $this->newContent( 'hello world.' );
295 $this->assertEquals( 'hello world.', $content->getNativeData() );
299 * @covers TextContent::getWikitextForTransclusion
301 public function testGetWikitextForTransclusion() {
302 $content = $this->newContent( 'hello world.' );
304 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
308 * @covers TextContent::getModel
310 public function testGetModel() {
311 $content = $this->newContent( "hello world." );
313 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
317 * @covers TextContent::getContentHandler
319 public function testGetContentHandler() {
320 $content = $this->newContent( "hello world." );
322 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
325 public static function dataIsEmpty() {
330 [ 'hallo welt.', false ],
335 * @dataProvider dataIsEmpty
336 * @covers TextContent::isEmpty
338 public function testIsEmpty( $text, $empty ) {
339 $content = $this->newContent( $text );
341 $this->assertEquals( $empty, $content->isEmpty() );
344 public static function dataEquals() {
346 [ new TextContent( "hallo" ), null, false ],
347 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
348 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
349 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
350 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
355 * @dataProvider dataEquals
356 * @covers TextContent::equals
358 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
359 $this->assertEquals( $equal, $a->equals( $b ) );
362 public static function dataGetDeletionUpdates() {
364 [ "TextContentTest_testGetSecondaryDataUpdates_1",
365 CONTENT_MODEL_TEXT
, "hello ''world''\n",
368 [ "TextContentTest_testGetSecondaryDataUpdates_2",
369 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
377 * @dataProvider dataGetDeletionUpdates
378 * @covers TextContent::getDeletionUpdates
380 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
381 $ns = $this->getDefaultWikitextNS();
382 $title = Title
::newFromText( $title, $ns );
384 $content = ContentHandler
::makeContent( $text, $title, $model );
386 $page = WikiPage
::factory( $title );
387 $page->doEditContent( $content, '' );
389 $updates = $content->getDeletionUpdates( $page );
391 // make updates accessible by class name
392 foreach ( $updates as $update ) {
393 $class = get_class( $update );
394 $updates[$class] = $update;
397 if ( !$expectedStuff ) {
398 $this->assertTrue( true ); // make phpunit happy
402 foreach ( $expectedStuff as $class => $fieldValues ) {
403 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
405 $update = $updates[$class];
407 foreach ( $fieldValues as $field => $value ) {
408 $v = $update->$field; # if the field doesn't exist, just crash and burn
409 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
413 $page->doDeleteArticle( '' );
416 public static function provideConvert() {
420 CONTENT_MODEL_WIKITEXT
,
426 CONTENT_MODEL_WIKITEXT
,
438 CONTENT_MODEL_JAVASCRIPT
,
446 * @dataProvider provideConvert
447 * @covers TextContent::convert
449 public function testConvert( $text, $model, $lossy, $expectedNative ) {
450 $content = $this->newContent( $text );
452 $converted = $content->convert( $model, $lossy );
454 if ( $expectedNative === false ) {
455 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
457 $this->assertInstanceOf( Content
::class, $converted );
458 $this->assertEquals( $expectedNative, $converted->getNativeData() );
463 * @covers TextContent::normalizeLineEndings
464 * @dataProvider provideNormalizeLineEndings
466 public function testNormalizeLineEndings( $input, $expected ) {
467 $this->assertEquals( $expected, TextContent
::normalizeLineEndings( $input ) );
470 public static function provideNormalizeLineEndings() {