MediaWiki  REL1_20
backup_LogTest.php
Go to the documentation of this file.
00001 <?php
00008 class BackupDumperLoggerTest extends DumpTestCase {
00009 
00010 
00011         // We'll add several log entries and users for this test. The following
00012         // variables hold the corresponding ids.
00013         private $userId1, $userId2;
00014         private $logId1, $logId2, $logId3;
00015 
00030         private function addLogEntry( $type, $subtype, User $user, $ns, $title,
00031                 $comment = null, $parameters = null ) {
00032 
00033                 $logEntry = new ManualLogEntry( $type, $subtype );
00034                 $logEntry->setPerformer( $user );
00035                 $logEntry->setTarget( Title::newFromText( $title, $ns ) );
00036                 if ( $comment !== null ) {
00037                         $logEntry->setComment( $comment );
00038                 }
00039                 if ( $parameters !== null ) {
00040                         $logEntry->setParameters( $parameters );
00041                 }
00042                 return $logEntry->insert();
00043         }
00044 
00045         function addDBData() {
00046                 $this->tablesUsed[] = 'logging';
00047                 $this->tablesUsed[] = 'user';
00048 
00049                 try {
00050                         $user1 = User::newFromName( 'BackupDumperLogUserA' );
00051                         $this->userId1 = $user1->getId();
00052                         if ( $this->userId1 === 0 ) {
00053                                 $user1->addToDatabase();
00054                                 $this->userId1 = $user1->getId();
00055                         }
00056                         $this->assertGreaterThan( 0, $this->userId1 );
00057 
00058                         $user2 = User::newFromName( 'BackupDumperLogUserB' );
00059                         $this->userId2 = $user2->getId();
00060                         if ( $this->userId2 === 0 ) {
00061                                 $user2->addToDatabase();
00062                                 $this->userId2 = $user2->getId();
00063                         }
00064                         $this->assertGreaterThan( 0, $this->userId2 );
00065 
00066                         $this->logId1 = $this->addLogEntry( 'type', 'subtype',
00067                                 $user1, NS_MAIN, "PageA" );
00068                         $this->assertGreaterThan( 0, $this->logId1 );
00069 
00070                         $this->logId2 = $this->addLogEntry( 'supress', 'delete',
00071                                 $user2, NS_TALK, "PageB", "SomeComment" );
00072                         $this->assertGreaterThan( 0, $this->logId2 );
00073 
00074                         $this->logId3 = $this->addLogEntry( 'move', 'delete',
00075                                 $user2, NS_MAIN, "PageA", "SomeOtherComment",
00076                                 array( 'key1' => 1,  3 => 'value3' ) );
00077                         $this->assertGreaterThan( 0, $this->logId3 );
00078 
00079                 } catch ( Exception $e ) {
00080                         // We'd love to pass $e directly. However, ... see
00081                         // documentation of exceptionFromAddDBData in
00082                         // DumpTestCase
00083                         $this->exceptionFromAddDBData = $e;
00084                 }
00085 
00086         }
00087 
00088 
00103         private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
00104                 $subtype, $title, $parameters = array() ) {
00105 
00106                 $this->assertNodeStart( "logitem" );
00107                 $this->skipWhitespace();
00108 
00109                 $this->assertTextNode( "id", $id );
00110                 $this->assertTextNode( "timestamp", false );
00111 
00112                 $this->assertNodeStart( "contributor" );
00113                 $this->skipWhitespace();
00114                 $this->assertTextNode( "username", $user_name );
00115                 $this->assertTextNode( "id", $user_id );
00116                 $this->assertNodeEnd( "contributor" );
00117                 $this->skipWhitespace();
00118 
00119                 if ( $comment !== null ) {
00120                         $this->assertTextNode( "comment", $comment );
00121                 }
00122                 $this->assertTextNode( "type", $type );
00123                 $this->assertTextNode( "action", $subtype );
00124                 $this->assertTextNode( "logtitle", $title );
00125 
00126                 $this->assertNodeStart( "params" );
00127                 $parameters_xml = unserialize( $this->xml->value );
00128                 $this->assertEquals( $parameters, $parameters_xml );
00129                 $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
00130                 $this->assertNodeEnd( "params" );
00131                 $this->skipWhitespace();
00132 
00133                 $this->assertNodeEnd( "logitem" );
00134                 $this->skipWhitespace();
00135         }
00136 
00137         function testPlain () {
00138                 global $wgContLang;
00139 
00140                 // Preparing the dump
00141                 $fname = $this->getNewTempFile();
00142                 $dumper = new BackupDumper( array ( "--output=file:" . $fname ) );
00143                 $dumper->startId = $this->logId1;
00144                 $dumper->endId = $this->logId3 + 1;
00145                 $dumper->reporting = false;
00146                 $dumper->setDb( $this->db );
00147 
00148                 // Performing the dump
00149                 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
00150 
00151                 // Analyzing the dumped data
00152                 $this->assertDumpStart( $fname );
00153 
00154                 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
00155                         $this->userId1, null, "type", "subtype", "PageA" );
00156 
00157                 $this->assertNotNull( $wgContLang, "Content language object validation" );
00158                 $namespace = $wgContLang->getNsText( NS_TALK );
00159                 $this->assertInternalType( 'string', $namespace );
00160                 $this->assertGreaterThan( 0, strlen( $namespace ) );
00161                 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
00162                         $this->userId2, "SomeComment", "supress", "delete",
00163                         $namespace . ":PageB" );
00164 
00165                 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
00166                         $this->userId2, "SomeOtherComment", "move", "delete",
00167                         "PageA", array( 'key1' => 1, 3 => 'value3' ) );
00168 
00169                 $this->assertDumpEnd();
00170         }
00171 
00172         function testXmlDumpsBackupUseCaseLogging() {
00173                 global $wgContLang;
00174 
00175                 // Preparing the dump
00176                 $fname = $this->getNewTempFile();
00177                 $dumper = new BackupDumper( array ( "--output=gzip:" . $fname,
00178                                 "--reporting=2" ) );
00179                 $dumper->startId = $this->logId1;
00180                 $dumper->endId = $this->logId3 + 1;
00181                 $dumper->setDb( $this->db );
00182 
00183                 // xmldumps-backup demands reporting, although this is currently not
00184                 // implemented in BackupDumper, when dumping logging data. We
00185                 // nevertheless capture the output of the dump process already now,
00186                 // to be able to alert (once dumping produces reports) that this test
00187                 // needs updates.
00188                 $dumper->stderr = fopen( 'php://output', 'a' );
00189                 if ( $dumper->stderr === FALSE ) {
00190                         $this->fail( "Could not open stream for stderr" );
00191                 }
00192 
00193                 // Performing the dump
00194                 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
00195 
00196                 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
00197 
00198                 // Analyzing the dumped data
00199                 $this->gunzip( $fname );
00200 
00201                 $this->assertDumpStart( $fname );
00202 
00203                 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
00204                         $this->userId1, null, "type", "subtype", "PageA" );
00205 
00206                 $this->assertNotNull( $wgContLang, "Content language object validation" );
00207                 $namespace = $wgContLang->getNsText( NS_TALK );
00208                 $this->assertInternalType( 'string', $namespace );
00209                 $this->assertGreaterThan( 0, strlen( $namespace ) );
00210                 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
00211                         $this->userId2, "SomeComment", "supress", "delete",
00212                         $namespace . ":PageB" );
00213 
00214                 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
00215                         $this->userId2, "SomeOtherComment", "move", "delete",
00216                         "PageA", array( 'key1' => 1, 3 => 'value3' ) );
00217 
00218                 $this->assertDumpEnd();
00219 
00220                 // Currently, no reporting is implemented. Alert via failure, once
00221                 // this changes.
00222                 // If reporting for log dumps has been implemented, please update
00223                 // the following statement to catch good output
00224                 $this->expectOutputString( '' );
00225         }
00226 
00227 }