Debian Codegraph @

tests/phpunit/maintenance/BackupDumperLoggerTest.php

raw symbols SCIP
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 * Tests for log dumps of BackupDumper
15 *
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
19 * work properly.
20 *
21 * @group Database
22 * @group Dump
23 * @covers \MediaWiki\Maintenance\BackupDumper
24 */
25 class BackupDumperLoggerTest extends DumpTestCase {
26
27 // We'll add several log entries and users for this test. The following
28 // variables hold the corresponding ids.
29 // phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.WrongStyle
30 private int $userId1;
31 private int $userId2;
32 private int $logId1;
33 private int $logId2;
34 private int $logId3;
35
36 /**
37 * adds a log entry to the database.
38 *
39 * @param string $type Type of the log entry
40 * @param string $subtype Subtype of the log entry
41 * @param User $user User that performs the logged operation
42 * @param int $ns Number of the namespace for the entry's target's title
43 * @param string $title Title of the entry's target
44 * @param string|null $comment Comment of the log entry
45 * @param array|null $parameters accompanying data that is attached to the entry
46 *
47 * @return int Id of the added log entry
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 // We'd love to pass $e directly. However, ... see
97 // documentation of exceptionFromAddDBData in
98 // DumpTestCase
99 $this->exceptionFromAddDBData = $e;
100 }
101 }
102
103 public function testPlain() {
104 // Preparing the dump
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 // Performing the dump
114 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
115
116 // Analyzing the dumped data
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 // Preparing the dump
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 // xmldumps-backup demands reporting, although this is currently not
155 // implemented in BackupDumper, when dumping logging data. We
156 // nevertheless capture the output of the dump process already now,
157 // to be able to alert (once dumping produces reports) that this test
158 // needs updates.
159 $dumper->stderr = fopen( 'php://output', 'a' );
160 if ( $dumper->stderr === false ) {
161 $this->fail( "Could not open stream for stderr" );
162 }
163
164 // Performing the dump
165 $dumper->execute();
166
167 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
168
169 // Analyzing the dumped data
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 // Currently, no reporting is implemented. Alert via failure, once
196 // this changes.
197 // If reporting for log dumps has been implemented, please update
198 // the following statement to catch good output
199 $this->expectOutputString( '' );
200 }
201 }