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