Debian Codegraph @

tests/phpunit/maintenance/BackupDumperPageTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
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;
13
14 /**
15 * Tests for page dumps of BackupDumper
16 *
17 * @group Database
18 * @group Dump
19 * @covers \MediaWiki\Maintenance\BackupDumper
20 *
21 */
22 class BackupDumperPageTest extends DumpTestCase {
23
24 use PageDumpTestDataTrait;
25
26 /** @var CloneDatabase */
27 private $dbClone;
28
29 /**
30 * @var IMaintainableDatabase
31 */
32 private $sinkDb;
33
34 public function addDBData() {
35 parent::addDBData();
36
37 $this->addTestPages( $this->getTestSysop()->getUser() );
38 }
39
40 protected function tearDown(): void {
41 if ( $this->dbClone ) {
42 $this->dbClone->destroy();
43 }
44 if ( $this->sinkDb ) {
45 $this->sinkDb->close( __METHOD__ );
46 }
47
48 parent::tearDown();
49 }
50
51 /**
52 * Returns a new database connection which is separate from the connections returned
53 * by the default LoadBalancer instance.
54 *
55 * @return IDatabase
56 */
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 );
62 }
63
64 // Make sure the DB connection has the fake table clones and the fake table prefix
65 $this->dbClone = MediaWikiIntegrationTestCase::setupDatabaseWithTestPrefix( $this->sinkDb );
66
67 // Make sure the DB connection has all the test data
68 $this->copyTestData( $this->db, $this->sinkDb );
69
70 return $this->sinkDb;
71 }
72
73 /**
74 * @param array $argv
75 * @param int $startId
76 * @param int $endId
77 *
78 * @return DumpBackup
79 */
80 private function newDumpBackup( $argv, $startId, $endId ) {
81 $dumper = new DumpBackup( $argv );
82 $dumper->startId = $startId;
83 $dumper->endId = $endId;
84 $dumper->reporting = false;
85
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() );
93 }
94
95 return $dumper;
96 }
97
98 public function schemaVersionProvider() {
99 foreach ( XmlDumpWriter::$supportedSchemas as $schemaVersion ) {
100 yield [ $schemaVersion ];
101 }
102 }
103
104 /**
105 * @dataProvider schemaVersionProvider
106 */
107 public function testFullTextPlain( $schemaVersion ) {
108 // Preparing the dump
109 $tmpFile = $this->getNewTempFile();
110
111 $dumper = $this->newDumpBackup(
112 [ '--full', '--quiet', '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ],
113 $this->pageId1,
114 $this->pageId5 + 1
115 );
116
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 );
122
123 // Checking syntax and schema
124 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );
125
126 // Checking file contents
127 $asserter = $this->getDumpAsserter( $schemaVersion );
128 $this->setSiteVarMappings( $asserter );
129 $this->setAllRevisionsVarMappings( $asserter );
130
131 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
132 $pagesTemplate = $this->getDumpTemplatePath( 'AllText', $schemaVersion );
133
134 $asserter->open( $tmpFile );
135 $asserter->assertDumpHead( $siteInfoTemplate );
136
137 // Check pages and revisions
138 $asserter->assertDOM( $pagesTemplate );
139 $asserter->assertDumpEnd();
140 }
141
142 /**
143 * @dataProvider schemaVersionProvider
144 */
145 public function testFullStubPlain( $schemaVersion ) {
146 // Preparing the dump
147 $tmpFile = $this->getNewTempFile();
148
149 $dumper = $this->newDumpBackup(
150 [
151 '--full',
152 '--quiet',
153 '--output',
154 'file:' . $tmpFile,
155 '--stub',
156 '--schema-version', $schemaVersion,
157 ],
158 $this->pageId1,
159 $this->pageId5 + 1
160 );
161
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 );
167
168 // Checking the dumped data
169 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );
170
171 $asserter = $this->getDumpAsserter( $schemaVersion );
172 $this->setSiteVarMappings( $asserter );
173 $this->setAllRevisionsVarMappings( $asserter );
174
175 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
176 $pagesTemplate = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion );
177
178 $asserter->open( $tmpFile );
179 $asserter->assertDumpHead( $siteInfoTemplate );
180
181 // Check pages and revisions
182 $asserter->assertDOM( $pagesTemplate );
183 $asserter->assertDumpEnd();
184 }
185
186 /**
187 * @dataProvider schemaVersionProvider
188 */
189 public function testCurrentStubPlain( $schemaVersion ) {
190 // Preparing the dump
191 $tmpFile = $this->getNewTempFile();
192
193 $dumper = $this->newDumpBackup(
194 [ '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ],
195 $this->pageId1,
196 $this->pageId5 + 1
197 );
198
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 );
204
205 // Checking the dumped data
206 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );
207
208 $asserter = $this->getDumpAsserter( $schemaVersion );
209 $this->setSiteVarMappings( $asserter );
210 $this->setAllRevisionsVarMappings( $asserter );
211
212 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
213 $pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion );
214
215 $asserter->open( $tmpFile );
216 $asserter->assertDumpHead( $siteInfoTemplate );
217
218 // Check pages and revisions
219 $asserter->assertDOM( $pagesTemplate );
220 $asserter->assertDumpEnd();
221 }
222
223 public function testCurrentStubGzip() {
224 global $wgXmlDumpSchemaVersion;
225
226 $this->checkHasGzip();
227
228 // Preparing the dump
229 $tmpFile = $this->getNewTempFile();
230
231 $dumper = $this->newDumpBackup(
232 [ '--output', 'gzip:' . $tmpFile ],
233 $this->pageId1,
234 $this->pageId5 + 1
235 );
236
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 );
242
243 // Checking the dumped data
244 $this->gunzip( $tmpFile );
245
246 $this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $wgXmlDumpSchemaVersion ) );
247
248 $asserter = $this->getDumpAsserter( $wgXmlDumpSchemaVersion );
249 $this->setSiteVarMappings( $asserter );
250 $this->setAllRevisionsVarMappings( $asserter );
251
252 $siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $wgXmlDumpSchemaVersion );
253 $pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $wgXmlDumpSchemaVersion );
254
255 $asserter->open( $tmpFile );
256 $asserter->assertDumpHead( $siteInfoTemplate );
257
258 // Check pages and revisions
259 $asserter->assertDOM( $pagesTemplate );
260 $asserter->assertDumpEnd();
261 }
262
263 /**
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)
270 *
271 * We reproduce such a setup with our mini fixture, although we omit
272 * chunks, and all the other gimmicks of xmldumps-backup.
273 *
274 * @dataProvider schemaVersionProvider
275 */
276 public function testXmlDumpsBackupUseCase( $schemaVersion ) {
277 $this->checkHasGzip();
278
279 $fnameMetaHistory = $this->getNewTempFile();
280 $fnameMetaCurrent = $this->getNewTempFile();
281 $fnameArticles = $this->getNewTempFile();
282
283 $expSiteInfo = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
284 $expMetaHistory = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion );
285 $expMetaCurrent = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion );
286 $expArticles = $this->getDumpTemplatePath( 'CurrentArticleStubs', $schemaVersion );
287
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
294 ],
295 $this->pageId1,
296 $this->pageId5 + 1
297 );
298 $dumper->reporting = true;
299
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" );
308 }
309
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 );
315
316 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
317 $this->assertNotEmpty( file_get_contents( $fnameReport ) );
318
319 // Checking meta-history -------------------------------------------------
320
321 $this->gunzip( $fnameMetaHistory );
322
323 $asserter = $this->getDumpAsserter( $schemaVersion );
324 $this->setSiteVarMappings( $asserter );
325 $this->setAllRevisionsVarMappings( $asserter );
326
327 $asserter->open( $fnameMetaHistory );
328 $asserter->assertDumpHead( $expSiteInfo );
329 $asserter->assertDOM( $expMetaHistory );
330 $asserter->assertDumpEnd();
331
332 // Checking meta-current -------------------------------------------------
333
334 $this->gunzip( $fnameMetaCurrent );
335 $this->assertDumpSchema( $fnameMetaCurrent, $this->getXmlSchemaPath( $schemaVersion ) );
336
337 $asserter = $this->getDumpAsserter( $schemaVersion );
338 $this->setSiteVarMappings( $asserter );
339 $this->setCurrentRevisionsVarMappings( $asserter );
340
341 $asserter->open( $fnameMetaCurrent );
342 $asserter->assertDumpHead( $expSiteInfo );
343 $asserter->assertDOM( $expMetaCurrent );
344 $asserter->assertDumpEnd();
345
346 // Checking articles -------------------------------------------------
347
348 $this->gunzip( $fnameArticles );
349 $this->assertDumpSchema( $fnameArticles, $this->getXmlSchemaPath( $schemaVersion ) );
350
351 $asserter = $this->getDumpAsserter( $schemaVersion );
352 $this->setSiteVarMappings( $asserter );
353 $this->setCurrentRevisionsVarMappings( $asserter );
354
355 $asserter->open( $fnameArticles );
356 $asserter->assertDumpHead( $expSiteInfo );
357 $asserter->assertDOM( $expArticles );
358 $asserter->assertDumpEnd();
359 }
360 }