(bug 17602) fix Monobook action tabs not quite touching the page body
[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
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
230 }