3 use MediaWiki\MediaWikiServices
;
6 * @group ContentHandler
8 * ^--- needed, because we do need the database to test link updates
10 class TextContentTest
extends MediaWikiLangTestCase
{
13 protected function setUp() {
18 $user->setName( '127.0.0.1' );
20 $this->context
= new RequestContext( new FauxRequest() );
21 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
22 $this->context
->setUser( $user );
24 $this->setMwGlobals( [
26 'wgTextModelsToParse' => [
27 CONTENT_MODEL_WIKITEXT
,
29 CONTENT_MODEL_JAVASCRIPT
,
32 'wgCapitalLinks' => true,
33 'wgHooks' => [], // bypass hook ContentGetParserOutput that force custom rendering
36 MWTidy
::destroySingleton();
39 protected function tearDown() {
40 MWTidy
::destroySingleton();
44 public function newContent( $text ) {
45 return new TextContent( $text );
48 public static function dataGetParserOutput() {
51 'TextContentTest_testGetParserOutput',
53 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
63 * @dataProvider dataGetParserOutput
64 * @covers TextContent::getParserOutput
66 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
67 $expectedFields = null
69 $title = Title
::newFromText( $title );
70 $content = ContentHandler
::makeContent( $text, $title, $model );
72 $po = $content->getParserOutput( $title );
74 $html = $po->getText();
75 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
77 $this->assertEquals( $expectedHtml, trim( $html ) );
79 if ( $expectedFields ) {
80 foreach ( $expectedFields as $field => $exp ) {
81 $f = 'get' . ucfirst( $field );
82 $v = call_user_func( [ $po, $f ] );
84 if ( is_array( $exp ) ) {
85 $this->assertArrayEquals( $exp, $v );
87 $this->assertEquals( $exp, $v );
92 // TODO: assert more properties
95 public static function dataPreSaveTransform() {
98 # 0: no signature resolution
108 # 2: newline normalization
109 "LF\n\nCRLF\r\n\r\nCR\r\rEND",
110 "LF\n\nCRLF\n\nCR\n\nEND",
116 * @dataProvider dataPreSaveTransform
117 * @covers TextContent::preSaveTransform
119 public function testPreSaveTransform( $text, $expected ) {
120 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(),
121 MediaWikiServices
::getInstance()->getContentLanguage() );
123 $content = $this->newContent( $text );
124 $content = $content->preSaveTransform(
125 $this->context
->getTitle(),
126 $this->context
->getUser(),
130 $this->assertEquals( $expected, $content->getNativeData() );
133 public static function dataPreloadTransform() {
143 * @dataProvider dataPreloadTransform
144 * @covers TextContent::preloadTransform
146 public function testPreloadTransform( $text, $expected ) {
147 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(),
148 MediaWikiServices
::getInstance()->getContentLanguage() );
150 $content = $this->newContent( $text );
151 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
153 $this->assertEquals( $expected, $content->getNativeData() );
156 public static function dataGetRedirectTarget() {
158 [ '#REDIRECT [[Test]]',
165 * @dataProvider dataGetRedirectTarget
166 * @covers TextContent::getRedirectTarget
168 public function testGetRedirectTarget( $text, $expected ) {
169 $content = $this->newContent( $text );
170 $t = $content->getRedirectTarget();
172 if ( is_null( $expected ) ) {
173 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
175 $this->assertEquals( $expected, $t->getPrefixedText() );
180 * @dataProvider dataGetRedirectTarget
181 * @covers TextContent::isRedirect
183 public function testIsRedirect( $text, $expected ) {
184 $content = $this->newContent( $text );
186 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
189 public static function dataIsCountable() {
205 * @dataProvider dataIsCountable
206 * @covers TextContent::isCountable
208 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
209 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
211 $content = $this->newContent( $text );
213 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
218 'isCountable() returned unexpected value ' . var_export( $v, true )
219 . ' instead of ' . var_export( $expected, true )
220 . " in mode `$mode` for text \"$text\""
224 public static function dataGetTextForSummary() {
234 [ '[[hello world]].',
242 * @dataProvider dataGetTextForSummary
243 * @covers TextContent::getTextForSummary
245 public function testGetTextForSummary( $text, $maxlength, $expected ) {
246 $content = $this->newContent( $text );
248 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
252 * @covers TextContent::getTextForSearchIndex
254 public function testGetTextForSearchIndex() {
255 $content = $this->newContent( 'hello world.' );
257 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
261 * @covers TextContent::copy
263 public function testCopy() {
264 $content = $this->newContent( 'hello world.' );
265 $copy = $content->copy();
267 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
268 $this->assertEquals( 'hello world.', $copy->getNativeData() );
272 * @covers TextContent::getSize
274 public function testGetSize() {
275 $content = $this->newContent( 'hello world.' );
277 $this->assertEquals( 12, $content->getSize() );
281 * @covers TextContent::getNativeData
283 public function testGetNativeData() {
284 $content = $this->newContent( 'hello world.' );
286 $this->assertEquals( 'hello world.', $content->getNativeData() );
290 * @covers TextContent::getWikitextForTransclusion
292 public function testGetWikitextForTransclusion() {
293 $content = $this->newContent( 'hello world.' );
295 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
299 * @covers TextContent::getModel
301 public function testGetModel() {
302 $content = $this->newContent( "hello world." );
304 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
308 * @covers TextContent::getContentHandler
310 public function testGetContentHandler() {
311 $content = $this->newContent( "hello world." );
313 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
316 public static function dataIsEmpty() {
321 [ 'hallo welt.', false ],
326 * @dataProvider dataIsEmpty
327 * @covers TextContent::isEmpty
329 public function testIsEmpty( $text, $empty ) {
330 $content = $this->newContent( $text );
332 $this->assertEquals( $empty, $content->isEmpty() );
335 public static function dataEquals() {
337 [ new TextContent( "hallo" ), null, false ],
338 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
339 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
340 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
341 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
346 * @dataProvider dataEquals
347 * @covers TextContent::equals
349 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
350 $this->assertEquals( $equal, $a->equals( $b ) );
353 public static function dataGetDeletionUpdates() {
355 [ "TextContentTest_testGetSecondaryDataUpdates_1",
356 CONTENT_MODEL_TEXT
, "hello ''world''\n",
359 [ "TextContentTest_testGetSecondaryDataUpdates_2",
360 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
368 * @dataProvider dataGetDeletionUpdates
369 * @covers TextContent::getDeletionUpdates
371 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
372 $ns = $this->getDefaultWikitextNS();
373 $title = Title
::newFromText( $title, $ns );
375 $content = ContentHandler
::makeContent( $text, $title, $model );
377 $page = WikiPage
::factory( $title );
378 $page->doEditContent( $content, '' );
380 $updates = $content->getDeletionUpdates( $page );
382 // make updates accessible by class name
383 foreach ( $updates as $update ) {
384 $class = get_class( $update );
385 $updates[$class] = $update;
388 if ( !$expectedStuff ) {
389 $this->assertTrue( true ); // make phpunit happy
393 foreach ( $expectedStuff as $class => $fieldValues ) {
394 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
396 $update = $updates[$class];
398 foreach ( $fieldValues as $field => $value ) {
399 $v = $update->$field; # if the field doesn't exist, just crash and burn
400 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
404 $page->doDeleteArticle( '' );
407 public static function provideConvert() {
411 CONTENT_MODEL_WIKITEXT
,
417 CONTENT_MODEL_WIKITEXT
,
429 CONTENT_MODEL_JAVASCRIPT
,
437 * @dataProvider provideConvert
438 * @covers TextContent::convert
440 public function testConvert( $text, $model, $lossy, $expectedNative ) {
441 $content = $this->newContent( $text );
443 $converted = $content->convert( $model, $lossy );
445 if ( $expectedNative === false ) {
446 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
448 $this->assertInstanceOf( Content
::class, $converted );
449 $this->assertEquals( $expectedNative, $converted->getNativeData() );
454 * @covers TextContent::normalizeLineEndings
455 * @dataProvider provideNormalizeLineEndings
457 public function testNormalizeLineEndings( $input, $expected ) {
458 $this->assertEquals( $expected, TextContent
::normalizeLineEndings( $input ) );
461 public static function provideNormalizeLineEndings() {