c8400568fb98c77355d7e884097797fde36ad4d9
4 * Parser-related tests that don't suit for parserTests.txt
6 class ExtraParserTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $contLang = Language
::factory( 'en' );
12 $this->setMwGlobals( array(
13 'wgShowDBErrorBacktrace' => true,
14 'wgLanguageCode' => 'en',
15 'wgContLang' => $contLang,
16 'wgLang' => Language
::factory( 'en' ),
17 'wgMemc' => new EmptyBagOStuff
,
18 'wgAlwaysUseTidy' => false,
19 'wgCleanSignatures' => true,
22 $this->options
= ParserOptions
::newFromUserAndLang( new User
, $contLang );
23 $this->options
->setTemplateCallback( array( __CLASS__
, 'statelessFetchTemplate' ) );
24 $this->parser
= new Parser
;
26 MagicWord
::clearCache();
30 * Bug 8689 - Long numeric lines kill the parser
34 function testBug8689() {
36 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
38 $t = Title
::newFromText( 'Unit test' );
39 $options = ParserOptions
::newFromUser( $wgUser );
40 $this->assertEquals( "<p>$longLine</p>",
41 $this->parser
->parse( $longLine, $t, $options )->getText() );
45 * Test the parser entry points
49 function testParse() {
50 $title = Title
::newFromText( __FUNCTION__
);
51 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
52 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
58 function testPreSaveTransform() {
60 $title = Title
::newFromText( __FUNCTION__
);
61 $outputText = $this->parser
->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options
);
63 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
69 function testPreprocess() {
70 $title = Title
::newFromText( __FUNCTION__
);
71 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
73 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
77 * cleanSig() makes all templates substs and removes tildes
79 function testCleanSig() {
80 $title = Title
::newFromText( __FUNCTION__
);
81 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
83 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
87 * cleanSig() should do nothing if disabled
89 function testCleanSigDisabled() {
90 $this->setMwGlobals( 'wgCleanSignatures', false );
92 $title = Title
::newFromText( __FUNCTION__
);
93 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
95 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
99 * cleanSigInSig() just removes tildes
100 * @dataProvider provideStringsForCleanSigInSig
102 function testCleanSigInSig( $in, $out ) {
103 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
106 public static function provideStringsForCleanSigInSig() {
108 array( "{{Foo}} ~~~~", "{{Foo}} " ),
110 array( "~~~~~", "" ),
114 function testGetSection() {
115 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
116 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
118 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
119 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
122 function testReplaceSection() {
123 $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" );
125 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
129 * Templates and comments are not affected, but noinclude/onlyinclude is.
131 function testGetPreloadText() {
132 $title = Title
::newFromText( __FUNCTION__
);
133 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
135 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
138 static function statelessFetchTemplate( $title, $parser = false ) {
139 $text = "Content of ''" . $title->getFullText() . "''";
144 'finalTitle' => $title,
151 function testTrackingCategory() {
152 $title = Title
::newFromText( __FUNCTION__
);
153 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
154 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
155 $expected = array( $cat->getDBkey() );
156 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
157 $result = $parserOutput->getCategoryLinks();
158 $this->assertEquals( $expected, $result );
164 function testTrackingCategorySpecial() {
165 // Special pages shouldn't have tracking cats.
166 $title = SpecialPage
::getTitleFor( 'Contributions' );
167 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
168 $result = $parserOutput->getCategoryLinks();
169 $this->assertEmpty( $result );