Add missing "use" clause to DumpTestCase
[lhc/web/wiklou.git] / tests / phpunit / maintenance / DumpTestCase.php
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use ContentHandler;
6 use ExecutableFinder;
7 use MediaWikiLangTestCase;
8 use Page;
9 use User;
10 use XMLReader;
11 use MWException;
12
13 /**
14 * Base TestCase for dumps
15 */
16 abstract class DumpTestCase extends MediaWikiLangTestCase {
17
18 /**
19 * exception to be rethrown once in sound PHPUnit surrounding
20 *
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.
26 *
27 * @var Exception|null
28 */
29 protected $exceptionFromAddDBData = null;
30
31 /**
32 * Holds the XMLReader used for analyzing an XML dump
33 *
34 * @var XMLReader|null
35 */
36 protected $xml = null;
37
38 /** @var bool|null Whether the 'gzip' utility is available */
39 protected static $hasGzip = null;
40
41 /**
42 * Skip the test if 'gzip' is not in $PATH.
43 *
44 * @return bool
45 */
46 protected function checkHasGzip() {
47 if ( self::$hasGzip === null ) {
48 self::$hasGzip = ( ExecutableFinder::findInDefaultPaths( 'gzip' ) !== false );
49 }
50
51 if ( !self::$hasGzip ) {
52 $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
53 }
54
55 return self::$hasGzip;
56 }
57
58 /**
59 * Adds a revision to a page, while returning the resuting revision's id
60 *
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)
65 *
66 * @throws MWException
67 * @return array
68 */
69 protected function addRevision( Page $page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT ) {
70 $status = $page->doEditContent(
71 ContentHandler::makeContent( $text, $page->getTitle(), $model ),
72 $summary
73 );
74
75 if ( $status->isGood() ) {
76 $value = $status->getValue();
77 $revision = $value['revision'];
78 $revision_id = $revision->getId();
79 $text_id = $revision->getTextId();
80
81 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
82 return [ $revision_id, $text_id ];
83 }
84 }
85
86 throw new MWException( "Could not determine revision id ("
87 . $status->getWikiText( false, false, 'en' ) . ")" );
88 }
89
90 /**
91 * gunzips the given file and stores the result in the original file name
92 *
93 * @param string $fname Filename to read the gzipped data from and stored
94 * the gunzipped data into
95 */
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" );
100 }
101
102 $contents = gzdecode( $gzipped_contents );
103
104 $this->assertEquals(
105 strlen( $contents ),
106 file_put_contents( $fname, $contents ),
107 '# bytes written'
108 );
109 }
110
111 /**
112 * Default set up function.
113 *
114 * Clears $wgUser, and reports errors from addDBData to PHPUnit
115 */
116 protected function setUp() {
117 parent::setUp();
118
119 // Check if any Exception is stored for rethrowing from addDBData
120 // @see self::exceptionFromAddDBData
121 if ( $this->exceptionFromAddDBData !== null ) {
122 throw $this->exceptionFromAddDBData;
123 }
124
125 $this->setMwGlobals( 'wgUser', new User() );
126 }
127
128 /**
129 * Checks for test output consisting only of lines containing ETA announcements
130 */
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.
135 // So we ...
136
137 // 1. ... add a dummy output checking to make PHPUnit not complain
138 // about unchecked test output
139 $this->expectOutputRegex( '//' );
140
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 ) {
147 $this->assertRegExp(
148 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
149 $line
150 );
151 }
152 }
153
154 /**
155 * Step the current XML reader until node end of given name is found.
156 *
157 * @param string $name Name of the closing element to look for
158 * (e.g.: "mediawiki" when looking for </mediawiki>)
159 *
160 * @return bool True if the end node could be found. false otherwise.
161 */
162 protected function skipToNodeEnd( $name ) {
163 while ( $this->xml->read() ) {
164 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
165 $this->xml->name == $name
166 ) {
167 return true;
168 }
169 }
170
171 return false;
172 }
173
174 /**
175 * Step the current XML reader to the first element start after the node
176 * end of a given name.
177 *
178 * @param string $name Name of the closing element to look for
179 * (e.g.: "mediawiki" when looking for </mediawiki>)
180 *
181 * @return bool True if new element after the closing of $name could be
182 * found. false otherwise.
183 */
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 ) {
189 return true;
190 }
191 }
192
193 return false;
194 }
195
196 /**
197 * Opens an XML file to analyze and optionally skips past siteinfo.
198 *
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>
202 */
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" );
210 }
211 }
212
213 /**
214 * Asserts that the xml reader is at the final closing tag of an xml file and
215 * closes the reader.
216 *
217 * @param string $name (optional) the name of the final tag
218 * (e.g.: "mediawiki" for </mediawiki>)
219 */
220 protected function assertDumpEnd( $name = "mediawiki" ) {
221 $this->assertNodeEnd( $name, false );
222 if ( $this->xml->read() ) {
223 $this->skipWhitespace();
224 }
225 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
226 "No proper entity left to parse" );
227 $this->xml->close();
228 }
229
230 /**
231 * Steps the xml reader over white space
232 */
233 protected function skipWhitespace() {
234 $cont = true;
235 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
236 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
237 $cont = $this->xml->read();
238 }
239 }
240
241 /**
242 * Asserts that the xml reader is at an element of given name, and optionally
243 * skips past it.
244 *
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
248 */
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" );
252 if ( $skip ) {
253 $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
254 }
255 }
256
257 /**
258 * Asserts that the xml reader is at an closing element of given name, and optionally
259 * skips past it.
260 *
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
264 */
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" );
268 if ( $skip ) {
269 $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
270 }
271 }
272
273 /**
274 * Asserts that the xml reader is at an element of given tag that contains a given text,
275 * and skips over the element.
276 *
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
282 * closing element.
283 */
284 protected function assertTextNode( $name, $text, $skip_ws = true ) {
285 $this->assertNodeStart( $name );
286
287 if ( $text !== false ) {
288 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
289 }
290 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
291 $this->assertNodeEnd( $name );
292
293 if ( $skip_ws ) {
294 $this->skipWhitespace();
295 }
296 }
297
298 /**
299 * Asserts that the xml reader is at the start of a page element and skips over the first
300 * tags, after checking them.
301 *
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.
305 *
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
309 */
310 protected function assertPageStart( $id, $ns, $name ) {
311 $this->assertNodeStart( "page" );
312 $this->skipWhitespace();
313
314 $this->assertTextNode( "title", $name );
315 $this->assertTextNode( "ns", $ns );
316 $this->assertTextNode( "id", $id );
317 }
318
319 /**
320 * Asserts that the xml reader is at the page's closing element and skips to the next
321 * element.
322 */
323 protected function assertPageEnd() {
324 $this->assertNodeEnd( "page" );
325 $this->skipWhitespace();
326 }
327
328 /**
329 * Asserts that the xml reader is at a revision and checks its representation before
330 * skipping over it.
331 *
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
338 * revision stub
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)
342 */
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
346 ) {
347 $this->assertNodeStart( "revision" );
348 $this->skipWhitespace();
349
350 $this->assertTextNode( "id", $id );
351 if ( $parentid !== false ) {
352 $this->assertTextNode( "parentid", $parentid );
353 }
354 $this->assertTextNode( "timestamp", false );
355
356 $this->assertNodeStart( "contributor" );
357 $this->skipWhitespace();
358 $this->assertTextNode( "ip", false );
359 $this->assertNodeEnd( "contributor" );
360 $this->skipWhitespace();
361
362 $this->assertTextNode( "comment", $summary );
363 $this->skipWhitespace();
364
365 $this->assertTextNode( "model", $model );
366 $this->skipWhitespace();
367
368 $this->assertTextNode( "format", $format );
369 $this->skipWhitespace();
370
371 if ( $this->xml->name == "text" ) {
372 // note: <text> tag may occur here or at the very end.
373 $text_found = true;
374 $this->assertText( $id, $text_id, $text_bytes, $text );
375 } else {
376 $text_found = false;
377 }
378
379 $this->assertTextNode( "sha1", $text_sha1 );
380
381 if ( !$text_found ) {
382 $this->assertText( $id, $text_id, $text_bytes, $text );
383 }
384
385 $this->assertNodeEnd( "revision" );
386 $this->skipWhitespace();
387 }
388
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 );
394 }
395
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" )
404 ) {
405 $this->xml->read();
406 }
407 $this->skipWhitespace();
408 } else {
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();
415 }
416 }
417 }