4 * Base TestCase for dumps
6 abstract class DumpTestCase
extends MediaWikiLangTestCase
{
9 * exception to be rethrown once in sound PHPUnit surrounding
11 * As the current MediaWikiTestCase::run is not robust enough to recover
12 * from thrown exceptions directly, we cannot throw frow within
13 * self::addDBData, although it would be appropriate. Hence, we catch the
14 * exception and store it until we are in setUp and may finally rethrow
15 * the exception without crashing the test suite.
19 protected $exceptionFromAddDBData = null;
22 * Holds the XMLReader used for analyzing an XML dump
26 protected $xml = null;
28 /** @var bool|null Whether the 'gzip' utility is available */
29 protected static $hasGzip = null;
32 * Skip the test if 'gzip' is not in $PATH.
36 protected function checkHasGzip() {
37 if ( self
::$hasGzip === null ) {
38 self
::$hasGzip = ( Installer
::locateExecutableInDefaultPaths( 'gzip' ) !== false );
41 if ( !self
::$hasGzip ) {
42 $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
45 return self
::$hasGzip;
49 * Adds a revision to a page, while returning the resuting revision's id
51 * @param Page $page Page to add the revision to
52 * @param string $text Revisions text
53 * @param string $summary Revisions summary
54 * @param string $model The model ID (defaults to wikitext)
59 protected function addRevision( Page
$page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT
) {
60 $status = $page->doEditContent(
61 ContentHandler
::makeContent( $text, $page->getTitle(), $model ),
65 if ( $status->isGood() ) {
66 $value = $status->getValue();
67 $revision = $value['revision'];
68 $revision_id = $revision->getId();
69 $text_id = $revision->getTextId();
71 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
72 return [ $revision_id, $text_id ];
76 throw new MWException( "Could not determine revision id ("
77 . $status->getWikiText( false, false, 'en' ) . ")" );
81 * gunzips the given file and stores the result in the original file name
83 * @param string $fname Filename to read the gzipped data from and stored
84 * the gunzipped data into
86 protected function gunzip( $fname ) {
87 $gzipped_contents = file_get_contents( $fname );
88 if ( $gzipped_contents === false ) {
89 $this->fail( "Could not get contents of $fname" );
92 $contents = gzdecode( $gzipped_contents );
96 file_put_contents( $fname, $contents ),
102 * Default set up function.
104 * Clears $wgUser, and reports errors from addDBData to PHPUnit
106 protected function setUp() {
109 // Check if any Exception is stored for rethrowing from addDBData
110 // @see self::exceptionFromAddDBData
111 if ( $this->exceptionFromAddDBData
!== null ) {
112 throw $this->exceptionFromAddDBData
;
115 $this->setMwGlobals( 'wgUser', new User() );
119 * Checks for test output consisting only of lines containing ETA announcements
121 function expectETAOutput() {
122 // Newer PHPUnits require assertion about the output using PHPUnit's own
123 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
124 // do not allow to check /each/ line of the output using /readable/ REs.
127 // 1. ... add a dummy output checking to make PHPUnit not complain
128 // about unchecked test output
129 $this->expectOutputRegex( '//' );
131 // 2. Do the real output checking on our own.
132 $lines = explode( "\n", $this->getActualOutput() );
133 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
134 $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
135 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
136 foreach ( $lines as $line ) {
138 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
145 * Step the current XML reader until node end of given name is found.
147 * @param string $name Name of the closing element to look for
148 * (e.g.: "mediawiki" when looking for </mediawiki>)
150 * @return bool True if the end node could be found. false otherwise.
152 protected function skipToNodeEnd( $name ) {
153 while ( $this->xml
->read() ) {
154 if ( $this->xml
->nodeType
== XMLReader
::END_ELEMENT
&&
155 $this->xml
->name
== $name
165 * Step the current XML reader to the first element start after the node
166 * end of a given name.
168 * @param string $name Name of the closing element to look for
169 * (e.g.: "mediawiki" when looking for </mediawiki>)
171 * @return bool True if new element after the closing of $name could be
172 * found. false otherwise.
174 protected function skipPastNodeEnd( $name ) {
175 $this->assertTrue( $this->skipToNodeEnd( $name ),
176 "Skipping to end of $name" );
177 while ( $this->xml
->read() ) {
178 if ( $this->xml
->nodeType
== XMLReader
::ELEMENT
) {
187 * Opens an XML file to analyze and optionally skips past siteinfo.
189 * @param string $fname Name of file to analyze
190 * @param bool $skip_siteinfo (optional) If true, step the xml reader
191 * to the first element after </siteinfo>
193 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
194 $this->xml
= new XMLReader();
195 $this->assertTrue( $this->xml
->open( $fname ),
196 "Opening temporary file $fname via XMLReader failed" );
197 if ( $skip_siteinfo ) {
198 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
199 "Skipping past end of siteinfo" );
204 * Asserts that the xml reader is at the final closing tag of an xml file and
207 * @param string $name (optional) the name of the final tag
208 * (e.g.: "mediawiki" for </mediawiki>)
210 protected function assertDumpEnd( $name = "mediawiki" ) {
211 $this->assertNodeEnd( $name, false );
212 if ( $this->xml
->read() ) {
213 $this->skipWhitespace();
215 $this->assertEquals( $this->xml
->nodeType
, XMLReader
::NONE
,
216 "No proper entity left to parse" );
221 * Steps the xml reader over white space
223 protected function skipWhitespace() {
225 while ( $cont && ( ( $this->xml
->nodeType
== XMLReader
::WHITESPACE
)
226 ||
( $this->xml
->nodeType
== XMLReader
::SIGNIFICANT_WHITESPACE
) ) ) {
227 $cont = $this->xml
->read();
232 * Asserts that the xml reader is at an element of given name, and optionally
235 * @param string $name The name of the element to check for
236 * (e.g.: "mediawiki" for <mediawiki>)
237 * @param bool $skip (optional) if true, skip past the found element
239 protected function assertNodeStart( $name, $skip = true ) {
240 $this->assertEquals( $name, $this->xml
->name
, "Node name" );
241 $this->assertEquals( XMLReader
::ELEMENT
, $this->xml
->nodeType
, "Node type" );
243 $this->assertTrue( $this->xml
->read(), "Skipping past start tag" );
248 * Asserts that the xml reader is at an closing element of given name, and optionally
251 * @param string $name The name of the closing element to check for
252 * (e.g.: "mediawiki" for </mediawiki>)
253 * @param bool $skip (optional) if true, skip past the found element
255 protected function assertNodeEnd( $name, $skip = true ) {
256 $this->assertEquals( $name, $this->xml
->name
, "Node name" );
257 $this->assertEquals( XMLReader
::END_ELEMENT
, $this->xml
->nodeType
, "Node type" );
259 $this->assertTrue( $this->xml
->read(), "Skipping past end tag" );
264 * Asserts that the xml reader is at an element of given tag that contains a given text,
265 * and skips over the element.
267 * @param string $name The name of the element to check for
268 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
269 * @param string|bool $text If string, check if it equals the elements text.
270 * If false, ignore the element's text
271 * @param bool $skip_ws (optional) if true, skip past white spaces that trail the
274 protected function assertTextNode( $name, $text, $skip_ws = true ) {
275 $this->assertNodeStart( $name );
277 if ( $text !== false ) {
278 $this->assertEquals( $text, $this->xml
->value
, "Text of node " . $name );
280 $this->assertTrue( $this->xml
->read(), "Skipping past processed text of " . $name );
281 $this->assertNodeEnd( $name );
284 $this->skipWhitespace();
289 * Asserts that the xml reader is at the start of a page element and skips over the first
290 * tags, after checking them.
292 * Besides the opening page element, this function also checks for and skips over the
293 * title, ns, and id tags. Hence after this function, the xml reader is at the first
294 * revision of the current page.
296 * @param int $id Id of the page to assert
297 * @param int $ns Number of namespage to assert
298 * @param string $name Title of the current page
300 protected function assertPageStart( $id, $ns, $name ) {
302 $this->assertNodeStart( "page" );
303 $this->skipWhitespace();
305 $this->assertTextNode( "title", $name );
306 $this->assertTextNode( "ns", $ns );
307 $this->assertTextNode( "id", $id );
311 * Asserts that the xml reader is at the page's closing element and skips to the next
314 protected function assertPageEnd() {
315 $this->assertNodeEnd( "page" );
316 $this->skipWhitespace();
320 * Asserts that the xml reader is at a revision and checks its representation before
323 * @param int $id Id of the revision
324 * @param string $summary Summary of the revision
325 * @param int $text_id Id of the revision's text
326 * @param int $text_bytes Number of bytes in the revision's text
327 * @param string $text_sha1 The base36 SHA-1 of the revision's text
328 * @param string|bool $text (optional) The revision's string, or false to check for a
330 * @param int|bool $parentid (optional) id of the parent revision
331 * @param string $model The expected content model id (default: CONTENT_MODEL_WIKITEXT)
332 * @param string $format The expected format model id (default: CONTENT_FORMAT_WIKITEXT)
334 protected function assertRevision( $id, $summary, $text_id, $text_bytes,
335 $text_sha1, $text = false, $parentid = false,
336 $model = CONTENT_MODEL_WIKITEXT
, $format = CONTENT_FORMAT_WIKITEXT
338 $this->assertNodeStart( "revision" );
339 $this->skipWhitespace();
341 $this->assertTextNode( "id", $id );
342 if ( $parentid !== false ) {
343 $this->assertTextNode( "parentid", $parentid );
345 $this->assertTextNode( "timestamp", false );
347 $this->assertNodeStart( "contributor" );
348 $this->skipWhitespace();
349 $this->assertTextNode( "ip", false );
350 $this->assertNodeEnd( "contributor" );
351 $this->skipWhitespace();
353 $this->assertTextNode( "comment", $summary );
354 $this->skipWhitespace();
356 $this->assertTextNode( "model", $model );
357 $this->skipWhitespace();
359 $this->assertTextNode( "format", $format );
360 $this->skipWhitespace();
362 if ( $this->xml
->name
== "text" ) {
363 // note: <text> tag may occur here or at the very end.
365 $this->assertText( $id, $text_id, $text_bytes, $text );
370 $this->assertTextNode( "sha1", $text_sha1 );
372 if ( !$text_found ) {
373 $this->assertText( $id, $text_id, $text_bytes, $text );
376 $this->assertNodeEnd( "revision" );
377 $this->skipWhitespace();
380 protected function assertText( $id, $text_id, $text_bytes, $text ) {
381 $this->assertNodeStart( "text", false );
382 if ( $text_bytes !== false ) {
383 $this->assertEquals( $this->xml
->getAttribute( "bytes" ), $text_bytes,
384 "Attribute 'bytes' of revision " . $id );
387 if ( $text === false ) {
388 // Testing for a stub
389 $this->assertEquals( $this->xml
->getAttribute( "id" ), $text_id,
390 "Text id of revision " . $id );
391 $this->assertFalse( $this->xml
->hasValue
, "Revision has text" );
392 $this->assertTrue( $this->xml
->read(), "Skipping text start tag" );
393 if ( ( $this->xml
->nodeType
== XMLReader
::END_ELEMENT
)
394 && ( $this->xml
->name
== "text" )
399 $this->skipWhitespace();
401 // Testing for a real dump
402 $this->assertTrue( $this->xml
->read(), "Skipping text start tag" );
403 $this->assertEquals( $text, $this->xml
->value
, "Text of revision " . $id );
404 $this->assertTrue( $this->xml
->read(), "Skipping past text" );
405 $this->assertNodeEnd( "text" );
406 $this->skipWhitespace();