Index: trunk/phase3/tests/phpunit/includes/ExtraParserTest.php |
— | — | @@ -1,8 +1,8 @@ |
2 | 2 | <?php |
| 3 | + |
3 | 4 | /** |
4 | 5 | * Parser-related tests that don't suit for parserTests.txt |
5 | 6 | */ |
6 | | - |
7 | 7 | class ExtraParserTest extends PHPUnit_Framework_TestCase { |
8 | 8 | |
9 | 9 | function setUp() { |
— | — | @@ -11,8 +11,12 @@ |
12 | 12 | global $wgShowDBErrorBacktrace; |
13 | 13 | |
14 | 14 | $wgShowDBErrorBacktrace = true; |
15 | | - if ( $wgContLang === null ) $wgContLang = new Language; |
| 15 | + $wgContLang = new Language( 'en' ); |
16 | 16 | $wgMemc = new FakeMemCachedClient; |
| 17 | + |
| 18 | + $this->options = new ParserOptions; |
| 19 | + $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) ); |
| 20 | + $this->parser = new Parser; |
17 | 21 | } |
18 | 22 | |
19 | 23 | // Bug 8689 - Long numeric lines kill the parser |
— | — | @@ -22,10 +26,86 @@ |
23 | 27 | $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n"; |
24 | 28 | |
25 | 29 | if ( $wgLang === null ) $wgLang = new Language; |
26 | | - $parser = new Parser(); |
| 30 | + |
27 | 31 | $t = Title::newFromText( 'Unit test' ); |
28 | 32 | $options = ParserOptions::newFromUser( $wgUser ); |
29 | 33 | $this->assertEquals( "<p>$longLine</p>", |
30 | | - $parser->parse( $longLine, $t, $options )->getText() ); |
| 34 | + $this->parser->parse( $longLine, $t, $options )->getText() ); |
31 | 35 | } |
| 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 | + } |
32 | 112 | } |
Index: trunk/phase3/includes/parser/Parser.php |
— | — | @@ -24,8 +24,10 @@ |
25 | 25 | * removes HTML comments and expands templates |
26 | 26 | * cleanSig() / cleanSigInSig() |
27 | 27 | * 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 |
30 | 32 | * getPreloadText() |
31 | 33 | * Removes <noinclude> sections, and <includeonly> tags. |
32 | 34 | * |
— | — | @@ -4026,7 +4028,7 @@ |
4027 | 4029 | * @param $clearState Boolean: whether to clear the parser state first |
4028 | 4030 | * @return String: the altered wiki markup |
4029 | 4031 | */ |
4030 | | - public function preSaveTransform( $text, Title $title, $user, $options, $clearState = true ) { |
| 4032 | + public function preSaveTransform( $text, Title $title, User $user, ParserOptions $options, $clearState = true ) { |
4031 | 4033 | $this->mOptions = $options; |
4032 | 4034 | $this->setTitle( $title ); |
4033 | 4035 | $this->setUser( $user ); |
— | — | @@ -4965,6 +4967,15 @@ |
4966 | 4968 | return $this->extractSections( $text, $section, "get", $deftext ); |
4967 | 4969 | } |
4968 | 4970 | |
| 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 | + */ |
4969 | 4980 | public function replaceSection( $oldtext, $section, $text ) { |
4970 | 4981 | return $this->extractSections( $oldtext, $section, "replace", $text ); |
4971 | 4982 | } |