Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / content / WikitextContentTest.php
blobda4e3bc57fe1faaadeb60ff0e0f1d4f72259b7bf
1 <?php
3 use MediaWiki\Content\JavaScriptContent;
4 use MediaWiki\Content\TextContent;
5 use MediaWiki\Content\WikitextContent;
6 use MediaWiki\Deferred\LinksUpdate\LinksDeletionUpdate;
7 use MediaWiki\Parser\Parser;
8 use MediaWiki\Parser\ParserOptions;
9 use MediaWiki\Title\Title;
11 /**
12 * @group ContentHandler
14 * @group Database
15 * ^--- needed, because we do need the database to test link updates
16 * @covers \MediaWiki\Content\WikitextContent
18 class WikitextContentTest extends TextContentTest {
19 public const SECTIONS = "Intro
21 == stuff ==
22 hello world
24 == test ==
25 just a test
27 == foo ==
28 more stuff
31 public function newContent( $text ) {
32 return new WikitextContent( $text );
35 public static function dataGetSection() {
36 return [
37 [ self::SECTIONS,
38 "0",
39 "Intro"
41 [ self::SECTIONS,
42 "2",
43 "== test ==
44 just a test"
46 [ self::SECTIONS,
47 "8",
48 false
53 /**
54 * @dataProvider dataGetSection
56 public function testGetSection( $text, $sectionId, $expectedText ) {
57 $content = $this->newContent( $text );
59 $sectionContent = $content->getSection( $sectionId );
60 if ( is_object( $sectionContent ) ) {
61 $sectionText = $sectionContent->getText();
62 } else {
63 $sectionText = $sectionContent;
66 $this->assertEquals( $expectedText, $sectionText );
69 public static function dataReplaceSection() {
70 return [
71 [ self::SECTIONS,
72 "0",
73 "No more",
74 null,
75 trim( preg_replace( '/^Intro/m', 'No more', self::SECTIONS ) )
77 [ self::SECTIONS,
78 "",
79 "No more",
80 null,
81 "No more"
83 [ self::SECTIONS,
84 "2",
85 "== TEST ==\nmore fun",
86 null,
87 trim( preg_replace(
88 '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==",
89 self::SECTIONS
90 ) )
92 [ self::SECTIONS,
93 "8",
94 "No more",
95 null,
96 self::SECTIONS
98 [ self::SECTIONS,
99 "new",
100 "No more",
101 "New",
102 trim( self::SECTIONS ) . "\n\n\n== New ==\n\nNo more"
108 * @dataProvider dataReplaceSection
110 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
111 $content = $this->newContent( $text );
112 /** @var WikitextContent $c */
113 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
115 $this->assertEquals( $expected, $c ? $c->getText() : null );
118 public function testAddSectionHeader() {
119 $content = $this->newContent( 'hello world' );
120 $content = $content->addSectionHeader( 'test' );
121 $this->assertEquals( "== test ==\n\nhello world", $content->getText() );
123 $content = $this->newContent( 'hello world' );
124 $content = $content->addSectionHeader( '' );
125 $this->assertEquals( "hello world", $content->getText() );
128 public static function dataPreSaveTransform() {
129 return [
130 [ 'hello this is ~~~',
131 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
133 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
134 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
136 [ // rtrim
137 " Foo \n ",
138 " Foo",
143 public static function dataGetRedirectTarget() {
144 return [
145 [ '#REDIRECT [[Test]]',
146 'Test',
148 [ '#REDIRECT Test',
149 null,
151 [ '* #REDIRECT [[Test]]',
152 null,
157 public static function dataGetTextForSummary() {
158 return [
159 [ "hello\nworld.",
161 'hello world.',
163 [ 'hello world.',
165 'hello...',
167 [ '[[hello world]].',
169 'hel...',
174 public static function dataIsCountable() {
175 return [
176 [ '',
177 null,
178 'any',
179 true
181 [ 'Foo',
182 null,
183 'any',
184 true
186 [ 'Foo',
187 null,
188 'link',
189 false
191 [ 'Foo [[bar]]',
192 null,
193 'link',
194 true
196 [ 'Foo',
197 true,
198 'link',
199 true
201 [ 'Foo [[bar]]',
202 false,
203 'link',
204 false
206 [ '#REDIRECT [[bar]]',
207 true,
208 'any',
209 false
211 [ '#REDIRECT [[bar]]',
212 true,
213 'link',
214 false
219 public function testMatchMagicWord() {
220 $mw = $this->getServiceContainer()->getMagicWordFactory()->get( "staticredirect" );
222 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
223 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
225 $content = $this->newContent( "#REDIRECT [[FOO]]" );
226 $this->assertFalse(
227 $content->matchMagicWord( $mw ),
228 "should not have matched magic word"
232 public function testUpdateRedirect() {
233 $target = Title::makeTitle( NS_MAIN, 'TestUpdateRedirect_target' );
235 // test with non-redirect page
236 $content = $this->newContent( "hello world." );
237 $newContent = $content->updateRedirect( $target );
239 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
241 // test with actual redirect
242 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
243 $newContent = $content->updateRedirect( $target );
245 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
246 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
248 $this->assertEquals(
249 $target->getFullText(),
250 $newContent->getRedirectTarget()->getFullText()
254 public function testGetModel() {
255 $content = $this->newContent( "hello world." );
257 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
260 public function testGetContentHandler() {
261 $content = $this->newContent( "hello world." );
263 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
267 * @covers \MediaWiki\Parser\ParserOptions
269 public function testRedirectParserOption() {
270 $title = Title::makeTitle( NS_MAIN, 'TestRedirectParserOption' );
271 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
273 // Set up hook and its reporting variables
274 $wikitext = null;
275 $redirectTarget = null;
276 $this->mergeMwGlobalArrayValue( 'wgHooks', [
277 'InternalParseBeforeLinks' => [
278 static function ( Parser $parser, $text, $stripState ) use ( &$wikitext, &$redirectTarget ) {
279 $wikitext = $text;
280 $redirectTarget = $parser->getOptions()->getRedirectTarget();
283 ] );
285 // Test with non-redirect page
286 $wikitext = false;
287 $redirectTarget = false;
288 $content = $this->newContent( 'hello world.' );
289 $options = ParserOptions::newFromAnon();
290 $options->setRedirectTarget( $title );
291 $contentRenderer->getParserOutput( $content, $title, null, $options );
292 $this->assertEquals( 'hello world.', $wikitext,
293 'Wikitext passed to hook was not as expected'
295 $this->assertNull( $redirectTarget, 'Redirect seen in hook was not null' );
296 $this->assertEquals( $title, $options->getRedirectTarget(),
297 'ParserOptions\' redirectTarget was changed'
300 // Test with a redirect page
301 $wikitext = false;
302 $redirectTarget = false;
303 $content = $this->newContent(
304 "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect."
306 $options = ParserOptions::newFromAnon();
307 $contentRenderer->getParserOutput( $content, $title, null, $options );
308 $this->assertEquals(
309 'hello redirect.',
310 $wikitext,
311 'Wikitext passed to hook was not as expected'
313 $this->assertNotEquals(
314 null,
315 $redirectTarget,
316 'Redirect seen in hook was null' );
317 $this->assertEquals(
318 'TestRedirectParserOption/redir',
319 $redirectTarget->getFullText(),
320 'Redirect seen in hook was not the expected title'
322 $this->assertNull(
323 $options->getRedirectTarget(),
324 'ParserOptions\' redirectTarget was changed'
328 public static function dataEquals() {
329 return [
330 [ new WikitextContent( "hallo" ), null, false ],
331 [ new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ],
332 [ new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
333 [ new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ],
334 [ new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ],
338 public static function dataGetDeletionUpdates() {
339 return [
341 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
342 [ LinksDeletionUpdate::class => [] ]
345 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
346 [ LinksDeletionUpdate::class => [] ]
348 // @todo more...?