Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / content / TextContentTest.php
blob0d5235f0041ce20364f27815816111c14ff5ce7c
1 <?php
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;
12 /**
13 * Needs database to do link updates.
15 * @group ContentHandler
16 * @group Database
17 * @covers \MediaWiki\Content\TextContent
18 * @covers \MediaWiki\Content\TextContentHandler
20 class TextContentTest extends MediaWikiLangTestCase {
21 /** @var RequestContext */
22 protected $context;
24 protected function setUp(): void {
25 parent::setUp();
27 // Anon user
28 $user = new User();
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,
40 CONTENT_MODEL_CSS,
41 CONTENT_MODEL_JAVASCRIPT,
43 MainConfigNames::CapitalLinks => true,
44 ] );
45 $this->clearHook( 'ContentGetParserOutput' );
48 /**
49 * NOTE: Overridden by subclass!
51 * @param string $text
52 * @return TextContent
54 public function newContent( $text ) {
55 return new TextContent( $text );
58 public static function dataGetRedirectTarget() {
59 return [
60 [ '#REDIRECT [[Test]]',
61 null,
66 /**
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" );
75 } else {
76 $this->assertEquals( $expected, $t->getPrefixedText() );
80 /**
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() {
90 return [
91 [ '',
92 null,
93 'any',
94 true
96 [ 'Foo',
97 null,
98 'any',
99 true
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 );
114 $this->assertEquals(
115 $expected,
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() {
124 return [
125 [ "hello\nworld.",
127 'hello world.',
129 [ 'hello world.',
131 'hello...',
133 [ '[[hello world]].',
135 '[[hel...',
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() {
182 return [
183 [ '', true ],
184 [ ' ', false ],
185 [ '0', false ],
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() {
200 return [
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() {
217 return [
218 [ // #0
219 'Hallo Welt',
220 CONTENT_MODEL_WIKITEXT,
221 'lossless',
222 'Hallo Welt'
224 [ // #1
225 'Hallo Welt',
226 CONTENT_MODEL_WIKITEXT,
227 'lossless',
228 'Hallo Welt'
230 [ // #1
231 'Hallo Welt',
232 CONTENT_MODEL_CSS,
233 'lossless',
234 'Hallo Welt'
236 [ // #1
237 'Hallo Welt',
238 CONTENT_MODEL_JAVASCRIPT,
239 'lossless',
240 'Hallo Welt'
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!" );
256 } else {
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() {
270 return [
272 "Foo\r\nbar",
273 "Foo\nbar"
276 "Foo\rbar",
277 "Foo\nbar"
280 "Foobar\n ",
281 "Foobar"
286 public function testSerialize() {
287 $cnt = $this->newContent( 'testing text' );
289 $this->assertSame( 'testing text', $cnt->serialize() );