| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use DumpBackup; |
| 6 |
use Exception; |
| 7 |
use ManualLogEntry; |
| 8 |
use MediaWiki\MediaWikiServices; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\User\User; |
| 11 |
use WikiExporter; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class BackupDumperLoggerTest extends DumpTestCase { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private int $userId1; |
| 31 |
private int $userId2; |
| 32 |
private int $logId1; |
| 33 |
private int $logId2; |
| 34 |
private int $logId3; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private function addLogEntry( $type, $subtype, User $user, $ns, $title, |
| 50 |
$comment = null, $parameters = null |
| 51 |
) { |
| 52 |
$logEntry = new ManualLogEntry( $type, $subtype ); |
| 53 |
$logEntry->setPerformer( $user ); |
| 54 |
$logEntry->setTarget( Title::newFromText( $title, $ns ) ); |
| 55 |
if ( $comment !== null ) { |
| 56 |
$logEntry->setComment( $comment ); |
| 57 |
} |
| 58 |
if ( $parameters !== null ) { |
| 59 |
$logEntry->setParameters( $parameters ); |
| 60 |
} |
| 61 |
|
| 62 |
return $logEntry->insert(); |
| 63 |
} |
| 64 |
|
| 65 |
public function addDBData() { |
| 66 |
try { |
| 67 |
$user1 = User::newFromName( 'BackupDumperLogUserA' ); |
| 68 |
$this->userId1 = $user1->getId(); |
| 69 |
if ( $this->userId1 === 0 ) { |
| 70 |
$user1->addToDatabase(); |
| 71 |
$this->userId1 = $user1->getId(); |
| 72 |
} |
| 73 |
$this->assertGreaterThan( 0, $this->userId1 ); |
| 74 |
|
| 75 |
$user2 = User::newFromName( 'BackupDumperLogUserB' ); |
| 76 |
$this->userId2 = $user2->getId(); |
| 77 |
if ( $this->userId2 === 0 ) { |
| 78 |
$user2->addToDatabase(); |
| 79 |
$this->userId2 = $user2->getId(); |
| 80 |
} |
| 81 |
$this->assertGreaterThan( 0, $this->userId2 ); |
| 82 |
|
| 83 |
$this->logId1 = $this->addLogEntry( 'type', 'subtype', |
| 84 |
$user1, NS_MAIN, "PageA" ); |
| 85 |
$this->assertGreaterThan( 0, $this->logId1 ); |
| 86 |
|
| 87 |
$this->logId2 = $this->addLogEntry( 'supress', 'delete', |
| 88 |
$user2, NS_TALK, "PageB", "SomeComment" ); |
| 89 |
$this->assertGreaterThan( 0, $this->logId2 ); |
| 90 |
|
| 91 |
$this->logId3 = $this->addLogEntry( 'move', 'delete', |
| 92 |
$user2, NS_MAIN, "PageA", "SomeOtherComment", |
| 93 |
[ 'key1' => 1, 3 => 'value3' ] ); |
| 94 |
$this->assertGreaterThan( 0, $this->logId3 ); |
| 95 |
} catch ( Exception $e ) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
$this->exceptionFromAddDBData = $e; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public function testPlain() { |
| 104 |
|
| 105 |
$fname = $this->getNewTempFile(); |
| 106 |
|
| 107 |
$dumper = new DumpBackup( [ '--output=file:' . $fname ] ); |
| 108 |
$dumper->startId = $this->logId1; |
| 109 |
$dumper->endId = $this->logId3 + 1; |
| 110 |
$dumper->reporting = false; |
| 111 |
$dumper->setDB( $this->getDb() ); |
| 112 |
|
| 113 |
|
| 114 |
$dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT ); |
| 115 |
|
| 116 |
|
| 117 |
$this->assertDumpSchema( $fname, $this->getXmlSchemaPath() ); |
| 118 |
|
| 119 |
$asserter = $this->getDumpAsserter(); |
| 120 |
$asserter->assertDumpStart( $fname ); |
| 121 |
|
| 122 |
$asserter->assertLogItem( $this->logId1, "BackupDumperLogUserA", |
| 123 |
$this->userId1, null, "type", "subtype", "PageA" ); |
| 124 |
|
| 125 |
$contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 126 |
$this->assertNotNull( $contLang, "Content language object validation" ); |
| 127 |
$namespace = $contLang->getNsText( NS_TALK ); |
| 128 |
$this->assertIsString( $namespace ); |
| 129 |
$this->assertGreaterThan( 0, strlen( $namespace ) ); |
| 130 |
$asserter->assertLogItem( $this->logId2, "BackupDumperLogUserB", |
| 131 |
$this->userId2, "SomeComment", "supress", "delete", |
| 132 |
$namespace . ":PageB" ); |
| 133 |
|
| 134 |
$asserter->assertLogItem( $this->logId3, "BackupDumperLogUserB", |
| 135 |
$this->userId2, "SomeOtherComment", "move", "delete", |
| 136 |
"PageA", [ 'key1' => 1, 3 => 'value3' ] ); |
| 137 |
|
| 138 |
$asserter->assertDumpEnd(); |
| 139 |
} |
| 140 |
|
| 141 |
public function testXmlDumpsBackupUseCaseLogging() { |
| 142 |
$this->checkHasGzip(); |
| 143 |
|
| 144 |
|
| 145 |
$fname = $this->getNewTempFile(); |
| 146 |
|
| 147 |
$dumper = new DumpBackup(); |
| 148 |
$dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname, |
| 149 |
'--reporting=2' ] ); |
| 150 |
$dumper->startId = $this->logId1; |
| 151 |
$dumper->endId = $this->logId3 + 1; |
| 152 |
$dumper->setDB( $this->getDb() ); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
$dumper->stderr = fopen( 'php://output', 'a' ); |
| 160 |
if ( $dumper->stderr === false ) { |
| 161 |
$this->fail( "Could not open stream for stderr" ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
$dumper->execute(); |
| 166 |
|
| 167 |
$this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" ); |
| 168 |
|
| 169 |
|
| 170 |
$this->gunzip( $fname ); |
| 171 |
|
| 172 |
$this->assertDumpSchema( $fname, $this->getXmlSchemaPath() ); |
| 173 |
|
| 174 |
$asserter = $this->getDumpAsserter(); |
| 175 |
$asserter->assertDumpStart( $fname ); |
| 176 |
|
| 177 |
$asserter->assertLogItem( $this->logId1, "BackupDumperLogUserA", |
| 178 |
$this->userId1, null, "type", "subtype", "PageA" ); |
| 179 |
|
| 180 |
$contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 181 |
$this->assertNotNull( $contLang, "Content language object validation" ); |
| 182 |
$namespace = $contLang->getNsText( NS_TALK ); |
| 183 |
$this->assertIsString( $namespace ); |
| 184 |
$this->assertGreaterThan( 0, strlen( $namespace ) ); |
| 185 |
$asserter->assertLogItem( $this->logId2, "BackupDumperLogUserB", |
| 186 |
$this->userId2, "SomeComment", "supress", "delete", |
| 187 |
$namespace . ":PageB" ); |
| 188 |
|
| 189 |
$asserter->assertLogItem( $this->logId3, "BackupDumperLogUserB", |
| 190 |
$this->userId2, "SomeOtherComment", "move", "delete", |
| 191 |
"PageA", [ 'key1' => 1, 3 => 'value3' ] ); |
| 192 |
|
| 193 |
$asserter->assertDumpEnd(); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
$this->expectOutputString( '' ); |
| 200 |
} |
| 201 |
} |