Merge "Move subclasses of LogFormatter to their own file"
[lhc/web/wiklou.git] / tests / phpunit / includes / ExtraParserTest.php
1 <?php
2
3 /**
4 * Parser-related tests that don't suit for parserTests.txt
5 */
6 class ExtraParserTest extends MediaWikiTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
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,
20 ) );
21
22 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
23 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
24 $this->parser = new Parser;
25
26 MagicWord::clearCache();
27 }
28
29 // Bug 8689 - Long numeric lines kill the parser
30 function testBug8689() {
31 global $wgUser;
32 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
33
34 $t = Title::newFromText( 'Unit test' );
35 $options = ParserOptions::newFromUser( $wgUser );
36 $this->assertEquals( "<p>$longLine</p>",
37 $this->parser->parse( $longLine, $t, $options )->getText() );
38 }
39
40 /* Test the parser entry points */
41 function testParse() {
42 $title = Title::newFromText( __FUNCTION__ );
43 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
44 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
45 }
46
47 function testPreSaveTransform() {
48 global $wgUser;
49 $title = Title::newFromText( __FUNCTION__ );
50 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
51
52 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
53 }
54
55 function testPreprocess() {
56 $title = Title::newFromText( __FUNCTION__ );
57 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
58
59 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
60 }
61
62 /**
63 * cleanSig() makes all templates substs and removes tildes
64 */
65 function testCleanSig() {
66 $title = Title::newFromText( __FUNCTION__ );
67 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
68
69 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
70 }
71
72 /**
73 * cleanSig() should do nothing if disabled
74 */
75 function testCleanSigDisabled() {
76 $this->setMwGlobals( 'wgCleanSignatures', false );
77
78 $title = Title::newFromText( __FUNCTION__ );
79 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
80
81 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
82 }
83
84 /**
85 * cleanSigInSig() just removes tildes
86 * @dataProvider provideStringsForCleanSigInSig
87 */
88 function testCleanSigInSig( $in, $out ) {
89 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
90 }
91
92 public static function provideStringsForCleanSigInSig() {
93 return array(
94 array( "{{Foo}} ~~~~", "{{Foo}} " ),
95 array( "~~~", "" ),
96 array( "~~~~~", "" ),
97 );
98 }
99
100 function testGetSection() {
101 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
102 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
103
104 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
105 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
106 }
107
108 function testReplaceSection() {
109 $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" );
110
111 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
112 }
113
114 /**
115 * Templates and comments are not affected, but noinclude/onlyinclude is.
116 */
117 function testGetPreloadText() {
118 $title = Title::newFromText( __FUNCTION__ );
119 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
120
121 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
122 }
123
124 static function statelessFetchTemplate( $title, $parser = false ) {
125 $text = "Content of ''" . $title->getFullText() . "''";
126 $deps = array();
127
128 return array(
129 'text' => $text,
130 'finalTitle' => $title,
131 'deps' => $deps );
132 }
133
134 /**
135 * @group Database
136 */
137 function testTrackingCategory() {
138 $title = Title::newFromText( __FUNCTION__ );
139 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
140 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
141 $expected = array( $cat->getDBkey() );
142 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
143 $result = $parserOutput->getCategoryLinks();
144 $this->assertEquals( $expected, $result );
145 }
146
147 /**
148 * @group Database
149 */
150 function testTrackingCategorySpecial() {
151 // Special pages shouldn't have tracking cats.
152 $title = SpecialPage::getTitleFor( 'Contributions' );
153 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
154 $result = $parserOutput->getCategoryLinks();
155 $this->assertEmpty( $result );
156 }
157 }