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