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->getText();
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->getText() );
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->getText() );
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() );
364 * @covers ParserOptions::getRedirectTarget
365 * @covers ParserOptions::setRedirectTarget
367 public function testRedirectParserOption() {
368 $title = Title
::newFromText( 'testRedirectParserOption' );
370 // Set up hook and its reporting variables
372 $redirectTarget = null;
373 $this->mergeMwGlobalArrayValue( 'wgHooks', [
374 'InternalParseBeforeLinks' => [
375 function ( &$parser, &$text, &$stripState ) use ( &$wikitext, &$redirectTarget ) {
377 $redirectTarget = $parser->getOptions()->getRedirectTarget();
382 // Test with non-redirect page
384 $redirectTarget = false;
385 $content = $this->newContent( 'hello world.' );
386 $options = ParserOptions
::newCanonical( 'canonical' );
387 $options->setRedirectTarget( $title );
388 $content->getParserOutput( $title, null, $options );
389 $this->assertEquals( 'hello world.', $wikitext,
390 'Wikitext passed to hook was not as expected'
392 $this->assertEquals( null, $redirectTarget, 'Redirect seen in hook was not null' );
393 $this->assertEquals( $title, $options->getRedirectTarget(),
394 'ParserOptions\' redirectTarget was changed'
397 // Test with a redirect page
399 $redirectTarget = false;
400 $content = $this->newContent(
401 "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect."
403 $options = ParserOptions
::newCanonical( 'canonical' );
404 $content->getParserOutput( $title, null, $options );
408 'Wikitext passed to hook was not as expected'
410 $this->assertNotEquals(
413 'Redirect seen in hook was null' );
415 'TestRedirectParserOption/redir',
416 $redirectTarget->getFullText(),
417 'Redirect seen in hook was not the expected title'
421 $options->getRedirectTarget(),
422 'ParserOptions\' redirectTarget was changed'
426 public static function dataEquals() {
428 [ new WikitextContent( "hallo" ), null, false ],
429 [ new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ],
430 [ new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
431 [ new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ],
432 [ new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ],
436 public static function dataGetDeletionUpdates() {
439 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
440 [ LinksDeletionUpdate
::class => [] ]
443 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
444 [ LinksDeletionUpdate
::class => [] ]
451 * @covers WikitextContent::preSaveTransform
452 * @covers WikitextContent::fillParserOutput
454 public function testHadSignature() {
455 $titleObj = Title
::newFromText( __CLASS__
);
457 $content = new WikitextContent( '~~~~' );
458 $pstContent = $content->preSaveTransform(
459 $titleObj, $this->getTestUser()->getUser(), new ParserOptions()
462 $this->assertTrue( $pstContent->getParserOutput( $titleObj )->getFlag( 'user-signature' ) );