Merge "FormatJson: Remove whitespace from empty arrays and objects"
[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 /** @var ParserOptions */
9 protected $options;
10 /** @var Parser */
11 protected $parser;
12
13 protected function setUp() {
14 parent::setUp();
15
16 $contLang = Language::factory( 'en' );
17 $this->setMwGlobals( array(
18 'wgShowDBErrorBacktrace' => true,
19 'wgLanguageCode' => 'en',
20 'wgContLang' => $contLang,
21 'wgLang' => Language::factory( 'en' ),
22 'wgMemc' => new EmptyBagOStuff,
23 'wgAlwaysUseTidy' => false,
24 'wgCleanSignatures' => true,
25 ) );
26
27 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
28 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
29 $this->parser = new Parser;
30
31 MagicWord::clearCache();
32 }
33
34 /**
35 * Bug 8689 - Long numeric lines kill the parser
36 * @covers Parser::parse
37 */
38 public function testBug8689() {
39 global $wgUser;
40 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
41
42 $t = Title::newFromText( 'Unit test' );
43 $options = ParserOptions::newFromUser( $wgUser );
44 $this->assertEquals( "<p>$longLine</p>",
45 $this->parser->parse( $longLine, $t, $options )->getText() );
46 }
47
48 /**
49 * Test the parser entry points
50 * @covers Parser::parse
51 */
52 public function testParse() {
53 $title = Title::newFromText( __FUNCTION__ );
54 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
55 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
56 }
57
58 /**
59 * @covers Parser::preSaveTransform
60 */
61 public function testPreSaveTransform() {
62 global $wgUser;
63 $title = Title::newFromText( __FUNCTION__ );
64 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
65
66 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
67 }
68
69 /**
70 * @covers Parser::preprocess
71 */
72 public function testPreprocess() {
73 $title = Title::newFromText( __FUNCTION__ );
74 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
75
76 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
77 }
78
79 /**
80 * cleanSig() makes all templates substs and removes tildes
81 * @covers Parser::cleanSig
82 */
83 public function testCleanSig() {
84 $title = Title::newFromText( __FUNCTION__ );
85 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
86
87 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
88 }
89
90 /**
91 * cleanSig() should do nothing if disabled
92 * @covers Parser::cleanSig
93 */
94 public function testCleanSigDisabled() {
95 $this->setMwGlobals( 'wgCleanSignatures', false );
96
97 $title = Title::newFromText( __FUNCTION__ );
98 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
99
100 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
101 }
102
103 /**
104 * cleanSigInSig() just removes tildes
105 * @dataProvider provideStringsForCleanSigInSig
106 * @covers Parser::cleanSigInSig
107 */
108 public function testCleanSigInSig( $in, $out ) {
109 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
110 }
111
112 public static function provideStringsForCleanSigInSig() {
113 return array(
114 array( "{{Foo}} ~~~~", "{{Foo}} " ),
115 array( "~~~", "" ),
116 array( "~~~~~", "" ),
117 );
118 }
119
120 /**
121 * @covers Parser::getSection
122 */
123 public function testGetSection() {
124 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
125 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
126
127 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
128 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
129 }
130
131 /**
132 * @covers Parser::replaceSection
133 */
134 public function testReplaceSection() {
135 $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" );
136
137 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
138 }
139
140 /**
141 * Templates and comments are not affected, but noinclude/onlyinclude is.
142 * @covers Parser::getPreloadText
143 */
144 public function testGetPreloadText() {
145 $title = Title::newFromText( __FUNCTION__ );
146 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
147
148 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
149 }
150
151 static function statelessFetchTemplate( $title, $parser = false ) {
152 $text = "Content of ''" . $title->getFullText() . "''";
153 $deps = array();
154
155 return array(
156 'text' => $text,
157 'finalTitle' => $title,
158 'deps' => $deps );
159 }
160
161 /**
162 * @group Database
163 * @covers Parser::parse
164 */
165 public function testTrackingCategory() {
166 $title = Title::newFromText( __FUNCTION__ );
167 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
168 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
169 $expected = array( $cat->getDBkey() );
170 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
171 $result = $parserOutput->getCategoryLinks();
172 $this->assertEquals( $expected, $result );
173 }
174
175 /**
176 * @group Database
177 * @covers Parser::parse
178 */
179 public function testTrackingCategorySpecial() {
180 // Special pages shouldn't have tracking cats.
181 $title = SpecialPage::getTitleFor( 'Contributions' );
182 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
183 $result = $parserOutput->getCategoryLinks();
184 $this->assertEmpty( $result );
185 }
186 }