MediaWiki  REL1_21
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 
00107                 $this->assertNodeStart( "logitem" );
00108                 $this->skipWhitespace();
00109 
00110                 $this->assertTextNode( "id", $id );
00111                 $this->assertTextNode( "timestamp", false );
00112 
00113                 $this->assertNodeStart( "contributor" );
00114                 $this->skipWhitespace();
00115                 $this->assertTextNode( "username", $user_name );
00116                 $this->assertTextNode( "id", $user_id );
00117                 $this->assertNodeEnd( "contributor" );
00118                 $this->skipWhitespace();
00119 
00120                 if ( $comment !== null ) {
00121                         $this->assertTextNode( "comment", $comment );
00122                 }
00123                 $this->assertTextNode( "type", $type );
00124                 $this->assertTextNode( "action", $subtype );
00125                 $this->assertTextNode( "logtitle", $title );
00126 
00127                 $this->assertNodeStart( "params" );
00128                 $parameters_xml = unserialize( $this->xml->value );
00129                 $this->assertEquals( $parameters, $parameters_xml );
00130                 $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
00131                 $this->assertNodeEnd( "params" );
00132                 $this->skipWhitespace();
00133 
00134                 $this->assertNodeEnd( "logitem" );
00135                 $this->skipWhitespace();
00136         }
00137 
00138         function testPlain() {
00139                 global $wgContLang;
00140 
00141                 // Preparing the dump
00142                 $fname = $this->getNewTempFile();
00143                 $dumper = new BackupDumper( array( "--output=file:" . $fname ) );
00144                 $dumper->startId = $this->logId1;
00145                 $dumper->endId = $this->logId3 + 1;
00146                 $dumper->reporting = false;
00147                 $dumper->setDb( $this->db );
00148 
00149                 // Performing the dump
00150                 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
00151 
00152                 // Analyzing the dumped data
00153                 $this->assertDumpStart( $fname );
00154 
00155                 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
00156                         $this->userId1, null, "type", "subtype", "PageA" );
00157 
00158                 $this->assertNotNull( $wgContLang, "Content language object validation" );
00159                 $namespace = $wgContLang->getNsText( NS_TALK );
00160                 $this->assertInternalType( 'string', $namespace );
00161                 $this->assertGreaterThan( 0, strlen( $namespace ) );
00162                 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
00163                         $this->userId2, "SomeComment", "supress", "delete",
00164                         $namespace . ":PageB" );
00165 
00166                 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
00167                         $this->userId2, "SomeOtherComment", "move", "delete",
00168                         "PageA", array( 'key1' => 1, 3 => 'value3' ) );
00169 
00170                 $this->assertDumpEnd();
00171         }
00172 
00173         function testXmlDumpsBackupUseCaseLogging() {
00174                 global $wgContLang;
00175 
00176                 $this->checkHasGzip();
00177 
00178                 // Preparing the dump
00179                 $fname = $this->getNewTempFile();
00180                 $dumper = new BackupDumper( array( "--output=gzip:" . $fname,
00181                         "--reporting=2" ) );
00182                 $dumper->startId = $this->logId1;
00183                 $dumper->endId = $this->logId3 + 1;
00184                 $dumper->setDb( $this->db );
00185 
00186                 // xmldumps-backup demands reporting, although this is currently not
00187                 // implemented in BackupDumper, when dumping logging data. We
00188                 // nevertheless capture the output of the dump process already now,
00189                 // to be able to alert (once dumping produces reports) that this test
00190                 // needs updates.
00191                 $dumper->stderr = fopen( 'php://output', 'a' );
00192                 if ( $dumper->stderr === false ) {
00193                         $this->fail( "Could not open stream for stderr" );
00194                 }
00195 
00196                 // Performing the dump
00197                 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
00198 
00199                 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
00200 
00201                 // Analyzing the dumped data
00202                 $this->gunzip( $fname );
00203 
00204                 $this->assertDumpStart( $fname );
00205 
00206                 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
00207                         $this->userId1, null, "type", "subtype", "PageA" );
00208 
00209                 $this->assertNotNull( $wgContLang, "Content language object validation" );
00210                 $namespace = $wgContLang->getNsText( NS_TALK );
00211                 $this->assertInternalType( 'string', $namespace );
00212                 $this->assertGreaterThan( 0, strlen( $namespace ) );
00213                 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
00214                         $this->userId2, "SomeComment", "supress", "delete",
00215                         $namespace . ":PageB" );
00216 
00217                 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
00218                         $this->userId2, "SomeOtherComment", "move", "delete",
00219                         "PageA", array( 'key1' => 1, 3 => 'value3' ) );
00220 
00221                 $this->assertDumpEnd();
00222 
00223                 // Currently, no reporting is implemented. Alert via failure, once
00224                 // this changes.
00225                 // If reporting for log dumps has been implemented, please update
00226                 // the following statement to catch good output
00227                 $this->expectOutputString( '' );
00228         }
00229 
00230 }