MediaWiki REL1_31
ParserMethodsTest.php
Go to the documentation of this file.
1<?php
2
9
10 public static function providePreSaveTransform() {
11 return [
12 [ 'hello this is ~~~',
13 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
14 ],
15 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
16 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
17 ],
18 ];
19 }
20
24 public function testPreSaveTransform( $text, $expected ) {
25 global $wgParser;
26
27 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
28 $user = new User();
29 $user->setName( "127.0.0.1" );
30 $popts = ParserOptions::newFromUser( $user );
31 $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
32
33 $this->assertEquals( $expected, $text );
34 }
35
36 public static function provideStripOuterParagraph() {
37 // This mimics the most common use case (stripping paragraphs generated by the parser).
38 $message = new RawMessage( "Message text." );
39
40 return [
41 [
42 "<p>Text.</p>",
43 "Text.",
44 ],
45 [
46 "<p class='foo'>Text.</p>",
47 "<p class='foo'>Text.</p>",
48 ],
49 [
50 "<p>Text.\n</p>\n",
51 "Text.",
52 ],
53 [
54 "<p>Text.</p><p>More text.</p>",
55 "<p>Text.</p><p>More text.</p>",
56 ],
57 [
58 $message->parse(),
59 "Message text.",
60 ],
61 ];
62 }
63
67 public function testStripOuterParagraph( $text, $expected ) {
68 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
69 }
70
76 public function testRecursiveParse() {
77 global $wgParser;
78 $title = Title::newFromText( 'foo' );
79 $po = new ParserOptions;
80 $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
81 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
82 }
83
84 public function helperParserFunc( $input, $args, $parser ) {
85 $title = Title::newFromText( 'foo' );
86 $po = new ParserOptions;
87 $parser->parse( $input, $title, $po );
88 return 'bar';
89 }
90
91 public function testCallParserFunction() {
92 global $wgParser;
93
94 // Normal parses test passing PPNodes. Test passing an array.
95 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
96 $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
97 $frame = $wgParser->getPreprocessor()->newFrame();
98 $ret = $wgParser->callParserFunction( $frame, '#tag',
99 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
100 );
101 $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
102 $this->assertSame( [
103 'found' => true,
104 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
105 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
106 }
107
112 public function testGetSections() {
113 global $wgParser;
114
115 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
116 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
117 $this->assertSame( [
118 [
119 'toclevel' => 1,
120 'level' => '2',
121 'line' => 'foo',
122 'number' => '1',
123 'index' => '1',
124 'fromtitle' => $title->getPrefixedDBkey(),
125 'byteoffset' => 0,
126 'anchor' => 'foo',
127 ],
128 [
129 'toclevel' => 1,
130 'level' => '2',
131 'line' => 'bar',
132 'number' => '2',
133 'index' => '',
134 'fromtitle' => false,
135 'byteoffset' => null,
136 'anchor' => 'bar',
137 ],
138 [
139 'toclevel' => 1,
140 'level' => '2',
141 'line' => 'baz',
142 'number' => '3',
143 'index' => '2',
144 'fromtitle' => $title->getPrefixedDBkey(),
145 'byteoffset' => 21,
146 'anchor' => 'baz',
147 ],
148 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
149 }
150
154 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
155 $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
156 }
157
158 public static function provideNormalizeLinkUrl() {
159 return [
160 [
161 'Escaping of unsafe characters',
162 'http://example.org/foo bar?param[]="value"&param[]=valüe',
163 'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
164 ],
165 [
166 'Case normalization of percent-encoded characters',
167 'http://example.org/%ab%cD%Ef%FF',
168 'http://example.org/%AB%CD%EF%FF',
169 ],
170 [
171 'Unescaping of safe characters',
172 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
173 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
174 ],
175 [
176 'Context-sensitive replacement of sometimes-safe characters',
177 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
178 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
179 ],
180 ];
181 }
182
183 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
184 // replaceSection(), getPreloadText()
185}
$wgParser
Definition Setup.php:917
if( $line===false) $args
Definition cdb.php:64
Base class that store and restore the Language objects.
Database Parser BlockLevelPass.
static provideStripOuterParagraph()
testNormalizeLinkUrl( $explanation, $url, $expected)
provideNormalizeLinkUrl
testGetSections()
Parser ParserOutput::getSections.
testStripOuterParagraph( $text, $expected)
provideStripOuterParagraph
testPreSaveTransform( $text, $expected)
providePreSaveTransform
helperParserFunc( $input, $args, $parser)
testRecursiveParse()
MWException Parser state cleared while parsing.
Set options of the Parser.
Variant of the Message class.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2603
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:2005
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:864
if(is_array($mode)) switch( $mode) $input