Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / maintenance / DumpTestCase.php
blobc846c0677d0eb7617372dd8b190225818e052230
1 <?php
3 /**
4 * Base TestCase for dumps
5 */
6 abstract class DumpTestCase extends MediaWikiLangTestCase {
8 /**
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.
17 * @var Exception|null
19 protected $exceptionFromAddDBData = null;
21 /**
22 * Holds the XMLReader used for analyzing an XML dump
24 * @var XMLReader|null
26 protected $xml = null;
28 /**
29 * Adds a revision to a page, while returning the resuting revision's id
31 * @param Page $page Page to add the revision to
32 * @param string $text Revisions text
33 * @param string $summary Revisions summary
34 * @param string $model The model ID (defaults to wikitext)
36 * @throws MWException
37 * @return array
39 protected function addRevision( Page $page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT ) {
40 $status = $page->doEditContent(
41 ContentHandler::makeContent( $text, $page->getTitle(), $model ),
42 $summary
45 if ( $status->isGood() ) {
46 $value = $status->getValue();
47 $revision = $value['revision'];
48 $revision_id = $revision->getId();
49 $text_id = $revision->getTextId();
51 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
52 return array( $revision_id, $text_id );
56 throw new MWException( "Could not determine revision id (" . $status->getWikiText() . ")" );
59 /**
60 * gunzips the given file and stores the result in the original file name
62 * @param string $fname Filename to read the gzipped data from and stored
63 * the gunzipped data into
65 protected function gunzip( $fname ) {
66 $gzipped_contents = file_get_contents( $fname );
67 if ( $gzipped_contents === false ) {
68 $this->fail( "Could not get contents of $fname" );
71 $contents = gzdecode( $gzipped_contents );
73 $this->assertEquals(
74 strlen( $contents ),
75 file_put_contents( $fname, $contents ),
76 '# bytes written'
80 /**
81 * Default set up function.
83 * Clears $wgUser, and reports errors from addDBData to PHPUnit
85 protected function setUp() {
86 parent::setUp();
88 // Check if any Exception is stored for rethrowing from addDBData
89 // @see self::exceptionFromAddDBData
90 if ( $this->exceptionFromAddDBData !== null ) {
91 throw $this->exceptionFromAddDBData;
94 $this->setMwGlobals( 'wgUser', new User() );
97 /**
98 * Checks for test output consisting only of lines containing ETA announcements
100 function expectETAOutput() {
101 // Newer PHPUnits require assertion about the output using PHPUnit's own
102 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
103 // do not allow to check /each/ line of the output using /readable/ REs.
104 // So we ...
106 // 1. ... add a dummy output checking to make PHPUnit not complain
107 // about unchecked test output
108 $this->expectOutputRegex( '//' );
110 // 2. Do the real output checking on our own.
111 $lines = explode( "\n", $this->getActualOutput() );
112 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
113 $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
114 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
115 foreach ( $lines as $line ) {
116 $this->assertRegExp(
117 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
118 $line
124 * Step the current XML reader until node end of given name is found.
126 * @param string $name Name of the closing element to look for
127 * (e.g.: "mediawiki" when looking for </mediawiki>)
129 * @return bool True if the end node could be found. false otherwise.
131 protected function skipToNodeEnd( $name ) {
132 while ( $this->xml->read() ) {
133 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
134 $this->xml->name == $name
136 return true;
140 return false;
144 * Step the current XML reader to the first element start after the node
145 * end of a given name.
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 new element after the closing of $name could be
151 * found. false otherwise.
153 protected function skipPastNodeEnd( $name ) {
154 $this->assertTrue( $this->skipToNodeEnd( $name ),
155 "Skipping to end of $name" );
156 while ( $this->xml->read() ) {
157 if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
158 return true;
162 return false;
166 * Opens an XML file to analyze and optionally skips past siteinfo.
168 * @param string $fname Name of file to analyze
169 * @param bool $skip_siteinfo (optional) If true, step the xml reader
170 * to the first element after </siteinfo>
172 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
173 $this->xml = new XMLReader();
174 $this->assertTrue( $this->xml->open( $fname ),
175 "Opening temporary file $fname via XMLReader failed" );
176 if ( $skip_siteinfo ) {
177 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
178 "Skipping past end of siteinfo" );
183 * Asserts that the xml reader is at the final closing tag of an xml file and
184 * closes the reader.
186 * @param string $name (optional) the name of the final tag
187 * (e.g.: "mediawiki" for </mediawiki>)
189 protected function assertDumpEnd( $name = "mediawiki" ) {
190 $this->assertNodeEnd( $name, false );
191 if ( $this->xml->read() ) {
192 $this->skipWhitespace();
194 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
195 "No proper entity left to parse" );
196 $this->xml->close();
200 * Steps the xml reader over white space
202 protected function skipWhitespace() {
203 $cont = true;
204 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
205 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
206 $cont = $this->xml->read();
211 * Asserts that the xml reader is at an element of given name, and optionally
212 * skips past it.
214 * @param string $name The name of the element to check for
215 * (e.g.: "mediawiki" for <mediawiki>)
216 * @param bool $skip (optional) if true, skip past the found element
218 protected function assertNodeStart( $name, $skip = true ) {
219 $this->assertEquals( $name, $this->xml->name, "Node name" );
220 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
221 if ( $skip ) {
222 $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
227 * Asserts that the xml reader is at an closing element of given name, and optionally
228 * skips past it.
230 * @param string $name The name of the closing element to check for
231 * (e.g.: "mediawiki" for </mediawiki>)
232 * @param bool $skip (optional) if true, skip past the found element
234 protected function assertNodeEnd( $name, $skip = true ) {
235 $this->assertEquals( $name, $this->xml->name, "Node name" );
236 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
237 if ( $skip ) {
238 $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
243 * Asserts that the xml reader is at an element of given tag that contains a given text,
244 * and skips over the element.
246 * @param string $name The name of the element to check for
247 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
248 * @param string|bool $text If string, check if it equals the elements text.
249 * If false, ignore the element's text
250 * @param bool $skip_ws (optional) if true, skip past white spaces that trail the
251 * closing element.
253 protected function assertTextNode( $name, $text, $skip_ws = true ) {
254 $this->assertNodeStart( $name );
256 if ( $text !== false ) {
257 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
259 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
260 $this->assertNodeEnd( $name );
262 if ( $skip_ws ) {
263 $this->skipWhitespace();
268 * Asserts that the xml reader is at the start of a page element and skips over the first
269 * tags, after checking them.
271 * Besides the opening page element, this function also checks for and skips over the
272 * title, ns, and id tags. Hence after this function, the xml reader is at the first
273 * revision of the current page.
275 * @param int $id Id of the page to assert
276 * @param int $ns Number of namespage to assert
277 * @param string $name Title of the current page
279 protected function assertPageStart( $id, $ns, $name ) {
281 $this->assertNodeStart( "page" );
282 $this->skipWhitespace();
284 $this->assertTextNode( "title", $name );
285 $this->assertTextNode( "ns", $ns );
286 $this->assertTextNode( "id", $id );
290 * Asserts that the xml reader is at the page's closing element and skips to the next
291 * element.
293 protected function assertPageEnd() {
294 $this->assertNodeEnd( "page" );
295 $this->skipWhitespace();
299 * Asserts that the xml reader is at a revision and checks its representation before
300 * skipping over it.
302 * @param int $id Id of the revision
303 * @param string $summary Summary of the revision
304 * @param int $text_id Id of the revision's text
305 * @param int $text_bytes Number of bytes in the revision's text
306 * @param string $text_sha1 The base36 SHA-1 of the revision's text
307 * @param string|bool $text (optional) The revision's string, or false to check for a
308 * revision stub
309 * @param int|bool $parentid (optional) id of the parent revision
310 * @param string $model The expected content model id (default: CONTENT_MODEL_WIKITEXT)
311 * @param string $format The expected format model id (default: CONTENT_FORMAT_WIKITEXT)
313 protected function assertRevision( $id, $summary, $text_id, $text_bytes,
314 $text_sha1, $text = false, $parentid = false,
315 $model = CONTENT_MODEL_WIKITEXT, $format = CONTENT_FORMAT_WIKITEXT
317 $this->assertNodeStart( "revision" );
318 $this->skipWhitespace();
320 $this->assertTextNode( "id", $id );
321 if ( $parentid !== false ) {
322 $this->assertTextNode( "parentid", $parentid );
324 $this->assertTextNode( "timestamp", false );
326 $this->assertNodeStart( "contributor" );
327 $this->skipWhitespace();
328 $this->assertTextNode( "ip", false );
329 $this->assertNodeEnd( "contributor" );
330 $this->skipWhitespace();
332 $this->assertTextNode( "comment", $summary );
333 $this->skipWhitespace();
335 $this->assertTextNode( "model", $model );
336 $this->skipWhitespace();
338 $this->assertTextNode( "format", $format );
339 $this->skipWhitespace();
341 if ( $this->xml->name == "text" ) {
342 // note: <text> tag may occur here or at the very end.
343 $text_found = true;
344 $this->assertText( $id, $text_id, $text_bytes, $text );
345 } else {
346 $text_found = false;
349 $this->assertTextNode( "sha1", $text_sha1 );
351 if ( !$text_found ) {
352 $this->assertText( $id, $text_id, $text_bytes, $text );
355 $this->assertNodeEnd( "revision" );
356 $this->skipWhitespace();
359 protected function assertText( $id, $text_id, $text_bytes, $text ) {
360 $this->assertNodeStart( "text", false );
361 if ( $text_bytes !== false ) {
362 $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
363 "Attribute 'bytes' of revision " . $id );
366 if ( $text === false ) {
367 // Testing for a stub
368 $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
369 "Text id of revision " . $id );
370 $this->assertFalse( $this->xml->hasValue, "Revision has text" );
371 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
372 if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
373 && ( $this->xml->name == "text" )
376 $this->xml->read();
378 $this->skipWhitespace();
379 } else {
380 // Testing for a real dump
381 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
382 $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
383 $this->assertTrue( $this->xml->read(), "Skipping past text" );
384 $this->assertNodeEnd( "text" );
385 $this->skipWhitespace();