3 use MediaWiki\Content\Content
;
4 use MediaWiki\Content\JavaScriptContent
;
5 use MediaWiki\Content\TextContent
;
6 use MediaWiki\Content\WikitextContent
;
7 use MediaWiki\Context\RequestContext
;
8 use MediaWiki\MainConfigNames
;
9 use MediaWiki\Title\Title
;
10 use MediaWiki\User\User
;
13 * Needs database to do link updates.
15 * @group ContentHandler
17 * @covers \MediaWiki\Content\TextContent
18 * @covers \MediaWiki\Content\TextContentHandler
20 class TextContentTest
extends MediaWikiLangTestCase
{
21 /** @var RequestContext */
24 protected function setUp(): void
{
29 $user->setName( '127.0.0.1' );
31 $this->context
= new RequestContext();
32 $this->context
->setTitle( Title
::makeTitle( NS_MAIN
, 'Test' ) );
33 $this->context
->setUser( $user );
35 RequestContext
::getMain()->setTitle( $this->context
->getTitle() );
37 $this->overrideConfigValues( [
38 MainConfigNames
::TextModelsToParse
=> [
39 CONTENT_MODEL_WIKITEXT
,
41 CONTENT_MODEL_JAVASCRIPT
,
43 MainConfigNames
::CapitalLinks
=> true,
45 $this->clearHook( 'ContentGetParserOutput' );
49 * NOTE: Overridden by subclass!
54 public function newContent( $text ) {
55 return new TextContent( $text );
58 public static function dataGetRedirectTarget() {
60 [ '#REDIRECT [[Test]]',
67 * @dataProvider dataGetRedirectTarget
69 public function testGetRedirectTarget( $text, $expected ) {
70 $content = $this->newContent( $text );
71 $t = $content->getRedirectTarget();
73 if ( $expected === null ) {
74 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
76 $this->assertEquals( $expected, $t->getPrefixedText() );
81 * @dataProvider dataGetRedirectTarget
83 public function testIsRedirect( $text, $expected ) {
84 $content = $this->newContent( $text );
86 $this->assertEquals( $expected !== null, $content->isRedirect() );
89 public static function dataIsCountable() {
105 * @dataProvider dataIsCountable
107 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
108 $this->overrideConfigValue( MainConfigNames
::ArticleCountMethod
, $mode );
110 $content = $this->newContent( $text );
112 $v = $content->isCountable( $hasLinks );
117 'isCountable() returned unexpected value ' . var_export( $v, true )
118 . ' instead of ' . var_export( $expected, true )
119 . " in mode `$mode` for text \"$text\""
123 public static function dataGetTextForSummary() {
133 [ '[[hello world]].',
141 * @dataProvider dataGetTextForSummary
143 public function testGetTextForSummary( $text, $maxlength, $expected ) {
144 $content = $this->newContent( $text );
146 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
151 public function testCopy() {
152 $content = $this->newContent( 'hello world.' );
153 $copy = $content->copy();
155 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
156 $this->assertEquals( 'hello world.', $copy->getText() );
159 public function testGetTextMethods() {
160 $content = $this->newContent( 'hello world.' );
162 $this->assertEquals( 12, $content->getSize() );
163 $this->assertEquals( 'hello world.', $content->getText() );
164 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
165 $this->assertEquals( 'hello world.', $content->getNativeData() );
166 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
169 public function testGetModel() {
170 $content = $this->newContent( "hello world." );
172 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
175 public function testGetContentHandler() {
176 $content = $this->newContent( "hello world." );
178 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
181 public static function dataIsEmpty() {
186 [ 'hallo welt.', false ],
191 * @dataProvider dataIsEmpty
193 public function testIsEmpty( $text, $empty ) {
194 $content = $this->newContent( $text );
196 $this->assertEquals( $empty, $content->isEmpty() );
199 public static function dataEquals() {
201 [ new TextContent( "hallo" ), null, false ],
202 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
203 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
204 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
205 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
210 * @dataProvider dataEquals
212 public function testEquals( Content
$a, ?Content
$b = null, $equal = false ) {
213 $this->assertEquals( $equal, $a->equals( $b ) );
216 public static function provideConvert() {
220 CONTENT_MODEL_WIKITEXT
,
226 CONTENT_MODEL_WIKITEXT
,
238 CONTENT_MODEL_JAVASCRIPT
,
246 * @dataProvider provideConvert
248 public function testConvert( $text, $model, $lossy, $expectedNative ) {
249 $content = $this->newContent( $text );
251 /** @var TextContent $converted */
252 $converted = $content->convert( $model, $lossy );
254 if ( $expectedNative === false ) {
255 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
257 $this->assertInstanceOf( Content
::class, $converted );
258 $this->assertEquals( $expectedNative, $converted->getText() );
263 * @dataProvider provideNormalizeLineEndings
265 public function testNormalizeLineEndings( $input, $expected ) {
266 $this->assertEquals( $expected, TextContent
::normalizeLineEndings( $input ) );
269 public static function provideNormalizeLineEndings() {
286 public function testSerialize() {
287 $cnt = $this->newContent( 'testing text' );
289 $this->assertSame( 'testing text', $cnt->serialize() );