MediaWiki  1.23.2
DumpTestCase.php
Go to the documentation of this file.
1 <?php
2 
6 abstract class DumpTestCase extends MediaWikiLangTestCase {
7 
19  protected $exceptionFromAddDBData = null;
20 
26  protected $xml = null;
27 
37  protected function addRevision( Page $page, $text, $summary ) {
38  $status = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), $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->getWikiText() . ")" );
49  }
50 
57  protected function gunzip( $fname ) {
58  $gzipped_contents = file_get_contents( $fname );
59  if ( $gzipped_contents === false ) {
60  $this->fail( "Could not get contents of $fname" );
61  }
62 
63  $contents = gzdecode( $gzipped_contents );
64 
65  $this->assertEquals(
66  strlen( $contents ),
67  file_put_contents( $fname, $contents ),
68  '# bytes written'
69  );
70  }
71 
77  protected function setUp() {
78  parent::setUp();
79 
80  // Check if any Exception is stored for rethrowing from addDBData
81  // @see self::exceptionFromAddDBData
82  if ( $this->exceptionFromAddDBData !== null ) {
84  }
85 
86  $this->setMwGlobals( 'wgUser', new User() );
87  }
88 
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 
120  protected function skipToNodeEnd( $name ) {
121  while ( $this->xml->read() ) {
122  if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
123  $this->xml->name == $name
124  ) {
125  return true;
126  }
127  }
128 
129  return false;
130  }
131 
142  protected function skipPastNodeEnd( $name ) {
143  $this->assertTrue( $this->skipToNodeEnd( $name ),
144  "Skipping to end of $name" );
145  while ( $this->xml->read() ) {
146  if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
147  return true;
148  }
149  }
150 
151  return false;
152  }
153 
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 
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 
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 
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 
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 
242  protected function assertTextNode( $name, $text, $skip_ws = true ) {
243  $this->assertNodeStart( $name );
244 
245  if ( $text !== false ) {
246  $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
247  }
248  $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
249  $this->assertNodeEnd( $name );
250 
251  if ( $skip_ws ) {
252  $this->skipWhitespace();
253  }
254  }
255 
268  protected function assertPageStart( $id, $ns, $name ) {
269 
270  $this->assertNodeStart( "page" );
271  $this->skipWhitespace();
272 
273  $this->assertTextNode( "title", $name );
274  $this->assertTextNode( "ns", $ns );
275  $this->assertTextNode( "id", $id );
276  }
277 
282  protected function assertPageEnd() {
283  $this->assertNodeEnd( "page" );
284  $this->skipWhitespace();
285  }
286 
302  protected function assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text = false, $parentid = false,
304  ) {
305  $this->assertNodeStart( "revision" );
306  $this->skipWhitespace();
307 
308  $this->assertTextNode( "id", $id );
309  if ( $parentid !== false ) {
310  $this->assertTextNode( "parentid", $parentid );
311  }
312  $this->assertTextNode( "timestamp", false );
313 
314  $this->assertNodeStart( "contributor" );
315  $this->skipWhitespace();
316  $this->assertTextNode( "ip", false );
317  $this->assertNodeEnd( "contributor" );
318  $this->skipWhitespace();
319 
320  $this->assertTextNode( "comment", $summary );
321  $this->skipWhitespace();
322 
323  if ( $this->xml->name == "text" ) {
324  // note: <text> tag may occur here or at the very end.
325  $text_found = true;
326  $this->assertText( $id, $text_id, $text_bytes, $text );
327  } else {
328  $text_found = false;
329  }
330 
331  $this->assertTextNode( "sha1", $text_sha1 );
332 
333  $this->assertTextNode( "model", $model );
334  $this->skipWhitespace();
335 
336  $this->assertTextNode( "format", $format );
337  $this->skipWhitespace();
338 
339  if ( !$text_found ) {
340  $this->assertText( $id, $text_id, $text_bytes, $text );
341  }
342 
343  $this->assertNodeEnd( "revision" );
344  $this->skipWhitespace();
345  }
346 
347  protected function assertText( $id, $text_id, $text_bytes, $text ) {
348  $this->assertNodeStart( "text", false );
349  if ( $text_bytes !== false ) {
350  $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
351  "Attribute 'bytes' of revision " . $id );
352  }
353 
354  if ( $text === false ) {
355  // Testing for a stub
356  $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
357  "Text id of revision " . $id );
358  $this->assertFalse( $this->xml->hasValue, "Revision has text" );
359  $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
360  if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
361  && ( $this->xml->name == "text" )
362  ) {
363 
364  $this->xml->read();
365  }
366  $this->skipWhitespace();
367  } else {
368  // Testing for a real dump
369  $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
370  $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
371  $this->assertTrue( $this->xml->read(), "Skipping past text" );
372  $this->assertNodeEnd( "text" );
373  $this->skipWhitespace();
374  }
375  }
376 }
Page
Abstract class for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: WikiPage.php:26
DumpTestCase
Base TestCase for dumps.
Definition: DumpTestCase.php:6
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
DumpTestCase\assertPageEnd
assertPageEnd()
Asserts that the xml reader is at the page's closing element and skips to the next element.
Definition: DumpTestCase.php:280
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:35
DumpTestCase\assertTextNode
assertTextNode( $name, $text, $skip_ws=true)
Asserts that the xml reader is at an element of given tag that contains a given text,...
Definition: DumpTestCase.php:240
DumpTestCase\assertText
assertText( $id, $text_id, $text_bytes, $text)
Definition: DumpTestCase.php:345
fail
as a message key or array as accepted by ApiBase::dieUsageMsg after processing request parameters Return false to let the request fail
Definition: hooks.txt:375
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:283
DumpTestCase\assertDumpEnd
assertDumpEnd( $name="mediawiki")
Asserts that the xml reader is at the final closing tag of an xml file and closes the reader.
Definition: DumpTestCase.php:176
DumpTestCase\setUp
setUp()
Default set up function.
Definition: DumpTestCase.php:75
csslex.WHITESPACE
string WHITESPACE
Definition: csslex.py:70
MWException
MediaWiki exception.
Definition: MWException.php:26
DumpTestCase\assertNodeEnd
assertNodeEnd( $name, $skip=true)
Asserts that the xml reader is at an closing element of given name, and optionally skips past it.
Definition: DumpTestCase.php:221
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
DumpTestCase\assertPageStart
assertPageStart( $id, $ns, $name)
Asserts that the xml reader is at the start of a page element and skips over the first tags,...
Definition: DumpTestCase.php:266
DumpTestCase\assertNodeStart
assertNodeStart( $name, $skip=true)
Asserts that the xml reader is at an element of given name, and optionally skips past it.
Definition: DumpTestCase.php:205
$lines
$lines
Definition: router.php:65
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
DumpTestCase\expectETAOutput
expectETAOutput()
Checks for test output consisting only of lines containing ETA announcements.
Definition: DumpTestCase.php:90
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:297
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:144
$line
$line
Definition: cdb.php:57
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
DumpTestCase\skipPastNodeEnd
skipPastNodeEnd( $name)
Step the current XML reader to the first element start after the node end of a given name.
Definition: DumpTestCase.php:140
DumpTestCase\assertDumpStart
assertDumpStart( $fname, $skip_siteinfo=true)
Opens an XML file to analyze and optionally skips past siteinfo.
Definition: DumpTestCase.php:159
$summary
$summary
Definition: importImages.php:120
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
DumpTestCase\gunzip
gunzip( $fname)
gunzips the given file and stores the result in the original file name
Definition: DumpTestCase.php:55
User
User
Definition: All_system_messages.txt:425
DumpTestCase\$xml
XMLReader null $xml
Holds the xmlreader used for analyzing an xml dump.
Definition: DumpTestCase.php:24
DumpTestCase\skipWhitespace
skipWhitespace()
Steps the xml reader over white space.
Definition: DumpTestCase.php:189
DumpTestCase\$exceptionFromAddDBData
Exception null $exceptionFromAddDBData
exception to be rethrown once in sound PHPUnit surrounding
Definition: DumpTestCase.php:18
DumpTestCase\assertRevision
assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text=false, $parentid=false, $model=CONTENT_MODEL_WIKITEXT, $format=CONTENT_FORMAT_WIKITEXT)
Asserts that the xml reader is at a revision and checks its representation before skipping over it.
Definition: DumpTestCase.php:300
DumpTestCase\skipToNodeEnd
skipToNodeEnd( $name)
Step the current XML reader until node end of given name is found.
Definition: DumpTestCase.php:118
DumpTestCase\addRevision
addRevision(Page $page, $text, $summary)
Adds a revision to a page, while returning the resuting revision's id.
Definition: DumpTestCase.php:35