7 class ParserMethodsTest
extends MediaWikiLangTestCase
{
9 public static function providePreSaveTransform() {
11 [ 'hello this is ~~~',
12 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
14 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
15 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
21 * @dataProvider providePreSaveTransform
22 * @covers Parser::preSaveTransform
24 public function testPreSaveTransform( $text, $expected ) {
27 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
29 $user->setName( "127.0.0.1" );
30 $popts = ParserOptions
::newFromUser( $user );
31 $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
33 $this->assertEquals( $expected, $text );
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." );
46 "<p class='foo'>Text.</p>",
47 "<p class='foo'>Text.</p>",
54 "<p>Text.</p><p>More text.</p>",
55 "<p>Text.</p><p>More text.</p>",
65 * @dataProvider provideStripOuterParagraph
66 * @covers Parser::stripOuterParagraph
68 public function testStripOuterParagraph( $text, $expected ) {
69 $this->assertEquals( $expected, Parser
::stripOuterParagraph( $text ) );
73 * @expectedException MWException
74 * @expectedExceptionMessage Parser state cleared while parsing.
75 * Did you call Parser::parse recursively?
76 * @covers Parser::lock
78 public function testRecursiveParse() {
80 $title = Title
::newFromText( 'foo' );
81 $po = new ParserOptions
;
82 $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
83 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
86 public function helperParserFunc( $input, $args, $parser ) {
87 $title = Title
::newFromText( 'foo' );
88 $po = new ParserOptions
;
89 $parser->parse( $input, $title, $po );
94 * @covers Parser::callParserFunction
96 public function testCallParserFunction() {
99 // Normal parses test passing PPNodes. Test passing an array.
100 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
101 $wgParser->startExternalParse( $title, new ParserOptions(), Parser
::OT_HTML
);
102 $frame = $wgParser->getPreprocessor()->newFrame();
103 $ret = $wgParser->callParserFunction( $frame, '#tag',
104 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
106 $ret['text'] = $wgParser->mStripState
->unstripBoth( $ret['text'] );
109 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
110 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
114 * @covers Parser::parse
115 * @covers ParserOutput::getSections
117 public function testGetSections() {
120 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
121 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
129 'fromtitle' => $title->getPrefixedDBkey(),
139 'fromtitle' => false,
140 'byteoffset' => null,
149 'fromtitle' => $title->getPrefixedDBkey(),
153 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
157 * @dataProvider provideNormalizeLinkUrl
158 * @covers Parser::normalizeLinkUrl
159 * @covers Parser::normalizeUrlComponent
161 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
162 $this->assertEquals( $expected, Parser
::normalizeLinkUrl( $url ), $explanation );
165 public static function provideNormalizeLinkUrl() {
168 'Escaping of unsafe characters',
169 'http://example.org/foo bar?param[]="value"¶m[]=valüe',
170 'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
173 'Case normalization of percent-encoded characters',
174 'http://example.org/%ab%cD%Ef%FF',
175 'http://example.org/%AB%CD%EF%FF',
178 'Unescaping of safe characters',
179 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
180 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
183 'Context-sensitive replacement of sometimes-safe characters',
184 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
185 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
190 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
191 // replaceSection(), getPreloadText()