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