c872993adf2c92bba81e43fb9dc82fada3103c9b
[lhc/web/wiklou.git] / tests / phpunit / maintenance / DumpTestCase.php
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use ContentHandler;
6 use ExecutableFinder;
7 use MediaWikiLangTestCase;
8 use Page;
9 use User;
10 use XMLReader;
11
12 /**
13 * Base TestCase for dumps
14 */
15 abstract class DumpTestCase extends MediaWikiLangTestCase {
16
17 /**
18 * exception to be rethrown once in sound PHPUnit surrounding
19 *
20 * As the current MediaWikiTestCase::run is not robust enough to recover
21 * from thrown exceptions directly, we cannot throw frow within
22 * self::addDBData, although it would be appropriate. Hence, we catch the
23 * exception and store it until we are in setUp and may finally rethrow
24 * the exception without crashing the test suite.
25 *
26 * @var Exception|null
27 */
28 protected $exceptionFromAddDBData = null;
29
30 /**
31 * Holds the XMLReader used for analyzing an XML dump
32 *
33 * @var XMLReader|null
34 */
35 protected $xml = null;
36
37 /** @var bool|null Whether the 'gzip' utility is available */
38 protected static $hasGzip = null;
39
40 /**
41 * Skip the test if 'gzip' is not in $PATH.
42 *
43 * @return bool
44 */
45 protected function checkHasGzip() {
46 if ( self::$hasGzip === null ) {
47 self::$hasGzip = ( ExecutableFinder::findInDefaultPaths( 'gzip' ) !== false );
48 }
49
50 if ( !self::$hasGzip ) {
51 $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
52 }
53
54 return self::$hasGzip;
55 }
56
57 /**
58 * Adds a revision to a page, while returning the resuting revision's id
59 *
60 * @param Page $page Page to add the revision to
61 * @param string $text Revisions text
62 * @param string $summary Revisions summary
63 * @param string $model The model ID (defaults to wikitext)
64 *
65 * @throws MWException
66 * @return array
67 */
68 protected function addRevision( Page $page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT ) {
69 $status = $page->doEditContent(
70 ContentHandler::makeContent( $text, $page->getTitle(), $model ),
71 $summary
72 );
73
74 if ( $status->isGood() ) {
75 $value = $status->getValue();
76 $revision = $value['revision'];
77 $revision_id = $revision->getId();
78 $text_id = $revision->getTextId();
79
80 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
81 return [ $revision_id, $text_id ];
82 }
83 }
84
85 throw new MWException( "Could not determine revision id ("
86 . $status->getWikiText( false, false, 'en' ) . ")" );
87 }
88
89 /**
90 * gunzips the given file and stores the result in the original file name
91 *
92 * @param string $fname Filename to read the gzipped data from and stored
93 * the gunzipped data into
94 */
95 protected function gunzip( $fname ) {
96 $gzipped_contents = file_get_contents( $fname );
97 if ( $gzipped_contents === false ) {
98 $this->fail( "Could not get contents of $fname" );
99 }
100
101 $contents = gzdecode( $gzipped_contents );
102
103 $this->assertEquals(
104 strlen( $contents ),
105 file_put_contents( $fname, $contents ),
106 '# bytes written'
107 );
108 }
109
110 /**
111 * Default set up function.
112 *
113 * Clears $wgUser, and reports errors from addDBData to PHPUnit
114 */
115 protected function setUp() {
116 parent::setUp();
117
118 // Check if any Exception is stored for rethrowing from addDBData
119 // @see self::exceptionFromAddDBData
120 if ( $this->exceptionFromAddDBData !== null ) {
121 throw $this->exceptionFromAddDBData;
122 }
123
124 $this->setMwGlobals( 'wgUser', new User() );
125 }
126
127 /**
128 * Checks for test output consisting only of lines containing ETA announcements
129 */
130 function expectETAOutput() {
131 // Newer PHPUnits require assertion about the output using PHPUnit's own
132 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
133 // do not allow to check /each/ line of the output using /readable/ REs.
134 // So we ...
135
136 // 1. ... add a dummy output checking to make PHPUnit not complain
137 // about unchecked test output
138 $this->expectOutputRegex( '//' );
139
140 // 2. Do the real output checking on our own.
141 $lines = explode( "\n", $this->getActualOutput() );
142 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
143 $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
144 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
145 foreach ( $lines as $line ) {
146 $this->assertRegExp(
147 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
148 $line
149 );
150 }
151 }
152
153 /**
154 * Step the current XML reader until node end of given name is found.
155 *
156 * @param string $name Name of the closing element to look for
157 * (e.g.: "mediawiki" when looking for </mediawiki>)
158 *
159 * @return bool True if the end node could be found. false otherwise.
160 */
161 protected function skipToNodeEnd( $name ) {
162 while ( $this->xml->read() ) {
163 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
164 $this->xml->name == $name
165 ) {
166 return true;
167 }
168 }
169
170 return false;
171 }
172
173 /**
174 * Step the current XML reader to the first element start after the node
175 * end of a given name.
176 *
177 * @param string $name Name of the closing element to look for
178 * (e.g.: "mediawiki" when looking for </mediawiki>)
179 *
180 * @return bool True if new element after the closing of $name could be
181 * found. false otherwise.
182 */
183 protected function skipPastNodeEnd( $name ) {
184 $this->assertTrue( $this->skipToNodeEnd( $name ),
185 "Skipping to end of $name" );
186 while ( $this->xml->read() ) {
187 if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
188 return true;
189 }
190 }
191
192 return false;
193 }
194
195 /**
196 * Opens an XML file to analyze and optionally skips past siteinfo.
197 *
198 * @param string $fname Name of file to analyze
199 * @param bool $skip_siteinfo (optional) If true, step the xml reader
200 * to the first element after </siteinfo>
201 */
202 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
203 $this->xml = new XMLReader();
204 $this->assertTrue( $this->xml->open( $fname ),
205 "Opening temporary file $fname via XMLReader failed" );
206 if ( $skip_siteinfo ) {
207 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
208 "Skipping past end of siteinfo" );
209 }
210 }
211
212 /**
213 * Asserts that the xml reader is at the final closing tag of an xml file and
214 * closes the reader.
215 *
216 * @param string $name (optional) the name of the final tag
217 * (e.g.: "mediawiki" for </mediawiki>)
218 */
219 protected function assertDumpEnd( $name = "mediawiki" ) {
220 $this->assertNodeEnd( $name, false );
221 if ( $this->xml->read() ) {
222 $this->skipWhitespace();
223 }
224 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
225 "No proper entity left to parse" );
226 $this->xml->close();
227 }
228
229 /**
230 * Steps the xml reader over white space
231 */
232 protected function skipWhitespace() {
233 $cont = true;
234 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
235 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
236 $cont = $this->xml->read();
237 }
238 }
239
240 /**
241 * Asserts that the xml reader is at an element of given name, and optionally
242 * skips past it.
243 *
244 * @param string $name The name of the element to check for
245 * (e.g.: "mediawiki" for <mediawiki>)
246 * @param bool $skip (optional) if true, skip past the found element
247 */
248 protected function assertNodeStart( $name, $skip = true ) {
249 $this->assertEquals( $name, $this->xml->name, "Node name" );
250 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
251 if ( $skip ) {
252 $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
253 }
254 }
255
256 /**
257 * Asserts that the xml reader is at an closing element of given name, and optionally
258 * skips past it.
259 *
260 * @param string $name The name of the closing element to check for
261 * (e.g.: "mediawiki" for </mediawiki>)
262 * @param bool $skip (optional) if true, skip past the found element
263 */
264 protected function assertNodeEnd( $name, $skip = true ) {
265 $this->assertEquals( $name, $this->xml->name, "Node name" );
266 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
267 if ( $skip ) {
268 $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
269 }
270 }
271
272 /**
273 * Asserts that the xml reader is at an element of given tag that contains a given text,
274 * and skips over the element.
275 *
276 * @param string $name The name of the element to check for
277 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
278 * @param string|bool $text If string, check if it equals the elements text.
279 * If false, ignore the element's text
280 * @param bool $skip_ws (optional) if true, skip past white spaces that trail the
281 * closing element.
282 */
283 protected function assertTextNode( $name, $text, $skip_ws = true ) {
284 $this->assertNodeStart( $name );
285
286 if ( $text !== false ) {
287 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
288 }
289 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
290 $this->assertNodeEnd( $name );
291
292 if ( $skip_ws ) {
293 $this->skipWhitespace();
294 }
295 }
296
297 /**
298 * Asserts that the xml reader is at the start of a page element and skips over the first
299 * tags, after checking them.
300 *
301 * Besides the opening page element, this function also checks for and skips over the
302 * title, ns, and id tags. Hence after this function, the xml reader is at the first
303 * revision of the current page.
304 *
305 * @param int $id Id of the page to assert
306 * @param int $ns Number of namespage to assert
307 * @param string $name Title of the current page
308 */
309 protected function assertPageStart( $id, $ns, $name ) {
310 $this->assertNodeStart( "page" );
311 $this->skipWhitespace();
312
313 $this->assertTextNode( "title", $name );
314 $this->assertTextNode( "ns", $ns );
315 $this->assertTextNode( "id", $id );
316 }
317
318 /**
319 * Asserts that the xml reader is at the page's closing element and skips to the next
320 * element.
321 */
322 protected function assertPageEnd() {
323 $this->assertNodeEnd( "page" );
324 $this->skipWhitespace();
325 }
326
327 /**
328 * Asserts that the xml reader is at a revision and checks its representation before
329 * skipping over it.
330 *
331 * @param int $id Id of the revision
332 * @param string $summary Summary of the revision
333 * @param int $text_id Id of the revision's text
334 * @param int $text_bytes Number of bytes in the revision's text
335 * @param string $text_sha1 The base36 SHA-1 of the revision's text
336 * @param string|bool $text (optional) The revision's string, or false to check for a
337 * revision stub
338 * @param int|bool $parentid (optional) id of the parent revision
339 * @param string $model The expected content model id (default: CONTENT_MODEL_WIKITEXT)
340 * @param string $format The expected format model id (default: CONTENT_FORMAT_WIKITEXT)
341 */
342 protected function assertRevision( $id, $summary, $text_id, $text_bytes,
343 $text_sha1, $text = false, $parentid = false,
344 $model = CONTENT_MODEL_WIKITEXT, $format = CONTENT_FORMAT_WIKITEXT
345 ) {
346 $this->assertNodeStart( "revision" );
347 $this->skipWhitespace();
348
349 $this->assertTextNode( "id", $id );
350 if ( $parentid !== false ) {
351 $this->assertTextNode( "parentid", $parentid );
352 }
353 $this->assertTextNode( "timestamp", false );
354
355 $this->assertNodeStart( "contributor" );
356 $this->skipWhitespace();
357 $this->assertTextNode( "ip", false );
358 $this->assertNodeEnd( "contributor" );
359 $this->skipWhitespace();
360
361 $this->assertTextNode( "comment", $summary );
362 $this->skipWhitespace();
363
364 $this->assertTextNode( "model", $model );
365 $this->skipWhitespace();
366
367 $this->assertTextNode( "format", $format );
368 $this->skipWhitespace();
369
370 if ( $this->xml->name == "text" ) {
371 // note: <text> tag may occur here or at the very end.
372 $text_found = true;
373 $this->assertText( $id, $text_id, $text_bytes, $text );
374 } else {
375 $text_found = false;
376 }
377
378 $this->assertTextNode( "sha1", $text_sha1 );
379
380 if ( !$text_found ) {
381 $this->assertText( $id, $text_id, $text_bytes, $text );
382 }
383
384 $this->assertNodeEnd( "revision" );
385 $this->skipWhitespace();
386 }
387
388 protected function assertText( $id, $text_id, $text_bytes, $text ) {
389 $this->assertNodeStart( "text", false );
390 if ( $text_bytes !== false ) {
391 $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
392 "Attribute 'bytes' of revision " . $id );
393 }
394
395 if ( $text === false ) {
396 // Testing for a stub
397 $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
398 "Text id of revision " . $id );
399 $this->assertFalse( $this->xml->hasValue, "Revision has text" );
400 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
401 if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
402 && ( $this->xml->name == "text" )
403 ) {
404 $this->xml->read();
405 }
406 $this->skipWhitespace();
407 } else {
408 // Testing for a real dump
409 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
410 $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
411 $this->assertTrue( $this->xml->read(), "Skipping past text" );
412 $this->assertNodeEnd( "text" );
413 $this->skipWhitespace();
414 }
415 }
416 }