3 use MediaWiki\MediaWikiServices
;
6 * @group ContentHandler
9 * ^--- needed, because we do need the database to test link updates
11 class WikitextContentTest
extends TextContentTest
{
12 public static $sections = "Intro
24 public function newContent( $text ) {
25 return new WikitextContent( $text );
28 public static function dataGetParserOutput() {
31 "WikitextContentTest_testGetParserOutput",
32 CONTENT_MODEL_WIKITEXT
,
34 "<div class=\"mw-parser-output\"><p>hello <i>world</i>\n</p>\n\n\n</div>"
40 public static function dataGetSecondaryDataUpdates() {
42 [ "WikitextContentTest_testGetSecondaryDataUpdates_1",
43 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
45 LinksUpdate
::class => [
51 [ "WikitextContentTest_testGetSecondaryDataUpdates_2",
52 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
54 LinksUpdate
::class => [
57 [ 'World_test_21344' => 0 ]
67 * @dataProvider dataGetSecondaryDataUpdates
69 * @covers WikitextContent::getSecondaryDataUpdates
71 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
72 $ns = $this->getDefaultWikitextNS();
73 $title = Title
::newFromText( $title, $ns );
75 $content = ContentHandler
::makeContent( $text, $title, $model );
77 $page = WikiPage
::factory( $title );
78 $page->doEditContent( $content, '' );
80 $updates = $content->getSecondaryDataUpdates( $title );
82 // make updates accessible by class name
83 foreach ( $updates as $update ) {
84 $class = get_class( $update );
85 $updates[$class] = $update;
88 foreach ( $expectedStuff as $class => $fieldValues ) {
89 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
91 $update = $updates[$class];
93 foreach ( $fieldValues as $field => $value ) {
94 $v = $update->$field; # if the field doesn't exist, just crash and burn
98 "unexpected value for field $field in instance of $class"
103 $page->doDeleteArticle( '' );
106 public static function dataGetSection() {
125 * @dataProvider dataGetSection
126 * @covers WikitextContent::getSection
128 public function testGetSection( $text, $sectionId, $expectedText ) {
129 $content = $this->newContent( $text );
131 $sectionContent = $content->getSection( $sectionId );
132 if ( is_object( $sectionContent ) ) {
133 $sectionText = $sectionContent->getNativeData();
135 $sectionText = $sectionContent;
138 $this->assertEquals( $expectedText, $sectionText );
141 public static function dataReplaceSection() {
147 trim( preg_replace( '/^Intro/sm', 'No more', self
::$sections ) )
157 "== TEST ==\nmore fun",
160 '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==",
174 trim( self
::$sections ) . "\n\n\n== New ==\n\nNo more"
180 * @dataProvider dataReplaceSection
181 * @covers WikitextContent::replaceSection
183 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
184 $content = $this->newContent( $text );
185 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
187 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
191 * @covers WikitextContent::addSectionHeader
193 public function testAddSectionHeader() {
194 $content = $this->newContent( 'hello world' );
195 $content = $content->addSectionHeader( 'test' );
197 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
200 public static function dataPreSaveTransform() {
202 [ 'hello this is ~~~',
203 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
205 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
206 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
215 public static function dataPreloadTransform() {
222 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
223 'hello \'\'this\'\' is bar',
228 public static function dataGetRedirectTarget() {
230 [ '#REDIRECT [[Test]]',
236 [ '* #REDIRECT [[Test]]',
242 public static function dataGetTextForSummary() {
252 [ '[[hello world]].',
259 public static function dataIsCountable() {
291 [ '#REDIRECT [[bar]]',
296 [ '#REDIRECT [[bar]]',
305 * @covers WikitextContent::matchMagicWord
307 public function testMatchMagicWord() {
308 $mw = MediaWikiServices
::getInstance()->getMagicWordFactory()->get( "staticredirect" );
310 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
311 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
313 $content = $this->newContent( "#REDIRECT [[FOO]]" );
315 $content->matchMagicWord( $mw ),
316 "should not have matched magic word"
321 * @covers WikitextContent::updateRedirect
323 public function testUpdateRedirect() {
324 $target = Title
::newFromText( "testUpdateRedirect_target" );
326 // test with non-redirect page
327 $content = $this->newContent( "hello world." );
328 $newContent = $content->updateRedirect( $target );
330 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
332 // test with actual redirect
333 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
334 $newContent = $content->updateRedirect( $target );
336 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
337 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
340 $target->getFullText(),
341 $newContent->getRedirectTarget()->getFullText()
346 * @covers WikitextContent::getModel
348 public function testGetModel() {
349 $content = $this->newContent( "hello world." );
351 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
355 * @covers WikitextContent::getContentHandler
357 public function testGetContentHandler() {
358 $content = $this->newContent( "hello world." );
360 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
363 public function testRedirectParserOption() {
364 $title = Title
::newFromText( 'testRedirectParserOption' );
366 // Set up hook and its reporting variables
368 $redirectTarget = null;
369 $this->mergeMwGlobalArrayValue( 'wgHooks', [
370 'InternalParseBeforeLinks' => [
371 function ( &$parser, &$text, &$stripState ) use ( &$wikitext, &$redirectTarget ) {
373 $redirectTarget = $parser->getOptions()->getRedirectTarget();
378 // Test with non-redirect page
380 $redirectTarget = false;
381 $content = $this->newContent( 'hello world.' );
382 $options = ParserOptions
::newCanonical( 'canonical' );
383 $options->setRedirectTarget( $title );
384 $content->getParserOutput( $title, null, $options );
385 $this->assertEquals( 'hello world.', $wikitext,
386 'Wikitext passed to hook was not as expected'
388 $this->assertEquals( null, $redirectTarget, 'Redirect seen in hook was not null' );
389 $this->assertEquals( $title, $options->getRedirectTarget(),
390 'ParserOptions\' redirectTarget was changed'
393 // Test with a redirect page
395 $redirectTarget = false;
396 $content = $this->newContent(
397 "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect."
399 $options = ParserOptions
::newCanonical( 'canonical' );
400 $content->getParserOutput( $title, null, $options );
404 'Wikitext passed to hook was not as expected'
406 $this->assertNotEquals(
409 'Redirect seen in hook was null' );
411 'TestRedirectParserOption/redir',
412 $redirectTarget->getFullText(),
413 'Redirect seen in hook was not the expected title'
417 $options->getRedirectTarget(),
418 'ParserOptions\' redirectTarget was changed'
422 public static function dataEquals() {
424 [ new WikitextContent( "hallo" ), null, false ],
425 [ new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ],
426 [ new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
427 [ new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ],
428 [ new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ],
432 public static function dataGetDeletionUpdates() {
434 [ "WikitextContentTest_testGetSecondaryDataUpdates_1",
435 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
436 [ LinksDeletionUpdate
::class => [] ]
438 [ "WikitextContentTest_testGetSecondaryDataUpdates_2",
439 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
440 [ LinksDeletionUpdate
::class => [] ]