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