123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?php
- use MediaWiki\MediaWikiServices;
- /**
- * @group ContentHandler
- *
- * @group Database
- * ^--- needed, because we do need the database to test link updates
- */
- class WikitextContentTest extends TextContentTest {
- public static $sections = "Intro
- == stuff ==
- hello world
- == test ==
- just a test
- == foo ==
- more stuff
- ";
- public function newContent( $text ) {
- return new WikitextContent( $text );
- }
- public static function dataGetParserOutput() {
- return [
- [
- "WikitextContentTest_testGetParserOutput",
- CONTENT_MODEL_WIKITEXT,
- "hello ''world''\n",
- "<div class=\"mw-parser-output\"><p>hello <i>world</i>\n</p>\n\n\n</div>"
- ],
- // TODO: more...?
- ];
- }
- public static function dataGetSecondaryDataUpdates() {
- return [
- [ "WikitextContentTest_testGetSecondaryDataUpdates_1",
- CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
- [
- LinksUpdate::class => [
- 'mRecursive' => true,
- 'mLinks' => []
- ]
- ]
- ],
- [ "WikitextContentTest_testGetSecondaryDataUpdates_2",
- CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
- [
- LinksUpdate::class => [
- 'mRecursive' => true,
- 'mLinks' => [
- [ 'World_test_21344' => 0 ]
- ]
- ]
- ]
- ],
- // TODO: more...?
- ];
- }
- /**
- * @dataProvider dataGetSecondaryDataUpdates
- * @group Database
- * @covers WikitextContent::getSecondaryDataUpdates
- */
- public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
- $ns = $this->getDefaultWikitextNS();
- $title = Title::newFromText( $title, $ns );
- $content = ContentHandler::makeContent( $text, $title, $model );
- $page = WikiPage::factory( $title );
- $page->doEditContent( $content, '' );
- $updates = $content->getSecondaryDataUpdates( $title );
- // make updates accessible by class name
- foreach ( $updates as $update ) {
- $class = get_class( $update );
- $updates[$class] = $update;
- }
- foreach ( $expectedStuff as $class => $fieldValues ) {
- $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
- $update = $updates[$class];
- foreach ( $fieldValues as $field => $value ) {
- $v = $update->$field; # if the field doesn't exist, just crash and burn
- $this->assertEquals(
- $value,
- $v,
- "unexpected value for field $field in instance of $class"
- );
- }
- }
- $page->doDeleteArticle( '' );
- }
- public static function dataGetSection() {
- return [
- [ self::$sections,
- "0",
- "Intro"
- ],
- [ self::$sections,
- "2",
- "== test ==
- just a test"
- ],
- [ self::$sections,
- "8",
- false
- ],
- ];
- }
- /**
- * @dataProvider dataGetSection
- * @covers WikitextContent::getSection
- */
- public function testGetSection( $text, $sectionId, $expectedText ) {
- $content = $this->newContent( $text );
- $sectionContent = $content->getSection( $sectionId );
- if ( is_object( $sectionContent ) ) {
- $sectionText = $sectionContent->getNativeData();
- } else {
- $sectionText = $sectionContent;
- }
- $this->assertEquals( $expectedText, $sectionText );
- }
- public static function dataReplaceSection() {
- return [
- [ self::$sections,
- "0",
- "No more",
- null,
- trim( preg_replace( '/^Intro/sm', 'No more', self::$sections ) )
- ],
- [ self::$sections,
- "",
- "No more",
- null,
- "No more"
- ],
- [ self::$sections,
- "2",
- "== TEST ==\nmore fun",
- null,
- trim( preg_replace(
- '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==",
- self::$sections
- ) )
- ],
- [ self::$sections,
- "8",
- "No more",
- null,
- self::$sections
- ],
- [ self::$sections,
- "new",
- "No more",
- "New",
- trim( self::$sections ) . "\n\n\n== New ==\n\nNo more"
- ],
- ];
- }
- /**
- * @dataProvider dataReplaceSection
- * @covers WikitextContent::replaceSection
- */
- public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
- $content = $this->newContent( $text );
- $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
- $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
- }
- /**
- * @covers WikitextContent::addSectionHeader
- */
- public function testAddSectionHeader() {
- $content = $this->newContent( 'hello world' );
- $content = $content->addSectionHeader( 'test' );
- $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
- }
- public static function dataPreSaveTransform() {
- return [
- [ 'hello this is ~~~',
- "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
- ],
- [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
- 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
- ],
- [ // rtrim
- " Foo \n ",
- " Foo",
- ],
- ];
- }
- public static function dataPreloadTransform() {
- return [
- [
- 'hello this is ~~~',
- "hello this is ~~~",
- ],
- [
- 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
- 'hello \'\'this\'\' is bar',
- ],
- ];
- }
- public static function dataGetRedirectTarget() {
- return [
- [ '#REDIRECT [[Test]]',
- 'Test',
- ],
- [ '#REDIRECT Test',
- null,
- ],
- [ '* #REDIRECT [[Test]]',
- null,
- ],
- ];
- }
- public static function dataGetTextForSummary() {
- return [
- [ "hello\nworld.",
- 16,
- 'hello world.',
- ],
- [ 'hello world.',
- 8,
- 'hello...',
- ],
- [ '[[hello world]].',
- 8,
- 'hel...',
- ],
- ];
- }
- public static function dataIsCountable() {
- return [
- [ '',
- null,
- 'any',
- true
- ],
- [ 'Foo',
- null,
- 'any',
- true
- ],
- [ 'Foo',
- null,
- 'link',
- false
- ],
- [ 'Foo [[bar]]',
- null,
- 'link',
- true
- ],
- [ 'Foo',
- true,
- 'link',
- true
- ],
- [ 'Foo [[bar]]',
- false,
- 'link',
- false
- ],
- [ '#REDIRECT [[bar]]',
- true,
- 'any',
- false
- ],
- [ '#REDIRECT [[bar]]',
- true,
- 'link',
- false
- ],
- ];
- }
- /**
- * @covers WikitextContent::matchMagicWord
- */
- public function testMatchMagicWord() {
- $mw = MediaWikiServices::getInstance()->getMagicWordFactory()->get( "staticredirect" );
- $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
- $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
- $content = $this->newContent( "#REDIRECT [[FOO]]" );
- $this->assertFalse(
- $content->matchMagicWord( $mw ),
- "should not have matched magic word"
- );
- }
- /**
- * @covers WikitextContent::updateRedirect
- */
- public function testUpdateRedirect() {
- $target = Title::newFromText( "testUpdateRedirect_target" );
- // test with non-redirect page
- $content = $this->newContent( "hello world." );
- $newContent = $content->updateRedirect( $target );
- $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
- // test with actual redirect
- $content = $this->newContent( "#REDIRECT [[Someplace]]" );
- $newContent = $content->updateRedirect( $target );
- $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
- $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
- $this->assertEquals(
- $target->getFullText(),
- $newContent->getRedirectTarget()->getFullText()
- );
- }
- /**
- * @covers WikitextContent::getModel
- */
- public function testGetModel() {
- $content = $this->newContent( "hello world." );
- $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
- }
- /**
- * @covers WikitextContent::getContentHandler
- */
- public function testGetContentHandler() {
- $content = $this->newContent( "hello world." );
- $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
- }
- public function testRedirectParserOption() {
- $title = Title::newFromText( 'testRedirectParserOption' );
- // Set up hook and its reporting variables
- $wikitext = null;
- $redirectTarget = null;
- $this->mergeMwGlobalArrayValue( 'wgHooks', [
- 'InternalParseBeforeLinks' => [
- function ( &$parser, &$text, &$stripState ) use ( &$wikitext, &$redirectTarget ) {
- $wikitext = $text;
- $redirectTarget = $parser->getOptions()->getRedirectTarget();
- }
- ]
- ] );
- // Test with non-redirect page
- $wikitext = false;
- $redirectTarget = false;
- $content = $this->newContent( 'hello world.' );
- $options = ParserOptions::newCanonical( 'canonical' );
- $options->setRedirectTarget( $title );
- $content->getParserOutput( $title, null, $options );
- $this->assertEquals( 'hello world.', $wikitext,
- 'Wikitext passed to hook was not as expected'
- );
- $this->assertEquals( null, $redirectTarget, 'Redirect seen in hook was not null' );
- $this->assertEquals( $title, $options->getRedirectTarget(),
- 'ParserOptions\' redirectTarget was changed'
- );
- // Test with a redirect page
- $wikitext = false;
- $redirectTarget = false;
- $content = $this->newContent(
- "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect."
- );
- $options = ParserOptions::newCanonical( 'canonical' );
- $content->getParserOutput( $title, null, $options );
- $this->assertEquals(
- 'hello redirect.',
- $wikitext,
- 'Wikitext passed to hook was not as expected'
- );
- $this->assertNotEquals(
- null,
- $redirectTarget,
- 'Redirect seen in hook was null' );
- $this->assertEquals(
- 'TestRedirectParserOption/redir',
- $redirectTarget->getFullText(),
- 'Redirect seen in hook was not the expected title'
- );
- $this->assertEquals(
- null,
- $options->getRedirectTarget(),
- 'ParserOptions\' redirectTarget was changed'
- );
- }
- public static function dataEquals() {
- return [
- [ new WikitextContent( "hallo" ), null, false ],
- [ new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ],
- [ new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
- [ new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ],
- [ new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ],
- ];
- }
- public static function dataGetDeletionUpdates() {
- return [
- [
- CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
- [ LinksDeletionUpdate::class => [] ]
- ],
- [
- CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
- [ LinksDeletionUpdate::class => [] ]
- ],
- // @todo more...?
- ];
- }
- /**
- * @covers WikitextContent::preSaveTransform
- * @covers WikitextContent::fillParserOutput
- */
- public function testHadSignature() {
- $titleObj = Title::newFromText( __CLASS__ );
- $content = new WikitextContent( '~~~~' );
- $pstContent = $content->preSaveTransform(
- $titleObj, $this->getTestUser()->getUser(), new ParserOptions()
- );
- $this->assertTrue( $pstContent->getParserOutput( $titleObj )->getFlag( 'user-signature' ) );
- }
- }
|