From: Platonides Date: Sun, 26 Dec 2010 19:30:10 +0000 (+0000) Subject: Document parser, add test cases for the external entry points to it. X-Git-Tag: 1.31.0-rc.0~33080 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=691b4618e21a10e201c26ce1e1591717b3a50d5a;p=lhc%2Fweb%2Fwiklou.git Document parser, add test cases for the external entry points to it. --- diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index a18fe736ec..4a8eadfc79 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -24,8 +24,10 @@ * removes HTML comments and expands templates * cleanSig() / cleanSigInSig() * Cleans a signature before saving it to preferences - * extractSections() - * Extracts sections from an article for section editing + * getSection() + * Return the content of a section from an article for section editing + * replaceSection() + * Replaces a section by number inside an article * getPreloadText() * Removes sections, and tags. * @@ -4026,7 +4028,7 @@ class Parser { * @param $clearState Boolean: whether to clear the parser state first * @return String: the altered wiki markup */ - public function preSaveTransform( $text, Title $title, $user, $options, $clearState = true ) { + public function preSaveTransform( $text, Title $title, User $user, ParserOptions $options, $clearState = true ) { $this->mOptions = $options; $this->setTitle( $title ); $this->setUser( $user ); @@ -4965,6 +4967,15 @@ class Parser { return $this->extractSections( $text, $section, "get", $deftext ); } + /** + * This function returns $oldtext after the content of the section + * specified by $section has been replaced with $text. + * + * @param $text String: former text of the article + * @param $section Numeric: section identifier + * @param $text String: replacing text + * #return String: modified text + */ public function replaceSection( $oldtext, $section, $text ) { return $this->extractSections( $oldtext, $section, "replace", $text ); } diff --git a/tests/phpunit/includes/ExtraParserTest.php b/tests/phpunit/includes/ExtraParserTest.php index f879011353..981bdfc1d0 100644 --- a/tests/phpunit/includes/ExtraParserTest.php +++ b/tests/phpunit/includes/ExtraParserTest.php @@ -1,8 +1,8 @@ options = new ParserOptions; + $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) ); + $this->parser = new Parser; } // Bug 8689 - Long numeric lines kill the parser @@ -22,10 +26,86 @@ class ExtraParserTest extends PHPUnit_Framework_TestCase { $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n"; if ( $wgLang === null ) $wgLang = new Language; - $parser = new Parser(); + $t = Title::newFromText( 'Unit test' ); $options = ParserOptions::newFromUser( $wgUser ); $this->assertEquals( "

$longLine

", - $parser->parse( $longLine, $t, $options )->getText() ); + $this->parser->parse( $longLine, $t, $options )->getText() ); + } + + /* Test the parser entry points */ + function testParse() { + $title = Title::newFromText( __METHOD__ ); + $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options ); + $this->assertEquals( "

Test\nContent of Template:Foo\nContent of Template:Bar\n

", $parserOutput->getText() ); + } + + function testPreSaveTransform() { + global $wgUser; + $title = Title::newFromText( __METHOD__ ); + $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options ); + + $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText ); + } + + function testPreprocess() { + $title = Title::newFromText( __METHOD__ ); + $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options ); + + $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText ); + } + + /** + * cleanSig() makes all templates substs and removes tildes + */ + function testCleanSig() { + $title = Title::newFromText( __METHOD__ ); + $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" ); + + $this->assertEquals( "{{SUBST:Foo}} ", $outputText ); + } + + /** + * cleanSigInSig() just removes tildes + */ + function testCleanSigInSig() { + $title = Title::newFromText( __METHOD__ ); + $outputText = $this->parser->cleanSigInSig( "{{Foo}} ~~~~" ); + + $this->assertEquals( "{{Foo}} ", $outputText ); + } + + function testGetSection() { + $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 ); + $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 ); + + $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 ); + $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 ); + } + + function testReplaceSection() { + $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" ); + + $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText ); + } + + /** + * Templates and comments are not affected, but noinclude/onlyinclude is. + */ + function testGetPreloadText() { + $title = Title::newFromText( __METHOD__ ); + $outputText = $this->parser->getPreloadText( "{{Foo}} censored information ", $title, $this->options ); + + $this->assertEquals( "{{Foo}} information ", $outputText ); + } + + static function statelessFetchTemplate( $title, $parser=false ) { + $text = "Content of ''" . $title->getFullText() . "''"; + $deps = array(); + + return array( + 'text' => $text, + 'finalTitle' => $title, + 'deps' => $deps ); } }