Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / parser / ParserMethodsTest.php
blob5e00384ae7373995d6369159fb1b8fc05f68d1e9
1 <?php
3 /**
4 * @group Database
5 */
7 class ParserMethodsTest extends MediaWikiLangTestCase {
9 public static function providePreSaveTransform() {
10 return [
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>',
20 /**
21 * @dataProvider providePreSaveTransform
22 * @covers Parser::preSaveTransform
24 public function testPreSaveTransform( $text, $expected ) {
25 global $wgParser;
27 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
28 $user = new User();
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." );
40 return [
42 "<p>Text.</p>",
43 "Text.",
46 "<p class='foo'>Text.</p>",
47 "<p class='foo'>Text.</p>",
50 "<p>Text.\n</p>\n",
51 "Text.",
54 "<p>Text.</p><p>More text.</p>",
55 "<p>Text.</p><p>More text.</p>",
58 $message->parse(),
59 "Message text.",
64 /**
65 * @dataProvider provideStripOuterParagraph
66 * @covers Parser::stripOuterParagraph
68 public function testStripOuterParagraph( $text, $expected ) {
69 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
72 /**
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() {
79 global $wgParser;
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 );
90 return 'bar';
93 /**
94 * @covers Parser::callParserFunction
96 public function testCallParserFunction() {
97 global $wgParser;
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'] );
107 $this->assertSame( [
108 'found' => true,
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() {
118 global $wgParser;
120 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
121 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
122 $this->assertSame( [
124 'toclevel' => 1,
125 'level' => '2',
126 'line' => 'foo',
127 'number' => '1',
128 'index' => '1',
129 'fromtitle' => $title->getPrefixedDBkey(),
130 'byteoffset' => 0,
131 'anchor' => 'foo',
134 'toclevel' => 1,
135 'level' => '2',
136 'line' => 'bar',
137 'number' => '2',
138 'index' => '',
139 'fromtitle' => false,
140 'byteoffset' => null,
141 'anchor' => 'bar',
144 'toclevel' => 1,
145 'level' => '2',
146 'line' => 'baz',
147 'number' => '3',
148 'index' => '2',
149 'fromtitle' => $title->getPrefixedDBkey(),
150 'byteoffset' => 21,
151 'anchor' => 'baz',
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() {
166 return [
168 'Escaping of unsafe characters',
169 'http://example.org/foo bar?param[]="value"&param[]=valüe',
170 'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%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()