Warn if stateful ParserOutput transforms are used
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserMethodsTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @covers Parser
6 */
7 class ParserMethodsTest extends MediaWikiLangTestCase {
8
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]]",
13 ],
14 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
15 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
16 ],
17 ];
18 }
19
20 /**
21 * @dataProvider providePreSaveTransform
22 */
23 public function testPreSaveTransform( $text, $expected ) {
24 global $wgParser;
25
26 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
27 $user = new User();
28 $user->setName( "127.0.0.1" );
29 $popts = ParserOptions::newFromUser( $user );
30 $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
31
32 $this->assertEquals( $expected, $text );
33 }
34
35 public static function provideStripOuterParagraph() {
36 // This mimics the most common use case (stripping paragraphs generated by the parser).
37 $message = new RawMessage( "Message text." );
38
39 return [
40 [
41 "<p>Text.</p>",
42 "Text.",
43 ],
44 [
45 "<p class='foo'>Text.</p>",
46 "<p class='foo'>Text.</p>",
47 ],
48 [
49 "<p>Text.\n</p>\n",
50 "Text.",
51 ],
52 [
53 "<p>Text.</p><p>More text.</p>",
54 "<p>Text.</p><p>More text.</p>",
55 ],
56 [
57 $message->parse(),
58 "Message text.",
59 ],
60 ];
61 }
62
63 /**
64 * @dataProvider provideStripOuterParagraph
65 */
66 public function testStripOuterParagraph( $text, $expected ) {
67 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
68 }
69
70 /**
71 * @expectedException MWException
72 * @expectedExceptionMessage Parser state cleared while parsing.
73 * Did you call Parser::parse recursively?
74 */
75 public function testRecursiveParse() {
76 global $wgParser;
77 $title = Title::newFromText( 'foo' );
78 $po = new ParserOptions;
79 $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
80 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
81 }
82
83 public function helperParserFunc( $input, $args, $parser ) {
84 $title = Title::newFromText( 'foo' );
85 $po = new ParserOptions;
86 $parser->parse( $input, $title, $po );
87 return 'bar';
88 }
89
90 public function testCallParserFunction() {
91 global $wgParser;
92
93 // Normal parses test passing PPNodes. Test passing an array.
94 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
95 $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
96 $frame = $wgParser->getPreprocessor()->newFrame();
97 $ret = $wgParser->callParserFunction( $frame, '#tag',
98 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
99 );
100 $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
101 $this->assertSame( [
102 'found' => true,
103 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
104 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
105 }
106
107 /**
108 * @covers Parser
109 * @covers ParserOutput::getSections
110 */
111 public function testGetSections() {
112 global $wgParser;
113
114 $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
115 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
116 $this->assertSame( [
117 [
118 'toclevel' => 1,
119 'level' => '2',
120 'line' => 'foo',
121 'number' => '1',
122 'index' => '1',
123 'fromtitle' => $title->getPrefixedDBkey(),
124 'byteoffset' => 0,
125 'anchor' => 'foo',
126 ],
127 [
128 'toclevel' => 1,
129 'level' => '2',
130 'line' => 'bar',
131 'number' => '2',
132 'index' => '',
133 'fromtitle' => false,
134 'byteoffset' => null,
135 'anchor' => 'bar',
136 ],
137 [
138 'toclevel' => 1,
139 'level' => '2',
140 'line' => 'baz',
141 'number' => '3',
142 'index' => '2',
143 'fromtitle' => $title->getPrefixedDBkey(),
144 'byteoffset' => 21,
145 'anchor' => 'baz',
146 ],
147 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
148 }
149
150 /**
151 * @dataProvider provideNormalizeLinkUrl
152 */
153 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
154 $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
155 }
156
157 public static function provideNormalizeLinkUrl() {
158 return [
159 [
160 'Escaping of unsafe characters',
161 'http://example.org/foo bar?param[]="value"&param[]=val├╝e',
162 'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
163 ],
164 [
165 'Case normalization of percent-encoded characters',
166 'http://example.org/%ab%cD%Ef%FF',
167 'http://example.org/%AB%CD%EF%FF',
168 ],
169 [
170 'Unescaping of safe characters',
171 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
172 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
173 ],
174 [
175 'Context-sensitive replacement of sometimes-safe characters',
176 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
177 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
178 ],
179 ];
180 }
181
182 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
183 // replaceSection(), getPreloadText()
184 }