]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/maintenance/backup_LogTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / maintenance / backup_LogTest.php
1 <?php
2 /**
3  * Tests for log dumps of BackupDumper
4  *
5  * Some of these tests use the old constuctor for TextPassDumper
6  * and the dump() function, while others use the new loadWithArgv( $args )
7  * function and execute(). This is to ensure both the old and new methods
8  * work properly.
9  *
10  * @group Database
11  * @group Dump
12  * @covers BackupDumper
13  */
14 class BackupDumperLoggerTest extends DumpTestCase {
15
16         // We'll add several log entries and users for this test. The following
17         // variables hold the corresponding ids.
18         private $userId1, $userId2;
19         private $logId1, $logId2, $logId3;
20
21         /**
22          * adds a log entry to the database.
23          *
24          * @param string $type Type of the log entry
25          * @param string $subtype Subtype of the log entry
26          * @param User $user User that performs the logged operation
27          * @param int $ns Number of the namespace for the entry's target's title
28          * @param string $title Title of the entry's target
29          * @param string $comment Comment of the log entry
30          * @param array $parameters (optional) accompanying data that is attached to the entry
31          *
32          * @return int Id of the added log entry
33          */
34         private function addLogEntry( $type, $subtype, User $user, $ns, $title,
35                 $comment = null, $parameters = null
36         ) {
37                 $logEntry = new ManualLogEntry( $type, $subtype );
38                 $logEntry->setPerformer( $user );
39                 $logEntry->setTarget( Title::newFromText( $title, $ns ) );
40                 if ( $comment !== null ) {
41                         $logEntry->setComment( $comment );
42                 }
43                 if ( $parameters !== null ) {
44                         $logEntry->setParameters( $parameters );
45                 }
46
47                 return $logEntry->insert();
48         }
49
50         function addDBData() {
51                 $this->tablesUsed[] = 'logging';
52                 $this->tablesUsed[] = 'user';
53
54                 try {
55                         $user1 = User::newFromName( 'BackupDumperLogUserA' );
56                         $this->userId1 = $user1->getId();
57                         if ( $this->userId1 === 0 ) {
58                                 $user1->addToDatabase();
59                                 $this->userId1 = $user1->getId();
60                         }
61                         $this->assertGreaterThan( 0, $this->userId1 );
62
63                         $user2 = User::newFromName( 'BackupDumperLogUserB' );
64                         $this->userId2 = $user2->getId();
65                         if ( $this->userId2 === 0 ) {
66                                 $user2->addToDatabase();
67                                 $this->userId2 = $user2->getId();
68                         }
69                         $this->assertGreaterThan( 0, $this->userId2 );
70
71                         $this->logId1 = $this->addLogEntry( 'type', 'subtype',
72                                 $user1, NS_MAIN, "PageA" );
73                         $this->assertGreaterThan( 0, $this->logId1 );
74
75                         $this->logId2 = $this->addLogEntry( 'supress', 'delete',
76                                 $user2, NS_TALK, "PageB", "SomeComment" );
77                         $this->assertGreaterThan( 0, $this->logId2 );
78
79                         $this->logId3 = $this->addLogEntry( 'move', 'delete',
80                                 $user2, NS_MAIN, "PageA", "SomeOtherComment",
81                                 [ 'key1' => 1, 3 => 'value3' ] );
82                         $this->assertGreaterThan( 0, $this->logId3 );
83                 } catch ( Exception $e ) {
84                         // We'd love to pass $e directly. However, ... see
85                         // documentation of exceptionFromAddDBData in
86                         // DumpTestCase
87                         $this->exceptionFromAddDBData = $e;
88                 }
89         }
90
91         /**
92          * asserts that the xml reader is at the beginning of a log entry and skips over
93          * it while analyzing it.
94          *
95          * @param int $id Id of the log entry
96          * @param string $user_name User name of the log entry's performer
97          * @param int $user_id User id of the log entry 's performer
98          * @param string|null $comment Comment of the log entry. If null, the comment text is ignored.
99          * @param string $type Type of the log entry
100          * @param string $subtype Subtype of the log entry
101          * @param string $title Title of the log entry's target
102          * @param array $parameters (optional) unserialized data accompanying the log entry
103          */
104         private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
105                 $subtype, $title, $parameters = []
106         ) {
107                 $this->assertNodeStart( "logitem" );
108                 $this->skipWhitespace();
109
110                 $this->assertTextNode( "id", $id );
111                 $this->assertTextNode( "timestamp", false );
112
113                 $this->assertNodeStart( "contributor" );
114                 $this->skipWhitespace();
115                 $this->assertTextNode( "username", $user_name );
116                 $this->assertTextNode( "id", $user_id );
117                 $this->assertNodeEnd( "contributor" );
118                 $this->skipWhitespace();
119
120                 if ( $comment !== null ) {
121                         $this->assertTextNode( "comment", $comment );
122                 }
123                 $this->assertTextNode( "type", $type );
124                 $this->assertTextNode( "action", $subtype );
125                 $this->assertTextNode( "logtitle", $title );
126
127                 $this->assertNodeStart( "params" );
128                 $parameters_xml = unserialize( $this->xml->value );
129                 $this->assertEquals( $parameters, $parameters_xml );
130                 $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
131                 $this->assertNodeEnd( "params" );
132                 $this->skipWhitespace();
133
134                 $this->assertNodeEnd( "logitem" );
135                 $this->skipWhitespace();
136         }
137
138         function testPlain() {
139                 global $wgContLang;
140
141                 // Preparing the dump
142                 $fname = $this->getNewTempFile();
143
144                 $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
145                 $dumper->startId = $this->logId1;
146                 $dumper->endId = $this->logId3 + 1;
147                 $dumper->reporting = false;
148                 $dumper->setDB( $this->db );
149
150                 // Performing the dump
151                 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
152
153                 // Analyzing the dumped data
154                 $this->assertDumpStart( $fname );
155
156                 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
157                         $this->userId1, null, "type", "subtype", "PageA" );
158
159                 $this->assertNotNull( $wgContLang, "Content language object validation" );
160                 $namespace = $wgContLang->getNsText( NS_TALK );
161                 $this->assertInternalType( 'string', $namespace );
162                 $this->assertGreaterThan( 0, strlen( $namespace ) );
163                 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
164                         $this->userId2, "SomeComment", "supress", "delete",
165                         $namespace . ":PageB" );
166
167                 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
168                         $this->userId2, "SomeOtherComment", "move", "delete",
169                         "PageA", [ 'key1' => 1, 3 => 'value3' ] );
170
171                 $this->assertDumpEnd();
172         }
173
174         function testXmlDumpsBackupUseCaseLogging() {
175                 global $wgContLang;
176
177                 $this->checkHasGzip();
178
179                 // Preparing the dump
180                 $fname = $this->getNewTempFile();
181
182                 $dumper = new DumpBackup();
183                 $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
184                         '--reporting=2' ] );
185                 $dumper->startId = $this->logId1;
186                 $dumper->endId = $this->logId3 + 1;
187                 $dumper->setDB( $this->db );
188
189                 // xmldumps-backup demands reporting, although this is currently not
190                 // implemented in BackupDumper, when dumping logging data. We
191                 // nevertheless capture the output of the dump process already now,
192                 // to be able to alert (once dumping produces reports) that this test
193                 // needs updates.
194                 $dumper->stderr = fopen( 'php://output', 'a' );
195                 if ( $dumper->stderr === false ) {
196                         $this->fail( "Could not open stream for stderr" );
197                 }
198
199                 // Performing the dump
200                 $dumper->execute();
201
202                 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
203
204                 // Analyzing the dumped data
205                 $this->gunzip( $fname );
206
207                 $this->assertDumpStart( $fname );
208
209                 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
210                         $this->userId1, null, "type", "subtype", "PageA" );
211
212                 $this->assertNotNull( $wgContLang, "Content language object validation" );
213                 $namespace = $wgContLang->getNsText( NS_TALK );
214                 $this->assertInternalType( 'string', $namespace );
215                 $this->assertGreaterThan( 0, strlen( $namespace ) );
216                 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
217                         $this->userId2, "SomeComment", "supress", "delete",
218                         $namespace . ":PageB" );
219
220                 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
221                         $this->userId2, "SomeOtherComment", "move", "delete",
222                         "PageA", [ 'key1' => 1, 3 => 'value3' ] );
223
224                 $this->assertDumpEnd();
225
226                 // Currently, no reporting is implemented. Alert via failure, once
227                 // this changes.
228                 // If reporting for log dumps has been implemented, please update
229                 // the following statement to catch good output
230                 $this->expectOutputString( '' );
231         }
232 }