MediaWiki  REL1_20
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->doEdit( $text, $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         public 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                                 return true;
00126                         }
00127                 }
00128                 return false;
00129         }
00130 
00141         protected function skipPastNodeEnd( $name ) {
00142                 $this->assertTrue( $this->skipToNodeEnd( $name ),
00143                         "Skipping to end of $name" );
00144                 while ( $this->xml->read() ) {
00145                         if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
00146                                 return true;
00147                         }
00148                 }
00149                 return false;
00150         }
00151 
00159         protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
00160                 $this->xml = new XMLReader();
00161                 $this->assertTrue( $this->xml->open( $fname ),
00162                         "Opening temporary file $fname via XMLReader failed" );
00163                 if ( $skip_siteinfo ) {
00164                         $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
00165                                 "Skipping past end of siteinfo" );
00166                 }
00167         }
00168 
00176         protected function assertDumpEnd( $name = "mediawiki" ) {
00177                 $this->assertNodeEnd( $name, false );
00178                 if ( $this->xml->read() ) {
00179                         $this->skipWhitespace();
00180                 }
00181                 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
00182                         "No proper entity left to parse" );
00183                 $this->xml->close();
00184         }
00185 
00189         protected function skipWhitespace() {
00190                 $cont = true;
00191                 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
00192                                 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
00193                         $cont = $this->xml->read();
00194                 }
00195         }
00196 
00205         protected function assertNodeStart( $name, $skip = true ) {
00206                 $this->assertEquals( $name, $this->xml->name, "Node name" );
00207                 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
00208                 if ( $skip ) {
00209                         $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
00210                 }
00211         }
00212 
00221         protected function assertNodeEnd( $name, $skip = true ) {
00222                 $this->assertEquals( $name, $this->xml->name, "Node name" );
00223                 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
00224                 if ( $skip ) {
00225                         $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
00226                 }
00227         }
00228 
00229 
00241         protected function assertTextNode( $name, $text, $skip_ws = true ) {
00242                 $this->assertNodeStart( $name );
00243 
00244                 if ( $text !== false ) {
00245                         $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
00246                 }
00247                 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
00248                 $this->assertNodeEnd( $name );
00249 
00250                 if ( $skip_ws ) {
00251                         $this->skipWhitespace();
00252                 }
00253         }
00254 
00267         protected function assertPageStart( $id, $ns, $name ) {
00268 
00269                 $this->assertNodeStart( "page" );
00270                 $this->skipWhitespace();
00271 
00272                 $this->assertTextNode( "title", $name );
00273                 $this->assertTextNode( "ns", $ns );
00274                 $this->assertTextNode( "id", $id );
00275 
00276         }
00277 
00282         protected function assertPageEnd() {
00283                 $this->assertNodeEnd( "page" );
00284                 $this->skipWhitespace();
00285         }
00286 
00300         protected function assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text = false, $parentid = false ) {
00301 
00302                 $this->assertNodeStart( "revision" );
00303                 $this->skipWhitespace();
00304 
00305                 $this->assertTextNode( "id", $id );
00306                 if ( $parentid !== false ) {
00307                         $this->assertTextNode( "parentid", $parentid );
00308                 }
00309                 $this->assertTextNode( "timestamp", false );
00310 
00311                 $this->assertNodeStart( "contributor" );
00312                 $this->skipWhitespace();
00313                 $this->assertTextNode( "ip", false );
00314                 $this->assertNodeEnd( "contributor" );
00315                 $this->skipWhitespace();
00316 
00317                 $this->assertTextNode( "comment", $summary );
00318 
00319                 $this->assertTextNode( "sha1", $text_sha1 );
00320 
00321                 $this->assertNodeStart( "text", false );
00322                 if ( $text_bytes !== false ) {
00323                         $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
00324                                 "Attribute 'bytes' of revision " . $id );
00325                 }
00326 
00327                 if ( $text === false ) {
00328                         // Testing for a stub
00329                         $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
00330                                 "Text id of revision " . $id );
00331                         $this->assertFalse( $this->xml->hasValue, "Revision has text" );
00332                         $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
00333                         if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
00334                                 && ( $this->xml->name == "text" ) ) {
00335 
00336                                 $this->xml->read();
00337                         }
00338                         $this->skipWhitespace();
00339                 } else {
00340                         // Testing for a real dump
00341                         $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
00342                         $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
00343                         $this->assertTrue( $this->xml->read(), "Skipping past text" );
00344                         $this->assertNodeEnd( "text" );
00345                         $this->skipWhitespace();
00346                 }
00347 
00348                 $this->assertNodeEnd( "revision" );
00349                 $this->skipWhitespace();
00350         }
00351 
00352 }