Merge "[FileRepo] Locking and transaction fixes."
[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 */
8 class BackupDumperLoggerTest extends DumpTestCase {
9
10
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;
15
16 /**
17 * adds a log entry to the database.
18 *
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
26 * to the entry
27 *
28 * @return int id of the added log entry
29 */
30 private function addLogEntry( $type, $subtype, User $user, $ns, $title,
31 $comment = null, $parameters = null ) {
32
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 );
38 }
39 if ( $parameters !== null ) {
40 $logEntry->setParameters( $parameters );
41 }
42 return $logEntry->insert();
43 }
44
45 function addDBData() {
46 $this->tablesUsed[] = 'logging';
47 $this->tablesUsed[] = 'user';
48
49 try {
50 $user1 = User::newFromName( 'BackupDumperLogUserA' );
51 $this->userId1 = $user1->getId();
52 if ( $this->userId1 === 0 ) {
53 $user1->addToDatabase();
54 $this->userId1 = $user1->getId();
55 }
56 $this->assertGreaterThan( 0, $this->userId1 );
57
58 $user2 = User::newFromName( 'BackupDumperLogUserB' );
59 $this->userId2 = $user2->getId();
60 if ( $this->userId2 === 0 ) {
61 $user2->addToDatabase();
62 $this->userId2 = $user2->getId();
63 }
64 $this->assertGreaterThan( 0, $this->userId2 );
65
66 $this->logId1 = $this->addLogEntry( 'type', 'subtype',
67 $user1, NS_MAIN, "PageA" );
68 $this->assertGreaterThan( 0, $this->logId1 );
69
70 $this->logId2 = $this->addLogEntry( 'supress', 'delete',
71 $user2, NS_TALK, "PageB", "SomeComment" );
72 $this->assertGreaterThan( 0, $this->logId2 );
73
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 );
78
79 } catch ( Exception $e ) {
80 // We'd love to pass $e directly. However, ... see
81 // documentation of exceptionFromAddDBData in
82 // DumpTestCase
83 $this->exceptionFromAddDBData = $e;
84 }
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 $this->assertNodeStart( "logitem" );
107 $this->skipWhitespace();
108
109 $this->assertTextNode( "id", $id );
110 $this->assertTextNode( "timestamp", false );
111
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();
118
119 if ( $comment !== null ) {
120 $this->assertTextNode( "comment", $comment );
121 }
122 $this->assertTextNode( "type", $type );
123 $this->assertTextNode( "action", $subtype );
124 $this->assertTextNode( "logtitle", $title );
125
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();
132
133 $this->assertNodeEnd( "logitem" );
134 $this->skipWhitespace();
135 }
136
137 function testPlain () {
138 global $wgContLang;
139
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 );
147
148 // Performing the dump
149 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
150
151 // Analyzing the dumped data
152 $this->assertDumpStart( $fname );
153
154 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
155 $this->userId1, null, "type", "subtype", "PageA" );
156
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" );
164
165 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
166 $this->userId2, "SomeOtherComment", "move", "delete",
167 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
168
169 $this->assertDumpEnd();
170 }
171
172 function testXmlDumpsBackupUseCaseLogging() {
173 global $wgContLang;
174
175 // Preparing the dump
176 $fname = $this->getNewTempFile();
177 $dumper = new BackupDumper( array ( "--output=gzip:" . $fname,
178 "--reporting=2" ) );
179 $dumper->startId = $this->logId1;
180 $dumper->endId = $this->logId3 + 1;
181 $dumper->setDb( $this->db );
182
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
187 // needs updates.
188 $dumper->stderr = fopen( 'php://output', 'a' );
189 if ( $dumper->stderr === FALSE ) {
190 $this->fail( "Could not open stream for stderr" );
191 }
192
193 // Performing the dump
194 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
195
196 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
197
198 // Analyzing the dumped data
199 $this->gunzip( $fname );
200
201 $this->assertDumpStart( $fname );
202
203 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
204 $this->userId1, null, "type", "subtype", "PageA" );
205
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" );
213
214 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
215 $this->userId2, "SomeOtherComment", "move", "delete",
216 "PageA", array( 'key1' => 1, 3 => 'value3' ) );
217
218 $this->assertDumpEnd();
219
220 // Currently, no reporting is implemented. Alert via failure, once
221 // this changes.
222 // If reporting for log dumps has been implemented, please update
223 // the following statement to catch good output
224 $this->expectOutputString( '' );
225 }
226
227 }