2012-06-23 18:28:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
2015-04-01 07:35:32 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
2017-02-27 04:44:31 +00:00
|
|
|
* @covers Parser
|
2015-04-01 07:35:32 +00:00
|
|
|
*/
|
2012-06-23 21:04:57 +00:00
|
|
|
class ParserMethodsTest extends MediaWikiLangTestCase {
|
2012-06-23 18:28:51 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function providePreSaveTransform() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'hello this is ~~~',
|
2013-02-15 10:24:31 +00:00
|
|
|
"hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
|
2013-02-15 10:24:31 +00:00
|
|
|
'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2012-06-23 18:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider providePreSaveTransform
|
2012-06-23 18:28:51 +00:00
|
|
|
*/
|
|
|
|
|
public function testPreSaveTransform( $text, $expected ) {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
|
|
|
|
|
$title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
|
|
|
|
|
$user = new User();
|
|
|
|
|
$user->setName( "127.0.0.1" );
|
|
|
|
|
$popts = ParserOptions::newFromUser( $user );
|
|
|
|
|
$text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $text );
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 15:21:36 +00:00
|
|
|
public static function provideStripOuterParagraph() {
|
|
|
|
|
// This mimics the most common use case (stripping paragraphs generated by the parser).
|
|
|
|
|
$message = new RawMessage( "Message text." );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p>Text.</p>",
|
|
|
|
|
"Text.",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p class='foo'>Text.</p>",
|
|
|
|
|
"<p class='foo'>Text.</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p>Text.\n</p>\n",
|
|
|
|
|
"Text.",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p>Text.</p><p>More text.</p>",
|
|
|
|
|
"<p>Text.</p><p>More text.</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
$message->parse(),
|
|
|
|
|
"Message text.",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2014-02-22 15:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideStripOuterParagraph
|
|
|
|
|
*/
|
|
|
|
|
public function testStripOuterParagraph( $text, $expected ) {
|
|
|
|
|
$this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-27 20:18:06 +00:00
|
|
|
/**
|
|
|
|
|
* @expectedException MWException
|
2015-09-30 06:08:31 +00:00
|
|
|
* @expectedExceptionMessage Parser state cleared while parsing.
|
|
|
|
|
* Did you call Parser::parse recursively?
|
2013-10-27 20:18:06 +00:00
|
|
|
*/
|
|
|
|
|
public function testRecursiveParse() {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
$title = Title::newFromText( 'foo' );
|
|
|
|
|
$po = new ParserOptions;
|
2016-02-17 09:09:32 +00:00
|
|
|
$wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
|
2013-10-27 20:18:06 +00:00
|
|
|
$wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-19 21:12:10 +00:00
|
|
|
public function helperParserFunc( $input, $args, $parser ) {
|
2013-10-27 20:18:06 +00:00
|
|
|
$title = Title::newFromText( 'foo' );
|
|
|
|
|
$po = new ParserOptions;
|
|
|
|
|
$parser->parse( $input, $title, $po );
|
|
|
|
|
return 'bar';
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-04 03:35:05 +00:00
|
|
|
public function testCallParserFunction() {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
|
|
|
|
|
// Normal parses test passing PPNodes. Test passing an array.
|
|
|
|
|
$title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
|
|
|
|
|
$wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
|
|
|
|
|
$frame = $wgParser->getPreprocessor()->newFrame();
|
|
|
|
|
$ret = $wgParser->callParserFunction( $frame, '#tag',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
|
2013-03-04 03:35:05 +00:00
|
|
|
);
|
|
|
|
|
$ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
2013-03-04 03:35:05 +00:00
|
|
|
'found' => true,
|
|
|
|
|
'text' => '<pre style="margin-left: 1.6em">foo</pre>',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
|
2013-03-04 03:35:05 +00:00
|
|
|
}
|
2013-10-09 15:03:40 +00:00
|
|
|
|
2013-10-22 23:50:38 +00:00
|
|
|
/**
|
2017-02-27 04:44:31 +00:00
|
|
|
* @covers Parser
|
2013-10-22 23:50:38 +00:00
|
|
|
* @covers ParserOutput::getSections
|
|
|
|
|
*/
|
2013-10-09 15:03:40 +00:00
|
|
|
public function testGetSections() {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
|
|
|
|
|
$title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
|
|
|
|
|
$out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
2013-10-09 15:03:40 +00:00
|
|
|
'toclevel' => 1,
|
|
|
|
|
'level' => '2',
|
|
|
|
|
'line' => 'foo',
|
|
|
|
|
'number' => '1',
|
|
|
|
|
'index' => '1',
|
|
|
|
|
'fromtitle' => $title->getPrefixedDBkey(),
|
|
|
|
|
'byteoffset' => 0,
|
|
|
|
|
'anchor' => 'foo',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-10-09 15:03:40 +00:00
|
|
|
'toclevel' => 1,
|
|
|
|
|
'level' => '2',
|
|
|
|
|
'line' => 'bar',
|
|
|
|
|
'number' => '2',
|
|
|
|
|
'index' => '',
|
|
|
|
|
'fromtitle' => false,
|
|
|
|
|
'byteoffset' => null,
|
|
|
|
|
'anchor' => 'bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-10-09 15:03:40 +00:00
|
|
|
'toclevel' => 1,
|
|
|
|
|
'level' => '2',
|
|
|
|
|
'line' => 'baz',
|
|
|
|
|
'number' => '3',
|
|
|
|
|
'index' => '2',
|
|
|
|
|
'fromtitle' => $title->getPrefixedDBkey(),
|
|
|
|
|
'byteoffset' => 21,
|
|
|
|
|
'anchor' => 'baz',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
], $out->getSections(), 'getSections() with proper value when <h2> is used' );
|
2013-10-09 15:03:40 +00:00
|
|
|
}
|
2013-12-21 02:14:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNormalizeLinkUrl
|
|
|
|
|
*/
|
|
|
|
|
public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
|
|
|
|
|
$this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideNormalizeLinkUrl() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Escaping of unsafe characters',
|
|
|
|
|
'http://example.org/foo bar?param[]="value"¶m[]=valüe',
|
|
|
|
|
'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Case normalization of percent-encoded characters',
|
|
|
|
|
'http://example.org/%ab%cD%Ef%FF',
|
|
|
|
|
'http://example.org/%AB%CD%EF%FF',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Unescaping of safe characters',
|
|
|
|
|
'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
|
|
|
|
|
'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Context-sensitive replacement of sometimes-safe characters',
|
|
|
|
|
'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
|
|
|
|
|
'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2013-12-21 02:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-24 17:52:09 +00:00
|
|
|
// @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
|
|
|
|
|
// replaceSection(), getPreloadText()
|
2012-06-23 18:28:51 +00:00
|
|
|
}
|