MediaWiki
REL1_22
|
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 public 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 public 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 public 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 public 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 public function testCleanSig() { 00066 $title = Title::newFromText( __FUNCTION__ ); 00067 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); 00068 00069 $this->assertEquals( "{{SUBST:Foo}} ", $outputText ); 00070 } 00071 00075 public function testCleanSigDisabled() { 00076 $this->setMwGlobals( 'wgCleanSignatures', false ); 00077 00078 $title = Title::newFromText( __FUNCTION__ ); 00079 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); 00080 00081 $this->assertEquals( "{{Foo}} ~~~~", $outputText ); 00082 } 00083 00088 public function testCleanSigInSig( $in, $out ) { 00089 $this->assertEquals( Parser::cleanSigInSig( $in ), $out ); 00090 } 00091 00092 public static function provideStringsForCleanSigInSig() { 00093 return array( 00094 array( "{{Foo}} ~~~~", "{{Foo}} " ), 00095 array( "~~~", "" ), 00096 array( "~~~~~", "" ), 00097 ); 00098 } 00099 00100 public function testGetSection() { 00101 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 ); 00102 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 ); 00103 00104 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 ); 00105 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 ); 00106 } 00107 00108 public function testReplaceSection() { 00109 $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" ); 00110 00111 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText ); 00112 } 00113 00117 public function testGetPreloadText() { 00118 $title = Title::newFromText( __FUNCTION__ ); 00119 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options ); 00120 00121 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText ); 00122 } 00123 00124 static function statelessFetchTemplate( $title, $parser = false ) { 00125 $text = "Content of ''" . $title->getFullText() . "''"; 00126 $deps = array(); 00127 00128 return array( 00129 'text' => $text, 00130 'finalTitle' => $title, 00131 'deps' => $deps ); 00132 } 00133 00137 public function testTrackingCategory() { 00138 $title = Title::newFromText( __FUNCTION__ ); 00139 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text(); 00140 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName ); 00141 $expected = array( $cat->getDBkey() ); 00142 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options ); 00143 $result = $parserOutput->getCategoryLinks(); 00144 $this->assertEquals( $expected, $result ); 00145 } 00146 00150 public function testTrackingCategorySpecial() { 00151 // Special pages shouldn't have tracking cats. 00152 $title = SpecialPage::getTitleFor( 'Contributions' ); 00153 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options ); 00154 $result = $parserOutput->getCategoryLinks(); 00155 $this->assertEmpty( $result ); 00156 } 00157 }