MediaWiki  REL1_24
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(
00134             "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
00135                 . "Section 2\n== Heading 3 ==\nSection 3\n",
00136             2
00137         );
00138         $outputText1 = $this->parser->getSection(
00139             "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
00140                 . "Section 2\n== Heading 3 ==\nSection 3\n",
00141             1
00142         );
00143 
00144         $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
00145         $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
00146     }
00147 
00151     public function testReplaceSection() {
00152         $outputText = $this->parser->replaceSection(
00153             "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
00154                 . "Section 2\n== Heading 3 ==\nSection 3\n",
00155             1,
00156             "New section 1"
00157         );
00158 
00159         $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
00160     }
00161 
00166     public function testGetPreloadText() {
00167         $title = Title::newFromText( __FUNCTION__ );
00168         $outputText = $this->parser->getPreloadText(
00169             "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
00170             $title,
00171             $this->options
00172         );
00173 
00174         $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
00175     }
00176 
00183     static function statelessFetchTemplate( $title, $parser = false ) {
00184         $text = "Content of ''" . $title->getFullText() . "''";
00185         $deps = array();
00186 
00187         return array(
00188             'text' => $text,
00189             'finalTitle' => $title,
00190             'deps' => $deps );
00191     }
00192 
00197     public function testTrackingCategory() {
00198         $title = Title::newFromText( __FUNCTION__ );
00199         $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
00200         $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
00201         $expected = array( $cat->getDBkey() );
00202         $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
00203         $result = $parserOutput->getCategoryLinks();
00204         $this->assertEquals( $expected, $result );
00205     }
00206 
00211     public function testTrackingCategorySpecial() {
00212         // Special pages shouldn't have tracking cats.
00213         $title = SpecialPage::getTitleFor( 'Contributions' );
00214         $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
00215         $result = $parserOutput->getCategoryLinks();
00216         $this->assertEmpty( $result );
00217     }
00218 }