MediaWiki  1.27.1
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 
39  protected function addRevision( Page $page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT ) {
40  $status = $page->doEditContent(
41  ContentHandler::makeContent( $text, $page->getTitle(), $model ),
42  $summary
43  );
44 
45  if ( $status->isGood() ) {
46  $value = $status->getValue();
47  $revision = $value['revision'];
48  $revision_id = $revision->getId();
49  $text_id = $revision->getTextId();
50 
51  if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) {
52  return [ $revision_id, $text_id ];
53  }
54  }
55 
56  throw new MWException( "Could not determine revision id ("
57  . $status->getWikiText( false, false, 'en' ) . ")" );
58  }
59 
66  protected function gunzip( $fname ) {
67  $gzipped_contents = file_get_contents( $fname );
68  if ( $gzipped_contents === false ) {
69  $this->fail( "Could not get contents of $fname" );
70  }
71 
72  $contents = gzdecode( $gzipped_contents );
73 
74  $this->assertEquals(
75  strlen( $contents ),
76  file_put_contents( $fname, $contents ),
77  '# bytes written'
78  );
79  }
80 
86  protected function setUp() {
87  parent::setUp();
88 
89  // Check if any Exception is stored for rethrowing from addDBData
90  // @see self::exceptionFromAddDBData
91  if ( $this->exceptionFromAddDBData !== null ) {
93  }
94 
95  $this->setMwGlobals( 'wgUser', new User() );
96  }
97 
101  function expectETAOutput() {
102  // Newer PHPUnits require assertion about the output using PHPUnit's own
103  // expectOutput[...] functions. However, the PHPUnit shipped prediactes
104  // do not allow to check /each/ line of the output using /readable/ REs.
105  // So we ...
106 
107  // 1. ... add a dummy output checking to make PHPUnit not complain
108  // about unchecked test output
109  $this->expectOutputRegex( '//' );
110 
111  // 2. Do the real output checking on our own.
112  $lines = explode( "\n", $this->getActualOutput() );
113  $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
114  $this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
115  $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
116  foreach ( $lines as $line ) {
117  $this->assertRegExp(
118  "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
119  $line
120  );
121  }
122  }
123 
132  protected function skipToNodeEnd( $name ) {
133  while ( $this->xml->read() ) {
134  if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
135  $this->xml->name == $name
136  ) {
137  return true;
138  }
139  }
140 
141  return false;
142  }
143 
154  protected function skipPastNodeEnd( $name ) {
155  $this->assertTrue( $this->skipToNodeEnd( $name ),
156  "Skipping to end of $name" );
157  while ( $this->xml->read() ) {
158  if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
159  return true;
160  }
161  }
162 
163  return false;
164  }
165 
173  protected function assertDumpStart( $fname, $skip_siteinfo = true ) {
174  $this->xml = new XMLReader();
175  $this->assertTrue( $this->xml->open( $fname ),
176  "Opening temporary file $fname via XMLReader failed" );
177  if ( $skip_siteinfo ) {
178  $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
179  "Skipping past end of siteinfo" );
180  }
181  }
182 
190  protected function assertDumpEnd( $name = "mediawiki" ) {
191  $this->assertNodeEnd( $name, false );
192  if ( $this->xml->read() ) {
193  $this->skipWhitespace();
194  }
195  $this->assertEquals( $this->xml->nodeType, XMLReader::NONE,
196  "No proper entity left to parse" );
197  $this->xml->close();
198  }
199 
203  protected function skipWhitespace() {
204  $cont = true;
205  while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
206  || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
207  $cont = $this->xml->read();
208  }
209  }
210 
219  protected function assertNodeStart( $name, $skip = true ) {
220  $this->assertEquals( $name, $this->xml->name, "Node name" );
221  $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
222  if ( $skip ) {
223  $this->assertTrue( $this->xml->read(), "Skipping past start tag" );
224  }
225  }
226 
235  protected function assertNodeEnd( $name, $skip = true ) {
236  $this->assertEquals( $name, $this->xml->name, "Node name" );
237  $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
238  if ( $skip ) {
239  $this->assertTrue( $this->xml->read(), "Skipping past end tag" );
240  }
241  }
242 
254  protected function assertTextNode( $name, $text, $skip_ws = true ) {
255  $this->assertNodeStart( $name );
256 
257  if ( $text !== false ) {
258  $this->assertEquals( $text, $this->xml->value, "Text of node " . $name );
259  }
260  $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
261  $this->assertNodeEnd( $name );
262 
263  if ( $skip_ws ) {
264  $this->skipWhitespace();
265  }
266  }
267 
280  protected function assertPageStart( $id, $ns, $name ) {
281 
282  $this->assertNodeStart( "page" );
283  $this->skipWhitespace();
284 
285  $this->assertTextNode( "title", $name );
286  $this->assertTextNode( "ns", $ns );
287  $this->assertTextNode( "id", $id );
288  }
289 
294  protected function assertPageEnd() {
295  $this->assertNodeEnd( "page" );
296  $this->skipWhitespace();
297  }
298 
314  protected function assertRevision( $id, $summary, $text_id, $text_bytes,
315  $text_sha1, $text = false, $parentid = false,
317  ) {
318  $this->assertNodeStart( "revision" );
319  $this->skipWhitespace();
320 
321  $this->assertTextNode( "id", $id );
322  if ( $parentid !== false ) {
323  $this->assertTextNode( "parentid", $parentid );
324  }
325  $this->assertTextNode( "timestamp", false );
326 
327  $this->assertNodeStart( "contributor" );
328  $this->skipWhitespace();
329  $this->assertTextNode( "ip", false );
330  $this->assertNodeEnd( "contributor" );
331  $this->skipWhitespace();
332 
333  $this->assertTextNode( "comment", $summary );
334  $this->skipWhitespace();
335 
336  $this->assertTextNode( "model", $model );
337  $this->skipWhitespace();
338 
339  $this->assertTextNode( "format", $format );
340  $this->skipWhitespace();
341 
342  if ( $this->xml->name == "text" ) {
343  // note: <text> tag may occur here or at the very end.
344  $text_found = true;
345  $this->assertText( $id, $text_id, $text_bytes, $text );
346  } else {
347  $text_found = false;
348  }
349 
350  $this->assertTextNode( "sha1", $text_sha1 );
351 
352  if ( !$text_found ) {
353  $this->assertText( $id, $text_id, $text_bytes, $text );
354  }
355 
356  $this->assertNodeEnd( "revision" );
357  $this->skipWhitespace();
358  }
359 
360  protected function assertText( $id, $text_id, $text_bytes, $text ) {
361  $this->assertNodeStart( "text", false );
362  if ( $text_bytes !== false ) {
363  $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
364  "Attribute 'bytes' of revision " . $id );
365  }
366 
367  if ( $text === false ) {
368  // Testing for a stub
369  $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id,
370  "Text id of revision " . $id );
371  $this->assertFalse( $this->xml->hasValue, "Revision has text" );
372  $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
373  if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
374  && ( $this->xml->name == "text" )
375  ) {
376 
377  $this->xml->read();
378  }
379  $this->skipWhitespace();
380  } else {
381  // Testing for a real dump
382  $this->assertTrue( $this->xml->read(), "Skipping text start tag" );
383  $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id );
384  $this->assertTrue( $this->xml->read(), "Skipping past text" );
385  $this->assertNodeEnd( "text" );
386  $this->skipWhitespace();
387  }
388  }
389 }
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:277
assertText($id, $text_id, $text_bytes, $text)
assertDumpEnd($name="mediawiki")
Asserts that the xml reader is at the final closing tag of an xml file and closes the reader...
skipPastNodeEnd($name)
Step the current XML reader to the first element start after the node end of a given name...
$value
assertPageStart($id, $ns, $name)
Asserts that the xml reader is at the start of a page element and skips over the first tags...
XMLReader null $xml
Holds the XMLReader used for analyzing an XML dump.
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:24
addRevision(Page $page, $text, $summary, $model=CONTENT_MODEL_WIKITEXT)
Adds a revision to a page, while returning the resuting revision's id.
assertTextNode($name, $text, $skip_ws=true)
Asserts that the xml reader is at an element of given tag that contains a given text, and skips over the element.
setUp()
Default set up function.
Exception null $exceptionFromAddDBData
exception to be rethrown once in sound PHPUnit surrounding
$summary
gunzip($fname)
gunzips the given file and stores the result in the original file name
assertNodeEnd($name, $skip=true)
Asserts that the xml reader is at an closing element of given name, and optionally skips past it...
Base class that store and restore the Language objects.
assertDumpStart($fname, $skip_siteinfo=true)
Opens an XML file to analyze and optionally skips past siteinfo.
static makeContent($text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
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
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...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$lines
Definition: router.php:66
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined...
Definition: Setup.php:35
$line
Definition: cdb.php:59
assertNodeStart($name, $skip=true)
Asserts that the xml reader is at an element of given name, and optionally skips past it...
Base TestCase for dumps.
Definition: DumpTestCase.php:6
skipToNodeEnd($name)
Step the current XML reader until node end of given name is found.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1004
skipWhitespace()
Steps the xml reader over white space.
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:292
setMwGlobals($pairs, $value=null)
expectETAOutput()
Checks for test output consisting only of lines containing ETA announcements.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2338
assertPageEnd()
Asserts that the xml reader is at the page's closing element and skips to the next element...
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310