MediaWiki  REL1_22
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 
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 
00087 
00102     private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
00103         $subtype, $title, $parameters = array()
00104     ) {
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         $this->checkHasGzip();
00176 
00177         // Preparing the dump
00178         $fname = $this->getNewTempFile();
00179         $dumper = new BackupDumper( array( "--output=gzip:" . $fname,
00180             "--reporting=2" ) );
00181         $dumper->startId = $this->logId1;
00182         $dumper->endId = $this->logId3 + 1;
00183         $dumper->setDb( $this->db );
00184 
00185         // xmldumps-backup demands reporting, although this is currently not
00186         // implemented in BackupDumper, when dumping logging data. We
00187         // nevertheless capture the output of the dump process already now,
00188         // to be able to alert (once dumping produces reports) that this test
00189         // needs updates.
00190         $dumper->stderr = fopen( 'php://output', 'a' );
00191         if ( $dumper->stderr === false ) {
00192             $this->fail( "Could not open stream for stderr" );
00193         }
00194 
00195         // Performing the dump
00196         $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
00197 
00198         $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
00199 
00200         // Analyzing the dumped data
00201         $this->gunzip( $fname );
00202 
00203         $this->assertDumpStart( $fname );
00204 
00205         $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
00206             $this->userId1, null, "type", "subtype", "PageA" );
00207 
00208         $this->assertNotNull( $wgContLang, "Content language object validation" );
00209         $namespace = $wgContLang->getNsText( NS_TALK );
00210         $this->assertInternalType( 'string', $namespace );
00211         $this->assertGreaterThan( 0, strlen( $namespace ) );
00212         $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
00213             $this->userId2, "SomeComment", "supress", "delete",
00214             $namespace . ":PageB" );
00215 
00216         $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
00217             $this->userId2, "SomeOtherComment", "move", "delete",
00218             "PageA", array( 'key1' => 1, 3 => 'value3' ) );
00219 
00220         $this->assertDumpEnd();
00221 
00222         // Currently, no reporting is implemented. Alert via failure, once
00223         // this changes.
00224         // If reporting for log dumps has been implemented, please update
00225         // the following statement to catch good output
00226         $this->expectOutputString( '' );
00227     }
00228 }