3 namespace MediaWiki\Tests\Maintenance
;
7 use MediaWikiLangTestCase
;
14 * Base TestCase for dumps
16 abstract class DumpTestCase
extends MediaWikiLangTestCase
{
19 * exception to be rethrown once in sound PHPUnit surrounding
21 * As the current MediaWikiTestCase::run is not robust enough to recover
22 * from thrown exceptions directly, we cannot throw frow within
23 * self::addDBData, although it would be appropriate. Hence, we catch the
24 * exception and store it until we are in setUp and may finally rethrow
25 * the exception without crashing the test suite.
27 * @var \Exception|null
29 protected $exceptionFromAddDBData = null;
32 * Holds the XMLReader used for analyzing an XML dump
36 protected $xml = null;
38 /** @var bool|null Whether the 'gzip' utility is available */
39 protected static $hasGzip = null;
42 * Skip the test if 'gzip' is not in $PATH.
46 protected function checkHasGzip() {
47 if ( self
::$hasGzip === null ) {
48 self
::$hasGzip = ( ExecutableFinder
::findInDefaultPaths( 'gzip' ) !== false );
51 if ( !self
::$hasGzip ) {
52 $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
55 return self
::$hasGzip;
59 * Adds a revision to a page, while returning the resuting revision's id
61 * @param Page $page Page to add the revision to
62 * @param string $text Revisions text
63 * @param string $summary Revisions summary
64 * @param string $model The model ID (defaults to wikitext)
69 protected function addRevision( Page
$page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT
) {
70 $status = $page->doEditContent(
71 ContentHandler
::makeContent( $text, $page->getTitle(), $model ),
75 if ( $status->isGood() ) {
76 $value = $status->getValue();
77 $revision = $value['revision'];
78 $revision_id = $revision->getId();
79 $text_id = $revision->getTextId();
81 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
82 return [ $revision_id, $text_id ];
86 throw new MWException( "Could not determine revision id ("
87 . $status->getWikiText( false, false, 'en' ) . ")" );
91 * gunzips the given file and stores the result in the original file name
93 * @param string $fname Filename to read the gzipped data from and stored
94 * the gunzipped data into
96 protected function gunzip( $fname ) {
97 $gzipped_contents = file_get_contents( $fname );
98 if ( $gzipped_contents === false ) {
99 $this->fail( "Could not get contents of $fname" );
102 $contents = gzdecode( $gzipped_contents );
106 file_put_contents( $fname, $contents ),
112 * Default set up function.
114 * Clears $wgUser, and reports errors from addDBData to PHPUnit
116 protected function setUp() {
119 // Check if any Exception is stored for rethrowing from addDBData
120 // @see self::exceptionFromAddDBData
121 if ( $this->exceptionFromAddDBData
!== null ) {
122 throw $this->exceptionFromAddDBData
;
125 $this->setMwGlobals( 'wgUser', new User() );
129 * Checks for test output consisting only of lines containing ETA announcements
131 function expectETAOutput() {
132 // Newer PHPUnits require assertion about the output using PHPUnit's own
133 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
134 // do not allow to check /each/ line of the output using /readable/ REs.
137 // 1. ... add a dummy output checking to make PHPUnit not complain
138 // about unchecked test output
139 $this->expectOutputRegex( '//' );
141 // 2. Do the real output checking on our own.
142 $lines = explode( "\n", $this->getActualOutput() );
143 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
144 $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
145 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
146 foreach ( $lines as $line ) {
148 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
155 * Step the current XML reader until node end of given name is found.
157 * @param string $name Name of the closing element to look for
158 * (e.g.: "mediawiki" when looking for </mediawiki>)
160 * @return bool True if the end node could be found. false otherwise.
162 protected function skipToNodeEnd( $name ) {
163 while ( $this->xml
->read() ) {
164 if ( $this->xml
->nodeType
== XMLReader
::END_ELEMENT
&&
165 $this->xml
->name
== $name
175 * Step the current XML reader to the first element start after the node
176 * end of a given name.
178 * @param string $name Name of the closing element to look for
179 * (e.g.: "mediawiki" when looking for </mediawiki>)
181 * @return bool True if new element after the closing of $name could be
182 * found. false otherwise.
184 protected function skipPastNodeEnd( $name ) {
185 $this->assertTrue( $this->skipToNodeEnd( $name ),
186 "Skipping to end of $name" );
187 while ( $this->xml
->read() ) {
188 if ( $this->xml
->nodeType
== XMLReader
::ELEMENT
) {
197 * Opens an XML file to analyze and optionally skips past siteinfo.
199 * @param string $fname Name of file to analyze
200 * @param bool $skip_siteinfo (optional) If true, step the xml reader
201 * to the first element after </siteinfo>
203 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
204 $this->xml
= new XMLReader();
205 $this->assertTrue( $this->xml
->open( $fname ),
206 "Opening temporary file $fname via XMLReader failed" );
207 if ( $skip_siteinfo ) {
208 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
209 "Skipping past end of siteinfo" );
214 * Asserts that the xml reader is at the final closing tag of an xml file and
217 * @param string $name (optional) the name of the final tag
218 * (e.g.: "mediawiki" for </mediawiki>)
220 protected function assertDumpEnd( $name = "mediawiki" ) {
221 $this->assertNodeEnd( $name, false );
222 if ( $this->xml
->read() ) {
223 $this->skipWhitespace();
225 $this->assertEquals( $this->xml
->nodeType
, XMLReader
::NONE
,
226 "No proper entity left to parse" );
231 * Steps the xml reader over white space
233 protected function skipWhitespace() {
235 while ( $cont && ( ( $this->xml
->nodeType
== XMLReader
::WHITESPACE
)
236 ||
( $this->xml
->nodeType
== XMLReader
::SIGNIFICANT_WHITESPACE
) ) ) {
237 $cont = $this->xml
->read();
242 * Asserts that the xml reader is at an element of given name, and optionally
245 * @param string $name The name of the element to check for
246 * (e.g.: "mediawiki" for <mediawiki>)
247 * @param bool $skip (optional) if true, skip past the found element
249 protected function assertNodeStart( $name, $skip = true ) {
250 $this->assertEquals( $name, $this->xml
->name
, "Node name" );
251 $this->assertEquals( XMLReader
::ELEMENT
, $this->xml
->nodeType
, "Node type" );
253 $this->assertTrue( $this->xml
->read(), "Skipping past start tag" );
258 * Asserts that the xml reader is at an closing element of given name, and optionally
261 * @param string $name The name of the closing element to check for
262 * (e.g.: "mediawiki" for </mediawiki>)
263 * @param bool $skip (optional) if true, skip past the found element
265 protected function assertNodeEnd( $name, $skip = true ) {
266 $this->assertEquals( $name, $this->xml
->name
, "Node name" );
267 $this->assertEquals( XMLReader
::END_ELEMENT
, $this->xml
->nodeType
, "Node type" );
269 $this->assertTrue( $this->xml
->read(), "Skipping past end tag" );
274 * Asserts that the xml reader is at an element of given tag that contains a given text,
275 * and skips over the element.
277 * @param string $name The name of the element to check for
278 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
279 * @param string|bool $text If string, check if it equals the elements text.
280 * If false, ignore the element's text
281 * @param bool $skip_ws (optional) if true, skip past white spaces that trail the
284 protected function assertTextNode( $name, $text, $skip_ws = true ) {
285 $this->assertNodeStart( $name );
287 if ( $text !== false ) {
288 $this->assertEquals( $text, $this->xml
->value
, "Text of node " . $name );
290 $this->assertTrue( $this->xml
->read(), "Skipping past processed text of " . $name );
291 $this->assertNodeEnd( $name );
294 $this->skipWhitespace();
299 * Asserts that the xml reader is at the start of a page element and skips over the first
300 * tags, after checking them.
302 * Besides the opening page element, this function also checks for and skips over the
303 * title, ns, and id tags. Hence after this function, the xml reader is at the first
304 * revision of the current page.
306 * @param int $id Id of the page to assert
307 * @param int $ns Number of namespage to assert
308 * @param string $name Title of the current page
310 protected function assertPageStart( $id, $ns, $name ) {
311 $this->assertNodeStart( "page" );
312 $this->skipWhitespace();
314 $this->assertTextNode( "title", $name );
315 $this->assertTextNode( "ns", $ns );
316 $this->assertTextNode( "id", $id );
320 * Asserts that the xml reader is at the page's closing element and skips to the next
323 protected function assertPageEnd() {
324 $this->assertNodeEnd( "page" );
325 $this->skipWhitespace();
329 * Asserts that the xml reader is at a revision and checks its representation before
332 * @param int $id Id of the revision
333 * @param string $summary Summary of the revision
334 * @param int $text_id Id of the revision's text
335 * @param int $text_bytes Number of bytes in the revision's text
336 * @param string $text_sha1 The base36 SHA-1 of the revision's text
337 * @param string|bool $text (optional) The revision's string, or false to check for a
339 * @param int|bool $parentid (optional) id of the parent revision
340 * @param string $model The expected content model id (default: CONTENT_MODEL_WIKITEXT)
341 * @param string $format The expected format model id (default: CONTENT_FORMAT_WIKITEXT)
343 protected function assertRevision( $id, $summary, $text_id, $text_bytes,
344 $text_sha1, $text = false, $parentid = false,
345 $model = CONTENT_MODEL_WIKITEXT
, $format = CONTENT_FORMAT_WIKITEXT
347 $this->assertNodeStart( "revision" );
348 $this->skipWhitespace();
350 $this->assertTextNode( "id", $id );
351 if ( $parentid !== false ) {
352 $this->assertTextNode( "parentid", $parentid );
354 $this->assertTextNode( "timestamp", false );
356 $this->assertNodeStart( "contributor" );
357 $this->skipWhitespace();
358 $this->assertTextNode( "ip", false );
359 $this->assertNodeEnd( "contributor" );
360 $this->skipWhitespace();
362 $this->assertTextNode( "comment", $summary );
363 $this->skipWhitespace();
365 $this->assertTextNode( "model", $model );
366 $this->skipWhitespace();
368 $this->assertTextNode( "format", $format );
369 $this->skipWhitespace();
371 if ( $this->xml
->name
== "text" ) {
372 // note: <text> tag may occur here or at the very end.
374 $this->assertText( $id, $text_id, $text_bytes, $text );
379 $this->assertTextNode( "sha1", $text_sha1 );
381 if ( !$text_found ) {
382 $this->assertText( $id, $text_id, $text_bytes, $text );
385 $this->assertNodeEnd( "revision" );
386 $this->skipWhitespace();
389 protected function assertText( $id, $text_id, $text_bytes, $text ) {
390 $this->assertNodeStart( "text", false );
391 if ( $text_bytes !== false ) {
392 $this->assertEquals( $this->xml
->getAttribute( "bytes" ), $text_bytes,
393 "Attribute 'bytes' of revision " . $id );
396 if ( $text === false ) {
397 // Testing for a stub
398 $this->assertEquals( $this->xml
->getAttribute( "id" ), $text_id,
399 "Text id of revision " . $id );
400 $this->assertFalse( $this->xml
->hasValue
, "Revision has text" );
401 $this->assertTrue( $this->xml
->read(), "Skipping text start tag" );
402 if ( ( $this->xml
->nodeType
== XMLReader
::END_ELEMENT
)
403 && ( $this->xml
->name
== "text" )
407 $this->skipWhitespace();
409 // Testing for a real dump
410 $this->assertTrue( $this->xml
->read(), "Skipping text start tag" );
411 $this->assertEquals( $text, $this->xml
->value
, "Text of revision " . $id );
412 $this->assertTrue( $this->xml
->read(), "Skipping past text" );
413 $this->assertNodeEnd( "text" );
414 $this->skipWhitespace();