3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\MediaWikiServices
;
14 * Tests for log dumps of BackupDumper
16 * Some of these tests use the old constuctor for TextPassDumper
17 * and the dump() function, while others use the new loadWithArgv( $args )
18 * function and execute(). This is to ensure both the old and new methods
23 * @covers BackupDumper
25 class BackupDumperLoggerTest
extends DumpTestCase
{
27 // We'll add several log entries and users for this test. The following
28 // variables hold the corresponding ids.
29 private $userId1, $userId2;
30 private $logId1, $logId2, $logId3;
33 * adds a log entry to the database.
35 * @param string $type Type of the log entry
36 * @param string $subtype Subtype of the log entry
37 * @param User $user User that performs the logged operation
38 * @param int $ns Number of the namespace for the entry's target's title
39 * @param string $title Title of the entry's target
40 * @param string $comment Comment of the log entry
41 * @param array $parameters (optional) accompanying data that is attached to the entry
43 * @return int Id of the added log entry
45 private function addLogEntry( $type, $subtype, User
$user, $ns, $title,
46 $comment = null, $parameters = null
48 $logEntry = new ManualLogEntry( $type, $subtype );
49 $logEntry->setPerformer( $user );
50 $logEntry->setTarget( Title
::newFromText( $title, $ns ) );
51 if ( $comment !== null ) {
52 $logEntry->setComment( $comment );
54 if ( $parameters !== null ) {
55 $logEntry->setParameters( $parameters );
58 return $logEntry->insert();
61 function addDBData() {
62 $this->tablesUsed
[] = 'logging';
63 $this->tablesUsed
[] = 'user';
66 $user1 = User
::newFromName( 'BackupDumperLogUserA' );
67 $this->userId1
= $user1->getId();
68 if ( $this->userId1
=== 0 ) {
69 $user1->addToDatabase();
70 $this->userId1
= $user1->getId();
72 $this->assertGreaterThan( 0, $this->userId1
);
74 $user2 = User
::newFromName( 'BackupDumperLogUserB' );
75 $this->userId2
= $user2->getId();
76 if ( $this->userId2
=== 0 ) {
77 $user2->addToDatabase();
78 $this->userId2
= $user2->getId();
80 $this->assertGreaterThan( 0, $this->userId2
);
82 $this->logId1
= $this->addLogEntry( 'type', 'subtype',
83 $user1, NS_MAIN
, "PageA" );
84 $this->assertGreaterThan( 0, $this->logId1
);
86 $this->logId2
= $this->addLogEntry( 'supress', 'delete',
87 $user2, NS_TALK
, "PageB", "SomeComment" );
88 $this->assertGreaterThan( 0, $this->logId2
);
90 $this->logId3
= $this->addLogEntry( 'move', 'delete',
91 $user2, NS_MAIN
, "PageA", "SomeOtherComment",
92 [ 'key1' => 1, 3 => 'value3' ] );
93 $this->assertGreaterThan( 0, $this->logId3
);
94 } catch ( Exception
$e ) {
95 // We'd love to pass $e directly. However, ... see
96 // documentation of exceptionFromAddDBData in
98 $this->exceptionFromAddDBData
= $e;
102 function testPlain() {
103 // Preparing the dump
104 $fname = $this->getNewTempFile();
106 $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
107 $dumper->startId
= $this->logId1
;
108 $dumper->endId
= $this->logId3 +
1;
109 $dumper->reporting
= false;
110 $dumper->setDB( $this->db
);
112 // Performing the dump
113 $dumper->dump( WikiExporter
::LOGS
, WikiExporter
::TEXT
);
115 // Analyzing the dumped data
116 $this->assertDumpSchema( $fname, $this->getXmlSchemaPath() );
118 $asserter = $this->getDumpAsserter();
119 $asserter->assertDumpStart( $fname );
121 $asserter->assertLogItem( $this->logId1
, "BackupDumperLogUserA",
122 $this->userId1
, null, "type", "subtype", "PageA" );
124 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
125 $this->assertNotNull( $contLang, "Content language object validation" );
126 $namespace = $contLang->getNsText( NS_TALK
);
127 $this->assertInternalType( 'string', $namespace );
128 $this->assertGreaterThan( 0, strlen( $namespace ) );
129 $asserter->assertLogItem( $this->logId2
, "BackupDumperLogUserB",
130 $this->userId2
, "SomeComment", "supress", "delete",
131 $namespace . ":PageB" );
133 $asserter->assertLogItem( $this->logId3
, "BackupDumperLogUserB",
134 $this->userId2
, "SomeOtherComment", "move", "delete",
135 "PageA", [ 'key1' => 1, 3 => 'value3' ] );
137 $asserter->assertDumpEnd();
140 function testXmlDumpsBackupUseCaseLogging() {
141 $this->checkHasGzip();
143 // Preparing the dump
144 $fname = $this->getNewTempFile();
146 $dumper = new DumpBackup();
147 $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
149 $dumper->startId
= $this->logId1
;
150 $dumper->endId
= $this->logId3 +
1;
151 $dumper->setDB( $this->db
);
153 // xmldumps-backup demands reporting, although this is currently not
154 // implemented in BackupDumper, when dumping logging data. We
155 // nevertheless capture the output of the dump process already now,
156 // to be able to alert (once dumping produces reports) that this test
158 $dumper->stderr
= fopen( 'php://output', 'a' );
159 if ( $dumper->stderr
=== false ) {
160 $this->fail( "Could not open stream for stderr" );
163 // Performing the dump
166 $this->assertTrue( fclose( $dumper->stderr
), "Closing stderr handle" );
168 // Analyzing the dumped data
169 $this->gunzip( $fname );
171 $this->assertDumpSchema( $fname, $this->getXmlSchemaPath() );
173 $asserter = $this->getDumpAsserter();
174 $asserter->assertDumpStart( $fname );
176 $asserter->assertLogItem( $this->logId1
, "BackupDumperLogUserA",
177 $this->userId1
, null, "type", "subtype", "PageA" );
179 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
180 $this->assertNotNull( $contLang, "Content language object validation" );
181 $namespace = $contLang->getNsText( NS_TALK
);
182 $this->assertInternalType( 'string', $namespace );
183 $this->assertGreaterThan( 0, strlen( $namespace ) );
184 $asserter->assertLogItem( $this->logId2
, "BackupDumperLogUserB",
185 $this->userId2
, "SomeComment", "supress", "delete",
186 $namespace . ":PageB" );
188 $asserter->assertLogItem( $this->logId3
, "BackupDumperLogUserB",
189 $this->userId2
, "SomeOtherComment", "move", "delete",
190 "PageA", [ 'key1' => 1, 3 => 'value3' ] );
192 $asserter->assertDumpEnd();
194 // Currently, no reporting is implemented. Alert via failure, once
196 // If reporting for log dumps has been implemented, please update
197 // the following statement to catch good output
198 $this->expectOutputString( '' );