Make lines short to pass phpcs in parser tests PHP files
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserMethodsTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6
7 class ParserMethodsTest extends MediaWikiLangTestCase {
8
9 public static function providePreSaveTransform() {
10 return array(
11 array( 'hello this is ~~~',
12 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
13 ),
14 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
15 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
16 ),
17 );
18 }
19
20 /**
21 * @dataProvider providePreSaveTransform
22 * @covers Parser::preSaveTransform
23 */
24 public function testPreSaveTransform( $text, $expected ) {
25 global $wgParser;
26
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 );
32
33 $this->assertEquals( $expected, $text );
34 }
35
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." );
39
40 return array(
41 array(
42 "<p>Text.</p>",
43 "Text.",
44 ),
45 array(
46 "<p class='foo'>Text.</p>",
47 "<p class='foo'>Text.</p>",
48 ),
49 array(
50 "<p>Text.\n</p>\n",
51 "Text.",
52 ),
53 array(
54 "<p>Text.</p><p>More text.</p>",
55 "<p>Text.</p><p>More text.</p>",
56 ),
57 array(
58 $message->parse(),
59 "Message text.",
60 ),
61 );
62 }
63
64 /**
65 * @dataProvider provideStripOuterParagraph
66 * @covers Parser::stripOuterParagraph
67 */
68 public function testStripOuterParagraph( $text, $expected ) {
69 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
70 }
71
72 /**
73 * @expectedException MWException
74 * @expectedExceptionMessage Parser state cleared while parsing.
75 * Did you call Parser::parse recursively?
76 * @covers Parser::lock
77 */
78 public function testRecursiveParse() {
79 global $wgParser;
80 $title = Title::newFromText( 'foo' );
81 $po = new ParserOptions;
82 $wgParser->setHook( 'recursivecallparser', array( $this, 'helperParserFunc' ) );
83 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
84 }
85
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';
91 }
92
93 /**
94 * @covers Parser::callParserFunction
95 */
96 public function testCallParserFunction() {
97 global $wgParser;
98
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 array( 'pre', 'foo', 'style' => 'margin-left: 1.6em' )
105 );
106 $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
107 $this->assertSame( array(
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}}' );
111 }
112
113 /**
114 * @covers Parser::parse
115 * @covers ParserOutput::getSections
116 */
117 public function testGetSections() {
118 global $wgParser;
119
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( array(
123 array(
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',
132 ),
133 array(
134 'toclevel' => 1,
135 'level' => '2',
136 'line' => 'bar',
137 'number' => '2',
138 'index' => '',
139 'fromtitle' => false,
140 'byteoffset' => null,
141 'anchor' => 'bar',
142 ),
143 array(
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',
152 ),
153 ), $out->getSections(), 'getSections() with proper value when <h2> is used' );
154 }
155
156 /**
157 * @dataProvider provideNormalizeLinkUrl
158 * @covers Parser::normalizeLinkUrl
159 * @covers Parser::normalizeUrlComponent
160 */
161 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
162 $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
163 }
164
165 public static function provideNormalizeLinkUrl() {
166 return array(
167 array(
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',
171 ),
172 array(
173 'Case normalization of percent-encoded characters',
174 'http://example.org/%ab%cD%Ef%FF',
175 'http://example.org/%AB%CD%EF%FF',
176 ),
177 array(
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',
181 ),
182 array(
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/?&=+;',
186 ),
187 );
188 }
189
190 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
191 // replaceSection(), getPreloadText()
192 }