File: BackupDumperPageTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (360 lines) | stat: -rw-r--r-- 11,364 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php

namespace MediaWiki\Tests\Maintenance;

use CloneDatabase;
use DumpBackup;
use MediaWiki\MainConfigNames;
use MediaWikiIntegrationTestCase;
use WikiExporter;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\IMaintainableDatabase;
use XmlDumpWriter;

/**
 * Tests for page dumps of BackupDumper
 *
 * @group Database
 * @group Dump
 * @covers \MediaWiki\Maintenance\BackupDumper
 *
 */
class BackupDumperPageTest extends DumpTestCase {

	use PageDumpTestDataTrait;

	/** @var CloneDatabase */
	private $dbClone;

	/**
	 * @var IMaintainableDatabase
	 */
	private $sinkDb;

	public function addDBData() {
		parent::addDBData();

		$this->addTestPages( $this->getTestSysop()->getUser() );
	}

	protected function tearDown(): void {
		if ( $this->dbClone ) {
			$this->dbClone->destroy();
		}
		if ( $this->sinkDb ) {
			$this->sinkDb->close( __METHOD__ );
		}

		parent::tearDown();
	}

	/**
	 * Returns a new database connection which is separate from the connections returned
	 * by the default LoadBalancer instance.
	 *
	 * @return IDatabase
	 */
	private function newSinkDbConnection() {
		if ( !$this->sinkDb ) {
			// Make an untracked DB_PRIMARY connection
			$sinkLb = $this->getServiceContainer()->getDBLoadBalancerFactory()->newMainLB();
			$this->sinkDb = $sinkLb->getConnectionInternal( DB_PRIMARY );
		}

		// Make sure the DB connection has the fake table clones and the fake table prefix
		$this->dbClone = MediaWikiIntegrationTestCase::setupDatabaseWithTestPrefix( $this->sinkDb );

		// Make sure the DB connection has all the test data
		$this->copyTestData( $this->db, $this->sinkDb );

		return $this->sinkDb;
	}

	/**
	 * @param array $argv
	 * @param int $startId
	 * @param int $endId
	 *
	 * @return DumpBackup
	 */
	private function newDumpBackup( $argv, $startId, $endId ) {
		$dumper = new DumpBackup( $argv );
		$dumper->startId = $startId;
		$dumper->endId = $endId;
		$dumper->reporting = false;

		// NOTE: The copyTestData() method used by newStreamingDBConnection()
		// doesn't work with SQLite (T217607).
		// But DatabaseSqlite doesn't support streaming anyway, so just skip that part.
		if ( $this->db->getType() === 'sqlite' ) {
			$dumper->setDB( $this->db );
		} else {
			$dumper->setDB( $this->newSinkDbConnection() );
		}

		return $dumper;
	}

	public function schemaVersionProvider() {
		foreach ( XmlDumpWriter::$supportedSchemas as $schemaVersion ) {
			yield [ $schemaVersion ];
		}
	}

	/**
	 * @dataProvider schemaVersionProvider
	 */
	public function testFullTextPlain( $schemaVersion ) {
		// Preparing the dump
		$tmpFile = $this->getNewTempFile();

		$dumper = $this->newDumpBackup(
			[ '--full', '--quiet', '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ],
			$this->pageId1,
			$this->pageId5 + 1
		);

		// Performing the dump. Suppress warnings, since we want to test
		// accessing broken revision data (page 5).
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
		$dumper->execute();
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );

		// Checking syntax and schema
		$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );

		// Checking file contents
		$asserter = $this->getDumpAsserter( $schemaVersion );
		$this->setSiteVarMappings( $asserter );
		$this->setAllRevisionsVarMappings( $asserter );

		$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
		$pagesTemplate = $this->getDumpTemplatePath( 'AllText', $schemaVersion );

		$asserter->open( $tmpFile );
		$asserter->assertDumpHead( $siteInfoTemplate );

