MediaWiki  REL1_21
DumpTestCase.php
Go to the documentation of this file.
00001 <?php
00002 
00006 abstract class DumpTestCase extends MediaWikiLangTestCase {
00007 
00019         protected $exceptionFromAddDBData = null;
00020 
00026         protected $xml = null;
00027 
00037         protected function addRevision( Page $page, $text, $summary ) {
00038                 $status = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), $summary );
00039                 if ( $status->isGood() ) {
00040                         $value = $status->getValue();
00041                         $revision = $value['revision'];
00042                         $revision_id = $revision->getId();
00043                         $text_id = $revision->getTextId();
00044                         if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
00045                                 return array( $revision_id, $text_id );
00046                         }
00047                 }
00048                 throw new MWException( "Could not determine revision id (" . $status->getWikiText() . ")" );
00049         }
00050 
00051 
00058         protected function gunzip( $fname ) {
00059                 $gzipped_contents = file_get_contents( $fname );
00060                 if ( $gzipped_contents === false ) {
00061                         $this->fail( "Could not get contents of $fname" );
00062                 }
00063                 // We resort to use gzinflate instead of gzdecode, as gzdecode
00064                 // need not be available
00065                 $contents = gzinflate( substr( $gzipped_contents, 10, -8 ) );
00066                 $this->assertEquals( strlen( $contents ),
00067                         file_put_contents( $fname, $contents ), "# bytes written" );
00068         }
00069 
00075         protected function setUp() {
00076                 global $wgUser;
00077 
00078                 parent::setUp();
00079 
00080                 // Check if any Exception is stored for rethrowing from addDBData
00081                 // @see self::exceptionFromAddDBData
00082                 if ( $this->exceptionFromAddDBData !== null ) {
00083                         throw $this->exceptionFromAddDBData;
00084                 }
00085 
00086                 $wgUser = new User();
00087         }
00088 
00092         function expectETAOutput() {
00093                 // Newer PHPUnits require assertion about the output using PHPUnit's own
00094                 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
00095                 // do not allow to check /each/ line of the output using /readable/ REs.
00096                 // So we ...
00097                 //
00098                 // 1. ... add a dummy output checking to make PHPUnit not complain
00099                 //    about unchecked test output
00100                 $this->expectOutputRegex( '//' );
00101 
00102                 // 2. Do the real output checking on our own.
00103                 $lines = explode( "\n", $this->getActualOutput() );
00104                 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
00105                 $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
00106                 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
00107                 foreach ( $lines as $line ) {
00108                         $this->assertRegExp( "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/", $line );
00109                 }
00110         }
00111 
00112 
00121         protected function skipToNodeEnd( $name ) {
00122                 while ( $this->xml->read() ) {
00123                         if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
00124                                 $this->xml->name == $name
00125                         ) {
00126                                 return true;
00127                         }
00128                 }
00129                 return false;
00130         }
00131 
00142         protected function skipPastNodeEnd( $name ) {
00143                 $this->assertTrue( $this->skipToNodeEnd( $name ),
00144                         "Skipping to end of $name" );
00145                 while ( $this->xml->read() ) {
00146                         if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
00147                                 return true;
00148                         }
00149                 }
00150                 return false;
00151         }
00152 
00160         protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
00161                 $this->xml = new XMLReader();
00162                 $this->assertTrue( $this->xml->open( $fname ),
00163                         "Opening temporary file $fname via XMLReader failed" );
00164                 if ( $skip_siteinfo ) {
00165                         $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
00166                                 "Skipping past end of siteinfo" );
00167                 }
00168         }
00169 
00177         protected function assertDumpEnd( $name = "mediawiki" ) {
00178                 $this->assertNodeEnd( $name, false );
00179                 if ( $this->xml->read() ) {
00180                         $this->skipWhitespace();
00181                 }
00182                 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
00183                         "No proper entity left to parse" );
00184                 $this->xml->close();
00185         }
00186 
00190         protected function skipWhitespace() {
00191                 $cont = true;
00192                 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
00193                         || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
00194                         $cont = $this->xml->read();
00195                 }
00196         }
00197 
00206         protected function assertNodeStart( $name, $skip = true ) {
00207                 $this->assertEquals( $name, $this->xml->name, "Node name" );
00208                 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
00209                 if ( $skip ) {
00210                         $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
00211                 }
00212         }
00213 
00222         protected function assertNodeEnd( $name, $skip = true ) {
00223                 $this->assertEquals( $name, $this->xml->name, "Node name" );
00224                 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
00225                 if ( $skip ) {
00226                         $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
00227                 }
00228         }
00229 
00230 
00242         protected function assertTextNode( $name, $text, $skip_ws = true ) {
00243                 $this->assertNodeStart( $name );
00244 
00245                 if ( $text !== false ) {
00246                         $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
00247                 }
00248                 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
00249                 $this->assertNodeEnd( $name );
00250 
00251                 if ( $skip_ws ) {
00252                         $this->skipWhitespace();
00253                 }
00254         }
00255 
00268         protected function assertPageStart( $id, $ns, $name ) {
00269 
00270                 $this->assertNodeStart( "page" );
00271                 $this->skipWhitespace();
00272 
00273                 $this->assertTextNode( "title", $name );
00274                 $this->assertTextNode( "ns", $ns );
00275                 $this->assertTextNode( "id", $id );
00276 
00277         }
00278 
00283         protected function assertPageEnd() {
00284                 $this->assertNodeEnd( "page" );
00285                 $this->skipWhitespace();
00286         }
00287 
00303         protected function assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text = false, $parentid = false,
00304                                                                            $model = CONTENT_MODEL_WIKITEXT, $format = CONTENT_FORMAT_WIKITEXT ) {
00305 
00306                 $this->assertNodeStart( "revision" );
00307                 $this->skipWhitespace();
00308 
00309                 $this->assertTextNode( "id", $id );
00310                 if ( $parentid !== false ) {
00311                         $this->assertTextNode( "parentid", $parentid );
00312                 }
00313                 $this->assertTextNode( "timestamp", false );
00314 
00315                 $this->assertNodeStart( "contributor" );
00316                 $this->skipWhitespace();
00317                 $this->assertTextNode( "ip", false );
00318                 $this->assertNodeEnd( "contributor" );
00319                 $this->skipWhitespace();
00320 
00321                 $this->assertTextNode( "comment", $summary );
00322                 $this->skipWhitespace();
00323 
00324                 if ( $this->xml->name == "text" ) {
00325                         // note: <text> tag may occur here or at the very end.
00326                         $text_found = true;
00327                         $this->assertText( $id, $text_id, $text_bytes, $text );
00328                 } else {
00329                         $text_found = false;
00330                 }
00331 
00332                 $this->assertTextNode( "sha1", $text_sha1 );
00333 
00334                 $this->assertTextNode( "model", $model );
00335                 $this->skipWhitespace();
00336 
00337                 $this->assertTextNode( "format", $format );
00338                 $this->skipWhitespace();
00339 
00340                 if ( !$text_found ) {
00341                         $this->assertText( $id, $text_id, $text_bytes, $text );
00342                 }
00343 
00344                 $this->assertNodeEnd( "revision" );
00345                 $this->skipWhitespace();
00346         }
00347 
00348         protected function assertText( $id, $text_id, $text_bytes, $text ) {
00349                 $this->assertNodeStart( "text", false );
00350                 if ( $text_bytes !== false ) {
00351                         $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
00352                                 "Attribute 'bytes' of revision " . $id );
00353                 }
00354 
00355                 if ( $text === false ) {
00356                         // Testing for a stub
00357                         $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
00358                                 "Text id of revision " . $id );
00359                         $this->assertFalse( $this->xml->hasValue, "Revision has text" );
00360                         $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
00361                         if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
00362                                 && ( $this->xml->name == "text" )
00363                         ) {
00364 
00365                                 $this->xml->read();
00366                         }
00367                         $this->skipWhitespace();
00368                 } else {
00369                         // Testing for a real dump
00370                         $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
00371                         $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
00372                         $this->assertTrue( $this->xml->read(), "Skipping past text" );
00373                         $this->assertNodeEnd( "text" );
00374                         $this->skipWhitespace();
00375                 }
00376         }
00377 }