Type hint against LinkTarget in WatchedItemStore
[lhc/web/wiklou.git] / tests / phpunit / includes / tidy / RemexDriverTest.php
1 <?php
2
3 class RemexDriverTest extends MediaWikiTestCase {
4 private static $remexTidyTestData = [
5 [
6 'Empty string',
7 "",
8 ""
9 ],
10 [
11 'Simple p-wrap',
12 "x",
13 "<p>x</p>"
14 ],
15 [
16 'No p-wrap of blank node',
17 " ",
18 " "
19 ],
20 [
21 'p-wrap terminated by div',
22 "x<div></div>",
23 "<p>x</p><div></div>"
24 ],
25 [
26 'p-wrap not terminated by span',
27 "x<span></span>",
28 "<p>x<span></span></p>"
29 ],
30 [
31 'An element is non-blank and so gets p-wrapped',
32 "<span></span>",
33 "<p><span></span></p>"
34 ],
35 [
36 'The blank flag is set after a block-level element',
37 "<div></div> ",
38 "<div></div> "
39 ],
40 [
41 'Blank detection between two block-level elements',
42 "<div></div> <div></div>",
43 "<div></div> <div></div>"
44 ],
45 [
46 'But p-wrapping of non-blank content works after an element',
47 "<div></div>x",
48 "<div></div><p>x</p>"
49 ],
50 [
51 'p-wrapping between two block-level elements',
52 "<div></div>x<div></div>",
53 "<div></div><p>x</p><div></div>"
54 ],
55 [
56 'p-wrap inside blockquote',
57 "<blockquote>x</blockquote>",
58 "<blockquote><p>x</p></blockquote>"
59 ],
60 [
61 'A comment is blank for p-wrapping purposes',
62 "<!-- x -->",
63 "<!-- x -->"
64 ],
65 [
66 'A comment is blank even when a p-wrap was opened by a text node',
67 " <!-- x -->",
68 " <!-- x -->"
69 ],
70 [
71 'A comment does not open a p-wrap',
72 "<!-- x -->x",
73 "<!-- x --><p>x</p>"
74 ],
75 [
76 'A comment does not close a p-wrap',
77 "x<!-- x -->",
78 "<p>x<!-- x --></p>"
79 ],
80 [
81 'Empty li',
82 "<ul><li></li></ul>",
83 "<ul><li class=\"mw-empty-elt\"></li></ul>"
84 ],
85 [
86 'li with element',
87 "<ul><li><span></span></li></ul>",
88 "<ul><li><span></span></li></ul>"
89 ],
90 [
91 'li with text',
92 "<ul><li>x</li></ul>",
93 "<ul><li>x</li></ul>"
94 ],
95 [
96 'Empty tr',
97 "<table><tbody><tr></tr></tbody></table>",
98 "<table><tbody><tr class=\"mw-empty-elt\"></tr></tbody></table>"
99 ],
100 [
101 'Empty p',
102 "<p>\n</p>",
103 "<p class=\"mw-empty-elt\">\n</p>"
104 ],
105 [
106 'No p-wrapping of an inline element which contains a block element (T150317)',
107 "<small><div>x</div></small>",
108 "<small><div>x</div></small>"
109 ],
110 [
111 'p-wrapping of an inline element which contains an inline element',
112 "<small><b>x</b></small>",
113 "<p><small><b>x</b></small></p>"
114 ],
115 [
116 'p-wrapping is enabled in a blockquote in an inline element',
117 "<small><blockquote>x</blockquote></small>",
118 "<small><blockquote><p>x</p></blockquote></small>"
119 ],
120 [
121 'All bare text should be p-wrapped even when surrounded by block tags',
122 "<small><blockquote>x</blockquote></small>y<div></div>z",
123 "<small><blockquote><p>x</p></blockquote></small><p>y</p><div></div><p>z</p>"
124 ],
125 [
126 'Split tag stack 1',
127 "<small>x<div>y</div>z</small>",
128 "<p><small>x</small></p><small><div>y</div></small><p><small>z</small></p>"
129 ],
130 [
131 'Split tag stack 2',
132 "<small><div>y</div>z</small>",
133 "<small><div>y</div></small><p><small>z</small></p>"
134 ],
135 [
136 'Split tag stack 3',
137 "<small>x<div>y</div></small>",
138 "<p><small>x</small></p><small><div>y</div></small>"
139 ],
140 [
141 'Split tag stack 4 (modified to use splittable tag)',
142 "a<code>b<i>c<div>d</div></i>e</code>",
143 "<p>a<code>b<i>c</i></code></p><code><i><div>d</div></i></code><p><code>e</code></p>"
144 ],
145 [
146 "Split tag stack regression check 1",
147 "x<span><div>y</div></span>",
148 "<p>x</p><span><div>y</div></span>"
149 ],
150 [
151 "Split tag stack regression check 2 (modified to use splittable tag)",
152 "a<code><i><div>d</div></i>e</code>",
153 "<p>a</p><code><i><div>d</div></i></code><p><code>e</code></p>"
154 ],
155 // Simple tests from pwrap.js
156 [
157 'Simple pwrap test 1',
158 'a',
159 '<p>a</p>'
160 ],
161 [
162 '<span> is not a splittable tag, but gets p-wrapped in simple wrapping scenarios',
163 '<span>a</span>',
164 '<p><span>a</span></p>'
165 ],
166 [
167 'Simple pwrap test 3',
168 'x <div>a</div> <div>b</div> y',
169 '<p>x </p><div>a</div> <div>b</div><p> y</p>'
170 ],
171 [
172 'Simple pwrap test 4',
173 'x<!--c--> <div>a</div> <div>b</div> <!--c-->y',
174 '<p>x<!--c--> </p><div>a</div> <div>b</div> <!--c--><p>y</p>'
175 ],
176 // Complex tests from pwrap.js
177 [
178 'Complex pwrap test 1',
179 '<i>x<div>a</div>y</i>',
180 '<p><i>x</i></p><i><div>a</div></i><p><i>y</i></p>'
181 ],
182 [
183 'Complex pwrap test 2',
184 'a<small>b</small><i>c<div>d</div>e</i>f',
185 '<p>a<small>b</small><i>c</i></p><i><div>d</div></i><p><i>e</i>f</p>'
186 ],
187 [
188 'Complex pwrap test 3',
189 'a<small>b<i>c<div>d</div></i>e</small>',
190 '<p>a<small>b<i>c</i></small></p><small><i><div>d</div></i></small><p><small>e</small></p>'
191 ],
192 [
193 'Complex pwrap test 4',
194 'x<small><div>y</div></small>',
195 '<p>x</p><small><div>y</div></small>'
196 ],
197 [
198 'Complex pwrap test 5',
199 'a<small><i><div>d</div></i>e</small>',
200 '<p>a</p><small><i><div>d</div></i></small><p><small>e</small></p>'
201 ],
202 // phpcs:disable Generic.Files.LineLength
203 [
204 'Complex pwrap test 6',
205 '<i>a<div>b</div>c<b>d<div>e</div>f</b>g</i>',
206 // PHP 5 does not allow concatenation in initialisation of a class static variable
207 '<p><i>a</i></p><i><div>b</div></i><p><i>c<b>d</b></i></p><i><b><div>e</div></b></i><p><i><b>f</b>g</i></p>'
208 ],
209 // phpcs:enable
210 /* FIXME the second <b> causes a stack split which clones the <i> even
211 * though no <p> is actually generated
212 [
213 'Complex pwrap test 7',
214 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>',
215 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>'
216 ],
217 */
218 // New local tests
219 [
220 'Blank text node after block end',
221 '<small>x<div>y</div> <b>z</b></small>',
222 '<p><small>x</small></p><small><div>y</div></small><p><small> <b>z</b></small></p>'
223 ],
224 [
225 'Text node fostering (FIXME: wrap missing)',
226 '<table>x</table>',
227 'x<table></table>'
228 ],
229 [
230 'Blockquote fostering',
231 '<table><blockquote>x</blockquote></table>',
232 '<blockquote><p>x</p></blockquote><table></table>'
233 ],
234 [
235 'Block element fostering',
236 '<table><div>x',
237 '<div>x</div><table></table>'
238 ],
239 [
240 'Formatting element fostering (FIXME: wrap missing)',
241 '<table><b>x',
242 '<b>x</b><table></table>'
243 ],
244 [
245 'AAA clone of p-wrapped element (FIXME: empty b)',
246 '<b>x<p>y</b>z</p>',
247 '<p><b>x</b></p><b></b><p><b>y</b>z</p>',
248 ],
249 [
250 'AAA with fostering (FIXME: wrap missing)',
251 '<table><b>1<p>2</b>3</p>',
252 '<b>1</b><p><b>2</b>3</p><table></table>'
253 ],
254 [
255 'AAA causes reparent of p-wrapped text node (T178632)',
256 '<i><blockquote>x</i></blockquote>',
257 '<i></i><blockquote><p><i>x</i></p></blockquote>',
258 ],
259 [
260 'p-wrap ended by reparenting (T200827)',
261 '<i><blockquote><p></i>',
262 '<i></i><blockquote><p><i></i></p><p><i></i></p></blockquote>',
263 ],
264 [
265 'style tag isn\'t p-wrapped (T186965)',
266 '<style>/* ... */</style>',
267 '<style>/* ... */</style>',
268 ],
269 [
270 'link tag isn\'t p-wrapped (T186965)',
271 '<link rel="foo" href="bar" />',
272 '<link rel="foo" href="bar" />',
273 ],
274 [
275 'style tag doesn\'t split p-wrapping (T208901)',
276 'foo <style>/* ... */</style> bar',
277 '<p>foo <style>/* ... */</style> bar</p>',
278 ],
279 [
280 'link tag doesn\'t split p-wrapping (T208901)',
281 'foo <link rel="foo" href="bar" /> bar',
282 '<p>foo <link rel="foo" href="bar" /> bar</p>',
283 ],
284 ];
285
286 public function provider() {
287 return self::$remexTidyTestData;
288 }
289
290 /**
291 * @dataProvider provider
292 * @covers MediaWiki\Tidy\RemexCompatFormatter
293 * @covers MediaWiki\Tidy\RemexCompatMunger
294 * @covers MediaWiki\Tidy\RemexDriver
295 * @covers MediaWiki\Tidy\RemexMungerData
296 */
297 public function testTidy( $desc, $input, $expected ) {
298 $r = new MediaWiki\Tidy\RemexDriver( [] );
299 $result = $r->tidy( $input );
300 $this->assertEquals( $expected, $result, $desc );
301 }
302
303 public function html5libProvider() {
304 $files = json_decode( file_get_contents( __DIR__ . '/html5lib-tests.json' ), true );
305 $tests = [];
306 foreach ( $files as $file => $fileTests ) {
307 foreach ( $fileTests as $i => $test ) {
308 $tests[] = [ "$file:$i", $test['data'] ];
309 }
310 }
311 return $tests;
312 }
313
314 /**
315 * This is a quick and dirty test to make sure none of the html5lib tests
316 * generate exceptions. We don't really know what the expected output is.
317 *
318 * @dataProvider html5libProvider
319 * @coversNothing
320 */
321 public function testHtml5Lib( $desc, $input ) {
322 $r = new MediaWiki\Tidy\RemexDriver( [] );
323 $result = $r->tidy( $input );
324 $this->assertTrue( true, $desc );
325 }
326 }