HTML5 Balancer
[lhc/web/wiklou.git] / tests / phpunit / includes / tidy / BalancerTest.php
1 <?php
2
3 class BalancerTest extends MediaWikiTestCase {
4 private $balancer;
5
6 /**
7 * Anything that needs to happen before your tests should go here.
8 */
9 protected function setUp() {
10 // Be sure to do call the parent setup and teardown functions.
11 // This makes sure that all the various cleanup and restorations
12 // happen as they should (including the restoration for setMwGlobals).
13 parent::setUp();
14 $this->balancer = new MediaWiki\Tidy\Balancer( [
15 'strict' => false, /* not strict */
16 'allowedHtmlElements' => null, /* no sanitization */
17 ] );
18 }
19
20 /**
21 * Anything cleanup you need to do should go here.
22 */
23 protected function tearDown() {
24 parent::tearDown();
25 }
26
27 /**
28 * @covers Balancer::balance
29 * @dataProvider provideBalancerTests
30 */
31 public function testBalancer( $description, $input, $expected ) {
32 $output = $this->balancer->balance( $input );
33 $this->assertEquals( $expected, $output, $description );
34 }
35
36 public static function provideBalancerTests() {
37 // Get the tests from html5lib-tests.json
38 $json = json_decode( file_get_contents(
39 __DIR__ . '/html5lib-tests.json'
40 ), true );
41 // Munge this slightly into the format phpunit expects
42 // for providers, and filter out HTML constructs which
43 // the balancer doesn't support.
44 $tests = [];
45 $start = '<html><head></head><body>';
46 $end = '</body></html>';
47 foreach ( $json as $filename => $cases ) {
48 foreach ( $cases as $case ) {
49 $html = $case['document']['html'];
50 if (
51 substr( $html, 0, strlen( $start ) ) !== $start ||
52 substr( $html, -strlen( $end ) ) !== $end
53 ) {
54 // Skip tests which involve stuff in the <head> or
55 // weird doctypes.
56 continue;
57 }
58 // We used to do this:
59 // $html = substr( $html, strlen( $start ), -strlen( $end ) );
60 // But now we use a different field in the test case,
61 // which reports how domino would parse this case in a
62 // no-quirks <body> context. (The original test case may
63 // have had a different context, or relied on quirks mode.)
64 $html = $case['document']['noQuirksBodyHtml'];
65 // Normalize case of SVG attributes.
66 $html = str_replace( 'foreignObject', 'foreignobject', $html );
67 // The Sanitizer sorts attributes.
68 $html = preg_replace( '/(size="[^"]+") (id="[^"]+")/', '$2 $1', $html );
69
70 if ( isset( $case['document']['props']['comment'] ) ) {
71 // Skip tests which include HTML comments, which
72 // the balancer requires to have been stripped.
73 continue;
74 }
75 if ( strpos( $case['data'], '<![CDATA[' ) !== false ) {
76 // Skip tests involving <![CDATA[ ]]> quoting.
77 continue;
78 }
79 if ( stripos( $case['data'], '<!DOCTYPE' ) !== false ) {
80 // Skip tests involving doctypes.
81 continue;
82 }
83 if ( preg_match( ',</?(html|head|body|frame|plaintext)>|<rdar:,i', $case['data'] ) ) {
84 // Skip tests involving some literal tags, which are
85 // unsupported but don't show up in the expected output.
86 continue;
87 }
88 if (
89 isset( $case['document']['props']['tags']['form'] ) ||
90 isset( $case['document']['props']['tags']['iframe'] ) ||
91 isset( $case['document']['props']['tags']['noembed'] ) ||
92 isset( $case['document']['props']['tags']['noscript'] ) ||
93 isset( $case['document']['props']['tags']['script'] ) ||
94 isset( $case['document']['props']['tags']['select'] ) ||
95 isset( $case['document']['props']['tags']['svg script'] ) ||
96 isset( $case['document']['props']['tags']['svg title'] ) ||
97 isset( $case['document']['props']['tags']['textarea'] ) ||
98 isset( $case['document']['props']['tags']['title'] ) ||
99 isset( $case['document']['props']['tags']['xmp'] )
100 ) {
101 // Skip tests with unsupported tags which *do* show
102 // up in the expected output.
103 continue;
104 }
105 if (
106 $filename === 'entities01.dat' ||
107 $filename === 'entities02.dat' ||
108 preg_match( '/&([a-z]+|#x[0-9A-F]+);/i', $case['data'] ) ||
109 preg_match( '/^(&|&#|&#X|&#x|&#45|&x-test|&AMP)$/', $case['data'] )
110 ) {
111 // Skip tests involving entity encoding.
112 continue;
113 }
114 if (
115 isset( $case['document']['props']['tagWithLt'] ) ||
116 isset( $case['document']['props']['attrWithFunnyChar'] ) ||
117 preg_match( ':^(</b test|<di|<foo bar=qux/>)$:', $case['data'] ) ||
118 preg_match( ':</p<p>:', $case['data'] )
119 ) {
120 // Skip tests with funny tag or attribute names,
121 // which are really tests of the HTML tokenizer, not
122 // the tree builder.
123 continue;
124 }
125 if (
126 stripos( $case['data'], 'encoding=" text/html "' ) !== false
127 ) {
128 // The Sanitizer normalizes whitespace in attribute
129 // values, which makes this test case invalid.
130 continue;
131 }
132 if ( $filename === 'plain-text-unsafe.dat' ) {
133 // Skip tests with ASCII null, etc.
134 continue;
135 }
136 $tests[] = [
137 $filename, # use better description?
138 $case['data'],
139 $html
140 ];
141 }
142 }
143 return $tests;
144 }
145 }