MediaWiki  REL1_23
ExtraParserTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class ExtraParserTest extends MediaWikiTestCase {
00007 
00009     protected $options;
00011     protected $parser;
00012 
00013     protected function setUp() {
00014         parent::setUp();
00015 
00016         $contLang = Language::factory( 'en' );
00017         $this->setMwGlobals( array(
00018             'wgShowDBErrorBacktrace' => true,
00019             'wgLanguageCode' => 'en',
00020             'wgContLang' => $contLang,
00021             'wgLang' => Language::factory( 'en' ),
00022             'wgMemc' => new EmptyBagOStuff,
00023             'wgAlwaysUseTidy' => false,
00024             'wgCleanSignatures' => true,
00025         ) );
00026 
00027         $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
00028         $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
00029         $this->parser = new Parser;
00030 
00031         MagicWord::clearCache();
00032     }
00033 
00038     public function testLongNumericLinesDontKillTheParser() {
00039         $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
00040 
00041         $title = Title::newFromText( 'Unit test' );
00042         $options = ParserOptions::newFromUser( new User() );
00043         $this->assertEquals( "<p>$longLine</p>",
00044             $this->parser->parse( $longLine, $title, $options )->getText() );
00045     }
00046 
00051     public function testParse() {
00052         $title = Title::newFromText( __FUNCTION__ );
00053         $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
00054         $this->assertEquals(
00055             "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
00056             $parserOutput->getText()
00057         );
00058     }
00059 
00063     public function testPreSaveTransform() {
00064         $title = Title::newFromText( __FUNCTION__ );
00065         $outputText = $this->parser->preSaveTransform(
00066             "Test\r\n{{subst:Foo}}\n{{Bar}}",
00067             $title,
00068             new User(),
00069             $this->options
00070         );
00071 
00072         $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
00073     }
00074 
00078     public function testPreprocess() {
00079         $title = Title::newFromText( __FUNCTION__ );
00080         $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
00081 
00082         $this->assertEquals(
00083             "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
00084             $outputText
00085         );
00086     }
00087 
00092     public function testCleanSig() {
00093         $title = Title::newFromText( __FUNCTION__ );
00094         $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
00095 
00096         $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
00097     }
00098 
00103     public function testCleanSigDisabled() {
00104         $this->setMwGlobals( 'wgCleanSignatures', false );
00105 
00106         $title = Title::newFromText( __FUNCTION__ );
00107         $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
00108 
00109         $this->assertEquals( "{{Foo}} ~~~~", $outputText );
00110     }
00111 
00117     public function testCleanSigInSig( $in, $out ) {
00118         $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
00119     }
00120 
00121     public static function provideStringsForCleanSigInSig() {
00122         return array(
00123             array( "{{Foo}} ~~~~", "{{Foo}} " ),
00124             array( "~~~", "" ),
00125             array( "~~~~~", "" ),
00126         );
00127     }
00128 
00132     public function testGetSection() {
00133         $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
00134         $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
00135 
00136         $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
00137         $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
00138     }
00139 
00143     public function testReplaceSection() {
00144         $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" );
00145 
00146         $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
00147     }
00148 
00153     public function testGetPreloadText() {
00154         $title = Title::newFromText( __FUNCTION__ );
00155         $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
00156 
00157         $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
00158     }
00159 
00166     static function statelessFetchTemplate( $title, $parser = false ) {
00167         $text = "Content of ''" . $title->getFullText() . "''";
00168         $deps = array();
00169 
00170         return array(
00171             'text' => $text,
00172             'finalTitle' => $title,
00173             'deps' => $deps );
00174     }
00175 
00180     public function testTrackingCategory() {
00181         $title = Title::newFromText( __FUNCTION__ );
00182         $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
00183         $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
00184         $expected = array( $cat->getDBkey() );
00185         $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
00186         $result = $parserOutput->getCategoryLinks();
00187         $this->assertEquals( $expected, $result );
00188     }
00189 
00194     public function testTrackingCategorySpecial() {
00195         // Special pages shouldn't have tracking cats.
00196         $title = SpecialPage::getTitleFor( 'Contributions' );
00197         $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
00198         $result = $parserOutput->getCategoryLinks();
00199         $this->assertEmpty( $result );
00200     }
00201 }