		// Check pages and revisions
		$asserter->assertDOM( $pagesTemplate );
		$asserter->assertDumpEnd();
	}

	/**
	 * @dataProvider schemaVersionProvider
	 */
	public function testFullStubPlain( $schemaVersion ) {
		// Preparing the dump
		$tmpFile = $this->getNewTempFile();

		$dumper = $this->newDumpBackup(
			[
				'--full',
				'--quiet',
				'--output',
				'file:' . $tmpFile,
				'--stub',
				'--schema-version', $schemaVersion,
			],
			$this->pageId1,
			$this->pageId5 + 1
		);

		// Performing the dump. Suppress warnings, since we want to test
		// accessing broken revision data (page 5).
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
		$dumper->execute();
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );

		// Checking the dumped data
		$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );

		$asserter = $this->getDumpAsserter( $schemaVersion );
		$this->setSiteVarMappings( $asserter );
		$this->setAllRevisionsVarMappings( $asserter );

		$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
		$pagesTemplate = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion );

		$asserter->open( $tmpFile );
		$asserter->assertDumpHead( $siteInfoTemplate );

		// Check pages and revisions
		$asserter->assertDOM( $pagesTemplate );
		$asserter->assertDumpEnd();
	}

	/**
	 * @dataProvider schemaVersionProvider
	 */
	public function testCurrentStubPlain( $schemaVersion ) {
		// Preparing the dump
		$tmpFile = $this->getNewTempFile();

		$dumper = $this->newDumpBackup(
			[ '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ],
			$this->pageId1,
			$this->pageId5 + 1
		);

		// Performing the dump. Suppress warnings, since we want to test
		// accessing broken revision data (page 5).
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
		$dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );

		// Checking the dumped data
		$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) );

		$asserter = $this->getDumpAsserter( $schemaVersion );
		$this->setSiteVarMappings( $asserter );
		$this->setAllRevisionsVarMappings( $asserter );

		$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
		$pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion );

		$asserter->open( $tmpFile );
		$asserter->assertDumpHead( $siteInfoTemplate );

		// Check pages and revisions
		$asserter->assertDOM( $pagesTemplate );
		$asserter->assertDumpEnd();
	}

	public function testCurrentStubGzip() {
		global $wgXmlDumpSchemaVersion;

		$this->checkHasGzip();

		// Preparing the dump
		$tmpFile = $this->getNewTempFile();

		$dumper = $this->newDumpBackup(
			[ '--output', 'gzip:' . $tmpFile ],
			$this->pageId1,
			$this->pageId5 + 1
		);

		// Performing the dump. Suppress warnings, since we want to test
		// accessing broken revision data (page 5).
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
		$dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );

		// Checking the dumped data
		$this->gunzip( $tmpFile );

		$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $wgXmlDumpSchemaVersion ) );

		$asserter = $this->getDumpAsserter( $wgXmlDumpSchemaVersion );
		$this->setSiteVarMappings( $asserter );
		$this->setAllRevisionsVarMappings( $asserter );

		$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $wgXmlDumpSchemaVersion );
		$pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $wgXmlDumpSchemaVersion );

		$asserter->open( $tmpFile );
		$asserter->assertDumpHead( $siteInfoTemplate );

		// Check pages and revisions
		$asserter->assertDOM( $pagesTemplate );
		$asserter->assertDumpEnd();
	}

	/**
	 * xmldumps-backup typically performs a single dump that that writes
	 * out three files
	 * - gzipped stubs of everything (meta-history)
	 * - gzipped stubs of latest revisions of all pages (meta-current)
	 * - gzipped stubs of latest revisions of all pages of namespage 0
	 *   (articles)
	 *
	 * We reproduce such a setup with our mini fixture, although we omit
	 * chunks, and all the other gimmicks of xmldumps-backup.
	 *
	 * @dataProvider schemaVersionProvider
	 */
	public function testXmlDumpsBackupUseCase( $schemaVersion ) {
		$this->checkHasGzip();

		$fnameMetaHistory = $this->getNewTempFile();
		$fnameMetaCurrent = $this->getNewTempFile();
		$fnameArticles = $this->getNewTempFile();

		$expSiteInfo = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion );
		$expMetaHistory = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion );
		$expMetaCurrent = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion );
		$expArticles = $this->getDumpTemplatePath( 'CurrentArticleStubs', $schemaVersion );

		$dumper = $this->newDumpBackup(
			[ "--quiet", "--full", "--stub", "--output=gzip:" . $fnameMetaHistory,
				"--output=gzip:" . $fnameMetaCurrent, "--filter=latest",
				"--output=gzip:" . $fnameArticles, "--filter=latest",
				"--filter=notalk", "--filter=namespace:!NS_USER",
				"--reporting=1000", '--schema-version', $schemaVersion
			],
			$this->pageId1,
			$this->pageId5 + 1
		);
		$dumper->reporting = true;

		// xmldumps-backup uses reporting. We will not check the exact reported
		// message, as they are dependent on the processing power of the used
		// computer. We only check that reporting does not crash the dumping
		// and that something is reported
		$fnameReport = $this->getNewTempFile();
		$dumper->stderr = fopen( $fnameReport, 'a' );
		if ( $dumper->stderr === false ) {
			$this->fail( "Could not open stream for stderr" );
		}

		// Performing the dump. Suppress warnings, since we want to test
		// accessing broken revision data (page 5).
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
		$dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
		$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );

		$this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
		$this->assertNotEmpty( file_get_contents( $fnameReport ) );

		// Checking meta-history -------------------------------------------------

		$this->gunzip( $fnameMetaHistory );

		$asserter = $this->getDumpAsserter( $schemaVersion );
		$this->setSiteVarMappings( $asserter );
		$this->setAllRevisionsVarMappings( $asserter );

		$asserter->open( $fnameMetaHistory );
		$asserter->assertDumpHead( $expSiteInfo );
		$asserter->assertDOM( $expMetaHistory );
		$asserter->assertDumpEnd();

		// Checking meta-current -------------------------------------------------

		$this->gunzip( $fnameMetaCurrent );
		$this->assertDumpSchema( $fnameMetaCurrent, $this->getXmlSchemaPath( $schemaVersion ) );

		$asserter = $this->getDumpAsserter( $schemaVersion );
		$this->setSiteVarMappings( $asserter );
		$this->setCurrentRevisionsVarMappings( $asserter );

		$asserter->open( $fnameMetaCurrent );
		$asserter->assertDumpHead( $expSiteInfo );
		$asserter->assertDOM( $expMetaCurrent );
		$asserter->assertDumpEnd();

		// Checking articles -------------------------------------------------

		$this->gunzip( $fnameArticles );
		$this->assertDumpSchema( $fnameArticles, $this->getXmlSchemaPath( $schemaVersion ) );

		$asserter = $this->getDumpAsserter( $schemaVersion );
		$this->setSiteVarMappings( $asserter );
		$this->setCurrentRevisionsVarMappings( $asserter );

		$asserter->open( $fnameArticles );
		$asserter->assertDumpHead( $expSiteInfo );
		$asserter->assertDOM( $expArticles );
		$asserter->assertDumpEnd();
	}
}