Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / BackupDumperPageTest.php
bloba9090eb6b5bca58ecff9d3bcfd8afd6cd7268075
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use CloneDatabase;
6 use DumpBackup;
7 use MediaWiki\MainConfigNames;
8 use MediaWikiIntegrationTestCase;
9 use WikiExporter;
10 use Wikimedia\Rdbms\IDatabase;
11 use Wikimedia\Rdbms\IMaintainableDatabase;
12 use XmlDumpWriter;
14 /**
15 * Tests for page dumps of BackupDumper
17 * @group Database
18 * @group Dump
19 * @covers \MediaWiki\Maintenance\BackupDumper
22 class BackupDumperPageTest extends DumpTestCase {
24 use PageDumpTestDataTrait;
26 /** @var CloneDatabase */
27 private $dbClone;
29 /**
30 * @var IMaintainableDatabase
32 private $sinkDb;
34 public function addDBData() {
35 parent::addDBData();
37 $this->addTestPages( $this->getTestSysop()->getUser() );
40 protected function tearDown(): void {
41 if ( $this->dbClone ) {
42 $this->dbClone->destroy();
44 if ( $this->sinkDb ) {
45 $this->sinkDb->close( __METHOD__ );
48 parent::tearDown();
51 /**
52 * Returns a new database connection which is separate from the connections returned
53 * by the default LoadBalancer instance.
55 * @return IDatabase
57 private function newSinkDbConnection() {
58 if ( !$this->sinkDb ) {
59 // Make an untracked DB_PRIMARY connection
60 $sinkLb = $this->getServiceContainer()->getDBLoadBalancerFactory()->newMainLB();
61 $this->sinkDb = $sinkLb->getConnectionInternal( DB_PRIMARY );
64 // Make sure the DB connection has the fake table clones and the fake table prefix
65 $this->dbClone = MediaWikiIntegrationTestCase::setupDatabaseWithTestPrefix( $this->sinkDb );
67 // Make sure the DB connection has all the test data
68 $this->copyTestData( $this->db, $this->sinkDb );
70 return $this->sinkDb;
73 /**
74 * @param array $argv
75 * @param int $startId
76 * @param int $endId
78 * @return DumpBackup
80 private function newDumpBackup( $argv, $startId, $endId ) {
81 $dumper = new DumpBackup( $argv );
82 $dumper->startId = $startId;
83 $dumper->endId = $endId;
84 $dumper->reporting = false;
86 // NOTE: The copyTestData() method used by newStreamingDBConnection()
87 // doesn't work with SQLite (T217607).
88 // But DatabaseSqlite doesn't support streaming anyway, so just skip that part.
89 if ( $this->db->getType() === 'sqlite' ) {
90 $dumper->setDB( $this->db );
91 } else {
92 $dumper->setDB( $this->newSinkDbConnection() );
95 return $dumper;
98 public function schemaVersionProvider() {
99 foreach ( XmlDumpWriter::$supportedSchemas as $schemaVersion ) {
100 yield [ $schemaVersion ];
105 * @dataProvider schemaVersionProvider
107 public function testFullTextPlain( $schemaVersion ) {
108 // Preparing the dump
109 $tmpFile = $this->getNewTempFile();
111 $dumper = $this->newDumpBackup(
112 [ '--full', '--quiet', '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ],
113 $this->pageId1,
114 $this->pageId5 + 1
117 // Performing the dump. Suppress warnings, since we want to test
118 // accessing broken revision data (page 5).
119 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
120 $dumper->execute();
121 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );
123 // Checking syntax and schema
124 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );
126 // Checking file contents
127 $asserter = $this->getDumpAsserter( $schemaVersion );
128 $this->setSiteVarMappings( $asserter );
129 $this->setAllRevisionsVarMappings( $asserter );
131 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
132 $pagesTemplate = $this->getDumpTemplatePath( 'AllText', $schemaVersion );
134 $asserter->open( $tmpFile );
135 $asserter->assertDumpHead( $siteInfoTemplate );
137 // Check pages and revisions
138 $asserter->assertDOM( $pagesTemplate );
139 $asserter->assertDumpEnd();
143 * @dataProvider schemaVersionProvider
145 public function testFullStubPlain( $schemaVersion ) {
146 // Preparing the dump
147 $tmpFile = $this->getNewTempFile();
149 $dumper = $this->newDumpBackup(
151 '--full',
152 '--quiet',
153 '--output',
154 'file:' . $tmpFile,
155 '--stub',
156 '--schema-version', $schemaVersion,
158 $this->pageId1,
159 $this->pageId5 + 1
162 // Performing the dump. Suppress warnings, since we want to test
163 // accessing broken revision data (page 5).
164 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
165 $dumper->execute();
166 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );
168 // Checking the dumped data
169 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );
171 $asserter = $this->getDumpAsserter( $schemaVersion );
172 $this->setSiteVarMappings( $asserter );
173 $this->setAllRevisionsVarMappings( $asserter );
175 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
176 $pagesTemplate = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion );
178 $asserter->open( $tmpFile );
179 $asserter->assertDumpHead( $siteInfoTemplate );
181 // Check pages and revisions
182 $asserter->assertDOM( $pagesTemplate );
183 $asserter->assertDumpEnd();
187 * @dataProvider schemaVersionProvider
189 public function testCurrentStubPlain( $schemaVersion ) {
190 // Preparing the dump
191 $tmpFile = $this->getNewTempFile();
193 $dumper = $this->newDumpBackup(
194 [ '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ],
195 $this->pageId1,
196 $this->pageId5 + 1
199 // Performing the dump. Suppress warnings, since we want to test
200 // accessing broken revision data (page 5).
201 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
202 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
203 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );
205 // Checking the dumped data
206 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );
208 $asserter = $this->getDumpAsserter( $schemaVersion );
209 $this->setSiteVarMappings( $asserter );
210 $this->setAllRevisionsVarMappings( $asserter );
212 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
213 $pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion );
215 $asserter->open( $tmpFile );
216 $asserter->assertDumpHead( $siteInfoTemplate );
218 // Check pages and revisions
219 $asserter->assertDOM( $pagesTemplate );
220 $asserter->assertDumpEnd();
223 public function testCurrentStubGzip() {
224 global $wgXmlDumpSchemaVersion;
226 $this->checkHasGzip();
228 // Preparing the dump
229 $tmpFile = $this->getNewTempFile();
231 $dumper = $this->newDumpBackup(
232 [ '--output', 'gzip:' . $tmpFile ],
233 $this->pageId1,
234 $this->pageId5 + 1
237 // Performing the dump. Suppress warnings, since we want to test
238 // accessing broken revision data (page 5).
239 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
240 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
241 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );
243 // Checking the dumped data
244 $this->gunzip( $tmpFile );
246 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $wgXmlDumpSchemaVersion ) );
248 $asserter = $this->getDumpAsserter( $wgXmlDumpSchemaVersion );
249 $this->setSiteVarMappings( $asserter );
250 $this->setAllRevisionsVarMappings( $asserter );
252 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $wgXmlDumpSchemaVersion );
253 $pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $wgXmlDumpSchemaVersion );
255 $asserter->open( $tmpFile );
256 $asserter->assertDumpHead( $siteInfoTemplate );
258 // Check pages and revisions
259 $asserter->assertDOM( $pagesTemplate );
260 $asserter->assertDumpEnd();
264 * xmldumps-backup typically performs a single dump that that writes
265 * out three files
266 * - gzipped stubs of everything (meta-history)
267 * - gzipped stubs of latest revisions of all pages (meta-current)
268 * - gzipped stubs of latest revisions of all pages of namespage 0
269 * (articles)
271 * We reproduce such a setup with our mini fixture, although we omit
272 * chunks, and all the other gimmicks of xmldumps-backup.
274 * @dataProvider schemaVersionProvider
276 public function testXmlDumpsBackupUseCase( $schemaVersion ) {
277 $this->checkHasGzip();
279 $fnameMetaHistory = $this->getNewTempFile();
280 $fnameMetaCurrent = $this->getNewTempFile();
281 $fnameArticles = $this->getNewTempFile();
283 $expSiteInfo = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
284 $expMetaHistory = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion );
285 $expMetaCurrent = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion );
286 $expArticles = $this->getDumpTemplatePath( 'CurrentArticleStubs', $schemaVersion );
288 $dumper = $this->newDumpBackup(
289 [ "--quiet", "--full", "--stub", "--output=gzip:" . $fnameMetaHistory,
290 "--output=gzip:" . $fnameMetaCurrent, "--filter=latest",
291 "--output=gzip:" . $fnameArticles, "--filter=latest",
292 "--filter=notalk", "--filter=namespace:!NS_USER",
293 "--reporting=1000", '--schema-version', $schemaVersion
295 $this->pageId1,
296 $this->pageId5 + 1
298 $dumper->reporting = true;
300 // xmldumps-backup uses reporting. We will not check the exact reported
301 // message, as they are dependent on the processing power of the used
302 // computer. We only check that reporting does not crash the dumping
303 // and that something is reported
304 $fnameReport = $this->getNewTempFile();
305 $dumper->stderr = fopen( $fnameReport, 'a' );
306 if ( $dumper->stderr === false ) {
307 $this->fail( "Could not open stream for stderr" );
310 // Performing the dump. Suppress warnings, since we want to test
311 // accessing broken revision data (page 5).
312 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
313 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
314 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );
316 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
317 $this->assertNotEmpty( file_get_contents( $fnameReport ) );
319 // Checking meta-history -------------------------------------------------
321 $this->gunzip( $fnameMetaHistory );
323 $asserter = $this->getDumpAsserter( $schemaVersion );
324 $this->setSiteVarMappings( $asserter );
325 $this->setAllRevisionsVarMappings( $asserter );
327 $asserter->open( $fnameMetaHistory );
328 $asserter->assertDumpHead( $expSiteInfo );
329 $asserter->assertDOM( $expMetaHistory );
330 $asserter->assertDumpEnd();
332 // Checking meta-current -------------------------------------------------
334 $this->gunzip( $fnameMetaCurrent );
335 $this->assertDumpSchema( $fnameMetaCurrent, $this->getXmlSchemaPath( $schemaVersion ) );
337 $asserter = $this->getDumpAsserter( $schemaVersion );
338 $this->setSiteVarMappings( $asserter );
339 $this->setCurrentRevisionsVarMappings( $asserter );
341 $asserter->open( $fnameMetaCurrent );
342 $asserter->assertDumpHead( $expSiteInfo );
343 $asserter->assertDOM( $expMetaCurrent );
344 $asserter->assertDumpEnd();
346 // Checking articles -------------------------------------------------
348 $this->gunzip( $fnameArticles );
349 $this->assertDumpSchema( $fnameArticles, $this->getXmlSchemaPath( $schemaVersion ) );
351 $asserter = $this->getDumpAsserter( $schemaVersion );
352 $this->setSiteVarMappings( $asserter );
353 $this->setCurrentRevisionsVarMappings( $asserter );
355 $asserter->open( $fnameArticles );
356 $asserter->assertDumpHead( $expSiteInfo );
357 $asserter->assertDOM( $expArticles );
358 $asserter->assertDumpEnd();