Merge "tablesorter: Unbreak abbreviated month name support"
[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 WikiPage: page to add the revision to
32 * @param $text string: revisions text
33 * @param $text string: revisions summare
34 *
35 * @throws MWExcepion
36 */
37 protected function addRevision( Page $page, $text, $summary ) {
38 $status = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), $summary );
39 if ( $status->isGood() ) {
40 $value = $status->getValue();
41 $revision = $value['revision'];
42 $revision_id = $revision->getId();
43 $text_id = $revision->getTextId();
44 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
45 return array( $revision_id, $text_id );
46 }
47 }
48 throw new MWException( "Could not determine revision id (" . $status->getWikiText() . ")" );
49 }
50
51
52 /**
53 * gunzips the given file and stores the result in the original file name
54 *
55 * @param $fname string: filename to read the gzipped data from and stored
56 * the gunzipped data into
57 */
58 protected function gunzip( $fname ) {
59 $gzipped_contents = file_get_contents( $fname );
60 if ( $gzipped_contents === false ) {
61 $this->fail( "Could not get contents of $fname" );
62 }
63
64 $contents = gzdecode( $gzipped_contents );
65
66 $this->assertEquals(
67 strlen( $contents ),
68 file_put_contents( $fname, $contents ),
69 '# bytes written'
70 );
71 }
72
73 /**
74 * Default set up function.
75 *
76 * Clears $wgUser, and reports errors from addDBData to PHPUnit
77 */
78 protected function setUp() {
79 parent::setUp();
80
81 // Check if any Exception is stored for rethrowing from addDBData
82 // @see self::exceptionFromAddDBData
83 if ( $this->exceptionFromAddDBData !== null ) {
84 throw $this->exceptionFromAddDBData;
85 }
86
87 $this->setMwGlobals( 'wgUser', new User() );
88 }
89
90 /**
91 * Checks for test output consisting only of lines containing ETA announcements
92 */
93 function expectETAOutput() {
94 // Newer PHPUnits require assertion about the output using PHPUnit's own
95 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
96 // do not allow to check /each/ line of the output using /readable/ REs.
97 // So we ...
98 //
99 // 1. ... add a dummy output checking to make PHPUnit not complain
100 // about unchecked test output
101 $this->expectOutputRegex( '//' );
102
103 // 2. Do the real output checking on our own.
104 $lines = explode( "\n", $this->getActualOutput() );
105 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
106 $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
107 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
108 foreach ( $lines as $line ) {
109 $this->assertRegExp( "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/", $line );
110 }
111 }
112
113
114 /**
115 * Step the current XML reader until node end of given name is found.
116 *
117 * @param $name string: name of the closing element to look for
118 * (e.g.: "mediawiki" when looking for </mediawiki>)
119 *
120 * @return bool: true iff the end node could be found. false otherwise.
121 */
122 protected function skipToNodeEnd( $name ) {
123 while ( $this->xml->read() ) {
124 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
125 $this->xml->name == $name
126 ) {
127 return true;
128 }
129 }
130
131 return false;
132 }
133
134 /**
135 * Step the current XML reader to the first element start after the node
136 * end of a given name.
137 *
138 * @param $name string: name of the closing element to look for
139 * (e.g.: "mediawiki" when looking for </mediawiki>)
140 *
141 * @return bool: true iff new element after the closing of $name could be
142 * found. false otherwise.
143 */
144 protected function skipPastNodeEnd( $name ) {
145 $this->assertTrue( $this->skipToNodeEnd( $name ),
146 "Skipping to end of $name" );
147 while ( $this->xml->read() ) {
148 if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
149 return true;
150 }
151 }
152
153 return false;
154 }
155
156 /**
157 * Opens an XML file to analyze and optionally skips past siteinfo.
158 *
159 * @param $fname string: name of file to analyze
160 * @param $skip_siteinfo bool: (optional) If true, step the xml reader
161 * to the first element after </siteinfo>
162 */
163 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
164 $this->xml = new XMLReader();
165 $this->assertTrue( $this->xml->open( $fname ),
166 "Opening temporary file $fname via XMLReader failed" );
167 if ( $skip_siteinfo ) {
168 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
169 "Skipping past end of siteinfo" );
170 }
171 }
172
173 /**
174 * Asserts that the xml reader is at the final closing tag of an xml file and
175 * closes the reader.
176 *
177 * @param $tag string: (optional) the name of the final tag
178 * (e.g.: "mediawiki" for </mediawiki>)
179 */
180 protected function assertDumpEnd( $name = "mediawiki" ) {
181 $this->assertNodeEnd( $name, false );
182 if ( $this->xml->read() ) {
183 $this->skipWhitespace();
184 }
185 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
186 "No proper entity left to parse" );
187 $this->xml->close();
188 }
189
190 /**
191 * Steps the xml reader over white space
192 */
193 protected function skipWhitespace() {
194 $cont = true;
195 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
196 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
197 $cont = $this->xml->read();
198 }
199 }
200
201 /**
202 * Asserts that the xml reader is at an element of given name, and optionally
203 * skips past it.
204 *
205 * @param $name string: the name of the element to check for
206 * (e.g.: "mediawiki" for <mediawiki>)
207 * @param $skip bool: (optional) if true, skip past the found element
208 */
209 protected function assertNodeStart( $name, $skip = true ) {
210 $this->assertEquals( $name, $this->xml->name, "Node name" );
211 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
212 if ( $skip ) {
213 $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
214 }
215 }
216
217 /**
218 * Asserts that the xml reader is at an closing element of given name, and optionally
219 * skips past it.
220 *
221 * @param $name string: the name of the closing element to check for
222 * (e.g.: "mediawiki" for </mediawiki>)
223 * @param $skip bool: (optional) if true, skip past the found element
224 */
225 protected function assertNodeEnd( $name, $skip = true ) {
226 $this->assertEquals( $name, $this->xml->name, "Node name" );
227 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
228 if ( $skip ) {
229 $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
230 }
231 }
232
233
234 /**
235 * Asserts that the xml reader is at an element of given tag that contains a given text,
236 * and skips over the element.
237 *
238 * @param $name string: the name of the element to check for
239 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
240 * @param $text string|false: If string, check if it equals the elements text.
241 * If false, ignore the element's text
242 * @param $skip_ws bool: (optional) if true, skip past white spaces that trail the
243 * closing element.
244 */
245 protected function assertTextNode( $name, $text, $skip_ws = true ) {
246 $this->assertNodeStart( $name );
247
248 if ( $text !== false ) {
249 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
250 }
251 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
252 $this->assertNodeEnd( $name );
253
254 if ( $skip_ws ) {
255 $this->skipWhitespace();
256 }
257 }
258
259 /**
260 * Asserts that the xml reader is at the start of a page element and skips over the first
261 * tags, after checking them.
262 *
263 * Besides the opening page element, this function also checks for and skips over the
264 * title, ns, and id tags. Hence after this function, the xml reader is at the first
265 * revision of the current page.
266 *
267 * @param $id int: id of the page to assert
268 * @param $ns int: number of namespage to assert
269 * @param $name string: title of the current page
270 */
271 protected function assertPageStart( $id, $ns, $name ) {
272
273 $this->assertNodeStart( "page" );
274 $this->skipWhitespace();
275
276 $this->assertTextNode( "title", $name );
277 $this->assertTextNode( "ns", $ns );
278 $this->assertTextNode( "id", $id );
279 }
280
281 /**
282 * Asserts that the xml reader is at the page's closing element and skips to the next
283 * element.
284 */
285 protected function assertPageEnd() {
286 $this->assertNodeEnd( "page" );
287 $this->skipWhitespace();
288 }
289
290 /**
291 * Asserts that the xml reader is at a revision and checks its representation before
292 * skipping over it.
293 *
294 * @param $id int: id of the revision
295 * @param $summary string: summary of the revision
296 * @param $text_id int: id of the revision's text
297 * @param $text_bytes int: # of bytes in the revision's text
298 * @param $text_sha1 string: the base36 SHA-1 of the revision's text
299 * @param $text string|false: (optional) The revision's string, or false to check for a
300 * revision stub
301 * @param $model String: the expected content model id (default: CONTENT_MODEL_WIKITEXT)
302 * @param $format String: the expected format model id (default: CONTENT_FORMAT_WIKITEXT)
303 * @param $parentid int|false: (optional) id of the parent revision
304 */
305 protected function assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text = false, $parentid = false,
306 $model = CONTENT_MODEL_WIKITEXT, $format = CONTENT_FORMAT_WIKITEXT
307 ) {
308 $this->assertNodeStart( "revision" );
309 $this->skipWhitespace();
310
311 $this->assertTextNode( "id", $id );
312 if ( $parentid !== false ) {
313 $this->assertTextNode( "parentid", $parentid );
314 }
315 $this->assertTextNode( "timestamp", false );
316
317 $this->assertNodeStart( "contributor" );
318 $this->skipWhitespace();
319 $this->assertTextNode( "ip", false );
320 $this->assertNodeEnd( "contributor" );
321 $this->skipWhitespace();
322
323 $this->assertTextNode( "comment", $summary );
324 $this->skipWhitespace();
325
326 if ( $this->xml->name == "text" ) {
327 // note: <text> tag may occur here or at the very end.
328 $text_found = true;
329 $this->assertText( $id, $text_id, $text_bytes, $text );
330 } else {
331 $text_found = false;
332 }
333
334 $this->assertTextNode( "sha1", $text_sha1 );
335
336 $this->assertTextNode( "model", $model );
337 $this->skipWhitespace();
338
339 $this->assertTextNode( "format", $format );
340 $this->skipWhitespace();
341
342 if ( !$text_found ) {
343 $this->assertText( $id, $text_id, $text_bytes, $text );
344 }
345
346 $this->assertNodeEnd( "revision" );
347 $this->skipWhitespace();
348 }
349
350 protected function assertText( $id, $text_id, $text_bytes, $text ) {
351 $this->assertNodeStart( "text", false );
352 if ( $text_bytes !== false ) {
353 $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
354 "Attribute 'bytes' of revision " . $id );
355 }
356
357 if ( $text === false ) {
358 // Testing for a stub
359 $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
360 "Text id of revision " . $id );
361 $this->assertFalse( $this->xml->hasValue, "Revision has text" );
362 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
363 if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
364 && ( $this->xml->name == "text" )
365 ) {
366
367 $this->xml->read();
368 }
369 $this->skipWhitespace();
370 } else {
371 // Testing for a real dump
372 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
373 $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
374 $this->assertTrue( $this->xml->read(), "Skipping past text" );
375 $this->assertNodeEnd( "text" );
376 $this->skipWhitespace();
377 }
378 }
379 }