Merge "Update wikimedia/wrappedstring to v2.2.0"
[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:|<isindex,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']['iframe'] ) ||
93 isset( $case['document']['props']['tags']['noembed'] ) ||
94 isset( $case['document']['props']['tags']['noscript'] ) ||
95 isset( $case['document']['props']['tags']['script'] ) ||
96 isset( $case['document']['props']['tags']['svg script'] ) ||
97 isset( $case['document']['props']['tags']['svg title'] ) ||
98 isset( $case['document']['props']['tags']['textarea'] ) ||
99 isset( $case['document']['props']['tags']['title'] ) ||
100 isset( $case['document']['props']['tags']['xmp'] )
101 ) {
102 // Skip tests with unsupported tags which *do* show
103 // up in the expected output.
104 continue;
105 }
106 if (
107 $filename === 'entities01.dat' ||
108 $filename === 'entities02.dat' ||
109 preg_match( '/&([a-z]+|#x[0-9A-F]+);/i', $case['data'] ) ||
110 preg_match( '/^(&|&#|&#X|&#x|&#45|&x-test|&AMP)$/', $case['data'] )
111 ) {
112 // Skip tests involving entity encoding.
113 continue;
114 }
115 if (
116 isset( $case['document']['props']['tagWithLt'] ) ||
117 isset( $case['document']['props']['attrWithFunnyChar'] ) ||
118 preg_match( ':^(</b test|<di|<foo bar=qux/>)$:', $case['data'] ) ||
119 preg_match( ':</p<p>:', $case['data'] )
120 ) {
121 // Skip tests with funny tag or attribute names,
122 // which are really tests of the HTML tokenizer, not
123 // the tree builder.
124 continue;
125 }
126 if (
127 stripos( $case['data'], 'encoding=" text/html "' ) !== false
128 ) {
129 // The Sanitizer normalizes whitespace in attribute
130 // values, which makes this test case invalid.
131 continue;
132 }
133 if ( $filename === 'plain-text-unsafe.dat' ) {
134 // Skip tests with ASCII null, etc.
135 continue;
136 }
137 $tests[] = [
138 $filename, # use better description?
139 $case['data'],
140 $html
141 ];
142 }
143 }
144 return $tests;
145 }
146 }