MediaWiki REL1_31
backup_LogTest.php
Go to the documentation of this file.
1<?php
2
4
5use DumpBackup;
7use Title;
8use User;
10
24
25 // We'll add several log entries and users for this test. The following
26 // variables hold the corresponding ids.
29
43 private function addLogEntry( $type, $subtype, User $user, $ns, $title,
44 $comment = null, $parameters = null
45 ) {
46 $logEntry = new ManualLogEntry( $type, $subtype );
47 $logEntry->setPerformer( $user );
48 $logEntry->setTarget( Title::newFromText( $title, $ns ) );
49 if ( $comment !== null ) {
50 $logEntry->setComment( $comment );
51 }
52 if ( $parameters !== null ) {
53 $logEntry->setParameters( $parameters );
54 }
55
56 return $logEntry->insert();
57 }
58
59 function addDBData() {
60 $this->tablesUsed[] = 'logging';
61 $this->tablesUsed[] = 'user';
62
63 try {
64 $user1 = User::newFromName( 'BackupDumperLogUserA' );
65 $this->userId1 = $user1->getId();
66 if ( $this->userId1 === 0 ) {
67 $user1->addToDatabase();
68 $this->userId1 = $user1->getId();
69 }
70 $this->assertGreaterThan( 0, $this->userId1 );
71
72 $user2 = User::newFromName( 'BackupDumperLogUserB' );
73 $this->userId2 = $user2->getId();
74 if ( $this->userId2 === 0 ) {
75 $user2->addToDatabase();
76 $this->userId2 = $user2->getId();
77 }
78 $this->assertGreaterThan( 0, $this->userId2 );
79
80 $this->logId1 = $this->addLogEntry( 'type', 'subtype',
81 $user1, NS_MAIN, "PageA" );
82 $this->assertGreaterThan( 0, $this->logId1 );
83
84 $this->logId2 = $this->addLogEntry( 'supress', 'delete',
85 $user2, NS_TALK, "PageB", "SomeComment" );
86 $this->assertGreaterThan( 0, $this->logId2 );
87
88 $this->logId3 = $this->addLogEntry( 'move', 'delete',
89 $user2, NS_MAIN, "PageA", "SomeOtherComment",
90 [ 'key1' => 1, 3 => 'value3' ] );
91 $this->assertGreaterThan( 0, $this->logId3 );
92 } catch ( Exception $e ) {
93 // We'd love to pass $e directly. However, ... see
94 // documentation of exceptionFromAddDBData in
95 // DumpTestCase
96 $this->exceptionFromAddDBData = $e;
97 }
98 }
99
113 private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
114 $subtype, $title, $parameters = []
115 ) {
116 $this->assertNodeStart( "logitem" );
117 $this->skipWhitespace();
118
119 $this->assertTextNode( "id", $id );
120 $this->assertTextNode( "timestamp", false );
121
122 $this->assertNodeStart( "contributor" );
123 $this->skipWhitespace();
124 $this->assertTextNode( "username", $user_name );
125 $this->assertTextNode( "id", $user_id );
126 $this->assertNodeEnd( "contributor" );
127 $this->skipWhitespace();
128
129 if ( $comment !== null ) {
130 $this->assertTextNode( "comment", $comment );
131 }
132 $this->assertTextNode( "type", $type );
133 $this->assertTextNode( "action", $subtype );
134 $this->assertTextNode( "logtitle", $title );
135
136 $this->assertNodeStart( "params" );
137 $parameters_xml = unserialize( $this->xml->value );
138 $this->assertEquals( $parameters, $parameters_xml );
139 $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
140 $this->assertNodeEnd( "params" );
141 $this->skipWhitespace();
142
143 $this->assertNodeEnd( "logitem" );
144 $this->skipWhitespace();
145 }
146
147 function testPlain() {
148 global $wgContLang;
149
150 // Preparing the dump
151 $fname = $this->getNewTempFile();
152
153 $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
154 $dumper->startId = $this->logId1;
155 $dumper->endId = $this->logId3 + 1;
156 $dumper->reporting = false;
157 $dumper->setDB( $this->db );
158
159 // Performing the dump
160 $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
161
162 // Analyzing the dumped data
163 $this->assertDumpStart( $fname );
164
165 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
166 $this->userId1, null, "type", "subtype", "PageA" );
167
168 $this->assertNotNull( $wgContLang, "Content language object validation" );
169 $namespace = $wgContLang->getNsText( NS_TALK );
170 $this->assertInternalType( 'string', $namespace );
171 $this->assertGreaterThan( 0, strlen( $namespace ) );
172 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
173 $this->userId2, "SomeComment", "supress", "delete",
174 $namespace . ":PageB" );
175
176 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
177 $this->userId2, "SomeOtherComment", "move", "delete",
178 "PageA", [ 'key1' => 1, 3 => 'value3' ] );
179
180 $this->assertDumpEnd();
181 }
182
184 global $wgContLang;
185
186 $this->checkHasGzip();
187
188 // Preparing the dump
189 $fname = $this->getNewTempFile();
190
191 $dumper = new DumpBackup();
192 $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
193 '--reporting=2' ] );
194 $dumper->startId = $this->logId1;
195 $dumper->endId = $this->logId3 + 1;
196 $dumper->setDB( $this->db );
197
198 // xmldumps-backup demands reporting, although this is currently not
199 // implemented in BackupDumper, when dumping logging data. We
200 // nevertheless capture the output of the dump process already now,
201 // to be able to alert (once dumping produces reports) that this test
202 // needs updates.
203 $dumper->stderr = fopen( 'php://output', 'a' );
204 if ( $dumper->stderr === false ) {
205 $this->fail( "Could not open stream for stderr" );
206 }
207
208 // Performing the dump
209 $dumper->execute();
210
211 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
212
213 // Analyzing the dumped data
214 $this->gunzip( $fname );
215
216 $this->assertDumpStart( $fname );
217
218 $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
219 $this->userId1, null, "type", "subtype", "PageA" );
220
221 $this->assertNotNull( $wgContLang, "Content language object validation" );
222 $namespace = $wgContLang->getNsText( NS_TALK );
223 $this->assertInternalType( 'string', $namespace );
224 $this->assertGreaterThan( 0, strlen( $namespace ) );
225 $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
226 $this->userId2, "SomeComment", "supress", "delete",
227 $namespace . ":PageB" );
228
229 $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
230 $this->userId2, "SomeOtherComment", "move", "delete",
231 "PageA", [ 'key1' => 1, 3 => 'value3' ] );
232
233 $this->assertDumpEnd();
234
235 // Currently, no reporting is implemented. Alert via failure, once
236 // this changes.
237 // If reporting for log dumps has been implemented, please update
238 // the following statement to catch good output
239 $this->expectOutputString( '' );
240 }
241}
unserialize( $serialized)
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:112
Class for creating log entries manually, to inject them into the database.
Definition LogEntry.php:432
getNewTempFile()
Obtains a new temporary file name.
assertLogItem( $id, $user_name, $user_id, $comment, $type, $subtype, $title, $parameters=[])
asserts that the xml reader is at the beginning of a log entry and skips over it while analyzing it.
addLogEntry( $type, $subtype, User $user, $ns, $title, $comment=null, $parameters=null)
adds a log entry to the database.
gunzip( $fname)
gunzips the given file and stores the result in the original file name
assertDumpStart( $fname, $skip_siteinfo=true)
Opens an XML file to analyze and optionally skips past siteinfo.
assertTextNode( $name, $text, $skip_ws=true)
Asserts that the xml reader is at an element of given tag that contains a given text,...
assertDumpEnd( $name="mediawiki")
Asserts that the xml reader is at the final closing tag of an xml file and closes the reader.
checkHasGzip()
Skip the test if 'gzip' is not in $PATH.
skipWhitespace()
Steps the xml reader over white space.
assertNodeEnd( $name, $skip=true)
Asserts that the xml reader is at an closing element of given name, and optionally skips past it.
assertNodeStart( $name, $skip=true)
Asserts that the xml reader is at an element of given name, and optionally skips past it.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
returning false will NOT prevent logging $e
Definition hooks.txt:2176
const NS_MAIN
Definition Defines.php:74
const NS_TALK
Definition Defines.php:75