Merge "Remove some duplicate code in ProfilerSimpleTrace"
[lhc/web/wiklou.git] / tests / phpunit / maintenance / DumpTestCase.php
1 <?php
2 global $IP;
3 require_once( "$IP/maintenance/backup.inc" );
4
5 /**
6 * Base TestCase for dumps
7 */
8 abstract class DumpTestCase extends MediaWikiTestCase {
9
10 /**
11 * exception to be rethrown once in sound PHPUnit surrounding
12 *
13 * As the current MediaWikiTestCase::run is not robust enough to recover
14 * from thrown exceptions directly, we cannot throw frow within
15 * self::addDBData, although it would be appropriate. Hence, we catch the
16 * exception and store it until we are in setUp and may finally rethrow
17 * the exception without crashing the test suite.
18 *
19 * @var Exception|null
20 */
21 protected $exceptionFromAddDBData = null;
22
23 /**
24 * Holds the xmlreader used for analyzing an xml dump
25 *
26 * @var XMLReader|null
27 */
28 protected $xml = null;
29
30 /**
31 * Adds a revision to a page, while returning the resuting revision's id
32 *
33 * @param $page WikiPage: page to add the revision to
34 * @param $text string: revisions text
35 * @param $text string: revisions summare
36 *
37 * @throws MWExcepion
38 */
39 protected function addRevision( Page $page, $text, $summary ) {
40 $status = $page->doEdit( $text, $summary );
41 if ( $status->isGood() ) {
42 $value = $status->getValue();
43 $revision = $value['revision'];
44 $revision_id = $revision->getId();
45 $text_id = $revision->getTextId();
46 if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
47 return array( $revision_id, $text_id );
48 }
49 }
50 throw new MWException( "Could not determine revision id (" . $status->getXML() . ")" );
51 }
52
53
54 /**
55 * gunzips the given file and stores the result in the original file name
56 *
57 * @param $fname string: filename to read the gzipped data from and stored
58 * the gunzipped data into
59 */
60 protected function gunzip( $fname ) {
61 $gzipped_contents = file_get_contents( $fname );
62 if ( $gzipped_contents === FALSE ) {
63 $this->fail( "Could not get contents of $fname" );
64 }
65 // We resort to use gzinflate instead of gzdecode, as gzdecode
66 // need not be available
67 $contents = gzinflate( substr( $gzipped_contents, 10, -8 ) );
68 $this->assertEquals( strlen( $contents ),
69 file_put_contents( $fname, $contents ), "# bytes written" );
70 }
71
72 /**
73 * obtains a new temporary file name
74 *
75 * The obtained filename is enlisted to be removed upon tearDown
76 *
77 * @returns string: absolute name of the temporary file
78 */
79 protected function getNewTempFile() {
80 $fname = tempnam( wfTempDir(), 'MW_PHPUnit_' . get_class( $this ) . '_' );
81 $this->tmpfiles[] = $fname;
82 return $fname;
83 }
84
85 /**
86 * Default set up function.
87 *
88 * Clears $wgUser, and reports errors from addDBData to PHPUnit
89 */
90 function setUp() {
91 global $wgUser;
92
93 parent::setUp();
94
95 // Check if any Exception is stored for rethrowing from addDBData
96 // @see self::exceptionFromAddDBData
97 if ( $this->exceptionFromAddDBData !== null ) {
98 throw $this->exceptionFromAddDBData;
99 }
100 $this->tmpfiles = array();
101
102 $wgUser = new User();
103 }
104
105 /**
106 * Default tear down function
107 *
108 * Removes all files that have been allocated via self::getNewTempFile, even if
109 * they turn out to be (empty or non-empty) directories now.
110 */
111 function tearDown() {
112 foreach ( $this->tmpfiles as $fname ) {
113 if ( is_file( $fname ) || ( is_link( $fname ) ) ) {
114 unlink( $fname );
115 } elseif ( is_dir( $fname ) ) {
116 wfRecursiveRemoveDir( $fname );
117 }
118 }
119 parent::tearDown();
120 }
121
122
123 /**
124 * Step the current XML reader until node end of given name is found.
125 *
126 * @param $name string: name of the closing element to look for
127 * (e.g.: "mediawiki" when looking for </mediawiki>)
128 *
129 * @return bool: true iff 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 return true;
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 $name string: name of the closing element to look for
146 * (e.g.: "mediawiki" when looking for </mediawiki>)
147 *
148 * @return bool: true iff 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 return false;
160 }
161
162 /**
163 * Opens an XML file to analyze and optionally skips past siteinfo.
164 *
165 * @param $fname string: name of file to analyze
166 * @param $skip_siteinfo bool: (optional) If true, step the xml reader
167 * to the first element after </siteinfo>
168 */
169 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
170 $this->xml = new XMLReader();
171 $this->assertTrue( $this->xml->open( $fname ),
172 "Opening temporary file $fname via XMLReader failed" );
173 if ( $skip_siteinfo ) {
174 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
175 "Skipping past end of siteinfo" );
176 }
177 }
178
179 /**
180 * Asserts that the xml reader is at the final closing tag of an xml file and
181 * closes the reader.
182 *
183 * @param $tag string: (optional) the name of the final tag
184 * (e.g.: "mediawiki" for </mediawiki>)
185 */
186 protected function assertDumpEnd( $name = "mediawiki" ) {
187 $this->assertNodeEnd( $name, false );
188 if ( $this->xml->read() ) {
189 $this->skipWhitespace();
190 }
191 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
192 "No proper entity left to parse" );
193 $this->xml->close();
194 }
195
196 /**
197 * Steps the xml reader over white space
198 */
199 protected function skipWhitespace() {
200 $cont = true;
201 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
202 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
203 $cont = $this->xml->read();
204 }
205 }
206
207 /**
208 * Asserts that the xml reader is at an element of given name, and optionally
209 * skips past it.
210 *
211 * @param $name string: the name of the element to check for
212 * (e.g.: "mediawiki" for <mediawiki>)
213 * @param $skip bool: (optional) if true, skip past the found element
214 */
215 protected function assertNodeStart( $name, $skip = true ) {
216 $this->assertEquals( $name, $this->xml->name, "Node name" );
217 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
218 if ( $skip ) {
219 $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
220 }
221 }
222
223 /**
224 * Asserts that the xml reader is at an closing element of given name, and optionally
225 * skips past it.
226 *
227 * @param $name string: the name of the closing element to check for
228 * (e.g.: "mediawiki" for </mediawiki>)
229 * @param $skip bool: (optional) if true, skip past the found element
230 */
231 protected function assertNodeEnd( $name, $skip = true ) {
232 $this->assertEquals( $name, $this->xml->name, "Node name" );
233 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
234 if ( $skip ) {
235 $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
236 }
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 $name string: the name of the element to check for
245 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
246 * @param $text string|false: If string, check if it equals the elements text.
247 * If false, ignore the element's text
248 * @param $skip_ws bool: (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 $id int: id of the page to assert
274 * @param $ns int: number of namespage to assert
275 * @param $name string: 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 /**
289 * Asserts that the xml reader is at the page's closing element and skips to the next
290 * element.
291 */
292 protected function assertPageEnd() {
293 $this->assertNodeEnd( "page" );
294 $this->skipWhitespace();
295 }
296
297 /**
298 * Asserts that the xml reader is at a revision and checks its representation before
299 * skipping over it.
300 *
301 * @param $id int: id of the revision
302 * @param $summary string: summary of the revision
303 * @param $text_id int: id of the revision's text
304 * @param $text_bytes int: # of bytes in the revision's text
305 * @param $text_sha1 string: the base36 SHA-1 of the revision's text
306 * @param $text string|false: (optional) The revision's string, or false to check for a
307 * revision stub
308 */
309 protected function assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text = false ) {
310
311 $this->assertNodeStart( "revision" );
312 $this->skipWhitespace();
313
314 $this->assertTextNode( "id", $id );
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
325 $this->assertNodeStart( "text", false );
326 if ( $text_bytes !== false ) {
327 $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
328 "Attribute 'bytes' of revision " . $id );
329 }
330
331
332 if ( $text === false ) {
333 // Testing for a stub
334 $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
335 "Text id of revision " . $id );
336 $this->assertFalse( $this->xml->hasValue, "Revision has text" );
337 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
338 if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
339 && ( $this->xml->name == "text" ) ) {
340
341 $this->xml->read();
342 }
343 $this->skipWhitespace();
344 } else {
345 // Testing for a real dump
346 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
347 $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
348 $this->assertTrue( $this->xml->read(), "Skipping past text" );
349 $this->assertNodeEnd( "text" );
350 $this->skipWhitespace();
351 }
352
353 $this->assertTextNode( "sha1", $text_sha1 );
354
355 $this->assertNodeEnd( "revision" );
356 $this->skipWhitespace();
357 }
358
359 }