From 61bb13a24a43ce78310424cd25f043cc6394b20e Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sat, 15 Oct 2011 20:21:52 +0000 Subject: [PATCH] Followup r80375: let PreprocessorTest work on Preprocessor_Hash etc as well as Preprocessor_Dom 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 element around = which doesn't appear to be in Preprocessor_Dom's output. --- .../includes/parser/PreprocessorTest.php | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/includes/parser/PreprocessorTest.php b/tests/phpunit/includes/parser/PreprocessorTest.php index b53ea1fae0..a396480567 100644 --- a/tests/phpunit/includes/parser/PreprocessorTest.php +++ b/tests/phpunit/includes/parser/PreprocessorTest.php @@ -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 ) ); } } -- 2.20.1