Merge "Linker: more consistent whitespace parsing in formatLinksInComment"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserMethodsTest.php
1 <?php
2 use MediaWiki\Storage\MutableRevisionRecord;
3 use MediaWiki\Storage\RevisionStore;
4 use MediaWiki\User\UserIdentityValue;
5
6 /**
7 * @group Database
8 * @covers Parser
9 * @covers BlockLevelPass
10 */
11 class ParserMethodsTest extends MediaWikiLangTestCase {
12
13 public static function providePreSaveTransform() {
14 return [
15 [ 'hello this is ~~~',
16 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
17 ],
18 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
19 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
20 ],
21 ];
22 }
23
24 /**
25 * @dataProvider providePreSaveTransform
26 */
27 public function testPreSaveTransform( $text, $expected ) {
28 global $wgParser;
29
30 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
31 $user = new User();
32 $user->setName( "127.0.0.1" );
33 $popts = ParserOptions::newFromUser( $user );
34 $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
35
36 $this->assertEquals( $expected, $text );
37 }
38
39 public static function provideStripOuterParagraph() {
40 // This mimics the most common use case (stripping paragraphs generated by the parser).
41 $message = new RawMessage( "Message text." );
42
43 return [
44 [
45 "<p>Text.</p>",
46 "Text.",
47 ],
48 [
49 "<p class='foo'>Text.</p>",
50 "<p class='foo'>Text.</p>",
51 ],
52 [
53 "<p>Text.\n</p>\n",
54 "Text.",
55 ],
56 [
57 "<p>Text.</p><p>More text.</p>",
58 "<p>Text.</p><p>More text.</p>",
59 ],
60 [
61 $message->parse(),
62 "Message text.",
63 ],
64 ];
65 }
66
67 /**
68 * @dataProvider provideStripOuterParagraph
69 */
70 public function testStripOuterParagraph( $text, $expected ) {
71 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
72 }
73
74 /**
75 * @expectedException MWException
76 * @expectedExceptionMessage Parser state cleared while parsing.
77 * Did you call Parser::parse recursively?
78 */
79 public function testRecursiveParse() {
80 global $wgParser;
81 $title = Title::newFromText( 'foo' );
82 $po = new ParserOptions;
83 $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
84 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
85 }
86
87 public function helperParserFunc( $input, $args, $parser ) {
88 $title = Title::newFromText( 'foo' );
89 $po = new ParserOptions;
90 $parser->parse( $input, $title, $po );
91 return 'bar';
92 }
93
94 public function testCallParserFunction() {
95 global $wgParser;
96
97 // Normal parses test passing PPNodes. Test passing an array.
98 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
99 $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
100 $frame = $wgParser->getPreprocessor()->newFrame();
101 $ret = $wgParser->callParserFunction( $frame, '#tag',
102 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
103 );
104 $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
105 $this->assertSame( [
106 'found' => true,
107 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
108 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
109 }
110
111 /**
112 * @covers Parser
113 * @covers ParserOutput::getSections
114 */
115 public function testGetSections() {
116 global $wgParser;
117
118 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
119 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
120 $this->assertSame( [
121 [
122 'toclevel' => 1,
123 'level' => '2',
124 'line' => 'foo',
125 'number' => '1',
126 'index' => '1',
127 'fromtitle' => $title->getPrefixedDBkey(),
128 'byteoffset' => 0,
129 'anchor' => 'foo',
130 ],
131 [
132 'toclevel' => 1,
133 'level' => '2',
134 'line' => 'bar',
135 'number' => '2',
136 'index' => '',
137 'fromtitle' => false,
138 'byteoffset' => null,
139 'anchor' => 'bar',
140 ],
141 [
142 'toclevel' => 1,
143 'level' => '2',
144 'line' => 'baz',
145 'number' => '3',
146 'index' => '2',
147 'fromtitle' => $title->getPrefixedDBkey(),
148 'byteoffset' => 21,
149 'anchor' => 'baz',
150 ],
151 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
152 }
153
154 /**
155 * @dataProvider provideNormalizeLinkUrl
156 */
157 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
158 $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
159 }
160
161 public static function provideNormalizeLinkUrl() {
162 return [
163 [
164 'Escaping of unsafe characters',
165 'http://example.org/foo bar?param[]="value"&param[]=valüe',
166 'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
167 ],
168 [
169 'Case normalization of percent-encoded characters',
170 'http://example.org/%ab%cD%Ef%FF',
171 'http://example.org/%AB%CD%EF%FF',
172 ],
173 [
174 'Unescaping of safe characters',
175 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
176 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
177 ],
178 [
179 'Context-sensitive replacement of sometimes-safe characters',
180 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
181 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
182 ],
183 ];
184 }
185
186 public function testWrapOutput() {
187 global $wgParser;
188 $title = Title::newFromText( 'foo' );
189 $po = new ParserOptions();
190 $wgParser->parse( 'Hello World', $title, $po );
191 $text = $wgParser->getOutput()->getText();
192
193 $this->assertContains( 'Hello World', $text );
194 $this->assertContains( '<div', $text );
195 $this->assertContains( 'class="mw-parser-output"', $text );
196 }
197
198 /**
199 * @param string $name
200 * @return Title
201 */
202 private function getMockTitle( $name ) {
203 $title = $this->getMock( Title::class );
204 $title->method( 'getPrefixedDBkey' )->willReturn( $name );
205 $title->method( 'getPrefixedText' )->willReturn( $name );
206 $title->method( 'getDBkey' )->willReturn( $name );
207 $title->method( 'getText' )->willReturn( $name );
208 $title->method( 'getNamespace' )->willReturn( 0 );
209 $title->method( 'getPageLanguage' )->willReturn( Language::factory( 'en' ) );
210
211 return $title;
212 }
213
214 public function provideRevisionAccess() {
215 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
216
217 $frank = $this->getMockBuilder( User::class )
218 ->disableOriginalConstructor()
219 ->getMock();
220
221 $frank->method( 'getName' )->willReturn( 'Frank' );
222
223 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
224 $po = new ParserOptions( $frank );
225
226 yield 'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
227 yield 'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
228
229 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
230 $po = new ParserOptions( $frank );
231
232 yield 'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
233
234 $oldRevision = new MutableRevisionRecord( $title );
235 $oldRevision->setId( 100 );
236 $oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor', 0 ) );
237 $oldRevision->setTimestamp( '20141111111111' );
238 $oldRevision->setContent( 'main', new WikitextContent( 'FAUX' ) );
239
240 $po = new ParserOptions( $frank );
241 $po->setCurrentRevisionCallback( function () use ( $oldRevision ) {
242 return new Revision( $oldRevision );
243 } );
244
245 yield 'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
246
247 $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
248
249 $po = new ParserOptions( $frank );
250 $po->setIsPreview( true );
251
252 yield 'preview without override, using context' => [
253 $text,
254 $po,
255 null,
256 'user:Frank;',
257 'user-subst:Frank;',
258 ];
259
260 $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
261 . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
262
263 $newRevision = new MutableRevisionRecord( $title );
264 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
265 $newRevision->setTimestamp( '20180808000000' );
266 $newRevision->setContent( 'main', new WikitextContent( 'NEW' ) );
267
268 $po = new ParserOptions( $frank );
269 $po->setIsPreview( true );
270 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
271 return new Revision( $newRevision );
272 } );
273
274 yield 'preview' => [
275 $text,
276 $po,
277 null,
278 'user:NewAuthor;time:20180808000000;',
279 'user-subst:NewAuthor;time-subst:20180808000000;',
280 ];
281
282 $po = new ParserOptions( $frank );
283 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
284 return new Revision( $newRevision );
285 } );
286
287 yield 'pre-save' => [
288 $text,
289 $po,
290 null,
291 'user:NewAuthor;time:20180808000000;',
292 'user-subst:NewAuthor;time-subst:20180808000000;',
293 ];
294
295 $text = "(ONE)<includeonly>(TWO)</includeonly>"
296 . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
297
298 $newRevision = new MutableRevisionRecord( $title );
299 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
300 $newRevision->setTimestamp( '20180808000000' );
301 $newRevision->setContent( 'main', new WikitextContent( $text ) );
302
303 $po = new ParserOptions( $frank );
304 $po->setIsPreview( true );
305 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
306 return new Revision( $newRevision );
307 } );
308
309 yield 'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
310 }
311
312 /**
313 * @dataProvider provideRevisionAccess
314 */
315 public function testRevisionAccess(
316 $text,
317 ParserOptions $po,
318 $revId,
319 $expectedInHtml,
320 $expectedInPst = null
321 ) {
322 global $wgParser;
323
324 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
325
326 $po->enableLimitReport( false );
327
328 $oldRevision = new MutableRevisionRecord( $title );
329 $oldRevision->setId( 100 );
330 $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor', 0 ) );
331 $oldRevision->setTimestamp( '20140404000000' );
332 $oldRevision->setContent( 'main', new WikitextContent( 'OLD' ) );
333
334 $currentRevision = new MutableRevisionRecord( $title );
335 $currentRevision->setId( 200 );
336 $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor', 0 ) );
337 $currentRevision->setTimestamp( '20160606000000' );
338 $currentRevision->setContent( 'main', new WikitextContent( 'CURRENT' ) );
339
340 $revisionStore = $this->getMockBuilder( RevisionStore::class )
341 ->disableOriginalConstructor()
342 ->getMock();
343
344 $revisionStore
345 ->method( 'getKnownCurrentRevision' )
346 ->willReturnMap( [
347 [ $title, 100, $oldRevision ],
348 [ $title, 200, $currentRevision ],
349 [ $title, 0, $currentRevision ],
350 ] );
351
352 $revisionStore
353 ->method( 'getRevisionById' )
354 ->willReturnMap( [
355 [ 100, 0, $oldRevision ],
356 [ 200, 0, $currentRevision ],
357 ] );
358
359 $this->setService( 'RevisionStore', $revisionStore );
360
361 $wgParser->parse( $text, $title, $po, true, true, $revId );
362 $html = $wgParser->getOutput()->getText();
363
364 $this->assertContains( $expectedInHtml, $html, 'In HTML' );
365
366 if ( $expectedInPst !== null ) {
367 $pst = $wgParser->preSaveTransform( $text, $title, $po->getUser(), $po );
368 $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' );
369 }
370 }
371
372 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
373 // replaceSection(), getPreloadText()
374 }