MediaWiki  REL1_24
ParserMethodsTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class ParserMethodsTest extends MediaWikiLangTestCase {
00004 
00005     public static function providePreSaveTransform() {
00006         return array(
00007             array( 'hello this is ~~~',
00008                 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
00009             ),
00010             array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
00011                 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
00012             ),
00013         );
00014     }
00015 
00020     public function testPreSaveTransform( $text, $expected ) {
00021         global $wgParser;
00022 
00023         $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
00024         $user = new User();
00025         $user->setName( "127.0.0.1" );
00026         $popts = ParserOptions::newFromUser( $user );
00027         $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
00028 
00029         $this->assertEquals( $expected, $text );
00030     }
00031 
00032     public static function provideStripOuterParagraph() {
00033         // This mimics the most common use case (stripping paragraphs generated by the parser).
00034         $message = new RawMessage( "Message text." );
00035 
00036         return array(
00037             array(
00038                 "<p>Text.</p>",
00039                 "Text.",
00040             ),
00041             array(
00042                 "<p class='foo'>Text.</p>",
00043                 "<p class='foo'>Text.</p>",
00044             ),
00045             array(
00046                 "<p>Text.\n</p>\n",
00047                 "Text.",
00048             ),
00049             array(
00050                 "<p>Text.</p><p>More text.</p>",
00051                 "<p>Text.</p><p>More text.</p>",
00052             ),
00053             array(
00054                 $message->parse(),
00055                 "Message text.",
00056             ),
00057         );
00058     }
00059 
00064     public function testStripOuterParagraph( $text, $expected ) {
00065         $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
00066     }
00067 
00073     public function testRecursiveParse() {
00074         global $wgParser;
00075         $title = Title::newFromText( 'foo' );
00076         $po = new ParserOptions;
00077         $wgParser->setHook( 'recursivecallparser', array( $this, 'helperParserFunc' ) );
00078         $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
00079     }
00080 
00081     public function helperParserFunc( $input, $args, $parser ) {
00082         $title = Title::newFromText( 'foo' );
00083         $po = new ParserOptions;
00084         $parser->parse( $input, $title, $po );
00085         return 'bar';
00086     }
00087 
00091     public function testCallParserFunction() {
00092         global $wgParser;
00093 
00094         // Normal parses test passing PPNodes. Test passing an array.
00095         $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
00096         $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
00097         $frame = $wgParser->getPreprocessor()->newFrame();
00098         $ret = $wgParser->callParserFunction( $frame, '#tag',
00099             array( 'pre', 'foo', 'style' => 'margin-left: 1.6em' )
00100         );
00101         $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
00102         $this->assertSame( array(
00103             'found' => true,
00104             'text' => '<pre style="margin-left: 1.6em">foo</pre>',
00105         ), $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
00106     }
00107 
00112     public function testGetSections() {
00113         global $wgParser;
00114 
00115         $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
00116         $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
00117         $this->assertSame( array(
00118             array(
00119                 'toclevel' => 1,
00120                 'level' => '2',
00121                 'line' => 'foo',
00122                 'number' => '1',
00123                 'index' => '1',
00124                 'fromtitle' => $title->getPrefixedDBkey(),
00125                 'byteoffset' => 0,
00126                 'anchor' => 'foo',
00127             ),
00128             array(
00129                 'toclevel' => 1,
00130                 'level' => '2',
00131                 'line' => 'bar',
00132                 'number' => '2',
00133                 'index' => '',
00134                 'fromtitle' => false,
00135                 'byteoffset' => null,
00136                 'anchor' => 'bar',
00137             ),
00138             array(
00139                 'toclevel' => 1,
00140                 'level' => '2',
00141                 'line' => 'baz',
00142                 'number' => '3',
00143                 'index' => '2',
00144                 'fromtitle' => $title->getPrefixedDBkey(),
00145                 'byteoffset' => 21,
00146                 'anchor' => 'baz',
00147             ),
00148         ), $out->getSections(), 'getSections() with proper value when <h2> is used' );
00149     }
00150 
00156     public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
00157         $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
00158     }
00159 
00160     public static function provideNormalizeLinkUrl() {
00161         return array(
00162             array(
00163                 'Escaping of unsafe characters',
00164                 'http://example.org/foo bar?param[]="value"&param[]=valüe',
00165                 'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
00166             ),
00167             array(
00168                 'Case normalization of percent-encoded characters',
00169                 'http://example.org/%ab%cD%Ef%FF',
00170                 'http://example.org/%AB%CD%EF%FF',
00171             ),
00172             array(
00173                 'Unescaping of safe characters',
00174                 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
00175                 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
00176             ),
00177             array(
00178                 'Context-sensitive replacement of sometimes-safe characters',
00179                 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
00180                 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
00181             ),
00182         );
00183     }
00184 
00185     // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
00186     // replaceSection(), getPreloadText()
00187 }