5cf172e6aa813a8b4326f3fc85a23918f1386a41
3 * Tests for log dumps of BackupDumper
8 class BackupDumperLoggerTest
extends DumpTestCase
{
11 // We'll add several log entries and users for this test. The following
12 // variables hold the corresponding ids.
13 private $userId1, $userId2;
14 private $logId1, $logId2, $logId3;
17 * adds a log entry to the database.
19 * @param $type string: type of the log entry
20 * @param $subtype string: subtype of the log entry
21 * @param $user User: user that performs the logged operation
22 * @param $ns int: number of the namespace for the entry's target's title
23 * @param $title string: title of the entry's target
24 * @param $comment string: comment of the log entry
25 * @param $parameters Array: (optional) accompanying data that is attached
28 * @return int id of the added log entry
30 private function addLogEntry( $type, $subtype, User
$user, $ns, $title,
31 $comment = null, $parameters = null
33 $logEntry = new ManualLogEntry( $type, $subtype );
34 $logEntry->setPerformer( $user );
35 $logEntry->setTarget( Title
::newFromText( $title, $ns ) );
36 if ( $comment !== null ) {
37 $logEntry->setComment( $comment );
39 if ( $parameters !== null ) {
40 $logEntry->setParameters( $parameters );
42 return $logEntry->insert();
45 function addDBData() {
46 $this->tablesUsed
[] = 'logging';
47 $this->tablesUsed
[] = 'user';
50 $user1 = User
::newFromName( 'BackupDumperLogUserA' );
51 $this->userId1
= $user1->getId();
52 if ( $this->userId1
=== 0 ) {
53 $user1->addToDatabase();
54 $this->userId1
= $user1->getId();
56 $this->assertGreaterThan( 0, $this->userId1
);
58 $user2 = User
::newFromName( 'BackupDumperLogUserB' );
59 $this->userId2
= $user2->getId();
60 if ( $this->userId2
=== 0 ) {
61 $user2->addToDatabase();
62 $this->userId2
= $user2->getId();
64 $this->assertGreaterThan( 0, $this->userId2
);
66 $this->logId1
= $this->addLogEntry( 'type', 'subtype',
67 $user1, NS_MAIN
, "PageA" );
68 $this->assertGreaterThan( 0, $this->logId1
);
70 $this->logId2
= $this->addLogEntry( 'supress', 'delete',
71 $user2, NS_TALK
, "PageB", "SomeComment" );
72 $this->assertGreaterThan( 0, $this->logId2
);
74 $this->logId3
= $this->addLogEntry( 'move', 'delete',
75 $user2, NS_MAIN
, "PageA", "SomeOtherComment",
76 array( 'key1' => 1, 3 => 'value3' ) );
77 $this->assertGreaterThan( 0, $this->logId3
);
79 } catch ( Exception
$e ) {
80 // We'd love to pass $e directly. However, ... see
81 // documentation of exceptionFromAddDBData in
83 $this->exceptionFromAddDBData
= $e;
90 * asserts that the xml reader is at the beginning of a log entry and skips over
91 * it while analyzing it.
93 * @param $id int: id of the log entry
94 * @param $user_name string: user name of the log entry's performer
95 * @param $user_id int: user id of the log entry 's performer
96 * @param $comment string|null: comment of the log entry. If null, the comment
98 * @param $type string: type of the log entry
99 * @param $subtype string: subtype of the log entry
100 * @param $title string: title of the log entry's target
101 * @param $parameters array: (optional) unserialized data accompanying the log entry
103 private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
104 $subtype, $title, $parameters = array()
107 $this->assertNodeStart( "logitem" );
108 $this->skipWhitespace();
110 $this->assertTextNode( "id", $id );
111 $this->assertTextNode( "timestamp", false );
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();
120 if ( $comment !== null ) {
121 $this->assertTextNode( "comment", $comment );
123 $this->assertTextNode( "type", $type );
124 $this->assertTextNode( "action", $subtype );
125 $this->assertTextNode( "logtitle", $title );
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();
134 $this->assertNodeEnd( "logitem" );
135 $this->skipWhitespace();
138 function testPlain() {
141 // Preparing the dump
142 $fname = $this->getNewTempFile();
143 $dumper = new BackupDumper( array( "--output=file:" . $fname ) );
144 $dumper->startId
= $this->logId1
;
145 $dumper->endId
= $this->logId3 +
1;
146 $dumper->reporting
= false;
147 $dumper->setDb( $this->db
);
149 // Performing the dump
150 $dumper->dump( WikiExporter
::LOGS
, WikiExporter
::TEXT
);
152 // Analyzing the dumped data
153 $this->assertDumpStart( $fname );
155 $this->assertLogItem( $this->logId1
, "BackupDumperLogUserA",
156 $this->userId1
, null, "type", "subtype", "PageA" );
158 $this->assertNotNull( $wgContLang, "Content language object validation" );
159 $namespace = $wgContLang->getNsText( NS_TALK
);
160 $this->assertInternalType( 'string', $namespace );
161 $this->assertGreaterThan( 0, strlen( $namespace ) );
162 $this->assertLogItem( $this->logId2
, "BackupDumperLogUserB",
163 $this->userId2
, "SomeComment", "supress", "delete",
164 $namespace . ":PageB" );
166 $this->assertLogItem( $this->logId3
, "BackupDumperLogUserB",
167 $this->userId2
, "SomeOtherComment", "move", "delete",
168 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
170 $this->assertDumpEnd();
173 function testXmlDumpsBackupUseCaseLogging() {
176 $this->checkHasGzip();
178 // Preparing the dump
179 $fname = $this->getNewTempFile();
180 $dumper = new BackupDumper( array( "--output=gzip:" . $fname,
182 $dumper->startId
= $this->logId1
;
183 $dumper->endId
= $this->logId3 +
1;
184 $dumper->setDb( $this->db
);
186 // xmldumps-backup demands reporting, although this is currently not
187 // implemented in BackupDumper, when dumping logging data. We
188 // nevertheless capture the output of the dump process already now,
189 // to be able to alert (once dumping produces reports) that this test
191 $dumper->stderr
= fopen( 'php://output', 'a' );
192 if ( $dumper->stderr
=== false ) {
193 $this->fail( "Could not open stream for stderr" );
196 // Performing the dump
197 $dumper->dump( WikiExporter
::LOGS
, WikiExporter
::TEXT
);
199 $this->assertTrue( fclose( $dumper->stderr
), "Closing stderr handle" );
201 // Analyzing the dumped data
202 $this->gunzip( $fname );
204 $this->assertDumpStart( $fname );
206 $this->assertLogItem( $this->logId1
, "BackupDumperLogUserA",
207 $this->userId1
, null, "type", "subtype", "PageA" );
209 $this->assertNotNull( $wgContLang, "Content language object validation" );
210 $namespace = $wgContLang->getNsText( NS_TALK
);
211 $this->assertInternalType( 'string', $namespace );
212 $this->assertGreaterThan( 0, strlen( $namespace ) );
213 $this->assertLogItem( $this->logId2
, "BackupDumperLogUserB",
214 $this->userId2
, "SomeComment", "supress", "delete",
215 $namespace . ":PageB" );
217 $this->assertLogItem( $this->logId3
, "BackupDumperLogUserB",
218 $this->userId2
, "SomeOtherComment", "move", "delete",
219 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
221 $this->assertDumpEnd();
223 // Currently, no reporting is implemented. Alert via failure, once
225 // If reporting for log dumps has been implemented, please update
226 // the following statement to catch good output
227 $this->expectOutputString( '' );