backup_LogTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Tests for log dumps of BackupDumper
  4. *
  5. * Some of these tests use the old constuctor for TextPassDumper
  6. * and the dump() function, while others use the new loadWithArgv( $args )
  7. * function and execute(). This is to ensure both the old and new methods
  8. * work properly.
  9. *
  10. * @group Database
  11. * @group Dump
  12. * @covers BackupDumper
  13. */
  14. class BackupDumperLoggerTest extends DumpTestCase {
  15. // We'll add several log entries and users for this test. The following
  16. // variables hold the corresponding ids.
  17. private $userId1, $userId2;
  18. private $logId1, $logId2, $logId3;
  19. /**
  20. * adds a log entry to the database.
  21. *
  22. * @param string $type Type of the log entry
  23. * @param string $subtype Subtype of the log entry
  24. * @param User $user User that performs the logged operation
  25. * @param int $ns Number of the namespace for the entry's target's title
  26. * @param string $title Title of the entry's target
  27. * @param string $comment Comment of the log entry
  28. * @param array $parameters (optional) accompanying data that is attached to the entry
  29. *
  30. * @return int Id of the added log entry
  31. */
  32. private function addLogEntry( $type, $subtype, User $user, $ns, $title,
  33. $comment = null, $parameters = null
  34. ) {
  35. $logEntry = new ManualLogEntry( $type, $subtype );
  36. $logEntry->setPerformer( $user );
  37. $logEntry->setTarget( Title::newFromText( $title, $ns ) );
  38. if ( $comment !== null ) {
  39. $logEntry->setComment( $comment );
  40. }
  41. if ( $parameters !== null ) {
  42. $logEntry->setParameters( $parameters );
  43. }
  44. return $logEntry->insert();
  45. }
  46. function addDBData() {
  47. $this->tablesUsed[] = 'logging';
  48. $this->tablesUsed[] = 'user';
  49. try {
  50. $user1 = User::newFromName( 'BackupDumperLogUserA' );
  51. $this->userId1 = $user1->getId();
  52. if ( $this->userId1 === 0 ) {
  53. $user1->addToDatabase();
  54. $this->userId1 = $user1->getId();
  55. }
  56. $this->assertGreaterThan( 0, $this->userId1 );
  57. $user2 = User::newFromName( 'BackupDumperLogUserB' );
  58. $this->userId2 = $user2->getId();
  59. if ( $this->userId2 === 0 ) {
  60. $user2->addToDatabase();
  61. $this->userId2 = $user2->getId();
  62. }
  63. $this->assertGreaterThan( 0, $this->userId2 );
  64. $this->logId1 = $this->addLogEntry( 'type', 'subtype',
  65. $user1, NS_MAIN, "PageA" );
  66. $this->assertGreaterThan( 0, $this->logId1 );
  67. $this->logId2 = $this->addLogEntry( 'supress', 'delete',
  68. $user2, NS_TALK, "PageB", "SomeComment" );
  69. $this->assertGreaterThan( 0, $this->logId2 );
  70. $this->logId3 = $this->addLogEntry( 'move', 'delete',
  71. $user2, NS_MAIN, "PageA", "SomeOtherComment",
  72. [ 'key1' => 1, 3 => 'value3' ] );
  73. $this->assertGreaterThan( 0, $this->logId3 );
  74. } catch ( Exception $e ) {
  75. // We'd love to pass $e directly. However, ... see
  76. // documentation of exceptionFromAddDBData in
  77. // DumpTestCase
  78. $this->exceptionFromAddDBData = $e;
  79. }
  80. }
  81. /**
  82. * asserts that the xml reader is at the beginning of a log entry and skips over
  83. * it while analyzing it.
  84. *
  85. * @param int $id Id of the log entry
  86. * @param string $user_name User name of the log entry's performer
  87. * @param int $user_id User id of the log entry 's performer
  88. * @param string|null $comment Comment of the log entry. If null, the comment text is ignored.
  89. * @param string $type Type of the log entry
  90. * @param string $subtype Subtype of the log entry
  91. * @param string $title Title of the log entry's target
  92. * @param array $parameters (optional) unserialized data accompanying the log entry
  93. */
  94. private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
  95. $subtype, $title, $parameters = []
  96. ) {
  97. $this->assertNodeStart( "logitem" );
  98. $this->skipWhitespace();
  99. $this->assertTextNode( "id", $id );
  100. $this->assertTextNode( "timestamp", false );
  101. $this->assertNodeStart( "contributor" );
  102. $this->skipWhitespace();
  103. $this->assertTextNode( "username", $user_name );
  104. $this->assertTextNode( "id", $user_id );
  105. $this->assertNodeEnd( "contributor" );
  106. $this->skipWhitespace();
  107. if ( $comment !== null ) {
  108. $this->assertTextNode( "comment", $comment );
  109. }
  110. $this->assertTextNode( "type", $type );
  111. $this->assertTextNode( "action", $subtype );
  112. $this->assertTextNode( "logtitle", $title );
  113. $this->assertNodeStart( "params" );
  114. $parameters_xml = unserialize( $this->xml->value );
  115. $this->assertEquals( $parameters, $parameters_xml );
  116. $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
  117. $this->assertNodeEnd( "params" );
  118. $this->skipWhitespace();
  119. $this->assertNodeEnd( "logitem" );
  120. $this->skipWhitespace();
  121. }
  122. function testPlain() {
  123. global $wgContLang;
  124. // Preparing the dump
  125. $fname = $this->getNewTempFile();
  126. $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
  127. $dumper->startId = $this->logId1;
  128. $dumper->endId = $this->logId3 + 1;
  129. $dumper->reporting = false;
  130. $dumper->setDB( $this->db );
  131. // Performing the dump
  132. $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
  133. // Analyzing the dumped data
  134. $this->assertDumpStart( $fname );
  135. $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
  136. $this->userId1, null, "type", "subtype", "PageA" );
  137. $this->assertNotNull( $wgContLang, "Content language object validation" );
  138. $namespace = $wgContLang->getNsText( NS_TALK );
  139. $this->assertInternalType( 'string', $namespace );
  140. $this->assertGreaterThan( 0, strlen( $namespace ) );
  141. $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
  142. $this->userId2, "SomeComment", "supress", "delete",
  143. $namespace . ":PageB" );
  144. $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
  145. $this->userId2, "SomeOtherComment", "move", "delete",
  146. "PageA", [ 'key1' => 1, 3 => 'value3' ] );
  147. $this->assertDumpEnd();
  148. }
  149. function testXmlDumpsBackupUseCaseLogging() {
  150. global $wgContLang;
  151. $this->checkHasGzip();
  152. // Preparing the dump
  153. $fname = $this->getNewTempFile();
  154. $dumper = new DumpBackup();
  155. $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
  156. '--reporting=2' ] );
  157. $dumper->startId = $this->logId1;
  158. $dumper->endId = $this->logId3 + 1;
  159. $dumper->setDB( $this->db );
  160. // xmldumps-backup demands reporting, although this is currently not
  161. // implemented in BackupDumper, when dumping logging data. We
  162. // nevertheless capture the output of the dump process already now,
  163. // to be able to alert (once dumping produces reports) that this test
  164. // needs updates.
  165. $dumper->stderr = fopen( 'php://output', 'a' );
  166. if ( $dumper->stderr === false ) {
  167. $this->fail( "Could not open stream for stderr" );
  168. }
  169. // Performing the dump
  170. $dumper->execute();
  171. $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
  172. // Analyzing the dumped data
  173. $this->gunzip( $fname );
  174. $this->assertDumpStart( $fname );
  175. $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
  176. $this->userId1, null, "type", "subtype", "PageA" );
  177. $this->assertNotNull( $wgContLang, "Content language object validation" );
  178. $namespace = $wgContLang->getNsText( NS_TALK );
  179. $this->assertInternalType( 'string', $namespace );
  180. $this->assertGreaterThan( 0, strlen( $namespace ) );
  181. $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
  182. $this->userId2, "SomeComment", "supress", "delete",
  183. $namespace . ":PageB" );
  184. $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
  185. $this->userId2, "SomeOtherComment", "move", "delete",
  186. "PageA", [ 'key1' => 1, 3 => 'value3' ] );
  187. $this->assertDumpEnd();
  188. // Currently, no reporting is implemented. Alert via failure, once
  189. // this changes.
  190. // If reporting for log dumps has been implemented, please update
  191. // the following statement to catch good output
  192. $this->expectOutputString( '' );
  193. }
  194. }