MediaWiki
REL1_21
|
00001 <?php 00002 00006 class ExtraParserTest extends MediaWikiTestCase { 00007 00008 protected function setUp() { 00009 parent::setUp(); 00010 00011 $contLang = Language::factory( 'en' ); 00012 $this->setMwGlobals( array( 00013 'wgShowDBErrorBacktrace' => true, 00014 'wgLanguageCode' => 'en', 00015 'wgContLang' => $contLang, 00016 'wgLang' => Language::factory( 'en' ), 00017 'wgMemc' => new EmptyBagOStuff, 00018 'wgAlwaysUseTidy' => false, 00019 'wgCleanSignatures' => true, 00020 ) ); 00021 00022 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang ); 00023 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) ); 00024 $this->parser = new Parser; 00025 00026 MagicWord::clearCache(); 00027 } 00028 00029 // Bug 8689 - Long numeric lines kill the parser 00030 function testBug8689() { 00031 global $wgUser; 00032 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n"; 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 $title = Title::newFromText( __FUNCTION__ ); 00067 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); 00068 00069 $this->assertEquals( "{{SUBST:Foo}} ", $outputText ); 00070 } 00071 00075 function testCleanSigDisabled() { 00076 global $wgCleanSignatures; 00077 $wgCleanSignatures = false; 00078 00079 $title = Title::newFromText( __FUNCTION__ ); 00080 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); 00081 00082 $this->assertEquals( "{{Foo}} ~~~~", $outputText ); 00083 } 00084 00089 function testCleanSigInSig( $in, $out ) { 00090 $this->assertEquals( Parser::cleanSigInSig( $in ), $out ); 00091 } 00092 00093 public static function provideStringsForCleanSigInSig() { 00094 return array( 00095 array( "{{Foo}} ~~~~", "{{Foo}} " ), 00096 array( "~~~", "" ), 00097 array( "~~~~~", "" ), 00098 ); 00099 } 00100 00101 function testGetSection() { 00102 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 ); 00103 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 ); 00104 00105 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 ); 00106 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 ); 00107 } 00108 00109 function testReplaceSection() { 00110 $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" ); 00111 00112 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText ); 00113 } 00114 00118 function testGetPreloadText() { 00119 $title = Title::newFromText( __FUNCTION__ ); 00120 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options ); 00121 00122 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText ); 00123 } 00124 00125 static function statelessFetchTemplate( $title, $parser = false ) { 00126 $text = "Content of ''" . $title->getFullText() . "''"; 00127 $deps = array(); 00128 00129 return array( 00130 'text' => $text, 00131 'finalTitle' => $title, 00132 'deps' => $deps ); 00133 } 00134 00138 function testTrackingCategory() { 00139 $title = Title::newFromText( __FUNCTION__ ); 00140 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text(); 00141 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName ); 00142 $expected = array( $cat->getDBkey() ); 00143 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options ); 00144 $result = $parserOutput->getCategoryLinks(); 00145 $this->assertEquals( $expected, $result ); 00146 } 00147 00151 function testTrackingCategorySpecial() { 00152 // Special pages shouldn't have tracking cats. 00153 $title = SpecialPage::getTitleFor( 'Contributions' ); 00154 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options ); 00155 $result = $parserOutput->getCategoryLinks(); 00156 $this->assertEmpty( $result ); 00157 } 00158 }