3 namespace MediaWiki\Tests\Maintenance
;
7 use MediaWikiLangTestCase
;
13 * Base TestCase for dumps
15 abstract class DumpTestCase
extends MediaWikiLangTestCase
{
18 * exception to be rethrown once in sound PHPUnit surrounding
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.
28 protected $exceptionFromAddDBData = null;
31 * Holds the XMLReader used for analyzing an XML dump
35 protected $xml = null;
37 /** @var bool|null Whether the 'gzip' utility is available */
38 protected static $hasGzip = null;
41 * Skip the test if 'gzip' is not in $PATH.
45 protected function checkHasGzip() {
46 if ( self
::$hasGzip === null ) {
47 self
::$hasGzip = ( ExecutableFinder
::findInDefaultPaths( 'gzip' ) !== false );
50 if ( !self
::$hasGzip ) {
51 $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
54 return self
::$hasGzip;
58 * Adds a revision to a page, while returning the resuting revision's id
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)
68 protected function addRevision( Page
$page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT
) {
69 $status = $page->doEditContent(
70 ContentHandler
::makeContent( $text, $page->getTitle(), $model ),
74 if ( $status->isGood() ) {
75 $value = $status->getValue();
76 $revision = $value['revision'];
77 $revision_id = $revision->getId();
78 $text_id = $revision->getTextId();
80 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
81 return [ $revision_id, $text_id ];
85 throw new MWException( "Could not determine revision id ("
86 . $status->getWikiText( false, false, 'en' ) . ")" );
90 * gunzips the given file and stores the result in the original file name
92 * @param string $fname Filename to read the gzipped data from and stored
93 * the gunzipped data into
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" );
101 $contents = gzdecode( $gzipped_contents );
105 file_put_contents( $fname, $contents ),
111 * Default set up function.
113 * Clears $wgUser, and reports errors from addDBData to PHPUnit
115 protected function setUp() {
118 // Check if any Exception is stored for rethrowing from addDBData
119 // @see self::exceptionFromAddDBData
120 if ( $this->exceptionFromAddDBData
!== null ) {
121 throw $this->exceptionFromAddDBData
;
124 $this->setMwGlobals( 'wgUser', new User() );
128 * Checks for test output consisting only of lines containing ETA announcements
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.
136 // 1. ... add a dummy output checking to make PHPUnit not complain
137 // about unchecked test output
138 $this->expectOutputRegex( '//' );
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 ) {
147 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
154 * Step the current XML reader until node end of given name is found.
156 * @param string $name Name of the closing element to look for
157 * (e.g.: "mediawiki" when looking for </mediawiki>)
159 * @return bool True if the end node could be found. false otherwise.
161 protected function skipToNodeEnd( $name ) {
162 while ( $this->xml
->read() ) {
163 if ( $this->xml
->nodeType
== XMLReader
::END_ELEMENT
&&
164 $this->xml
->name
== $name
174 * Step the current XML reader to the first element start after the node
175 * end of a given name.
177 * @param string $name Name of the closing element to look for
178 * (e.g.: "mediawiki" when looking for </mediawiki>)
180 * @return bool True if new element after the closing of $name could be
181 * found. false otherwise.
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
) {
196 * Opens an XML file to analyze and optionally skips past siteinfo.
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>
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" );
213 * Asserts that the xml reader is at the final closing tag of an xml file and
216 * @param string $name (optional) the name of the final tag
217 * (e.g.: "mediawiki" for </mediawiki>)
219 protected function assertDumpEnd( $name = "mediawiki" ) {
220 $this->assertNodeEnd( $name, false );
221 if ( $this->xml
->read() ) {
222 $this->skipWhitespace();
224 $this->assertEquals( $this->xml
->nodeType
, XMLReader
::NONE
,
225 "No proper entity left to parse" );
230 * Steps the xml reader over white space
232 protected function skipWhitespace() {
234 while ( $cont && ( ( $this->xml
->nodeType
== XMLReader
::WHITESPACE
)
235 ||
( $this->xml
->nodeType
== XMLReader
::SIGNIFICANT_WHITESPACE
) ) ) {
236 $cont = $this->xml
->read();
241 * Asserts that the xml reader is at an element of given name, and optionally
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
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" );
252 $this->assertTrue( $this->xml
->read(), "Skipping past start tag" );
257 * Asserts that the xml reader is at an closing element of given name, and optionally
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
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" );
268 $this->assertTrue( $this->xml
->read(), "Skipping past end tag" );
273 * Asserts that the xml reader is at an element of given tag that contains a given text,
274 * and skips over the element.
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
283 protected function assertTextNode( $name, $text, $skip_ws = true ) {
284 $this->assertNodeStart( $name );
286 if ( $text !== false ) {
287 $this->assertEquals( $text, $this->xml
->value
, "Text of node " . $name );
289 $this->assertTrue( $this->xml
->read(), "Skipping past processed text of " . $name );
290 $this->assertNodeEnd( $name );
293 $this->skipWhitespace();
298 * Asserts that the xml reader is at the start of a page element and skips over the first
299 * tags, after checking them.
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.
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
309 protected function assertPageStart( $id, $ns, $name ) {
310 $this->assertNodeStart( "page" );
311 $this->skipWhitespace();
313 $this->assertTextNode( "title", $name );
314 $this->assertTextNode( "ns", $ns );
315 $this->assertTextNode( "id", $id );
319 * Asserts that the xml reader is at the page's closing element and skips to the next
322 protected function assertPageEnd() {
323 $this->assertNodeEnd( "page" );
324 $this->skipWhitespace();
328 * Asserts that the xml reader is at a revision and checks its representation before
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
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)
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
346 $this->assertNodeStart( "revision" );
347 $this->skipWhitespace();
349 $this->assertTextNode( "id", $id );
350 if ( $parentid !== false ) {
351 $this->assertTextNode( "parentid", $parentid );
353 $this->assertTextNode( "timestamp", false );
355 $this->assertNodeStart( "contributor" );
356 $this->skipWhitespace();
357 $this->assertTextNode( "ip", false );
358 $this->assertNodeEnd( "contributor" );
359 $this->skipWhitespace();
361 $this->assertTextNode( "comment", $summary );
362 $this->skipWhitespace();
364 $this->assertTextNode( "model", $model );
365 $this->skipWhitespace();
367 $this->assertTextNode( "format", $format );
368 $this->skipWhitespace();
370 if ( $this->xml
->name
== "text" ) {
371 // note: <text> tag may occur here or at the very end.
373 $this->assertText( $id, $text_id, $text_bytes, $text );
378 $this->assertTextNode( "sha1", $text_sha1 );
380 if ( !$text_found ) {
381 $this->assertText( $id, $text_id, $text_bytes, $text );
384 $this->assertNodeEnd( "revision" );
385 $this->skipWhitespace();
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 );
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" )
406 $this->skipWhitespace();
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();