Followup r80375: let PreprocessorTest work on Preprocessor_Hash etc as well as Prepro...
authorBrion Vibber <brion@users.mediawiki.org>
Sat, 15 Oct 2011 20:21:52 +0000 (20:21 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sat, 15 Oct 2011 20:21:52 +0000 (20:21 +0000)
Using same technique as ApiExpandTemplates to serialize the object tree back to XML, rather than asking for the DOM implementation's internal XML return function.
Have to also perform normalization on the test cases, as they aren't normalized to what libxml2 serializes. :P

Note that there are 4 test failures currently with Preprocessor_Hash, as it makes a separate <equals> element around = which doesn't appear to be in Preprocessor_Dom's output.

tests/phpunit/includes/parser/PreprocessorTest.php

index b53ea1f..a396480 100644 (file)
@@ -104,11 +104,40 @@ class PreprocessorTest extends MediaWikiTestCase {
                );
        }
 
+       /**
+        * Get XML preprocessor tree from the preprocessor (which may not be the
+        * native XML-based one).
+        *
+        * @param string $wikiText
+        * @return string
+        */
+       function preprocessToXml( $wikiText ) {
+               $dom = $this->mPreprocessor->preprocessToObj( $wikiText );
+               if ( is_callable( array( $dom, 'saveXML' ) ) ) {
+                       return $dom->saveXML();
+               } else {
+                       return $this->normalizeXml( $dom->__toString() );
+               }
+       }
+
+       /**
+        * Normalize XML string to the form that a DOMDocument saves out.
+        *
+        * @param string $xml
+        * @return string
+        */
+       function normalizeXml( $xml ) {
+               $dom = new DOMDocument();
+               // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2 don't barf when the XML is >256 levels deep
+               $dom->loadXML( $xml, 1 << 19 );
+               return $dom->saveXML();
+       }
+
        /**
         * @dataProvider provideCases
         */
        function testPreprocessorOutput( $wikiText, $expectedXml ) {
-               $this->assertEquals( $expectedXml, $this->mPreprocessor->preprocessToXml( $wikiText ) );
+               $this->assertEquals( $this->normalizeXml( $expectedXml ), $this->preprocessToXml( $wikiText ) );
        }
 
        /**
@@ -129,11 +158,12 @@ class PreprocessorTest extends MediaWikiTestCase {
        function testPreprocessorOutputFiles( $filename ) {
                $folder = dirname( __FILE__ ) . "/../../../parser/preprocess";
                $wikiText = file_get_contents( "$folder/$filename.txt" );
-               $output = $this->mPreprocessor->preprocessToXml( $wikiText );
+               $output = $this->preprocessToXml( $wikiText );
 
                $expectedFilename = "$folder/$filename.expected";
                if ( file_exists( $expectedFilename ) ) {
-                       $this->assertStringEqualsFile( $expectedFilename, $output );
+                       $expectedXml = $this->normalizeXml( file_get_contents( $expectedFilename ) );
+                       $this->assertEquals( $expectedXml, $output );
                } else {
                        $tempFilename = tempnam( $folder, "$filename." );
                        file_put_contents( $tempFilename, $output );
@@ -188,7 +218,7 @@ class PreprocessorTest extends MediaWikiTestCase {
         * @dataProvider provideHeadings
         */
        function testHeadings( $wikiText, $expectedXml ) {
-               $this->assertEquals( $expectedXml, $this->mPreprocessor->preprocessToXml( $wikiText ) );
+               $this->assertEquals( $this->normalizeXml( $expectedXml ), $this->preprocessToXml( $wikiText ) );
        }
 }