Merge "Fix "UTPage" creation in tests"
[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 * @see Bug 8689
36 * @covers Parser::parse
37 */
38 public function testLongNumericLinesDontKillTheParser() {
39 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
40
41 $title = Title::newFromText( 'Unit test' );
42 $options = ParserOptions::newFromUser( new User() );
43 $this->assertEquals( "<p>$longLine</p>",
44 $this->parser->parse( $longLine, $title, $options )->getText() );
45 }
46
47 /**
48 * Test the parser entry points
49 * @covers Parser::parse
50 */
51 public function testParse() {
52 $title = Title::newFromText( __FUNCTION__ );
53 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
54 $this->assertEquals(
55 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
56 $parserOutput->getText()
57 );
58 }
59
60 /**
61 * @covers Parser::preSaveTransform
62 */
63 public function testPreSaveTransform() {
64 $title = Title::newFromText( __FUNCTION__ );
65 $outputText = $this->parser->preSaveTransform(
66 "Test\r\n{{subst:Foo}}\n{{Bar}}",
67 $title,
68 new User(),
69 $this->options
70 );
71
72 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
73 }
74
75 /**
76 * @covers Parser::preprocess
77 */
78 public function testPreprocess() {
79 $title = Title::newFromText( __FUNCTION__ );
80 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
81
82 $this->assertEquals(
83 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
84 $outputText
85 );
86 }
87
88 /**
89 * cleanSig() makes all templates substs and removes tildes
90 * @covers Parser::cleanSig
91 */
92 public function testCleanSig() {
93 $title = Title::newFromText( __FUNCTION__ );
94 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
95
96 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
97 }
98
99 /**
100 * cleanSig() should do nothing if disabled
101 * @covers Parser::cleanSig
102 */
103 public function testCleanSigDisabled() {
104 $this->setMwGlobals( 'wgCleanSignatures', false );
105
106 $title = Title::newFromText( __FUNCTION__ );
107 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
108
109 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
110 }
111
112 /**
113 * cleanSigInSig() just removes tildes
114 * @dataProvider provideStringsForCleanSigInSig
115 * @covers Parser::cleanSigInSig
116 */
117 public function testCleanSigInSig( $in, $out ) {
118 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
119 }
120
121 public static function provideStringsForCleanSigInSig() {
122 return array(
123 array( "{{Foo}} ~~~~", "{{Foo}} " ),
124 array( "~~~", "" ),
125 array( "~~~~~", "" ),
126 );
127 }
128
129 /**
130 * @covers Parser::getSection
131 */
132 public function testGetSection() {
133 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
134 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
135
136 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
137 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
138 }
139
140 /**
141 * @covers Parser::replaceSection
142 */
143 public function testReplaceSection() {
144 $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" );
145
146 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
147 }
148
149 /**
150 * Templates and comments are not affected, but noinclude/onlyinclude is.
151 * @covers Parser::getPreloadText
152 */
153 public function testGetPreloadText() {
154 $title = Title::newFromText( __FUNCTION__ );
155 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
156
157 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
158 }
159
160 /**
161 * @param Title $title
162 * @param bool $parser
163 *
164 * @return array
165 */
166 static function statelessFetchTemplate( $title, $parser = false ) {
167 $text = "Content of ''" . $title->getFullText() . "''";
168 $deps = array();
169
170 return array(
171 'text' => $text,
172 'finalTitle' => $title,
173 'deps' => $deps );
174 }
175
176 /**
177 * @group Database
178 * @covers Parser::parse
179 */
180 public function testTrackingCategory() {
181 $title = Title::newFromText( __FUNCTION__ );
182 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
183 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
184 $expected = array( $cat->getDBkey() );
185 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
186 $result = $parserOutput->getCategoryLinks();
187 $this->assertEquals( $expected, $result );
188 }
189
190 /**
191 * @group Database
192 * @covers Parser::parse
193 */
194 public function testTrackingCategorySpecial() {
195 // Special pages shouldn't have tracking cats.
196 $title = SpecialPage::getTitleFor( 'Contributions' );
197 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
198 $result = $parserOutput->getCategoryLinks();
199 $this->assertEmpty( $result );
200 }
201 }