MediaWiki REL1_33
ParserMethodsTest.php
Go to the documentation of this file.
1<?php
6
13
14 public static function providePreSaveTransform() {
15 return [
16 [ 'hello this is ~~~',
17 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
18 ],
19 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
20 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
21 ],
22 ];
23 }
24
28 public function testPreSaveTransform( $text, $expected ) {
29 global $wgParser;
30
31 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
32 $user = new User();
33 $user->setName( "127.0.0.1" );
34 $popts = ParserOptions::newFromUser( $user );
35 $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
36
37 $this->assertEquals( $expected, $text );
38 }
39
40 public static function provideStripOuterParagraph() {
41 // This mimics the most common use case (stripping paragraphs generated by the parser).
42 $message = new RawMessage( "Message text." );
43
44 return [
45 [
46 "<p>Text.</p>",
47 "Text.",
48 ],
49 [
50 "<p class='foo'>Text.</p>",
51 "<p class='foo'>Text.</p>",
52 ],
53 [
54 "<p>Text.\n</p>\n",
55 "Text.",
56 ],
57 [
58 "<p>Text.</p><p>More text.</p>",
59 "<p>Text.</p><p>More text.</p>",
60 ],
61 [
62 $message->parse(),
63 "Message text.",
64 ],
65 ];
66 }
67
71 public function testStripOuterParagraph( $text, $expected ) {
72 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
73 }
74
80 public function testRecursiveParse() {
81 global $wgParser;
82 $title = Title::newFromText( 'foo' );
83 $po = new ParserOptions;
84 $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
85 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
86 }
87
88 public function helperParserFunc( $input, $args, $parser ) {
89 $title = Title::newFromText( 'foo' );
90 $po = new ParserOptions;
91 $parser->parse( $input, $title, $po );
92 return 'bar';
93 }
94
95 public function testCallParserFunction() {
96 global $wgParser;
97
98 // Normal parses test passing PPNodes. Test passing an array.
99 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
100 $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
101 $frame = $wgParser->getPreprocessor()->newFrame();
102 $ret = $wgParser->callParserFunction( $frame, '#tag',
103 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
104 );
105 $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
106 $this->assertSame( [
107 'found' => true,
108 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
109 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
110 }
111
116 public function testGetSections() {
117 global $wgParser;
118
119 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
120 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
121 $this->assertSame( [
122 [
123 'toclevel' => 1,
124 'level' => '2',
125 'line' => 'foo',
126 'number' => '1',
127 'index' => '1',
128 'fromtitle' => $title->getPrefixedDBkey(),
129 'byteoffset' => 0,
130 'anchor' => 'foo',
131 ],
132 [
133 'toclevel' => 1,
134 'level' => '2',
135 'line' => 'bar',
136 'number' => '2',
137 'index' => '',
138 'fromtitle' => false,
139 'byteoffset' => null,
140 'anchor' => 'bar',
141 ],
142 [
143 'toclevel' => 1,
144 'level' => '2',
145 'line' => 'baz',
146 'number' => '3',
147 'index' => '2',
148 'fromtitle' => $title->getPrefixedDBkey(),
149 'byteoffset' => 21,
150 'anchor' => 'baz',
151 ],
152 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
153 }
154
158 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
159 $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
160 }
161
162 public static function provideNormalizeLinkUrl() {
163 return [
164 [
165 'Escaping of unsafe characters',
166 'http://example.org/foo bar?param[]="value"&param[]=valüe',
167 'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
168 ],
169 [
170 'Case normalization of percent-encoded characters',
171 'http://example.org/%ab%cD%Ef%FF',
172 'http://example.org/%AB%CD%EF%FF',
173 ],
174 [
175 'Unescaping of safe characters',
176 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
177 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
178 ],
179 [
180 'Context-sensitive replacement of sometimes-safe characters',
181 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
182 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
183 ],
184 [
185 'IPv6 links aren\'t escaped',
186 'http://[::1]/foobar',
187 'http://[::1]/foobar',
188 ],
189 [
190 'non-IPv6 links aren\'t unescaped',
191 'http://%5B::1%5D/foobar',
192 'http://%5B::1%5D/foobar',
193 ],
194 ];
195 }
196
197 public function testWrapOutput() {
198 global $wgParser;
199 $title = Title::newFromText( 'foo' );
200 $po = new ParserOptions();
201 $wgParser->parse( 'Hello World', $title, $po );
202 $text = $wgParser->getOutput()->getText();
203
204 $this->assertContains( 'Hello World', $text );
205 $this->assertContains( '<div', $text );
206 $this->assertContains( 'class="mw-parser-output"', $text );
207 }
208
213 private function getMockTitle( $name ) {
214 $title = $this->getMock( Title::class );
215 $title->method( 'getPrefixedDBkey' )->willReturn( $name );
216 $title->method( 'getPrefixedText' )->willReturn( $name );
217 $title->method( 'getDBkey' )->willReturn( $name );
218 $title->method( 'getText' )->willReturn( $name );
219 $title->method( 'getNamespace' )->willReturn( 0 );
220 $title->method( 'getPageLanguage' )->willReturn( Language::factory( 'en' ) );
221
222 return $title;
223 }
224
225 public function provideRevisionAccess() {
226 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
227
228 $frank = $this->getMockBuilder( User::class )
229 ->disableOriginalConstructor()
230 ->getMock();
231
232 $frank->method( 'getName' )->willReturn( 'Frank' );
233
234 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
235 $po = new ParserOptions( $frank );
236
237 yield 'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
238 yield 'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
239
240 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
241 $po = new ParserOptions( $frank );
242
243 yield 'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
244
245 $oldRevision = new MutableRevisionRecord( $title );
246 $oldRevision->setId( 100 );
247 $oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor', 0 ) );
248 $oldRevision->setTimestamp( '20141111111111' );
249 $oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'FAUX' ) );
250
251 $po = new ParserOptions( $frank );
252 $po->setCurrentRevisionCallback( function () use ( $oldRevision ) {
253 return new Revision( $oldRevision );
254 } );
255
256 yield 'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
257
258 $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
259
260 $po = new ParserOptions( $frank );
261 $po->setIsPreview( true );
262
263 yield 'preview without override, using context' => [
264 $text,
265 $po,
266 null,
267 'user:Frank;',
268 'user-subst:Frank;',
269 ];
270
271 $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
272 . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
273
274 $newRevision = new MutableRevisionRecord( $title );
275 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
276 $newRevision->setTimestamp( '20180808000000' );
277 $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'NEW' ) );
278
279 $po = new ParserOptions( $frank );
280 $po->setIsPreview( true );
281 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
282 return new Revision( $newRevision );
283 } );
284
285 yield 'preview' => [
286 $text,
287 $po,
288 null,
289 'user:NewAuthor;time:20180808000000;',
290 'user-subst:NewAuthor;time-subst:20180808000000;',
291 ];
292
293 $po = new ParserOptions( $frank );
294 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
295 return new Revision( $newRevision );
296 } );
297
298 yield 'pre-save' => [
299 $text,
300 $po,
301 null,
302 'user:NewAuthor;time:20180808000000;',
303 'user-subst:NewAuthor;time-subst:20180808000000;',
304 ];
305
306 $text = "(ONE)<includeonly>(TWO)</includeonly>"
307 . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
308
309 $newRevision = new MutableRevisionRecord( $title );
310 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
311 $newRevision->setTimestamp( '20180808000000' );
312 $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
313
314 $po = new ParserOptions( $frank );
315 $po->setIsPreview( true );
316 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
317 return new Revision( $newRevision );
318 } );
319
320 yield 'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
321 }
322
326 public function testRevisionAccess(
327 $text,
328 ParserOptions $po,
329 $revId,
330 $expectedInHtml,
331 $expectedInPst = null
332 ) {
333 global $wgParser;
334
335 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
336
337 $po->enableLimitReport( false );
338
339 $oldRevision = new MutableRevisionRecord( $title );
340 $oldRevision->setId( 100 );
341 $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor', 0 ) );
342 $oldRevision->setTimestamp( '20140404000000' );
343 $oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'OLD' ) );
344
345 $currentRevision = new MutableRevisionRecord( $title );
346 $currentRevision->setId( 200 );
347 $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor', 0 ) );
348 $currentRevision->setTimestamp( '20160606000000' );
349 $currentRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'CURRENT' ) );
350
351 $revisionStore = $this->getMockBuilder( RevisionStore::class )
352 ->disableOriginalConstructor()
353 ->getMock();
354
355 $revisionStore
356 ->method( 'getKnownCurrentRevision' )
357 ->willReturnMap( [
358 [ $title, 100, $oldRevision ],
359 [ $title, 200, $currentRevision ],
360 [ $title, 0, $currentRevision ],
361 ] );
362
363 $revisionStore
364 ->method( 'getRevisionById' )
365 ->willReturnMap( [
366 [ 100, 0, $oldRevision ],
367 [ 200, 0, $currentRevision ],
368 ] );
369
370 $this->setService( 'RevisionStore', $revisionStore );
371
372 $wgParser->parse( $text, $title, $po, true, true, $revId );
373 $html = $wgParser->getOutput()->getText();
374
375 $this->assertContains( $expectedInHtml, $html, 'In HTML' );
376
377 if ( $expectedInPst !== null ) {
378 $pst = $wgParser->preSaveTransform( $text, $title, $po->getUser(), $po );
379 $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' );
380 }
381 }
382
383 public static function provideGuessSectionNameFromWikiText() {
384 return [
385 [ '1/2', 'html5', '#1/2' ],
386 [ '1/2', 'legacy', '#1.2F2' ],
387 ];
388 }
389
391 public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) {
392 $this->setMwGlobals( [ 'wgFragmentMode' => [ $mode ] ] );
393 global $wgParser;
394 $result = $wgParser->guessSectionNameFromWikiText( $input );
395 $this->assertEquals( $result, $expected );
396 }
397
398 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
399 // replaceSection(), getPreloadText()
400}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$wgParser
Definition Setup.php:886
if( $line===false) $args
Definition cdb.php:64
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Mutable RevisionRecord implementation, for building new revision entries programmatically.
Service for looking up page revisions.
Value object representing a content slot associated with a page revision.
Value object representing a user's identity.
Database Parser BlockLevelPass.
static provideStripOuterParagraph()
static provideGuessSectionNameFromWikiText()
testNormalizeLinkUrl( $explanation, $url, $expected)
provideNormalizeLinkUrl
testGetSections()
Parser ParserOutput::getSections.
testStripOuterParagraph( $text, $expected)
provideStripOuterParagraph
testPreSaveTransform( $text, $expected)
providePreSaveTransform
testRevisionAccess( $text, ParserOptions $po, $revId, $expectedInHtml, $expectedInPst=null)
provideRevisionAccess
testGuessSectionNameFromWikiText( $input, $mode, $expected)
provideGuessSectionNameFromWikiText
helperParserFunc( $input, $args, $parser)
testRecursiveParse()
MWException Parser state cleared while parsing.
Set options of the Parser.
enableLimitReport( $x=true)
Enable limit report in an HTML comment on output.
Variant of the Message class.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
Content object for wiki text pages.
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition hooks.txt:1834
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:855
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2011
if(is_array($mode)) switch( $mode) $input