Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / maintenance / DumpTestCase.php
blob4f0d24fbb599007525100f36dbfc5bdf627aa553
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 $text Revisions summare
35 * @throws MWExcepion
37 protected function addRevision( Page $page, $text, $summary ) {
38 $status = $page->doEditContent(
39 ContentHandler::makeContent( $text, $page->getTitle() ),
40 $summary
43 if ( $status->isGood() ) {
44 $value = $status->getValue();
45 $revision = $value['revision'];
46 $revision_id = $revision->getId();
47 $text_id = $revision->getTextId();
49 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
50 return array( $revision_id, $text_id );
54 throw new MWException( "Could not determine revision id (" . $status->getWikiText() . ")" );
57 /**
58 * gunzips the given file and stores the result in the original file name
60 * @param string $fname Filename to read the gzipped data from and stored
61 * the gunzipped data into
63 protected function gunzip( $fname ) {
64 $gzipped_contents = file_get_contents( $fname );
65 if ( $gzipped_contents === false ) {
66 $this->fail( "Could not get contents of $fname" );
69 $contents = gzdecode( $gzipped_contents );
71 $this->assertEquals(
72 strlen( $contents ),
73 file_put_contents( $fname, $contents ),
74 '# bytes written'
78 /**
79 * Default set up function.
81 * Clears $wgUser, and reports errors from addDBData to PHPUnit
83 protected function setUp() {
84 parent::setUp();
86 // Check if any Exception is stored for rethrowing from addDBData
87 // @see self::exceptionFromAddDBData
88 if ( $this->exceptionFromAddDBData !== null ) {
89 throw $this->exceptionFromAddDBData;
92 $this->setMwGlobals( 'wgUser', new User() );
95 /**
96 * Checks for test output consisting only of lines containing ETA announcements
98 function expectETAOutput() {
99 // Newer PHPUnits require assertion about the output using PHPUnit's own
100 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
101 // do not allow to check /each/ line of the output using /readable/ REs.
102 // So we ...
104 // 1. ... add a dummy output checking to make PHPUnit not complain
105 // about unchecked test output
106 $this->expectOutputRegex( '//' );
108 // 2. Do the real output checking on our own.
109 $lines = explode( "\n", $this->getActualOutput() );
110 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
111 $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
112 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
113 foreach ( $lines as $line ) {
114 $this->assertRegExp(
115 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
116 $line
122 * Step the current XML reader until node end of given name is found.
124 * @param string $name Name of the closing element to look for
125 * (e.g.: "mediawiki" when looking for </mediawiki>)
127 * @return bool True if the end node could be found. false otherwise.
129 protected function skipToNodeEnd( $name ) {
130 while ( $this->xml->read() ) {
131 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
132 $this->xml->name == $name
134 return true;
138 return false;
142 * Step the current XML reader to the first element start after the node
143 * end of a given name.
145 * @param string $name Name of the closing element to look for
146 * (e.g.: "mediawiki" when looking for </mediawiki>)
148 * @return bool True if new element after the closing of $name could be
149 * found. false otherwise.
151 protected function skipPastNodeEnd( $name ) {
152 $this->assertTrue( $this->skipToNodeEnd( $name ),
153 "Skipping to end of $name" );
154 while ( $this->xml->read() ) {
155 if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
156 return true;
160 return false;
164 * Opens an XML file to analyze and optionally skips past siteinfo.
166 * @param string $fname Name of file to analyze
167 * @param bool $skip_siteinfo (optional) If true, step the xml reader
168 * to the first element after </siteinfo>
170 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
171 $this->xml = new XMLReader();
172 $this->assertTrue( $this->xml->open( $fname ),
173 "Opening temporary file $fname via XMLReader failed" );
174 if ( $skip_siteinfo ) {
175 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
176 "Skipping past end of siteinfo" );
181 * Asserts that the xml reader is at the final closing tag of an xml file and
182 * closes the reader.
184 * @param string $tag (optional) the name of the final tag
185 * (e.g.: "mediawiki" for </mediawiki>)
187 protected function assertDumpEnd( $name = "mediawiki" ) {
188 $this->assertNodeEnd( $name, false );
189 if ( $this->xml->read() ) {
190 $this->skipWhitespace();
192 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
193 "No proper entity left to parse" );
194 $this->xml->close();
198 * Steps the xml reader over white space
200 protected function skipWhitespace() {
201 $cont = true;
202 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
203 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
204 $cont = $this->xml->read();
209 * Asserts that the xml reader is at an element of given name, and optionally
210 * skips past it.
212 * @param string $name The name of the element to check for
213 * (e.g.: "mediawiki" for <mediawiki>)
214 * @param bool $skip (optional) if true, skip past the found element
216 protected function assertNodeStart( $name, $skip = true ) {
217 $this->assertEquals( $name, $this->xml->name, "Node name" );
218 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
219 if ( $skip ) {
220 $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
225 * Asserts that the xml reader is at an closing element of given name, and optionally
226 * skips past it.
228 * @param string $name The name of the closing element to check for
229 * (e.g.: "mediawiki" for </mediawiki>)
230 * @param bool $skip (optional) if true, skip past the found element
232 protected function assertNodeEnd( $name, $skip = true ) {
233 $this->assertEquals( $name, $this->xml->name, "Node name" );
234 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
235 if ( $skip ) {
236 $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
241 * Asserts that the xml reader is at an element of given tag that contains a given text,
242 * and skips over the element.
244 * @param string $name The name of the element to check for
245 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
246 * @param string|bool $text If string, check if it equals the elements text.
247 * If false, ignore the element's text
248 * @param bool $skip_ws (optional) if true, skip past white spaces that trail the
249 * closing element.
251 protected function assertTextNode( $name, $text, $skip_ws = true ) {
252 $this->assertNodeStart( $name );
254 if ( $text !== false ) {
255 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
257 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
258 $this->assertNodeEnd( $name );
260 if ( $skip_ws ) {
261 $this->skipWhitespace();
266 * Asserts that the xml reader is at the start of a page element and skips over the first
267 * tags, after checking them.
269 * Besides the opening page element, this function also checks for and skips over the
270 * title, ns, and id tags. Hence after this function, the xml reader is at the first
271 * revision of the current page.
273 * @param int $id Id of the page to assert
274 * @param int $ns Number of namespage to assert
275 * @param string $name Title of the current page
277 protected function assertPageStart( $id, $ns, $name ) {
279 $this->assertNodeStart( "page" );
280 $this->skipWhitespace();
282 $this->assertTextNode( "title", $name );
283 $this->assertTextNode( "ns", $ns );
284 $this->assertTextNode( "id", $id );
288 * Asserts that the xml reader is at the page's closing element and skips to the next
289 * element.
291 protected function assertPageEnd() {
292 $this->assertNodeEnd( "page" );
293 $this->skipWhitespace();
297 * Asserts that the xml reader is at a revision and checks its representation before
298 * skipping over it.
300 * @param int $id Id of the revision
301 * @param string $summary Summary of the revision
302 * @param int $text_id Id of the revision's text
303 * @param int $text_bytes Number of bytes in the revision's text
304 * @param string $text_sha1 The base36 SHA-1 of the revision's text
305 * @param string|bool $text (optional) The revision's string, or false to check for a
306 * revision stub
307 * @param string $model The expected content model id (default: CONTENT_MODEL_WIKITEXT)
308 * @param string $format The expected format model id (default: CONTENT_FORMAT_WIKITEXT)
309 * @param int|bool $parentid (optional) id of the parent revision
311 protected function assertRevision( $id, $summary, $text_id, $text_bytes,
312 $text_sha1, $text = false, $parentid = false,
313 $model = CONTENT_MODEL_WIKITEXT, $format = CONTENT_FORMAT_WIKITEXT
315 $this->assertNodeStart( "revision" );
316 $this->skipWhitespace();
318 $this->assertTextNode( "id", $id );
319 if ( $parentid !== false ) {
320 $this->assertTextNode( "parentid", $parentid );
322 $this->assertTextNode( "timestamp", false );
324 $this->assertNodeStart( "contributor" );
325 $this->skipWhitespace();
326 $this->assertTextNode( "ip", false );
327 $this->assertNodeEnd( "contributor" );
328 $this->skipWhitespace();
330 $this->assertTextNode( "comment", $summary );
331 $this->skipWhitespace();
333 if ( $this->xml->name == "text" ) {
334 // note: <text> tag may occur here or at the very end.
335 $text_found = true;
336 $this->assertText( $id, $text_id, $text_bytes, $text );
337 } else {
338 $text_found = false;
341 $this->assertTextNode( "sha1", $text_sha1 );
343 $this->assertTextNode( "model", $model );
344 $this->skipWhitespace();
346 $this->assertTextNode( "format", $format );
347 $this->skipWhitespace();
349 if ( !$text_found ) {
350 $this->assertText( $id, $text_id, $text_bytes, $text );
353 $this->assertNodeEnd( "revision" );
354 $this->skipWhitespace();
357 protected function assertText( $id, $text_id, $text_bytes, $text ) {
358 $this->assertNodeStart( "text", false );
359 if ( $text_bytes !== false ) {
360 $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
361 "Attribute 'bytes' of revision " . $id );
364 if ( $text === false ) {
365 // Testing for a stub
366 $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
367 "Text id of revision " . $id );
368 $this->assertFalse( $this->xml->hasValue, "Revision has text" );
369 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
370 if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
371 && ( $this->xml->name == "text" )
374 $this->xml->read();
376 $this->skipWhitespace();
377 } else {
378 // Testing for a real dump
379 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
380 $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
381 $this->assertTrue( $this->xml->read(), "Skipping past text" );
382 $this->assertNodeEnd( "text" );
383 $this->skipWhitespace();