7 class ParserMethodsTest
extends MediaWikiLangTestCase
{
9 public static function providePreSaveTransform() {
11 array( 'hello this is ~~~',
12 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
14 array( '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. Did you call Parser::parse recursively?
75 * @covers Parser::lock
77 public function testRecursiveParse() {
79 $title = Title
::newFromText( 'foo' );
80 $po = new ParserOptions
;
81 $wgParser->setHook( 'recursivecallparser', array( $this, 'helperParserFunc' ) );
82 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
85 public function helperParserFunc( $input, $args, $parser ) {
86 $title = Title
::newFromText( 'foo' );
87 $po = new ParserOptions
;
88 $parser->parse( $input, $title, $po );
93 * @covers Parser::callParserFunction
95 public function testCallParserFunction() {
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 array( 'pre', 'foo', 'style' => 'margin-left: 1.6em' )
105 $ret['text'] = $wgParser->mStripState
->unstripBoth( $ret['text'] );
106 $this->assertSame( array(
108 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
109 ), $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
113 * @covers Parser::parse
114 * @covers ParserOutput::getSections
116 public function testGetSections() {
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( array(
128 'fromtitle' => $title->getPrefixedDBkey(),
138 'fromtitle' => false,
139 'byteoffset' => null,
148 'fromtitle' => $title->getPrefixedDBkey(),
152 ), $out->getSections(), 'getSections() with proper value when <h2> is used' );
156 * @dataProvider provideNormalizeLinkUrl
157 * @covers Parser::normalizeLinkUrl
158 * @covers Parser::normalizeUrlComponent
160 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
161 $this->assertEquals( $expected, Parser
::normalizeLinkUrl( $url ), $explanation );
164 public static function provideNormalizeLinkUrl() {
167 'Escaping of unsafe characters',
168 'http://example.org/foo bar?param[]="value"¶m[]=valüe',
169 'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
172 'Case normalization of percent-encoded characters',
173 'http://example.org/%ab%cD%Ef%FF',
174 'http://example.org/%AB%CD%EF%FF',
177 'Unescaping of safe characters',
178 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
179 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
182 'Context-sensitive replacement of sometimes-safe characters',
183 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
184 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
189 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
190 // replaceSection(), getPreloadText()