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() ) {
106 $this->assertNodeStart( "logitem" );
107 $this->skipWhitespace();
109 $this->assertTextNode( "id", $id );
110 $this->assertTextNode( "timestamp", false );
112 $this->assertNodeStart( "contributor" );
113 $this->skipWhitespace();
114 $this->assertTextNode( "username", $user_name );
115 $this->assertTextNode( "id", $user_id );
116 $this->assertNodeEnd( "contributor" );
117 $this->skipWhitespace();
119 if ( $comment !== null ) {
120 $this->assertTextNode( "comment", $comment );
122 $this->assertTextNode( "type", $type );
123 $this->assertTextNode( "action", $subtype );
124 $this->assertTextNode( "logtitle", $title );
126 $this->assertNodeStart( "params" );
127 $parameters_xml = unserialize( $this->xml
->value
);
128 $this->assertEquals( $parameters, $parameters_xml );
129 $this->assertTrue( $this->xml
->read(), "Skipping past processed text of params" );
130 $this->assertNodeEnd( "params" );
131 $this->skipWhitespace();
133 $this->assertNodeEnd( "logitem" );
134 $this->skipWhitespace();
137 function testPlain () {
140 // Preparing the dump
141 $fname = $this->getNewTempFile();
142 $dumper = new BackupDumper( array ( "--output=file:" . $fname ) );
143 $dumper->startId
= $this->logId1
;
144 $dumper->endId
= $this->logId3 +
1;
145 $dumper->reporting
= false;
146 $dumper->setDb( $this->db
);
148 // Performing the dump
149 $dumper->dump( WikiExporter
::LOGS
, WikiExporter
::TEXT
);
151 // Analyzing the dumped data
152 $this->assertDumpStart( $fname );
154 $this->assertLogItem( $this->logId1
, "BackupDumperLogUserA",
155 $this->userId1
, null, "type", "subtype", "PageA" );
157 $this->assertNotNull( $wgContLang, "Content language object validation" );
158 $namespace = $wgContLang->getNsText( NS_TALK
);
159 $this->assertInternalType( 'string', $namespace );
160 $this->assertGreaterThan( 0, strlen( $namespace ) );
161 $this->assertLogItem( $this->logId2
, "BackupDumperLogUserB",
162 $this->userId2
, "SomeComment", "supress", "delete",
163 $namespace . ":PageB" );
165 $this->assertLogItem( $this->logId3
, "BackupDumperLogUserB",
166 $this->userId2
, "SomeOtherComment", "move", "delete",
167 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
169 $this->assertDumpEnd();
172 function testXmlDumpsBackupUseCaseLogging() {
175 // Preparing the dump
176 $fname = $this->getNewTempFile();
177 $dumper = new BackupDumper( array ( "--output=gzip:" . $fname,
179 $dumper->startId
= $this->logId1
;
180 $dumper->endId
= $this->logId3 +
1;
181 $dumper->setDb( $this->db
);
183 // xmldumps-backup demands reporting, although this is currently not
184 // implemented in BackupDumper, when dumping logging data. We
185 // nevertheless capture the output of the dump process already now,
186 // to be able to alert (once dumping produces reports) that this test
188 $dumper->stderr
= fopen( 'php://output', 'a' );
189 if ( $dumper->stderr
=== FALSE ) {
190 $this->fail( "Could not open stream for stderr" );
193 // Performing the dump
194 $dumper->dump( WikiExporter
::LOGS
, WikiExporter
::TEXT
);
196 $this->assertTrue( fclose( $dumper->stderr
), "Closing stderr handle" );
198 // Analyzing the dumped data
199 $this->gunzip( $fname );
201 $this->assertDumpStart( $fname );
203 $this->assertLogItem( $this->logId1
, "BackupDumperLogUserA",
204 $this->userId1
, null, "type", "subtype", "PageA" );
206 $this->assertNotNull( $wgContLang, "Content language object validation" );
207 $namespace = $wgContLang->getNsText( NS_TALK
);
208 $this->assertInternalType( 'string', $namespace );
209 $this->assertGreaterThan( 0, strlen( $namespace ) );
210 $this->assertLogItem( $this->logId2
, "BackupDumperLogUserB",
211 $this->userId2
, "SomeComment", "supress", "delete",
212 $namespace . ":PageB" );
214 $this->assertLogItem( $this->logId3
, "BackupDumperLogUserB",
215 $this->userId2
, "SomeOtherComment", "move", "delete",
216 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
218 $this->assertDumpEnd();
220 // Currently, no reporting is implemented. Alert via failure, once
222 // If reporting for log dumps has been implemented, please update
223 // the following statement to catch good output
224 $this->expectOutputString( '' );