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