TableDiffFormatter: Don't repeatedly call array_shift()
[lhc/web/wiklou.git] / tests / phpunit / maintenance / DumpTestCase.php
1 <?php
2
3 /**
4 * Base TestCase for dumps
5 */
6 abstract class DumpTestCase extends MediaWikiLangTestCase {
7
8 /**
9 * exception to be rethrown once in sound PHPUnit surrounding
10 *
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.
16 *
17 * @var Exception|null
18 */
19 protected $exceptionFromAddDBData = null;
20
21 /**
22 * Holds the XMLReader used for analyzing an XML dump
23 *
24 * @var XMLReader|null
25 */
26 protected $xml = null;
27
28 /**
29 * Adds a revision to a page, while returning the resuting revision's id
30 *
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)
35 *
36 * @throws MWException
37 * @return array
38 */
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
43 );
44
45 if ( $status->isGood() ) {
46 $value = $status->getValue();
47 $revision = $value['revision'];
48 $revision_id = $revision->getId();
49 $text_id = $revision->getTextId();
50
51 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
52 return [ $revision_id, $text_id ];
53 }
54 }
55
56 throw new MWException( "Could not determine revision id (" . $status->getWikiText() . ")" );
57 }
58
59 /**
60 * gunzips the given file and stores the result in the original file name
61 *
62 * @param string $fname Filename to read the gzipped data from and stored
63 * the gunzipped data into
64 */
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" );
69 }
70
71 $contents = gzdecode( $gzipped_contents );
72
73 $this->assertEquals(
74 strlen( $contents ),
75 file_put_contents( $fname, $contents ),
76 '# bytes written'
77 );
78 }
79
80 /**
81 * Default set up function.
82 *
83 * Clears $wgUser, and reports errors from addDBData to PHPUnit
84 */
85 protected function setUp() {
86 parent::setUp();
87
88 // Check if any Exception is stored for rethrowing from addDBData
89 // @see self::exceptionFromAddDBData
90 if ( $this->exceptionFromAddDBData !== null ) {
91 throw $this->exceptionFromAddDBData;
92 }
93
94 $this->setMwGlobals( 'wgUser', new User() );
95 }
96
97 /**
98 * Checks for test output consisting only of lines containing ETA announcements
99 */
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 ...
105
106 // 1. ... add a dummy output checking to make PHPUnit not complain
107 // about unchecked test output
108 $this->expectOutputRegex( '//' );
109
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
119 );
120 }
121 }
122
123 /**
124 * Step the current XML reader until node end of given name is found.
125 *
126 * @param string $name Name of the closing element to look for
127 * (e.g.: "mediawiki" when looking for </mediawiki>)
128 *
129 * @return bool True if the end node could be found. false otherwise.
130 */
131 protected function skipToNodeEnd( $name ) {
132 while ( $this->xml->read() ) {
133 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
134 $this->xml->name == $name
135 ) {
136 return true;
137 }
138 }
139
140 return false;
141 }
142
143 /**
144 * Step the current XML reader to the first element start after the node
145 * end of a given name.
146 *
147 * @param string $name Name of the closing element to look for
148 * (e.g.: "mediawiki" when looking for </mediawiki>)
149 *
150 * @return bool True if new element after the closing of $name could be
151 * found. false otherwise.
152 */
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;
159 }
160 }
161
162 return false;
163 }
164
165 /**
166 * Opens an XML file to analyze and optionally skips past siteinfo.
167 *
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>
171 */
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" );
179 }
180 }
181
182 /**
183 * Asserts that the xml reader is at the final closing tag of an xml file and
184 * closes the reader.
185 *
186 * @param string $name (optional) the name of the final tag
187 * (e.g.: "mediawiki" for </mediawiki>)
188 */
189 protected function assertDumpEnd( $name = "mediawiki" ) {
190 $this->assertNodeEnd( $name, false );
191 if ( $this->xml->read() ) {
192 $this->skipWhitespace();
193 }
194 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
195 "No proper entity left to parse" );
196 $this->xml->close();
197 }
198
199 /**
200 * Steps the xml reader over white space
201 */
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();
207 }
208 }
209
210 /**
211 * Asserts that the xml reader is at an element of given name, and optionally
212 * skips past it.
213 *
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
217 */
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" );
223 }
224 }
225
226 /**
227 * Asserts that the xml reader is at an closing element of given name, and optionally
228 * skips past it.
229 *
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
233 */
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" );
239 }
240 }
241
242 /**
243 * Asserts that the xml reader is at an element of given tag that contains a given text,
244 * and skips over the element.
245 *
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.
252 */
253 protected function assertTextNode( $name, $text, $skip_ws = true ) {
254 $this->assertNodeStart( $name );
255
256 if ( $text !== false ) {
257 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
258 }
259 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
260 $this->assertNodeEnd( $name );
261
262 if ( $skip_ws ) {
263 $this->skipWhitespace();
264 }
265 }
266
267 /**
268 * Asserts that the xml reader is at the start of a page element and skips over the first
269 * tags, after checking them.
270 *
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.
274 *
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
278 */
279 protected function assertPageStart( $id, $ns, $name ) {
280
281 $this->assertNodeStart( "page" );
282 $this->skipWhitespace();
283
284 $this->assertTextNode( "title", $name );
285 $this->assertTextNode( "ns", $ns );
286 $this->assertTextNode( "id", $id );
287 }
288
289 /**
290 * Asserts that the xml reader is at the page's closing element and skips to the next
291 * element.
292 */
293 protected function assertPageEnd() {
294 $this->assertNodeEnd( "page" );
295 $this->skipWhitespace();
296 }
297
298 /**
299 * Asserts that the xml reader is at a revision and checks its representation before
300 * skipping over it.
301 *
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)
312 */
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
316 ) {
317 $this->assertNodeStart( "revision" );
318 $this->skipWhitespace();
319
320 $this->assertTextNode( "id", $id );
321 if ( $parentid !== false ) {
322 $this->assertTextNode( "parentid", $parentid );
323 }
324 $this->assertTextNode( "timestamp", false );
325
326 $this->assertNodeStart( "contributor" );
327 $this->skipWhitespace();
328 $this->assertTextNode( "ip", false );
329 $this->assertNodeEnd( "contributor" );
330 $this->skipWhitespace();
331
332 $this->assertTextNode( "comment", $summary );
333 $this->skipWhitespace();
334
335 $this->assertTextNode( "model", $model );
336 $this->skipWhitespace();
337
338 $this->assertTextNode( "format", $format );
339 $this->skipWhitespace();
340
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;
347 }
348
349 $this->assertTextNode( "sha1", $text_sha1 );
350
351 if ( !$text_found ) {
352 $this->assertText( $id, $text_id, $text_bytes, $text );
353 }
354
355 $this->assertNodeEnd( "revision" );
356 $this->skipWhitespace();
357 }
358
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 );
364 }
365
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" )
374 ) {
375
376 $this->xml->read();
377 }
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();
386 }
387 }
388 }