Change position of <model> and <format> tags in XML dumps.
[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 summare
34 * @return array
35 * @throws MWException
36 */
37 protected function addRevision( Page $page, $text, $summary ) {
38 $status = $page->doEditContent(
39 ContentHandler::makeContent( $text, $page->getTitle() ),
40 $summary
41 );
42
43 if ( $status->isGood() ) {
44 $value = $status->getValue();
45 $revision = $value['revision'];
46 $revision_id = $revision->getId();
47 $text_id = $revision->getTextId();
48
49 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
50 return array( $revision_id, $text_id );
51 }
52 }
53
54 throw new MWException( "Could not determine revision id (" . $status->getWikiText() . ")" );
55 }
56
57 /**
58 * gunzips the given file and stores the result in the original file name
59 *
60 * @param string $fname Filename to read the gzipped data from and stored
61 * the gunzipped data into
62 */
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" );
67 }
68
69 $contents = gzdecode( $gzipped_contents );
70
71 $this->assertEquals(
72 strlen( $contents ),
73 file_put_contents( $fname, $contents ),
74 '# bytes written'
75 );
76 }
77
78 /**
79 * Default set up function.
80 *
81 * Clears $wgUser, and reports errors from addDBData to PHPUnit
82 */
83 protected function setUp() {
84 parent::setUp();
85
86 // Check if any Exception is stored for rethrowing from addDBData
87 // @see self::exceptionFromAddDBData
88 if ( $this->exceptionFromAddDBData !== null ) {
89 throw $this->exceptionFromAddDBData;
90 }
91
92 $this->setMwGlobals( 'wgUser', new User() );
93 }
94
95 /**
96 * Checks for test output consisting only of lines containing ETA announcements
97 */
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 ...
103 //
104 // 1. ... add a dummy output checking to make PHPUnit not complain
105 // about unchecked test output
106 $this->expectOutputRegex( '//' );
107
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
117 );
118 }
119 }
120
121 /**
122 * Step the current XML reader until node end of given name is found.
123 *
124 * @param string $name Name of the closing element to look for
125 * (e.g.: "mediawiki" when looking for </mediawiki>)
126 *
127 * @return bool True if the end node could be found. false otherwise.
128 */
129 protected function skipToNodeEnd( $name ) {
130 while ( $this->xml->read() ) {
131 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
132 $this->xml->name == $name
133 ) {
134 return true;
135 }
136 }
137
138 return false;
139 }
140
141 /**
142 * Step the current XML reader to the first element start after the node
143 * end of a given name.
144 *
145 * @param string $name Name of the closing element to look for
146 * (e.g.: "mediawiki" when looking for </mediawiki>)
147 *
148 * @return bool True if new element after the closing of $name could be
149 * found. false otherwise.
150 */
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;
157 }
158 }
159
160 return false;
161 }
162
163 /**
164 * Opens an XML file to analyze and optionally skips past siteinfo.
165 *
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>
169 */
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" );
177 }
178 }
179
180 /**
181 * Asserts that the xml reader is at the final closing tag of an xml file and
182 * closes the reader.
183 *
184 * @param string $name (optional) the name of the final tag
185 * (e.g.: "mediawiki" for </mediawiki>)
186 */
187 protected function assertDumpEnd( $name = "mediawiki" ) {
188 $this->assertNodeEnd( $name, false );
189 if ( $this->xml->read() ) {
190 $this->skipWhitespace();
191 }
192 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
193 "No proper entity left to parse" );
194 $this->xml->close();
195 }
196
197 /**
198 * Steps the xml reader over white space
199 */
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();
205 }
206 }
207
208 /**
209 * Asserts that the xml reader is at an element of given name, and optionally
210 * skips past it.
211 *
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
215 */
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" );
221 }
222 }
223
224 /**
225 * Asserts that the xml reader is at an closing element of given name, and optionally
226 * skips past it.
227 *
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
231 */
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" );
237 }
238 }
239
240 /**
241 * Asserts that the xml reader is at an element of given tag that contains a given text,
242 * and skips over the element.
243 *
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.
250 */
251 protected function assertTextNode( $name, $text, $skip_ws = true ) {
252 $this->assertNodeStart( $name );
253
254 if ( $text !== false ) {
255 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
256 }
257 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
258 $this->assertNodeEnd( $name );
259
260 if ( $skip_ws ) {
261 $this->skipWhitespace();
262 }
263 }
264
265 /**
266 * Asserts that the xml reader is at the start of a page element and skips over the first
267 * tags, after checking them.
268 *
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.
272 *
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
276 */
277 protected function assertPageStart( $id, $ns, $name ) {
278
279 $this->assertNodeStart( "page" );
280 $this->skipWhitespace();
281
282 $this->assertTextNode( "title", $name );
283 $this->assertTextNode( "ns", $ns );
284 $this->assertTextNode( "id", $id );
285 }
286
287 /**
288 * Asserts that the xml reader is at the page's closing element and skips to the next
289 * element.
290 */
291 protected function assertPageEnd() {
292 $this->assertNodeEnd( "page" );
293 $this->skipWhitespace();
294 }
295
296 /**
297 * Asserts that the xml reader is at a revision and checks its representation before
298 * skipping over it.
299 *
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 int|bool $parentid (optional) id of the parent revision
308 * @param string $model The expected content model id (default: CONTENT_MODEL_WIKITEXT)
309 * @param string $format The expected format model id (default: CONTENT_FORMAT_WIKITEXT)
310 */
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
314 ) {
315 $this->assertNodeStart( "revision" );
316 $this->skipWhitespace();
317
318 $this->assertTextNode( "id", $id );
319 if ( $parentid !== false ) {
320 $this->assertTextNode( "parentid", $parentid );
321 }
322 $this->assertTextNode( "timestamp", false );
323
324 $this->assertNodeStart( "contributor" );
325 $this->skipWhitespace();
326 $this->assertTextNode( "ip", false );
327 $this->assertNodeEnd( "contributor" );
328 $this->skipWhitespace();
329
330 $this->assertTextNode( "comment", $summary );
331 $this->skipWhitespace();
332
333 $this->assertTextNode( "model", $model );
334 $this->skipWhitespace();
335
336 $this->assertTextNode( "format", $format );
337 $this->skipWhitespace();
338
339 if ( $this->xml->name == "text" ) {
340 // note: <text> tag may occur here or at the very end.
341 $text_found = true;
342 $this->assertText( $id, $text_id, $text_bytes, $text );
343 } else {
344 $text_found = false;
345 }
346
347 $this->assertTextNode( "sha1", $text_sha1 );
348
349 if ( !$text_found ) {
350 $this->assertText( $id, $text_id, $text_bytes, $text );
351 }
352
353 $this->assertNodeEnd( "revision" );
354 $this->skipWhitespace();
355 }
356
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 );
362 }
363
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" )
372 ) {
373
374 $this->xml->read();
375 }
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();
384 }
385 }
386 }