MediaWiki
REL1_19
|
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 00026 // Bug 8689 - Long numeric lines kill the parser 00027 function testBug8689() { 00028 global $wgLang; 00029 global $wgUser; 00030 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n"; 00031 00032 if ( $wgLang === null ) $wgLang = new Language; 00033 00034 $t = Title::newFromText( 'Unit test' ); 00035 $options = ParserOptions::newFromUser( $wgUser ); 00036 $this->assertEquals( "<p>$longLine</p>", 00037 $this->parser->parse( $longLine, $t, $options )->getText() ); 00038 } 00039 00040 /* Test the parser entry points */ 00041 function testParse() { 00042 $title = Title::newFromText( __FUNCTION__ ); 00043 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options ); 00044 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() ); 00045 } 00046 00047 function testPreSaveTransform() { 00048 global $wgUser; 00049 $title = Title::newFromText( __FUNCTION__ ); 00050 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options ); 00051 00052 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText ); 00053 } 00054 00055 function testPreprocess() { 00056 $title = Title::newFromText( __FUNCTION__ ); 00057 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options ); 00058 00059 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText ); 00060 } 00061 00065 function testCleanSig() { 00066 global $wgCleanSignatures; 00067 $oldCleanSignature = $wgCleanSignatures; 00068 $wgCleanSignatures = true; 00069 00070 $title = Title::newFromText( __FUNCTION__ ); 00071 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); 00072 00073 $wgCleanSignatures = $oldCleanSignature; 00074 00075 $this->assertEquals( "{{SUBST:Foo}} ", $outputText ); 00076 } 00077 00081 function testCleanSigDisabled() { 00082 global $wgCleanSignatures; 00083 $oldCleanSignature = $wgCleanSignatures; 00084 $wgCleanSignatures = false; 00085 00086 $title = Title::newFromText( __FUNCTION__ ); 00087 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); 00088 00089 $wgCleanSignatures = $oldCleanSignature; 00090 00091 $this->assertEquals( "{{Foo}} ~~~~", $outputText ); 00092 } 00093 00098 function testCleanSigInSig( $in, $out ) { 00099 $this->assertEquals( Parser::cleanSigInSig( $in), $out ); 00100 } 00101 00102 function provideStringsForCleanSigInSig() { 00103 return array( 00104 array( "{{Foo}} ~~~~", "{{Foo}} " ), 00105 array( "~~~", "" ), 00106 array( "~~~~~", "" ), 00107 ); 00108 } 00109 00110 function testGetSection() { 00111 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 ); 00112 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 ); 00113 00114 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 ); 00115 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 ); 00116 } 00117 00118 function testReplaceSection() { 00119 $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" ); 00120 00121 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText ); 00122 } 00123 00127 function testGetPreloadText() { 00128 $title = Title::newFromText( __FUNCTION__ ); 00129 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options ); 00130 00131 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText ); 00132 } 00133 00134 static function statelessFetchTemplate( $title, $parser=false ) { 00135 $text = "Content of ''" . $title->getFullText() . "''"; 00136 $deps = array(); 00137 00138 return array( 00139 'text' => $text, 00140 'finalTitle' => $title, 00141 'deps' => $deps ); 00142 } 00143 00147 function testTrackingCategory() { 00148 $title = Title::newFromText( __FUNCTION__ ); 00149 $catName = wfMsgForContent( 'broken-file-category' ); 00150 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName ); 00151 $expected = array( $cat->getDBkey() ); 00152 $parserOutput = $this->parser->parse( "[[file:nonexistent]]" , $title, $this->options ); 00153 $result = $parserOutput->getCategoryLinks(); 00154 $this->assertEquals( $expected, $result ); 00155 } 00156 00160 function testTrackingCategorySpecial() { 00161 // Special pages shouldn't have tracking cats. 00162 $title = SpecialPage::getTitleFor( 'Contributions' ); 00163 $parserOutput = $this->parser->parse( "[[file:nonexistent]]" , $title, $this->options ); 00164 $result = $parserOutput->getCategoryLinks(); 00165 $this->assertEmpty( $result ); 00166 } 00167 }