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