DumpTestCase.php 12 KB

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