DumpTestCase.php 13 KB

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