Merge "@covers tags for maintenance tests"
[lhc/web/wiklou.git] / tests / phpunit / maintenance / backup_LogTest.php
1 <?php
2 /**
3 * Tests for log dumps of BackupDumper
4 *
5 * @group Database
6 * @group Dump
7 * @covers BackupDumper
8 */
9 class BackupDumperLoggerTest extends DumpTestCase {
10
11
12 // We'll add several log entries and users for this test. The following
13 // variables hold the corresponding ids.
14 private $userId1, $userId2;
15 private $logId1, $logId2, $logId3;
16
17 /**
18 * adds a log entry to the database.
19 *
20 * @param $type string: type of the log entry
21 * @param $subtype string: subtype of the log entry
22 * @param $user User: user that performs the logged operation
23 * @param $ns int: number of the namespace for the entry's target's title
24 * @param $title string: title of the entry's target
25 * @param $comment string: comment of the log entry
26 * @param $parameters Array: (optional) accompanying data that is attached
27 * to the entry
28 *
29 * @return int id of the added log entry
30 */
31 private function addLogEntry( $type, $subtype, User $user, $ns, $title,
32 $comment = null, $parameters = null
33 ) {
34 $logEntry = new ManualLogEntry( $type, $subtype );
35 $logEntry->setPerformer( $user );
36 $logEntry->setTarget( Title::newFromText( $title, $ns ) );
37 if ( $comment !== null ) {
38 $logEntry->setComment( $comment );
39 }
40 if ( $parameters !== null ) {
41 $logEntry->setParameters( $parameters );
42 }
43
44 return $logEntry->insert();
45 }
46
47 function addDBData() {
48 $this->tablesUsed[] = 'logging';
49 $this->tablesUsed[] = 'user';
50
51 try {
52 $user1 = User::newFromName( 'BackupDumperLogUserA' );
53 $this->userId1 = $user1->getId();
54 if ( $this->userId1 === 0 ) {
55 $user1->addToDatabase();
56 $this->userId1 = $user1->getId();
57 }
58 $this->assertGreaterThan( 0, $this->userId1 );
59
60 $user2 = User::newFromName( 'BackupDumperLogUserB' );
61 $this->userId2 = $user2->getId();
62 if ( $this->userId2 === 0 ) {
63 $user2->addToDatabase();
64 $this->userId2 = $user2->getId();
65 }
66 $this->assertGreaterThan( 0, $this->userId2 );
67
68 $this->logId1 = $this->addLogEntry( 'type', 'subtype',
69 $user1, NS_MAIN, "PageA" );
70 $this->assertGreaterThan( 0, $this->logId1 );
71
72 $this->logId2 = $this->addLogEntry( 'supress', 'delete',
73 $user2, NS_TALK, "PageB", "SomeComment" );
74 $this->assertGreaterThan( 0, $this->logId2 );
75
76 $this->logId3 = $this->addLogEntry( 'move', 'delete',
77 $user2, NS_MAIN, "PageA", "SomeOtherComment",
78 array( 'key1' => 1, 3 => 'value3' ) );
79 $this->assertGreaterThan( 0, $this->logId3 );
80 } catch ( Exception $e ) {
81 // We'd love to pass $e directly. However, ... see
82 // documentation of exceptionFromAddDBData in
83 // DumpTestCase
84 $this->exceptionFromAddDBData = $e;
85 }
86 }
87
88
89 /**
90 * asserts that the xml reader is at the beginning of a log entry and skips over
91 * it while analyzing it.
92 *
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
97 * text is ignored.
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
102 */
103 private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
104 $subtype, $title, $parameters = array()
105 ) {
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 $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 );
148
149 // Performing the dump
150 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
151
152 // Analyzing the dumped data
153 $this->assertDumpStart( $fname );
154
155 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
156 $this->userId1, null, "type", "subtype", "PageA" );
157
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" );
165
166 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
167 $this->userId2, "SomeOtherComment", "move", "delete",
168 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
169
170 $this->assertDumpEnd();
171 }
172
173 function testXmlDumpsBackupUseCaseLogging() {
174 global $wgContLang;
175
176 $this->checkHasGzip();
177
178 // Preparing the dump
179 $fname = $this->getNewTempFile();
180 $dumper = new BackupDumper( array( "--output=gzip:" . $fname,
181 "--reporting=2" ) );
182 $dumper->startId = $this->logId1;
183 $dumper->endId = $this->logId3 + 1;
184 $dumper->setDb( $this->db );
185
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
190 // needs updates.
191 $dumper->stderr = fopen( 'php://output', 'a' );
192 if ( $dumper->stderr === false ) {
193 $this->fail( "Could not open stream for stderr" );
194 }
195
196 // Performing the dump
197 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
198
199 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
200
201 // Analyzing the dumped data
202 $this->gunzip( $fname );
203
204 $this->assertDumpStart( $fname );
205
206 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
207 $this->userId1, null, "type", "subtype", "PageA" );
208
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" );
216
217 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
218 $this->userId2, "SomeOtherComment", "move", "delete",
219 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
220
221 $this->assertDumpEnd();
222
223 // Currently, no reporting is implemented. Alert via failure, once
224 // this changes.
225 // If reporting for log dumps has been implemented, please update
226 // the following statement to catch good output
227 $this->expectOutputString( '' );
228 }
229 }