3 namespace MediaWiki\Tests\Maintenance
;
5 use PHPUnit\Framework\Assert
;
9 * Helper for asserting the structure of an XML dump stream.
14 * Holds the XMLReader used for analyzing an XML dump
18 protected $xml = null;
21 * XML dump schema version
25 protected $schemaVersion;
28 * DumpAsserts constructor.
30 * @param string $schemaVersion see XML_DUMP_SCHEMA_VERSION_XX
32 public function __construct( $schemaVersion ) {
33 $this->schemaVersion
= $schemaVersion;
37 * Step the current XML reader until node end of given name is found.
39 * @param string $name Name of the closing element to look for
40 * (e.g.: "mediawiki" when looking for </mediawiki>)
42 * @return bool True if the end node could be found. false otherwise.
44 public function skipToNodeEnd( $name ) {
45 while ( $this->xml
->read() ) {
46 if ( $this->xml
->nodeType
== XMLReader
::END_ELEMENT
&&
47 $this->xml
->name
== $name
57 * Step the current XML reader to the first element start after the node
58 * end of a given name.
60 * @param string $name Name of the closing element to look for
61 * (e.g.: "mediawiki" when looking for </mediawiki>)
63 * @return bool True if new element after the closing of $name could be
64 * found. false otherwise.
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
) {
79 * Opens an XML file to analyze and optionally skips past siteinfo.
81 * @param string $fname Name of file to analyze
82 * @param bool $skip_siteinfo (optional) If true, step the xml reader
83 * to the first element after </siteinfo>
85 public function assertDumpStart( $fname, $skip_siteinfo = true ) {
86 $this->xml
= new XMLReader();
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" );
97 * Asserts that the xml reader is at the final closing tag of an xml file and
100 * @param string $name (optional) the name of the final tag
101 * (e.g.: "mediawiki" for </mediawiki>)
103 public function assertDumpEnd( $name = "mediawiki" ) {
104 $this->assertNodeEnd( $name, false );
105 if ( $this->xml
->read() ) {
106 $this->skipWhitespace();
108 Assert
::assertEquals( $this->xml
->nodeType
, XMLReader
::NONE
,
109 "No proper entity left to parse" );
114 * Steps the xml reader over white space
116 public function skipWhitespace() {
118 while ( $cont && ( ( $this->xml
->nodeType
== XMLReader
::WHITESPACE
)
119 ||
( $this->xml
->nodeType
== XMLReader
::SIGNIFICANT_WHITESPACE
) ) ) {
120 $cont = $this->xml
->read();
125 * Asserts that the xml reader is at an element of given name, and optionally
128 * @param string $name The name of the element to check for
129 * (e.g.: "mediawiki" for <mediawiki>)
130 * @param bool $skip (optional) if true, skip past the found element
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" );
136 Assert
::assertTrue( $this->xml
->read(), "Skipping past start tag" );
141 * Asserts that the xml reader is at an closing element of given name, and optionally
144 * @param string $name The name of the closing element to check for
145 * (e.g.: "mediawiki" for </mediawiki>)
146 * @param bool $skip (optional) if true, skip past the found element
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" );
152 Assert
::assertTrue( $this->xml
->read(), "Skipping past end tag" );
157 * Asserts that the xml reader is at an element of given tag that contains a given text,
158 * and skips over the element.
160 * @param string $name The name of the element to check for
161 * (e.g.: "mediawiki" for <mediawiki>...</mediawiki>)
162 * @param string|bool $text If string, check if it equals the elements text.
163 * If false, ignore the element's text
164 * @param bool $skip_ws (optional) if true, skip past white spaces that trail the
167 public function assertTextNode( $name, $text, $skip_ws = true ) {
168 $this->assertNodeStart( $name );
170 if ( $text !== false ) {
171 Assert
::assertEquals( $text, $this->xml
->value
, "Text of node " . $name );
173 Assert
::assertTrue( $this->xml
->read(), "Skipping past processed text of " . $name );
174 $this->assertNodeEnd( $name );
177 $this->skipWhitespace();
182 * Asserts that the xml reader is at the start of a page element and skips over the first
183 * tags, after checking them.
185 * Besides the opening page element, this function also checks for and skips over the
186 * title, ns, and id tags. Hence after this function, the xml reader is at the first
187 * revision of the current page.
189 * @param int $id Id of the page to assert
190 * @param int $ns Number of namespage to assert
191 * @param string $name Title of the current page
193 public function assertPageStart( $id, $ns, $name ) {
194 $this->assertNodeStart( "page" );
195 $this->skipWhitespace();
197 $this->assertTextNode( "title", $name );
198 $this->assertTextNode( "ns", $ns );
199 $this->assertTextNode( "id", $id );
203 * Asserts that the xml reader is at the page's closing element and skips to the next
206 public function assertPageEnd() {
207 $this->assertNodeEnd( "page" );
208 $this->skipWhitespace();
212 * Asserts that the xml reader is at a revision and checks its representation before
215 * @param int $id Id of the revision
216 * @param string $summary Summary of the revision
217 * @param int $text_id Id of the revision's text
218 * @param int $text_bytes Number of bytes in the revision's text
219 * @param string $text_sha1 The base36 SHA-1 of the revision's text
220 * @param string|bool $text (optional) The revision's string, or false to check for a
222 * @param int|bool $parentid (optional) id of the parent revision
223 * @param string $model The expected content model id (default: CONTENT_MODEL_WIKITEXT)
224 * @param string $format The expected format model id (default: CONTENT_FORMAT_WIKITEXT)
226 public function assertRevision( $id, $summary, $text_id, $text_bytes,
227 $text_sha1, $text = false, $parentid = false,
228 $model = CONTENT_MODEL_WIKITEXT
, $format = CONTENT_FORMAT_WIKITEXT
230 $this->assertNodeStart( "revision" );
231 $this->skipWhitespace();
233 $this->assertTextNode( "id", $id );
234 if ( $parentid !== false ) {
235 $this->assertTextNode( "parentid", $parentid );
237 $this->assertTextNode( "timestamp", false );
239 $this->assertNodeStart( "contributor" );
240 $this->skipWhitespace();
241 $this->assertTextNode( "username", false );
242 $this->assertTextNode( "id", false );
243 $this->assertNodeEnd( "contributor" );
244 $this->skipWhitespace();
246 $this->assertTextNode( "comment", $summary );
247 $this->skipWhitespace();
249 $this->assertTextNode( "model", $model );
250 $this->skipWhitespace();
252 $this->assertTextNode( "format", $format );
253 $this->skipWhitespace();
255 if ( $this->xml
->name
== "text" ) {
256 // note: <text> tag may occur here or at the very end.
258 $this->assertText( $id, $text_id, $text_bytes, $text );
263 $this->assertTextNode( "sha1", $text_sha1 );
265 if ( !$text_found ) {
266 $this->assertText( $id, $text_id, $text_bytes, $text );
269 $this->assertNodeEnd( "revision" );
270 $this->skipWhitespace();
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 );
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" )
291 $this->skipWhitespace();
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();
303 * asserts that the xml reader is at the beginning of a log entry and skips over
304 * it while analyzing it.
306 * @param int $id Id of the log entry
307 * @param string $user_name User name of the log entry's performer
308 * @param int $user_id User id of the log entry 's performer
309 * @param string|null $comment Comment of the log entry. If null, the comment text is ignored.
310 * @param string $type Type of the log entry
311 * @param string $subtype Subtype of the log entry
312 * @param string $title Title of the log entry's target
313 * @param array $parameters (optional) unserialized data accompanying the log entry
315 public function assertLogItem( $id, $user_name, $user_id, $comment, $type,
316 $subtype, $title, $parameters = []
318 $this->assertNodeStart( "logitem" );
319 $this->skipWhitespace();
321 $this->assertTextNode( "id", $id );
322 $this->assertTextNode( "timestamp", false );
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();
331 if ( $comment !== null ) {
332 $this->assertTextNode( "comment", $comment );
334 $this->assertTextNode( "type", $type );
335 $this->assertTextNode( "action", $subtype );
336 $this->assertTextNode( "logtitle", $title );
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();
345 $this->assertNodeEnd( "logitem" );
346 $this->skipWhitespace();