r99909 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99908‎ | r99909 | r99910 >
Date:20:21, 15 October 2011
Author:brion
Status:resolved (Comments)
Tags:
Comment:
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 <equals> element around = which doesn't appear to be in Preprocessor_Dom's output.
Modified paths:
  • /trunk/phase3/tests/phpunit/includes/parser/PreprocessorTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/parser/PreprocessorTest.php
@@ -105,10 +105,39 @@
106106 }
107107
108108 /**
 109+ * Get XML preprocessor tree from the preprocessor (which may not be the
 110+ * native XML-based one).
 111+ *
 112+ * @param string $wikiText
 113+ * @return string
 114+ */
 115+ function preprocessToXml( $wikiText ) {
 116+ $dom = $this->mPreprocessor->preprocessToObj( $wikiText );
 117+ if ( is_callable( array( $dom, 'saveXML' ) ) ) {
 118+ return $dom->saveXML();
 119+ } else {
 120+ return $this->normalizeXml( $dom->__toString() );
 121+ }
 122+ }
 123+
 124+ /**
 125+ * Normalize XML string to the form that a DOMDocument saves out.
 126+ *
 127+ * @param string $xml
 128+ * @return string
 129+ */
 130+ function normalizeXml( $xml ) {
 131+ $dom = new DOMDocument();
 132+ // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2 don't barf when the XML is >256 levels deep
 133+ $dom->loadXML( $xml, 1 << 19 );
 134+ return $dom->saveXML();
 135+ }
 136+
 137+ /**
109138 * @dataProvider provideCases
110139 */
111140 function testPreprocessorOutput( $wikiText, $expectedXml ) {
112 - $this->assertEquals( $expectedXml, $this->mPreprocessor->preprocessToXml( $wikiText ) );
 141+ $this->assertEquals( $this->normalizeXml( $expectedXml ), $this->preprocessToXml( $wikiText ) );
113142 }
114143
115144 /**
@@ -129,11 +158,12 @@
130159 function testPreprocessorOutputFiles( $filename ) {
131160 $folder = dirname( __FILE__ ) . "/../../../parser/preprocess";
132161 $wikiText = file_get_contents( "$folder/$filename.txt" );
133 - $output = $this->mPreprocessor->preprocessToXml( $wikiText );
 162+ $output = $this->preprocessToXml( $wikiText );
134163
135164 $expectedFilename = "$folder/$filename.expected";
136165 if ( file_exists( $expectedFilename ) ) {
137 - $this->assertStringEqualsFile( $expectedFilename, $output );
 166+ $expectedXml = $this->normalizeXml( file_get_contents( $expectedFilename ) );
 167+ $this->assertEquals( $expectedXml, $output );
138168 } else {
139169 $tempFilename = tempnam( $folder, "$filename." );
140170 file_put_contents( $tempFilename, $output );
@@ -188,7 +218,7 @@
189219 * @dataProvider provideHeadings
190220 */
191221 function testHeadings( $wikiText, $expectedXml ) {
192 - $this->assertEquals( $expectedXml, $this->mPreprocessor->preprocessToXml( $wikiText ) );
 222+ $this->assertEquals( $this->normalizeXml( $expectedXml ), $this->preprocessToXml( $wikiText ) );
193223 }
194224 }
195225

Follow-up revisions

RevisionCommit summaryAuthorDate
r101136Follow-up r99909. If there is a preprocessToXml method, use that....platonides15:02, 28 October 2011
r101138An alternative to using DOMDocument for normalizing when using Preprocessor_H...platonides15:13, 28 October 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r80375Add preprocessing testsplatonides08:35, 15 January 2011

Comments

#Comment by Platonides (talk | contribs)   19:33, 18 October 2011

Could you make this work with extensions/NativePreprocessor/Preprocessor_Native.php ?

What was wrong with having a preprocessToXml() method in the preprocessor?

#Comment by Brion VIBBER (talk | contribs)   00:24, 1 November 2011

In theory it ought to work with Preprocessor_Native if the object has a _toString() method that returns XML.

Having a standard preprocessToXml() method wouldn't be wrong at all -- it might be awesome! -- but it doesn't actually exist on the Preprocessor interface. There just happened to be one on Preprocessor_DOM which seems to have been mostly an internal method.

Generally, having a clean standard way to take "the object tree I got from the preprocessor" and say "now serialize it as XML" is what I'd want, rather than having to start from source every time.

#Comment by Platonides (talk | contribs)   00:39, 1 November 2011

Well, the object is an array, so it didn't work. (I changed this in r101136 to use the preprocessToXml if available)

#Comment by Hashar (talk | contribs)   09:29, 10 January 2012

Platonides: is this revisions fine with follow up or does it need additional review?

#Comment by Platonides (talk | contribs)   17:56, 12 January 2012

Yes, it's ok.

Status & tagging log