setUserLang( 'en' ); $this->overrideConfigValues( [ MainConfigNames::ShowExceptionDetails => true, MainConfigNames::CleanSignatures => true, MainConfigNames::LanguageCode => 'en', ] ); $services = $this->getServiceContainer(); $contLang = $services->getContentLanguage(); // FIXME: This test should pass without setting global content language $this->options = ParserOptions::newFromUserAndLang( new User, $contLang ); $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] ); $this->parser = $services->getParserFactory()->create(); } /** * @see T10689 * @covers \MediaWiki\Parser\Parser::parse */ public function testLongNumericLinesDontKillTheParser() { $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n"; $title = Title::makeTitle( NS_MAIN, 'Unit test' ); $options = ParserOptions::newFromUser( new User() ); $this->assertEquals( "
$longLine
", $this->parser->parse( $longLine, $title, $options )->getRawText() ); } /** * @covers \MediaWiki\Parser\Parser::braceSubstitution * @covers \MediaWiki\SpecialPage\SpecialPageFactory::capturePath */ public function testSpecialPageTransclusionRestoresGlobalState() { $text = "{{Special:ApiHelp/help}}"; $title = Title::makeTitle( NS_MAIN, 'TestSpecialPageTransclusionRestoresGlobalState' ); $options = ParserOptions::newFromUser( new User() ); RequestContext::getMain()->setTitle( $title ); $parsed = $this->parser->parse( $text, $title, $options )->getRawText(); $this->assertStringContainsString( 'apihelp-header', $parsed ); } /** * Test the parser entry points * @covers \MediaWiki\Parser\Parser::parse */ public function testParse() { $title = Title::newFromText( __FUNCTION__ ); $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options ); $this->assertEquals( "Test\nContent of Template:Foo\nContent of Template:Bar\n
", $parserOutput->getRawText() ); } /** * @covers \MediaWiki\Parser\Parser::preSaveTransform */ public function testPreSaveTransform() { $title = Title::newFromText( __FUNCTION__ ); $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, new User(), $this->options ); $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText ); $outputText = $this->parser->preSaveTransform( "hello\n\n{{subst:ns:0}}", $title, new User(), $this->options ); $this->assertEquals( "hello", $outputText, "Pre-save transform removes trailing whitespace after substituting templates" ); } /** * @covers \MediaWiki\Parser\Parser::preprocess */ public function testPreprocess() { $title = Title::newFromText( __FUNCTION__ ); $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options ); $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText ); } /** * cleanSig() makes all templates substs and removes tildes * @covers \MediaWiki\Parser\Parser::cleanSig */ public function testCleanSig() { $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); $this->assertEquals( "{{SUBST:Foo}} ", $outputText ); } /** * cleanSig() should do nothing if disabled * @covers \MediaWiki\Parser\Parser::cleanSig */ public function testCleanSigDisabled() { $this->overrideConfigValue( MainConfigNames::CleanSignatures, false ); $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); $this->assertEquals( "{{Foo}} ~~~~", $outputText ); } /** * cleanSigInSig() just removes tildes * @dataProvider provideStringsForCleanSigInSig * @covers \MediaWiki\Parser\Parser::cleanSigInSig */ public function testCleanSigInSig( $in, $out ) { $this->assertEquals( Parser::cleanSigInSig( $in ), $out ); } public static function provideStringsForCleanSigInSig() { return [ [ "{{Foo}} ~~~~", "{{Foo}} " ], [ "~~~", "" ], [ "~~~~~", "" ], ]; } /** * @covers \MediaWiki\Parser\Parser::getSection */ public function testGetSection() { $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n" . "Section 2\n== Heading 3 ==\nSection 3\n", 2 ); $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n" . "Section 2\n== Heading 3 ==\nSection 3\n", 1 ); $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 ); $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 ); } /** * @covers \MediaWiki\Parser\Parser::replaceSection */ public function testReplaceSection() { $outputText = $this->parser->replaceSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n" . "Section 2\n== Heading 3 ==\nSection 3\n", 1, "New section 1" ); $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText ); } /** * Templates and comments are not affected, but noinclude/onlyinclude is. * @covers \MediaWiki\Parser\Parser::getPreloadText */ public function testGetPreloadText() { $title = Title::newFromText( __FUNCTION__ ); $outputText = $this->parser->getPreloadText( "{{Foo}}