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