Add @covers for RemexStripTagHandler
[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 * @group Database
7 */
8 class ExtraParserTest extends MediaWikiTestCase {
9
10 /** @var ParserOptions */
11 protected $options;
12 /** @var Parser */
13 protected $parser;
14
15 protected function setUp() {
16 parent::setUp();
17
18 $contLang = Language::factory( 'en' );
19 $this->setMwGlobals( [
20 'wgShowDBErrorBacktrace' => true,
21 'wgCleanSignatures' => true,
22 ] );
23 $this->setUserLang( 'en' );
24 $this->setContentLang( $contLang );
25
26 // FIXME: This test should pass without setting global content language
27 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
28 $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
29 $this->parser = new Parser;
30
31 MagicWord::clearCache();
32 }
33
34 /**
35 * @see T10689
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( [ 'unwrap' => true ] ) );
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( [ 'unwrap' => true ] )
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 [
123 [ "{{Foo}} ~~~~", "{{Foo}} " ],
124 [ "~~~", "" ],
125 [ "~~~~~", "" ],
126 ];
127 }
128
129 /**
130 * @covers Parser::getSection
131 */
132 public function testGetSection() {
133 $outputText2 = $this->parser->getSection(
134 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
135 . "Section 2\n== Heading 3 ==\nSection 3\n",
136 2
137 );
138 $outputText1 = $this->parser->getSection(
139 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
140 . "Section 2\n== Heading 3 ==\nSection 3\n",
141 1
142 );
143
144 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
145 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
146 }
147
148 /**
149 * @covers Parser::replaceSection
150 */
151 public function testReplaceSection() {
152 $outputText = $this->parser->replaceSection(
153 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
154 . "Section 2\n== Heading 3 ==\nSection 3\n",
155 1,
156 "New section 1"
157 );
158
159 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
160 }
161
162 /**
163 * Templates and comments are not affected, but noinclude/onlyinclude is.
164 * @covers Parser::getPreloadText
165 */
166 public function testGetPreloadText() {
167 $title = Title::newFromText( __FUNCTION__ );
168 $outputText = $this->parser->getPreloadText(
169 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
170 $title,
171 $this->options
172 );
173
174 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
175 }
176
177 /**
178 * @param Title $title
179 * @param bool $parser
180 *
181 * @return array
182 */
183 static function statelessFetchTemplate( $title, $parser = false ) {
184 $text = "Content of ''" . $title->getFullText() . "''";
185 $deps = [];
186
187 return [
188 'text' => $text,
189 'finalTitle' => $title,
190 'deps' => $deps ];
191 }
192
193 /**
194 * @covers Parser::parse
195 */
196 public function testTrackingCategory() {
197 $title = Title::newFromText( __FUNCTION__ );
198 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
199 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
200 $expected = [ $cat->getDBkey() ];
201 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
202 $result = $parserOutput->getCategoryLinks();
203 $this->assertEquals( $expected, $result );
204 }
205
206 /**
207 * @covers Parser::parse
208 */
209 public function testTrackingCategorySpecial() {
210 // Special pages shouldn't have tracking cats.
211 $title = SpecialPage::getTitleFor( 'Contributions' );
212 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
213 $result = $parserOutput->getCategoryLinks();
214 $this->assertEmpty( $result );
215 }
216 }