3 class ParserMethodsTest
extends MediaWikiLangTestCase
{
5 public static function providePreSaveTransform() {
7 array( 'hello this is ~~~',
8 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
10 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
11 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
17 * @dataProvider providePreSaveTransform
18 * @covers Parser::preSaveTransform
20 public function testPreSaveTransform( $text, $expected ) {
23 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
25 $user->setName( "127.0.0.1" );
26 $popts = ParserOptions
::newFromUser( $user );
27 $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
29 $this->assertEquals( $expected, $text );
32 public static function provideStripOuterParagraph() {
33 // This mimics the most common use case (stripping paragraphs generated by the parser).
34 $message = new RawMessage( "Message text." );
42 "<p class='foo'>Text.</p>",
43 "<p class='foo'>Text.</p>",
50 "<p>Text.</p><p>More text.</p>",
51 "<p>Text.</p><p>More text.</p>",
61 * @dataProvider provideStripOuterParagraph
62 * @covers Parser::stripOuterParagraph
64 public function testStripOuterParagraph( $text, $expected ) {
65 $this->assertEquals( $expected, Parser
::stripOuterParagraph( $text ) );
69 * @expectedException MWException
70 * @expectedExceptionMessage Parser state cleared while parsing. Did you call Parser::parse recursively?
71 * @covers Parser::lock
73 public function testRecursiveParse() {
75 $title = Title
::newFromText( 'foo' );
76 $po = new ParserOptions
;
77 $wgParser->setHook( 'recursivecallparser', array( $this, 'helperParserFunc' ) );
78 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
81 public function helperParserFunc( $input, $args, $parser ) {
82 $title = Title
::newFromText( 'foo' );
83 $po = new ParserOptions
;
84 $parser->parse( $input, $title, $po );
89 * @covers Parser::callParserFunction
91 public function testCallParserFunction() {
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 array( 'pre', 'foo', 'style' => 'margin-left: 1.6em' )
101 $ret['text'] = $wgParser->mStripState
->unstripBoth( $ret['text'] );
102 $this->assertSame( array(
104 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
105 ), $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
109 * @covers Parser::parse
110 * @covers ParserOutput::getSections
112 public function testGetSections() {
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( array(
124 'fromtitle' => $title->getPrefixedDBkey(),
134 'fromtitle' => false,
135 'byteoffset' => null,
144 'fromtitle' => $title->getPrefixedDBkey(),
148 ), $out->getSections(), 'getSections() with proper value when <h2> is used' );
152 * @dataProvider provideNormalizeLinkUrl
153 * @covers Parser::normalizeLinkUrl
154 * @covers Parser::normalizeUrlComponent
156 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
157 $this->assertEquals( $expected, Parser
::normalizeLinkUrl( $url ), $explanation );
160 public static function provideNormalizeLinkUrl() {
163 'Escaping of unsafe characters',
164 'http://example.org/foo bar?param[]="value"¶m[]=valüe',
165 'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
168 'Case normalization of percent-encoded characters',
169 'http://example.org/%ab%cD%Ef%FF',
170 'http://example.org/%AB%CD%EF%FF',
173 'Unescaping of safe characters',
174 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
175 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
178 'Context-sensitive replacement of sometimes-safe characters',
179 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
180 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
185 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
186 // replaceSection(), getPreloadText()