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