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