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
;
12 * @group ContentHandler
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
31 public function newContent( $text ) {
32 return new WikitextContent( $text );
35 public static function dataGetSection() {
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();
63 $sectionText = $sectionContent;
66 $this->assertEquals( $expectedText, $sectionText );
69 public static function dataReplaceSection() {
75 trim( preg_replace( '/^Intro/m', 'No more', self
::SECTIONS
) )
85 "== TEST ==\nmore fun",
88 '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==",
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() {
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>',
143 public static function dataGetRedirectTarget() {
145 [ '#REDIRECT [[Test]]',
151 [ '* #REDIRECT [[Test]]',
157 public static function dataGetTextForSummary() {
167 [ '[[hello world]].',
174 public static function dataIsCountable() {
206 [ '#REDIRECT [[bar]]',
211 [ '#REDIRECT [[bar]]',
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]]" );
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" );
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
275 $redirectTarget = null;
276 $this->mergeMwGlobalArrayValue( 'wgHooks', [
277 'InternalParseBeforeLinks' => [
278 static function ( Parser
$parser, $text, $stripState ) use ( &$wikitext, &$redirectTarget ) {
280 $redirectTarget = $parser->getOptions()->getRedirectTarget();
285 // Test with non-redirect page
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
302 $redirectTarget = false;
303 $content = $this->newContent(
304 "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect."
306 $options = ParserOptions
::newFromAnon();
307 $contentRenderer->getParserOutput( $content, $title, null, $options );
311 'Wikitext passed to hook was not as expected'
313 $this->assertNotEquals(
316 'Redirect seen in hook was null' );
318 'TestRedirectParserOption/redir',
319 $redirectTarget->getFullText(),
320 'Redirect seen in hook was not the expected title'
323 $options->getRedirectTarget(),
324 'ParserOptions\' redirectTarget was changed'
328 public static function dataEquals() {
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() {
341 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
342 [ LinksDeletionUpdate
::class => [] ]
345 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
346 [ LinksDeletionUpdate
::class => [] ]