MediaWiki  REL1_20
ExtraParserTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class ExtraParserTest extends MediaWikiTestCase {
00007 
00008         function setUp() {
00009                 global $wgMemc;
00010                 global $wgContLang;
00011                 global $wgShowDBErrorBacktrace;
00012                 global $wgLanguageCode;
00013                 global $wgAlwaysUseTidy;
00014 
00015                 $wgShowDBErrorBacktrace = true;
00016                 $wgLanguageCode = 'en';
00017                 $wgContLang = new Language( 'en' );
00018                 $wgMemc = new EmptyBagOStuff;
00019                 $wgAlwaysUseTidy = false;
00020                 
00021                 $this->options = new ParserOptions;
00022                 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
00023                 $this->parser = new Parser;
00024 
00025                 MagicWord::clearCache();
00026         }
00027 
00028         // Bug 8689 - Long numeric lines kill the parser
00029         function testBug8689() {
00030                 global $wgLang;
00031                 global $wgUser;
00032                 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
00033 
00034                 if ( $wgLang === null ) $wgLang = new Language;
00035                 
00036                 $t = Title::newFromText( 'Unit test' );
00037                 $options = ParserOptions::newFromUser( $wgUser );
00038                 $this->assertEquals( "<p>$longLine</p>",
00039                         $this->parser->parse( $longLine, $t, $options )->getText() );
00040         }
00041         
00042         /* Test the parser entry points */
00043         function testParse() {
00044                 $title = Title::newFromText( __FUNCTION__ );
00045                 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
00046                 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
00047         }
00048         
00049         function testPreSaveTransform() {
00050                 global $wgUser;
00051                 $title = Title::newFromText( __FUNCTION__ );
00052                 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
00053 
00054                 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
00055         }
00056         
00057         function testPreprocess() {
00058                 $title = Title::newFromText( __FUNCTION__ );
00059                 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
00060                 
00061                 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
00062         }
00063         
00067         function testCleanSig() {
00068                 global $wgCleanSignatures;
00069                 $oldCleanSignature = $wgCleanSignatures;
00070                 $wgCleanSignatures = true;
00071 
00072                 $title = Title::newFromText( __FUNCTION__ );
00073                 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
00074 
00075                 $wgCleanSignatures = $oldCleanSignature;
00076                 
00077                 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
00078         }
00079 
00083         function testCleanSigDisabled() {
00084                 global $wgCleanSignatures;
00085                 $oldCleanSignature = $wgCleanSignatures;
00086                 $wgCleanSignatures = false;
00087 
00088                 $title = Title::newFromText( __FUNCTION__ );
00089                 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
00090 
00091                 $wgCleanSignatures = $oldCleanSignature;
00092                 
00093                 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
00094         }
00095         
00100         function testCleanSigInSig( $in, $out ) {
00101                 $this->assertEquals( Parser::cleanSigInSig( $in), $out );
00102         }
00103         
00104         function provideStringsForCleanSigInSig() {
00105                 return array(
00106                         array( "{{Foo}} ~~~~", "{{Foo}} " ),
00107                         array( "~~~", "" ),
00108                         array( "~~~~~", "" ),
00109                 );
00110         }
00111         
00112         function testGetSection() {
00113                 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
00114                 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
00115                 
00116                 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
00117                 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
00118         }
00119         
00120         function testReplaceSection() {
00121                 $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" );
00122                 
00123                 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
00124         }
00125         
00129         function testGetPreloadText() {
00130                 $title = Title::newFromText( __FUNCTION__ );
00131                 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
00132                 
00133                 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
00134         }
00135         
00136         static function statelessFetchTemplate( $title, $parser=false ) {
00137                 $text = "Content of ''" . $title->getFullText() . "''";
00138                 $deps = array();
00139                 
00140                 return array(
00141                         'text' => $text,
00142                         'finalTitle' => $title,
00143                         'deps' => $deps );
00144         }
00145 
00149         function testTrackingCategory() {
00150                 $title = Title::newFromText( __FUNCTION__ );
00151                 $catName =  wfMessage( 'broken-file-category' )->inContentLanguage()->text();
00152                 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
00153                 $expected = array( $cat->getDBkey() );
00154                 $parserOutput = $this->parser->parse( "[[file:nonexistent]]" , $title, $this->options );
00155                 $result = $parserOutput->getCategoryLinks();
00156                 $this->assertEquals( $expected, $result );
00157         }
00158 
00162         function testTrackingCategorySpecial() {
00163                 // Special pages shouldn't have tracking cats.
00164                 $title = SpecialPage::getTitleFor( 'Contributions' );
00165                 $parserOutput = $this->parser->parse( "[[file:nonexistent]]" , $title, $this->options );
00166                 $result = $parserOutput->getCategoryLinks();
00167                 $this->assertEmpty( $result );
00168         }
00169  }