Merge "Avoid master queries when old filter checks are viewed"
[lhc/web/wiklou.git] / tests / phpunit / includes / tidy / BalancerTest.php
index 7b94e40..d93181c 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
 class BalancerTest extends MediaWikiTestCase {
-       private $balancer;
 
        /**
         * Anything that needs to happen before your tests should go here.
@@ -11,26 +10,20 @@ class BalancerTest extends MediaWikiTestCase {
                // This makes sure that all the various cleanup and restorations
                // happen as they should (including the restoration for setMwGlobals).
                parent::setUp();
-               $this->balancer = new MediaWiki\Tidy\Balancer( [
-                       'strict' => false, /* not strict */
-                       'allowedHtmlElements' => null, /* no sanitization */
-                       'tidyCompat' => false, /* standard parser */
-               ] );
        }
 
        /**
-        * Anything cleanup you need to do should go here.
-        */
-       protected function tearDown() {
-               parent::tearDown();
-       }
-
-       /**
-        * @covers Balancer::balance
+        * @covers MediaWiki\Tidy\Balancer::balance
         * @dataProvider provideBalancerTests
         */
-       public function testBalancer( $description, $input, $expected ) {
-               $output = $this->balancer->balance( $input );
+       public function testBalancer( $description, $input, $expected, $useTidy ) {
+               $balancer = new MediaWiki\Tidy\Balancer( [
+                       'strict' => false, /* not strict */
+                       'allowedHtmlElements' => null, /* no sanitization */
+                       'tidyCompat' => $useTidy, /* standard parser */
+                       'allowComments' => true, /* comment parsing */
+               ] );
+               $output = $balancer->balance( $input );
 
                // Ignore self-closing tags
                $output = preg_replace( '/\s*\/>/', '>', $output );
@@ -47,15 +40,16 @@ class BalancerTest extends MediaWikiTestCase {
                // for providers, and filter out HTML constructs which
                // the balancer doesn't support.
                $tests = [];
-               $start = '<html><head></head><body>';
-               $end = '</body></html>';
+               $okre = "~ \A
+                       (?i:<!DOCTYPE\ html>)?
+                       <html><head></head><body>
+                       .*
+                       </body></html>
+               \z ~xs";
                foreach ( $json as $filename => $cases ) {
                        foreach ( $cases as $case ) {
                                $html = $case['document']['html'];
-                               if (
-                                       substr( $html, 0, strlen( $start ) ) !== $start ||
-                                       substr( $html, -strlen( $end ) ) !== $end
-                               ) {
+                               if ( !preg_match( $okre, $html ) ) {
                                        // Skip tests which involve stuff in the <head> or
                                        // weird doctypes.
                                        continue;
@@ -69,35 +63,43 @@ class BalancerTest extends MediaWikiTestCase {
                                $html = $case['document']['noQuirksBodyHtml'];
                                // Normalize case of SVG attributes.
                                $html = str_replace( 'foreignObject', 'foreignobject', $html );
+                               // Normalize case of MathML attributes.
+                               $html = str_replace( 'definitionURL', 'definitionurl', $html );
 
-                               if ( isset( $case['document']['props']['comment'] ) ) {
-                                       // Skip tests which include HTML comments, which
-                                       // the balancer requires to have been stripped.
+                               if (
+                                       isset( $case['document']['props']['comment'] ) &&
+                                       preg_match( ',<!--[^>]*<,', $html )
+                               ) {
+                                       // Skip tests which include HTML comments containing
+                                       // the < character, which we don't support.
                                        continue;
                                }
                                if ( strpos( $case['data'], '<![CDATA[' ) !== false ) {
                                        // Skip tests involving <![CDATA[ ]]> quoting.
                                        continue;
                                }
-                               if ( stripos( $case['data'], '<!DOCTYPE' ) !== false ) {
-                                       // Skip tests involving doctypes.
+                               if (
+                                       stripos( $case['data'], '<!DOCTYPE' ) !== false &&
+                                       stripos( $case['data'], '<!DOCTYPE html>' ) === false
+                               ) {
+                                       // Skip tests involving unusual doctypes.
                                        continue;
                                }
-                               if ( preg_match( ',</?(html|head|body|frame|plaintext)>|<rdar:,i', $case['data'] ) ) {
+                               $literalre = "~ <rdar: | < /? (
+                                       html | head | body | frame | frameset | plaintext
+                               ) > ~xi";
+                               if ( preg_match( $literalre, $case['data'] ) ) {
                                        // Skip tests involving some literal tags, which are
                                        // unsupported but don't show up in the expected output.
                                        continue;
                                }
                                if (
-                                       isset( $case['document']['props']['tags']['form'] ) ||
                                        isset( $case['document']['props']['tags']['iframe'] ) ||
                                        isset( $case['document']['props']['tags']['noembed'] ) ||
                                        isset( $case['document']['props']['tags']['noscript'] ) ||
                                        isset( $case['document']['props']['tags']['script'] ) ||
-                                       isset( $case['document']['props']['tags']['select'] ) ||
                                        isset( $case['document']['props']['tags']['svg script'] ) ||
                                        isset( $case['document']['props']['tags']['svg title'] ) ||
-                                       isset( $case['document']['props']['tags']['textarea'] ) ||
                                        isset( $case['document']['props']['tags']['title'] ) ||
                                        isset( $case['document']['props']['tags']['xmp'] )
                                ) {
@@ -118,7 +120,8 @@ class BalancerTest extends MediaWikiTestCase {
                                        isset( $case['document']['props']['tagWithLt'] ) ||
                                        isset( $case['document']['props']['attrWithFunnyChar'] ) ||
                                        preg_match( ':^(</b test|<di|<foo bar=qux/>)$:', $case['data'] ) ||
-                                       preg_match( ':</p<p>:', $case['data'] )
+                                       preg_match( ':</p<p>:', $case['data'] ) ||
+                                       preg_match( ':<b &=&amp>|<p/x/y/z>:', $case['data'] )
                                ) {
                                        // Skip tests with funny tag or attribute names,
                                        // which are really tests of the HTML tokenizer, not
@@ -126,7 +129,7 @@ class BalancerTest extends MediaWikiTestCase {
                                        continue;
                                }
                                if (
-                                       stripos( $case['data'], 'encoding=" text/html "' ) !== false
+                                       preg_match( ':encoding=" text/html "|type=" hidden":', $case['data'] )
                                ) {
                                        // The Sanitizer normalizes whitespace in attribute
                                        // values, which makes this test case invalid.
@@ -136,13 +139,26 @@ class BalancerTest extends MediaWikiTestCase {
                                        // Skip tests with ASCII null, etc.
                                        continue;
                                }
+                               $data = preg_replace(
+                                       '~<!DOCTYPE html>~i', '', $case['data']
+                               );
                                $tests[] = [
                                        $filename, # use better description?
-                                       $case['data'],
-                                       $html
+                                       $data,
+                                       $html,
+                                       false # strict HTML5 compat mode, no tidy
                                ];
                        }
                }
+
+               # Some additional tests for mediawiki-specific features
+               $tests[] = [
+                       'Round-trip serialization for <pre>/<listing>/<textarea>',
+                       "<pre>\n\na</pre><listing>\n\nb</listing><textarea>\n\nc</textarea>",
+                       "<pre>\n\na</pre><listing>\n\nb</listing><textarea>\n\nc</textarea>",
+                       true # use the tidy-compatible mode
+               ];
+
                return $tests;
        }
 }