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