Merge "Add option to rebuild message files on a different folder. It also creates...
[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 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 * Step the current XML reader until node end of given name is found.
93 *
94 * @param $name string: name of the closing element to look for
95 * (e.g.: "mediawiki" when looking for </mediawiki>)
96 *
97 * @return bool: true iff the end node could be found. false otherwise.
98 */
99 protected function skipToNodeEnd( $name ) {
100 while ( $this->xml->read() ) {
101 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
102 $this->xml->name == $name ) {
103 return true;
104 }
105 }
106 return false;
107 }
108
109 /**
110 * Step the current XML reader to the first element start after the node
111 * end of a given name.
112 *
113 * @param $name string: name of the closing element to look for
114 * (e.g.: "mediawiki" when looking for </mediawiki>)
115 *
116 * @return bool: true iff new element after the closing of $name could be
117 * found. false otherwise.
118 */
119 protected function skipPastNodeEnd( $name ) {
120 $this->assertTrue( $this->skipToNodeEnd( $name ),
121 "Skipping to end of $name" );
122 while ( $this->xml->read() ) {
123 if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
124 return true;
125 }
126 }
127 return false;
128 }
129
130 /**
131 * Opens an XML file to analyze and optionally skips past siteinfo.
132 *
133 * @param $fname string: name of file to analyze
134 * @param $skip_siteinfo bool: (optional) If true, step the xml reader
135 * to the first element after </siteinfo>
136 */
137 protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
138 $this->xml = new XMLReader();
139 $this->assertTrue( $this->xml->open( $fname ),
140 "Opening temporary file $fname via XMLReader failed" );
141 if ( $skip_siteinfo ) {
142 $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
143 "Skipping past end of siteinfo" );
144 }
145 }
146
147 /**
148 * Asserts that the xml reader is at the final closing tag of an xml file and
149 * closes the reader.
150 *
151 * @param $tag string: (optional) the name of the final tag
152 * (e.g.: "mediawiki" for </mediawiki>)
153 */
154 protected function assertDumpEnd( $name = "mediawiki" ) {
155 $this->assertNodeEnd( $name, false );
156 if ( $this->xml->read() ) {
157 $this->skipWhitespace();
158 }
159 $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
160 "No proper entity left to parse" );
161 $this->xml->close();
162 }
163
164 /**
165 * Steps the xml reader over white space
166 */
167 protected function skipWhitespace() {
168 $cont = true;
169 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
170 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
171 $cont = $this->xml->read();
172 }
173 }
174
175 /**
176 * Asserts that the xml reader is at an element of given name, and optionally
177 * skips past it.
178 *
179 * @param $name string: the name of the element to check for
180 * (e.g.: "mediawiki" for <mediawiki>)
181 * @param $skip bool: (optional) if true, skip past the found element
182 */
183 protected function assertNodeStart( $name, $skip = true ) {
184 $this->assertEquals( $name, $this->xml->name, "Node name" );
185 $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
186 if ( $skip ) {
187 $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
188 }
189 }
190
191 /**
192 * Asserts that the xml reader is at an closing element of given name, and optionally
193 * skips past it.
194 *
195 * @param $name string: the name of the closing element to check for
196 * (e.g.: "mediawiki" for </mediawiki>)
197 * @param $skip bool: (optional) if true, skip past the found element
198 */
199 protected function assertNodeEnd( $name, $skip = true ) {
200 $this->assertEquals( $name, $this->xml->name, "Node name" );
201 $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
202 if ( $skip ) {
203 $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
204 }
205 }
206
207
208 /**
209 * Asserts that the xml reader is at an element of given tag that contains a given text,
210 * and skips over the element.
211 *
212 * @param $name string: the name of the element to check for
213 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
214 * @param $text string|false: If string, check if it equals the elements text.
215 * If false, ignore the element's text
216 * @param $skip_ws bool: (optional) if true, skip past white spaces that trail the
217 * closing element.
218 */
219 protected function assertTextNode( $name, $text, $skip_ws = true ) {
220 $this->assertNodeStart( $name );
221
222 if ( $text !== false ) {
223 $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
224 }
225 $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
226 $this->assertNodeEnd( $name );
227
228 if ( $skip_ws ) {
229 $this->skipWhitespace();
230 }
231 }
232
233 /**
234 * Asserts that the xml reader is at the start of a page element and skips over the first
235 * tags, after checking them.
236 *
237 * Besides the opening page element, this function also checks for and skips over the
238 * title, ns, and id tags. Hence after this function, the xml reader is at the first
239 * revision of the current page.
240 *
241 * @param $id int: id of the page to assert
242 * @param $ns int: number of namespage to assert
243 * @param $name string: title of the current page
244 */
245 protected function assertPageStart( $id, $ns, $name ) {
246
247 $this->assertNodeStart( "page" );
248 $this->skipWhitespace();
249
250 $this->assertTextNode( "title", $name );
251 $this->assertTextNode( "ns", $ns );
252 $this->assertTextNode( "id", $id );
253
254 }
255
256 /**
257 * Asserts that the xml reader is at the page's closing element and skips to the next
258 * element.
259 */
260 protected function assertPageEnd() {
261 $this->assertNodeEnd( "page" );
262 $this->skipWhitespace();
263 }
264
265 /**
266 * Asserts that the xml reader is at a revision and checks its representation before
267 * skipping over it.
268 *
269 * @param $id int: id of the revision
270 * @param $summary string: summary of the revision
271 * @param $text_id int: id of the revision's text
272 * @param $text_bytes int: # of bytes in the revision's text
273 * @param $text_sha1 string: the base36 SHA-1 of the revision's text
274 * @param $text string|false: (optional) The revision's string, or false to check for a
275 * revision stub
276 */
277 protected function assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text = false ) {
278
279 $this->assertNodeStart( "revision" );
280 $this->skipWhitespace();
281
282 $this->assertTextNode( "id", $id );
283 $this->assertTextNode( "timestamp", false );
284
285 $this->assertNodeStart( "contributor" );
286 $this->skipWhitespace();
287 $this->assertTextNode( "ip", false );
288 $this->assertNodeEnd( "contributor" );
289 $this->skipWhitespace();
290
291 $this->assertTextNode( "comment", $summary );
292
293 $this->assertNodeStart( "text", false );
294 if ( $text_bytes !== false ) {
295 $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
296 "Attribute 'bytes' of revision " . $id );
297 }
298
299
300 if ( $text === false ) {
301 // Testing for a stub
302 $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
303 "Text id of revision " . $id );
304 $this->assertFalse( $this->xml->hasValue, "Revision has text" );
305 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
306 if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
307 && ( $this->xml->name == "text" ) ) {
308
309 $this->xml->read();
310 }
311 $this->skipWhitespace();
312 } else {
313 // Testing for a real dump
314 $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
315 $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
316 $this->assertTrue( $this->xml->read(), "Skipping past text" );
317 $this->assertNodeEnd( "text" );
318 $this->skipWhitespace();
319 }
320
321 $this->assertTextNode( "sha1", $text_sha1 );
322
323 $this->assertNodeEnd( "revision" );
324 $this->skipWhitespace();
325 }
326
327 }