r79020 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79019‎ | r79020 | r79021 >
Date:19:30, 26 December 2010
Author:platonides
Status:resolved (Comments)
Tags:
Comment:
Document parser, add test cases for the external entry points to it.
Modified paths:
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/ExtraParserTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/ExtraParserTest.php
@@ -1,8 +1,8 @@
22 <?php
 3+
34 /**
45 * Parser-related tests that don't suit for parserTests.txt
56 */
6 -
77 class ExtraParserTest extends PHPUnit_Framework_TestCase {
88
99 function setUp() {
@@ -11,8 +11,12 @@
1212 global $wgShowDBErrorBacktrace;
1313
1414 $wgShowDBErrorBacktrace = true;
15 - if ( $wgContLang === null ) $wgContLang = new Language;
 15+ $wgContLang = new Language( 'en' );
1616 $wgMemc = new FakeMemCachedClient;
 17+
 18+ $this->options = new ParserOptions;
 19+ $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
 20+ $this->parser = new Parser;
1721 }
1822
1923 // Bug 8689 - Long numeric lines kill the parser
@@ -22,10 +26,86 @@
2327 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
2428
2529 if ( $wgLang === null ) $wgLang = new Language;
26 - $parser = new Parser();
 30+
2731 $t = Title::newFromText( 'Unit test' );
2832 $options = ParserOptions::newFromUser( $wgUser );
2933 $this->assertEquals( "<p>$longLine</p>",
30 - $parser->parse( $longLine, $t, $options )->getText() );
 34+ $this->parser->parse( $longLine, $t, $options )->getText() );
3135 }
 36+
 37+ /* Test the parser entry points */
 38+ function testParse() {
 39+ $title = Title::newFromText( __METHOD__ );
 40+ $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
 41+ $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
 42+ }
 43+
 44+ function testPreSaveTransform() {
 45+ global $wgUser;
 46+ $title = Title::newFromText( __METHOD__ );
 47+ $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
 48+
 49+ $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
 50+ }
 51+
 52+ function testPreprocess() {
 53+ $title = Title::newFromText( __METHOD__ );
 54+ $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
 55+
 56+ $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
 57+ }
 58+
 59+ /**
 60+ * cleanSig() makes all templates substs and removes tildes
 61+ */
 62+ function testCleanSig() {
 63+ $title = Title::newFromText( __METHOD__ );
 64+ $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
 65+
 66+ $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
 67+ }
 68+
 69+ /**
 70+ * cleanSigInSig() just removes tildes
 71+ */
 72+ function testCleanSigInSig() {
 73+ $title = Title::newFromText( __METHOD__ );
 74+ $outputText = $this->parser->cleanSigInSig( "{{Foo}} ~~~~" );
 75+
 76+ $this->assertEquals( "{{Foo}} ", $outputText );
 77+ }
 78+
 79+ function testGetSection() {
 80+ $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
 81+ $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
 82+
 83+ $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
 84+ $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
 85+ }
 86+
 87+ function testReplaceSection() {
 88+ $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" );
 89+
 90+ $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
 91+ }
 92+
 93+ /**
 94+ * Templates and comments are not affected, but noinclude/onlyinclude is.
 95+ */
 96+ function testGetPreloadText() {
 97+ $title = Title::newFromText( __METHOD__ );
 98+ $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
 99+
 100+ $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
 101+ }
 102+
 103+ static function statelessFetchTemplate( $title, $parser=false ) {
 104+ $text = "Content of ''" . $title->getFullText() . "''";
 105+ $deps = array();
 106+
 107+ return array(
 108+ 'text' => $text,
 109+ 'finalTitle' => $title,
 110+ 'deps' => $deps );
 111+ }
32112 }
Index: trunk/phase3/includes/parser/Parser.php
@@ -24,8 +24,10 @@
2525 * removes HTML comments and expands templates
2626 * cleanSig() / cleanSigInSig()
2727 * Cleans a signature before saving it to preferences
28 - * extractSections()
29 - * Extracts sections from an article for section editing
 28+ * getSection()
 29+ * Return the content of a section from an article for section editing
 30+ * replaceSection()
 31+ * Replaces a section by number inside an article
3032 * getPreloadText()
3133 * Removes <noinclude> sections, and <includeonly> tags.
3234 *
@@ -4026,7 +4028,7 @@
40274029 * @param $clearState Boolean: whether to clear the parser state first
40284030 * @return String: the altered wiki markup
40294031 */
4030 - public function preSaveTransform( $text, Title $title, $user, $options, $clearState = true ) {
 4032+ public function preSaveTransform( $text, Title $title, User $user, ParserOptions $options, $clearState = true ) {
40314033 $this->mOptions = $options;
40324034 $this->setTitle( $title );
40334035 $this->setUser( $user );
@@ -4965,6 +4967,15 @@
49664968 return $this->extractSections( $text, $section, "get", $deftext );
49674969 }
49684970
 4971+ /**
 4972+ * This function returns $oldtext after the content of the section
 4973+ * specified by $section has been replaced with $text.
 4974+ *
 4975+ * @param $text String: former text of the article
 4976+ * @param $section Numeric: section identifier
 4977+ * @param $text String: replacing text
 4978+ * #return String: modified text
 4979+ */
49694980 public function replaceSection( $oldtext, $section, $text ) {
49704981 return $this->extractSections( $oldtext, $section, "replace", $text );
49714982 }

Comments

#Comment by Bawolff (talk | contribs)   23:48, 23 June 2011

Note, testCleanSig fails if you have $wgCleanSignatures set to false (like it happened to be on my test wiki). I'm unclear if unit tests are supposed to still work with non-default config, so I'm unclear if that's an issue or not.

Status & tagging log