2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
2010-12-26 19:30:10 +00:00
|
|
|
|
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( [
|
2012-10-08 10:56:20 +00:00
|
|
|
'wgShowDBErrorBacktrace' => true,
|
|
|
|
|
'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
|
|
|
|
|
|
|
|
MagicWord::clearCache();
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
2014-03-07 20:22:13 +00:00
|
|
|
* @see Bug 8689
|
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>",
|
2014-03-07 20:22:13 +00:00
|
|
|
$this->parser->parse( $longLine, $title, $options )->getText() );
|
2010-12-26 19:30:10 +00:00
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
|
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>",
|
|
|
|
|
$parserOutput->getText()
|
|
|
|
|
);
|
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
|
|
|
|
|
*/
|
2013-02-14 11:22:13 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
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 );
|
|
|
|
|
}
|
2013-02-14 11:22:13 +00:00
|
|
|
}
|