2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
2010-12-26 19:30:10 +00:00
|
|
|
|
2018-07-24 16:44:09 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* Parser-related tests that don't suit for parserTests.txt
|
2015-04-01 07:35:32 +00:00
|
|
|
*
|
|
|
|
|
* @group Database
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class ExtraParserTest extends MediaWikiTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/** @var ParserOptions */
|
|
|
|
|
protected $options;
|
|
|
|
|
/** @var Parser */
|
|
|
|
|
protected $parser;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2012-10-14 19:34:26 +00:00
|
|
|
$contLang = Language::factory( 'en' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2018-07-17 16:51:36 +00:00
|
|
|
'wgShowExceptionDetails' => true,
|
2012-10-08 10:56:20 +00:00
|
|
|
'wgCleanSignatures' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2016-03-09 16:47:58 +00:00
|
|
|
$this->setUserLang( 'en' );
|
|
|
|
|
$this->setContentLang( $contLang );
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2016-03-09 16:47:58 +00:00
|
|
|
// FIXME: This test should pass without setting global content language
|
2012-10-14 19:34:26 +00:00
|
|
|
$this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->parser = new Parser;
|
2012-08-29 08:42:47 +00:00
|
|
|
|
2018-07-24 16:44:09 +00:00
|
|
|
MediaWikiServices::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
2017-02-20 23:45:58 +00:00
|
|
|
* @see T10689
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Parser::parse
|
|
|
|
|
*/
|
2014-03-07 20:22:13 +00:00
|
|
|
public function testLongNumericLinesDontKillTheParser() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2014-03-07 20:22:13 +00:00
|
|
|
$title = Title::newFromText( 'Unit test' );
|
|
|
|
|
$options = ParserOptions::newFromUser( new User() );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( "<p>$longLine</p>",
|
2017-12-22 18:32:49 +00:00
|
|
|
$this->parser->parse( $longLine, $title, $options )->getText( [ 'unwrap' => true ] ) );
|
2010-12-26 19:30:10 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2018-10-17 23:23:27 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Parser::braceSubstitution
|
|
|
|
|
* @covers SpecialPageFactory::capturePath
|
|
|
|
|
*/
|
|
|
|
|
public function testSpecialPageTransclusionRestoresGlobalState() {
|
|
|
|
|
$text = "{{Special:ApiHelp/help}}";
|
|
|
|
|
$title = Title::newFromText( 'testSpecialPageTransclusionRestoresGlobalState' );
|
|
|
|
|
$options = ParserOptions::newFromUser( new User() );
|
|
|
|
|
|
|
|
|
|
RequestContext::getMain()->setTitle( $title );
|
|
|
|
|
RequestContext::getMain()->getWikiPage()->CustomTestProp = true;
|
|
|
|
|
|
|
|
|
|
$parsed = $this->parser->parse( $text, $title, $options )->getText();
|
|
|
|
|
$this->assertContains( 'apihelp-header', $parsed );
|
|
|
|
|
|
|
|
|
|
// Verify that this property wasn't wiped out by the parse
|
|
|
|
|
$this->assertTrue( RequestContext::getMain()->getWikiPage()->CustomTestProp );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* Test the parser entry points
|
|
|
|
|
* @covers Parser::parse
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testParse() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2013-01-28 10:27:15 +00:00
|
|
|
$parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
|
2014-03-07 20:22:13 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
|
2017-12-22 18:32:49 +00:00
|
|
|
$parserOutput->getText( [ 'unwrap' => true ] )
|
2014-03-07 20:22:13 +00:00
|
|
|
);
|
2010-12-26 19:30:10 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Parser::preSaveTransform
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testPreSaveTransform() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2014-03-07 20:22:13 +00:00
|
|
|
$outputText = $this->parser->preSaveTransform(
|
|
|
|
|
"Test\r\n{{subst:Foo}}\n{{Bar}}",
|
|
|
|
|
$title,
|
|
|
|
|
new User(),
|
|
|
|
|
$this->options
|
|
|
|
|
);
|
2011-01-23 18:43:08 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
|
|
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Parser::preprocess
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testPreprocess() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2013-01-28 10:27:15 +00:00
|
|
|
$outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2014-03-07 20:22:13 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
|
|
|
|
|
$outputText
|
|
|
|
|
);
|
2010-12-26 19:30:10 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
/**
|
|
|
|
|
* cleanSig() makes all templates substs and removes tildes
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Parser::cleanSig
|
2010-12-26 19:30:10 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCleanSig() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2010-12-26 19:30:10 +00:00
|
|
|
$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->assertEquals( "{{SUBST:Foo}} ", $outputText );
|
|
|
|
|
}
|
2011-08-17 22:09:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* cleanSig() should do nothing if disabled
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Parser::cleanSig
|
2011-08-17 22:09:57 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCleanSigDisabled() {
|
2013-03-21 19:35:44 +00:00
|
|
|
$this->setMwGlobals( 'wgCleanSignatures', false );
|
2011-08-17 22:09:57 +00:00
|
|
|
|
|
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
|
|
|
|
$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2011-08-17 22:09:57 +00:00
|
|
|
$this->assertEquals( "{{Foo}} ~~~~", $outputText );
|
|
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
/**
|
|
|
|
|
* cleanSigInSig() just removes tildes
|
2011-12-06 23:07:13 +00:00
|
|
|
* @dataProvider provideStringsForCleanSigInSig
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Parser::cleanSigInSig
|
2010-12-26 19:30:10 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCleanSigInSig( $in, $out ) {
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertEquals( Parser::cleanSigInSig( $in ), $out );
|
2011-12-06 23:07:13 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideStringsForCleanSigInSig() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ "{{Foo}} ~~~~", "{{Foo}} " ],
|
|
|
|
|
[ "~~~", "" ],
|
|
|
|
|
[ "~~~~~", "" ],
|
|
|
|
|
];
|
2010-12-26 19:30:10 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Parser::getSection
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testGetSection() {
|
2014-04-24 09:57:41 +00:00
|
|
|
$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
|
|
|
|
|
);
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
|
|
|
|
|
$this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
|
|
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Parser::replaceSection
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testReplaceSection() {
|
2014-04-24 09:57:41 +00:00
|
|
|
$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"
|
|
|
|
|
);
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
|
|
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
/**
|
|
|
|
|
* Templates and comments are not affected, but noinclude/onlyinclude is.
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Parser::getPreloadText
|
2010-12-26 19:30:10 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testGetPreloadText() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2014-04-24 09:57:41 +00:00
|
|
|
$outputText = $this->parser->getPreloadText(
|
|
|
|
|
"{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
|
|
|
|
|
$title,
|
|
|
|
|
$this->options
|
|
|
|
|
);
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
|
|
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2014-03-07 20:22:13 +00:00
|
|
|
/**
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param bool $parser
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
public static function statelessFetchTemplate( $title, $parser = false ) {
|
2010-12-26 19:30:10 +00:00
|
|
|
$text = "Content of ''" . $title->getFullText() . "''";
|
2016-02-17 09:09:32 +00:00
|
|
|
$deps = [];
|
2013-02-14 11:22:13 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2010-12-26 19:30:10 +00:00
|
|
|
'text' => $text,
|
|
|
|
|
'finalTitle' => $title,
|
2016-02-17 09:09:32 +00:00
|
|
|
'deps' => $deps ];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2011-12-19 21:00:03 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Parser::parse
|
2011-12-19 21:00:03 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTrackingCategory() {
|
2011-08-21 19:28:35 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2013-02-14 11:22:13 +00:00
|
|
|
$catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
|
2011-08-21 19:28:35 +00:00
|
|
|
$cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
|
2016-02-17 09:09:32 +00:00
|
|
|
$expected = [ $cat->getDBkey() ];
|
2013-01-28 10:27:15 +00:00
|
|
|
$parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
|
2011-08-21 19:28:35 +00:00
|
|
|
$result = $parserOutput->getCategoryLinks();
|
|
|
|
|
$this->assertEquals( $expected, $result );
|
|
|
|
|
}
|
2011-12-19 21:00:03 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Parser::parse
|
2011-12-19 21:00:03 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTrackingCategorySpecial() {
|
2011-08-21 19:28:35 +00:00
|
|
|
// Special pages shouldn't have tracking cats.
|
|
|
|
|
$title = SpecialPage::getTitleFor( 'Contributions' );
|
2013-01-28 10:27:15 +00:00
|
|
|
$parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
|
2011-08-21 19:28:35 +00:00
|
|
|
$result = $parserOutput->getCategoryLinks();
|
|
|
|
|
$this->assertEmpty( $result );
|
|
|
|
|
}
|
2018-05-19 13:29:52 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Parser::parseLinkParameter
|
|
|
|
|
* @dataProvider provideParseLinkParameter
|
|
|
|
|
*/
|
|
|
|
|
public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
|
|
|
|
|
$this->parser->startExternalParse( Title::newFromText( __FUNCTION__ ),
|
|
|
|
|
$this->options, Parser::OT_HTML );
|
|
|
|
|
$output = $this->parser->parseLinkParameter( $input );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected[0], $output[0], "$desc (type)" );
|
|
|
|
|
|
|
|
|
|
if ( $expected[0] === 'link-title' ) {
|
|
|
|
|
$this->assertTrue( $expected[1]->equals( $output[1] ), "$desc (target)" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( $expected[1], $output[1], "$desc (target)" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $expectedLinks as $func => $expected ) {
|
|
|
|
|
$output = $this->parser->getOutput()->$func();
|
|
|
|
|
$this->assertEquals( $expected, $output, "$desc ($func)" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideParseLinkParameter() {
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'',
|
|
|
|
|
[ 'no-link', false ],
|
|
|
|
|
[],
|
|
|
|
|
'Return no link when requested',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'https://example.com/',
|
|
|
|
|
[ 'link-url', 'https://example.com/' ],
|
|
|
|
|
[ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
|
|
|
|
|
'External link',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'//example.com/',
|
|
|
|
|
[ 'link-url', '//example.com/' ],
|
|
|
|
|
[ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
|
|
|
|
|
'External link',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'Test',
|
|
|
|
|
[ 'link-title', Title::newFromText( 'Test' ) ],
|
|
|
|
|
[ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
|
|
|
|
|
'Internal link',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'mw:Test',
|
|
|
|
|
[ 'link-title', Title::newFromText( 'mw:Test' ) ],
|
|
|
|
|
[ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
|
|
|
|
|
'Internal link (interwiki)',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'https://',
|
|
|
|
|
[ null, false ],
|
|
|
|
|
[],
|
|
|
|
|
'Invalid link target',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'<>',
|
|
|
|
|
[ null, false ],
|
|
|
|
|
[],
|
|
|
|
|
'Invalid link target',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
' ',
|
|
|
|
|
[ null, false ],
|
|
|
|
|
[],
|
|
|
|
|
'Invalid link target',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
}
|