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
|
|
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class ExtraParserTest extends MediaWikiTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
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' );
|
2012-10-08 10:56:20 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgShowDBErrorBacktrace' => true,
|
|
|
|
|
'wgLanguageCode' => 'en',
|
2012-10-14 19:34:26 +00:00
|
|
|
'wgContLang' => $contLang,
|
2012-10-08 10:56:20 +00:00
|
|
|
'wgLang' => Language::factory( 'en' ),
|
|
|
|
|
'wgMemc' => new EmptyBagOStuff,
|
|
|
|
|
'wgAlwaysUseTidy' => false,
|
|
|
|
|
'wgCleanSignatures' => true,
|
|
|
|
|
) );
|
2010-12-26 19:30:10 +00:00
|
|
|
|
2012-10-14 19:34:26 +00:00
|
|
|
$this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
|
|
|
|
|
$this->parser = new Parser;
|
2012-08-29 08:42:47 +00:00
|
|
|
|
|
|
|
|
MagicWord::clearCache();
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Bug 8689 - Long numeric lines kill the parser
|
|
|
|
|
function testBug8689() {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
$longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
|
2010-12-26 19:30:10 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$t = Title::newFromText( 'Unit test' );
|
|
|
|
|
$options = ParserOptions::newFromUser( $wgUser );
|
|
|
|
|
$this->assertEquals( "<p>$longLine</p>",
|
2010-12-26 19:30:10 +00:00
|
|
|
$this->parser->parse( $longLine, $t, $options )->getText() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Test the parser entry points */
|
|
|
|
|
function testParse() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2010-12-26 19:30:10 +00:00
|
|
|
$parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
|
|
|
|
|
$this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testPreSaveTransform() {
|
2011-06-22 17:45:31 +00:00
|
|
|
global $wgUser;
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2010-12-26 19:30:10 +00:00
|
|
|
$outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $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 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testPreprocess() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2010-12-26 19:30:10 +00:00
|
|
|
$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
|
|
|
|
|
*/
|
|
|
|
|
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}} ~~~~" );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "{{SUBST:Foo}} ", $outputText );
|
|
|
|
|
}
|
2011-08-17 22:09:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* cleanSig() should do nothing if disabled
|
|
|
|
|
*/
|
|
|
|
|
function testCleanSigDisabled() {
|
|
|
|
|
global $wgCleanSignatures;
|
|
|
|
|
$wgCleanSignatures = false;
|
|
|
|
|
|
|
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
|
|
|
|
$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "{{Foo}} ~~~~", $outputText );
|
|
|
|
|
}
|
2010-12-26 19:30:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* cleanSigInSig() just removes tildes
|
2011-12-06 23:07:13 +00:00
|
|
|
* @dataProvider provideStringsForCleanSigInSig
|
2010-12-26 19:30:10 +00:00
|
|
|
*/
|
2011-12-06 23:07:13 +00:00
|
|
|
function testCleanSigInSig( $in, $out ) {
|
|
|
|
|
$this->assertEquals( Parser::cleanSigInSig( $in), $out );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideStringsForCleanSigInSig() {
|
2011-12-06 23:07:13 +00:00
|
|
|
return array(
|
|
|
|
|
array( "{{Foo}} ~~~~", "{{Foo}} " ),
|
|
|
|
|
array( "~~~", "" ),
|
|
|
|
|
array( "~~~~~", "" ),
|
|
|
|
|
);
|
2010-12-26 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testGetSection() {
|
|
|
|
|
$outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
|
|
|
|
|
$outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 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 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testReplaceSection() {
|
|
|
|
|
$outputText = $this->parser->replaceSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 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.
|
|
|
|
|
*/
|
|
|
|
|
function testGetPreloadText() {
|
2010-12-27 16:51:59 +00:00
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2010-12-26 19:30:10 +00:00
|
|
|
$outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function statelessFetchTemplate( $title, $parser=false ) {
|
|
|
|
|
$text = "Content of ''" . $title->getFullText() . "''";
|
|
|
|
|
$deps = array();
|
|
|
|
|
|
|
|
|
|
return array(
|
|
|
|
|
'text' => $text,
|
|
|
|
|
'finalTitle' => $title,
|
|
|
|
|
'deps' => $deps );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2011-12-19 21:00:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2011-08-21 19:28:35 +00:00
|
|
|
function testTrackingCategory() {
|
|
|
|
|
$title = Title::newFromText( __FUNCTION__ );
|
2012-08-19 23:05:20 +00:00
|
|
|
$catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
|
2011-08-21 19:28:35 +00:00
|
|
|
$cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
|
|
|
|
|
$expected = array( $cat->getDBkey() );
|
|
|
|
|
$parserOutput = $this->parser->parse( "[[file:nonexistent]]" , $title, $this->options );
|
|
|
|
|
$result = $parserOutput->getCategoryLinks();
|
|
|
|
|
$this->assertEquals( $expected, $result );
|
|
|
|
|
}
|
2011-12-19 21:00:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2011-08-21 19:28:35 +00:00
|
|
|
function testTrackingCategorySpecial() {
|
|
|
|
|
// Special pages shouldn't have tracking cats.
|
|
|
|
|
$title = SpecialPage::getTitleFor( 'Contributions' );
|
|
|
|
|
$parserOutput = $this->parser->parse( "[[file:nonexistent]]" , $title, $this->options );
|
|
|
|
|
$result = $parserOutput->getCategoryLinks();
|
|
|
|
|
$this->assertEmpty( $result );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|