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