MediaWiki  1.33.0
DumpAsserter.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use PHPUnit\Framework\Assert;
6 use XMLReader;
7 
11 class DumpAsserter {
12 
18  protected $xml = null;
19 
25  protected $schemaVersion;
26 
32  public function __construct( $schemaVersion ) {
33  $this->schemaVersion = $schemaVersion;
34  }
35 
44  public function skipToNodeEnd( $name ) {
45  while ( $this->xml->read() ) {
46  if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
47  $this->xml->name == $name
48  ) {
49  return true;
50  }
51  }
52 
53  return false;
54  }
55 
66  public function skipPastNodeEnd( $name ) {
67  Assert::assertTrue( $this->skipToNodeEnd( $name ),
68  "Skipping to end of $name" );
69  while ( $this->xml->read() ) {
70  if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
71  return true;
72  }
73  }
74 
75  return false;
76  }
77 
85  public function assertDumpStart( $fname, $skip_siteinfo = true ) {
86  $this->xml = new XMLReader();
87 
88  Assert::assertTrue( $this->xml->open( $fname ),
89  "Opening temporary file $fname via XMLReader failed" );
90  if ( $skip_siteinfo ) {
91  Assert::assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
92  "Skipping past end of siteinfo" );
93  }
94  }
95 
103  public function assertDumpEnd( $name = "mediawiki" ) {
104  $this->assertNodeEnd( $name, false );
105  if ( $this->xml->read() ) {
106  $this->skipWhitespace();
107  }
108  Assert::assertEquals( $this->xml->nodeType, XMLReader::NONE,
109  "No proper entity left to parse" );
110  $this->xml->close();
111  }
112 
116  public function skipWhitespace() {
117  $cont = true;
118  while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
119  || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
120  $cont = $this->xml->read();
121  }
122  }
123 
132  public function assertNodeStart( $name, $skip = true ) {
133  Assert::assertEquals( $name, $this->xml->name, "Node name" );
134  Assert::assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
135  if ( $skip ) {
136  Assert::assertTrue( $this->xml->read(), "Skipping past start tag" );
137  }
138  }
139 
148  public function assertNodeEnd( $name, $skip = true ) {
149  Assert::assertEquals( $name, $this->xml->name, "Node name" );
150  Assert::assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
151  if ( $skip ) {
152  Assert::assertTrue( $this->xml->read(), "Skipping past end tag" );
153  }
154  }
155 
167  public function assertTextNode( $name, $text, $skip_ws = true ) {
168  $this->assertNodeStart( $name );
169 
170  if ( $text !== false ) {
171  Assert::assertEquals( $text, $this->xml->value, "Text of node " . $name );
172  }
173  Assert::assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
174  $this->assertNodeEnd( $name );
175 
176  if ( $skip_ws ) {
177  $this->skipWhitespace();
178  }
179  }
180 
193  public function assertPageStart( $id, $ns, $name ) {
194  $this->assertNodeStart( "page" );
195  $this->skipWhitespace();
196 
197  $this->assertTextNode( "title", $name );
198  $this->assertTextNode( "ns", $ns );
199  $this->assertTextNode( "id", $id );
200  }
201 
206  public function assertPageEnd() {
207  $this->assertNodeEnd( "page" );
208  $this->skipWhitespace();
209  }
210 
226  public function assertRevision( $id, $summary, $text_id, $text_bytes,
227  $text_sha1, $text = false, $parentid = false,
229  ) {
230  $this->assertNodeStart( "revision" );
231  $this->skipWhitespace();
232 
233  $this->assertTextNode( "id", $id );
234  if ( $parentid !== false ) {
235  $this->assertTextNode( "parentid", $parentid );
236  }
237  $this->assertTextNode( "timestamp", false );
238 
239  $this->assertNodeStart( "contributor" );
240  $this->skipWhitespace();
241  $this->assertTextNode( "username", false );
242  $this->assertTextNode( "id", false );
243  $this->assertNodeEnd( "contributor" );
244  $this->skipWhitespace();
245 
246  $this->assertTextNode( "comment", $summary );
247  $this->skipWhitespace();
248 
249  $this->assertTextNode( "model", $model );
250  $this->skipWhitespace();
251 
252  $this->assertTextNode( "format", $format );
253  $this->skipWhitespace();
254 
255  if ( $this->xml->name == "text" ) {
256  // note: <text> tag may occur here or at the very end.
257  $text_found = true;
258  $this->assertText( $id, $text_id, $text_bytes, $text );
259  } else {
260  $text_found = false;
261  }
262 
263  $this->assertTextNode( "sha1", $text_sha1 );
264 
265  if ( !$text_found ) {
266  $this->assertText( $id, $text_id, $text_bytes, $text );
267  }
268 
269  $this->assertNodeEnd( "revision" );
270  $this->skipWhitespace();
271  }
272 
273  public function assertText( $id, $text_id, $text_bytes, $text ) {
274  $this->assertNodeStart( "text", false );
275  if ( $text_bytes !== false ) {
276  Assert::assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
277  "Attribute 'bytes' of revision " . $id );
278  }
279 
280  if ( $text === false ) {
281  // Testing for a stub
282  Assert::assertEquals( $this->xml->getAttribute( "id" ), $text_id,
283  "Text id of revision " . $id );
284  Assert::assertFalse( $this->xml->hasValue, "Revision has text" );
285  Assert::assertTrue( $this->xml->read(), "Skipping text start tag" );
286  if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
287  && ( $this->xml->name == "text" )
288  ) {
289  $this->xml->read();
290  }
291  $this->skipWhitespace();
292  } else {
293  // Testing for a real dump
294  Assert::assertTrue( $this->xml->read(), "Skipping text start tag" );
295  Assert::assertEquals( $text, $this->xml->value, "Text of revision " . $id );
296  Assert::assertTrue( $this->xml->read(), "Skipping past text" );
297  $this->assertNodeEnd( "text" );
298  $this->skipWhitespace();
299  }
300  }
301 
315  public function assertLogItem( $id, $user_name, $user_id, $comment, $type,
316  $subtype, $title, $parameters = []
317  ) {
318  $this->assertNodeStart( "logitem" );
319  $this->skipWhitespace();
320 
321  $this->assertTextNode( "id", $id );
322  $this->assertTextNode( "timestamp", false );
323 
324  $this->assertNodeStart( "contributor" );
325  $this->skipWhitespace();
326  $this->assertTextNode( "username", $user_name );
327  $this->assertTextNode( "id", $user_id );
328  $this->assertNodeEnd( "contributor" );
329  $this->skipWhitespace();
330 
331  if ( $comment !== null ) {
332  $this->assertTextNode( "comment", $comment );
333  }
334  $this->assertTextNode( "type", $type );
335  $this->assertTextNode( "action", $subtype );
336  $this->assertTextNode( "logtitle", $title );
337 
338  $this->assertNodeStart( "params" );
339  $parameters_xml = unserialize( $this->xml->value );
340  Assert::assertEquals( $parameters, $parameters_xml );
341  Assert::assertTrue( $this->xml->read(), "Skipping past processed text of params" );
342  $this->assertNodeEnd( "params" );
343  $this->skipWhitespace();
344 
345  $this->assertNodeEnd( "logitem" );
346  $this->skipWhitespace();
347  }
348 }
MediaWiki\Tests\Maintenance\DumpAsserter\assertDumpStart
assertDumpStart( $fname, $skip_siteinfo=true)
Opens an XML file to analyze and optionally skips past siteinfo.
Definition: DumpAsserter.php:85
MediaWiki\Tests\Maintenance\DumpAsserter\assertDumpEnd
assertDumpEnd( $name="mediawiki")
Asserts that the xml reader is at the final closing tag of an xml file and closes the reader.
Definition: DumpAsserter.php:103
MediaWiki\Tests\Maintenance\DumpAsserter\assertText
assertText( $id, $text_id, $text_bytes, $text)
Definition: DumpAsserter.php:273
MediaWiki\Tests\Maintenance\DumpAsserter\$xml
XMLReader null $xml
Holds the XMLReader used for analyzing an XML dump.
Definition: DumpAsserter.php:18
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
MediaWiki\Tests\Maintenance\DumpAsserter\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: DumpAsserter.php:167
MediaWiki\Tests\Maintenance\DumpAsserter\skipWhitespace
skipWhitespace()
Steps the xml reader over white space.
Definition: DumpAsserter.php:116
php
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
MediaWiki\Tests\Maintenance\DumpAsserter\__construct
__construct( $schemaVersion)
DumpAsserts constructor.
Definition: DumpAsserter.php:32
MediaWiki\Tests\Maintenance\DumpAsserter
Helper for asserting the structure of an XML dump stream.
Definition: DumpAsserter.php:11
MediaWiki\Tests\Maintenance
Definition: backup_LogTest.php:3
MediaWiki\Tests\Maintenance\DumpAsserter\assertNodeStart
assertNodeStart( $name, $skip=true)
Asserts that the xml reader is at an element of given name, and optionally skips past it.
Definition: DumpAsserter.php:132
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
MediaWiki\Tests\Maintenance\DumpAsserter\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: DumpAsserter.php:193
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:250
MediaWiki\Tests\Maintenance\DumpAsserter\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: DumpAsserter.php:226
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:123
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
MediaWiki\Tests\Maintenance\DumpAsserter\assertNodeEnd
assertNodeEnd( $name, $skip=true)
Asserts that the xml reader is at an closing element of given name, and optionally skips past it.
Definition: DumpAsserter.php:148
MediaWiki\Tests\Maintenance\DumpAsserter\assertLogItem
assertLogItem( $id, $user_name, $user_id, $comment, $type, $subtype, $title, $parameters=[])
asserts that the xml reader is at the beginning of a log entry and skips over it while analyzing it.
Definition: DumpAsserter.php:315
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
MediaWiki\Tests\Maintenance\DumpAsserter\skipPastNodeEnd
skipPastNodeEnd( $name)
Step the current XML reader to the first element start after the node end of a given name.
Definition: DumpAsserter.php:66
MediaWiki\Tests\Maintenance\DumpAsserter\assertPageEnd
assertPageEnd()
Asserts that the xml reader is at the page's closing element and skips to the next element.
Definition: DumpAsserter.php:206
MediaWiki\Tests\Maintenance\DumpAsserter\$schemaVersion
string $schemaVersion
XML dump schema version.
Definition: DumpAsserter.php:25
MediaWiki\Tests\Maintenance\DumpAsserter\skipToNodeEnd
skipToNodeEnd( $name)
Step the current XML reader until node end of given name is found.
Definition: DumpAsserter.php:44
$type
$type
Definition: testCompression.php:48