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() {
16 // trigger purging of all page related tables
17 $this->tablesUsed
[] = 'page';
18 $this->tablesUsed
[] = 'revision';
22 $user->setName( '127.0.0.1' );
24 $this->context
= new RequestContext( new FauxRequest() );
25 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
26 $this->context
->setUser( $user );
28 $this->setMwGlobals( [
30 'wgTextModelsToParse' => [
31 CONTENT_MODEL_WIKITEXT
,
33 CONTENT_MODEL_JAVASCRIPT
,
35 'wgTidyConfig' => [ 'driver' => 'RemexHtml' ],
36 'wgCapitalLinks' => true,
37 'wgHooks' => [], // bypass hook ContentGetParserOutput that force custom rendering
40 MWTidy
::destroySingleton();
43 protected function tearDown() {
44 MWTidy
::destroySingleton();
48 public function newContent( $text ) {
49 return new TextContent( $text );
52 public static function dataGetParserOutput() {
55 'TextContentTest_testGetParserOutput',
57 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
67 * @dataProvider dataGetParserOutput
68 * @covers TextContent::getParserOutput
70 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
71 $expectedFields = null
73 $title = Title
::newFromText( $title );
74 $content = ContentHandler
::makeContent( $text, $title, $model );
76 $po = $content->getParserOutput( $title );
78 $html = $po->getText();
79 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
81 $this->assertEquals( $expectedHtml, trim( $html ) );
83 if ( $expectedFields ) {
84 foreach ( $expectedFields as $field => $exp ) {
85 $f = 'get' . ucfirst( $field );
86 $v = call_user_func( [ $po, $f ] );
88 if ( is_array( $exp ) ) {
89 $this->assertArrayEquals( $exp, $v );
91 $this->assertEquals( $exp, $v );
96 // TODO: assert more properties
99 public static function dataPreSaveTransform() {
102 # 0: no signature resolution
112 # 2: newline normalization
113 "LF\n\nCRLF\r\n\r\nCR\r\rEND",
114 "LF\n\nCRLF\n\nCR\n\nEND",
120 * @dataProvider dataPreSaveTransform
121 * @covers TextContent::preSaveTransform
123 public function testPreSaveTransform( $text, $expected ) {
124 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(),
125 MediaWikiServices
::getInstance()->getContentLanguage() );
127 $content = $this->newContent( $text );
128 $content = $content->preSaveTransform(
129 $this->context
->getTitle(),
130 $this->context
->getUser(),
134 $this->assertEquals( $expected, $content->getNativeData() );
137 public static function dataPreloadTransform() {
147 * @dataProvider dataPreloadTransform
148 * @covers TextContent::preloadTransform
150 public function testPreloadTransform( $text, $expected ) {
151 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(),
152 MediaWikiServices
::getInstance()->getContentLanguage() );
154 $content = $this->newContent( $text );
155 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
157 $this->assertEquals( $expected, $content->getNativeData() );
160 public static function dataGetRedirectTarget() {
162 [ '#REDIRECT [[Test]]',
169 * @dataProvider dataGetRedirectTarget
170 * @covers TextContent::getRedirectTarget
172 public function testGetRedirectTarget( $text, $expected ) {
173 $content = $this->newContent( $text );
174 $t = $content->getRedirectTarget();
176 if ( is_null( $expected ) ) {
177 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
179 $this->assertEquals( $expected, $t->getPrefixedText() );
184 * @dataProvider dataGetRedirectTarget
185 * @covers TextContent::isRedirect
187 public function testIsRedirect( $text, $expected ) {
188 $content = $this->newContent( $text );
190 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
193 public static function dataIsCountable() {
209 * @dataProvider dataIsCountable
210 * @covers TextContent::isCountable
212 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
213 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
215 $content = $this->newContent( $text );
217 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
222 'isCountable() returned unexpected value ' . var_export( $v, true )
223 . ' instead of ' . var_export( $expected, true )
224 . " in mode `$mode` for text \"$text\""
228 public static function dataGetTextForSummary() {
238 [ '[[hello world]].',
246 * @dataProvider dataGetTextForSummary
247 * @covers TextContent::getTextForSummary
249 public function testGetTextForSummary( $text, $maxlength, $expected ) {
250 $content = $this->newContent( $text );
252 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
256 * @covers TextContent::getTextForSearchIndex
258 public function testGetTextForSearchIndex() {
259 $content = $this->newContent( 'hello world.' );
261 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
265 * @covers TextContent::copy
267 public function testCopy() {
268 $content = $this->newContent( 'hello world.' );
269 $copy = $content->copy();
271 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
272 $this->assertEquals( 'hello world.', $copy->getNativeData() );
276 * @covers TextContent::getSize
278 public function testGetSize() {
279 $content = $this->newContent( 'hello world.' );
281 $this->assertEquals( 12, $content->getSize() );
285 * @covers TextContent::getNativeData
287 public function testGetNativeData() {
288 $content = $this->newContent( 'hello world.' );
290 $this->assertEquals( 'hello world.', $content->getNativeData() );
294 * @covers TextContent::getWikitextForTransclusion
296 public function testGetWikitextForTransclusion() {
297 $content = $this->newContent( 'hello world.' );
299 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
303 * @covers TextContent::getModel
305 public function testGetModel() {
306 $content = $this->newContent( "hello world." );
308 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
312 * @covers TextContent::getContentHandler
314 public function testGetContentHandler() {
315 $content = $this->newContent( "hello world." );
317 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
320 public static function dataIsEmpty() {
325 [ 'hallo welt.', false ],
330 * @dataProvider dataIsEmpty
331 * @covers TextContent::isEmpty
333 public function testIsEmpty( $text, $empty ) {
334 $content = $this->newContent( $text );
336 $this->assertEquals( $empty, $content->isEmpty() );
339 public static function dataEquals() {
341 [ new TextContent( "hallo" ), null, false ],
342 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
343 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
344 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
345 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
350 * @dataProvider dataEquals
351 * @covers TextContent::equals
353 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
354 $this->assertEquals( $equal, $a->equals( $b ) );
357 public static function dataGetDeletionUpdates() {
360 CONTENT_MODEL_TEXT
, "hello ''world''\n",
364 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
372 * @dataProvider dataGetDeletionUpdates
373 * @covers TextContent::getDeletionUpdates
375 public function testDeletionUpdates( $model, $text, $expectedStuff ) {
376 $page = $this->getNonexistingTestPage( get_class( $this ) . '-' . $this->getName() );
377 $title = $page->getTitle();
379 $content = ContentHandler
::makeContent( $text, $title, $model );
380 $page->doEditContent( $content, '' );
382 $updates = $content->getDeletionUpdates( $page );
384 // make updates accessible by class name
385 foreach ( $updates as $update ) {
386 $class = get_class( $update );
387 $updates[$class] = $update;
390 foreach ( $expectedStuff as $class => $fieldValues ) {
391 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
393 $update = $updates[$class];
395 foreach ( $fieldValues as $field => $value ) {
396 $v = $update->$field; # if the field doesn't exist, just crash and burn
397 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
401 // make phpunit happy even if $expectedStuff was empty
402 $this->assertTrue( true );
405 public static function provideConvert() {
409 CONTENT_MODEL_WIKITEXT
,
415 CONTENT_MODEL_WIKITEXT
,
427 CONTENT_MODEL_JAVASCRIPT
,
435 * @dataProvider provideConvert
436 * @covers TextContent::convert
438 public function testConvert( $text, $model, $lossy, $expectedNative ) {
439 $content = $this->newContent( $text );
441 $converted = $content->convert( $model, $lossy );
443 if ( $expectedNative === false ) {
444 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
446 $this->assertInstanceOf( Content
::class, $converted );
447 $this->assertEquals( $expectedNative, $converted->getNativeData() );
452 * @covers TextContent::normalizeLineEndings
453 * @dataProvider provideNormalizeLineEndings
455 public function testNormalizeLineEndings( $input, $expected ) {
456 $this->assertEquals( $expected, TextContent
::normalizeLineEndings( $input ) );
459 public static function provideNormalizeLineEndings() {