f5dd98b3fbf59c3964c1d69b39e203ed3722377e
[lhc/web/wiklou.git] / tests / phpunit / maintenance / backupTextPassTest.php
1 <?php
2
3 require_once __DIR__ . "/../../../maintenance/backupTextPass.inc";
4
5 /**
6 * Tests for TextPassDumper that rely on the database
7 *
8 * @group Database
9 * @group Dump
10 * @covers TextPassDumper
11 */
12 class TextPassDumperDatabaseTest extends DumpTestCase {
13
14 // We'll add several pages, revision and texts. The following variables hold the
15 // corresponding ids.
16 private $pageId1, $pageId2, $pageId3, $pageId4;
17 private static $numOfPages = 4;
18 private $revId1_1, $textId1_1;
19 private $revId2_1, $textId2_1, $revId2_2, $textId2_2;
20 private $revId2_3, $textId2_3, $revId2_4, $textId2_4;
21 private $revId3_1, $textId3_1, $revId3_2, $textId3_2;
22 private $revId4_1, $textId4_1;
23 private static $numOfRevs = 8;
24
25 function addDBData() {
26 $this->tablesUsed[] = 'page';
27 $this->tablesUsed[] = 'revision';
28 $this->tablesUsed[] = 'text';
29
30 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', array(
31 "BackupTextPassTestModel" => "BackupTextPassTestModelHandler"
32 ) );
33
34 $ns = $this->getDefaultWikitextNS();
35
36 try {
37 // Simple page
38 $title = Title::newFromText( 'BackupDumperTestP1', $ns );
39 $page = WikiPage::factory( $title );
40 list( $this->revId1_1, $this->textId1_1 ) = $this->addRevision( $page,
41 "BackupDumperTestP1Text1", "BackupDumperTestP1Summary1" );
42 $this->pageId1 = $page->getId();
43
44 // Page with more than one revision
45 $title = Title::newFromText( 'BackupDumperTestP2', $ns );
46 $page = WikiPage::factory( $title );
47 list( $this->revId2_1, $this->textId2_1 ) = $this->addRevision( $page,
48 "BackupDumperTestP2Text1", "BackupDumperTestP2Summary1" );
49 list( $this->revId2_2, $this->textId2_2 ) = $this->addRevision( $page,
50 "BackupDumperTestP2Text2", "BackupDumperTestP2Summary2" );
51 list( $this->revId2_3, $this->textId2_3 ) = $this->addRevision( $page,
52 "BackupDumperTestP2Text3", "BackupDumperTestP2Summary3" );
53 list( $this->revId2_4, $this->textId2_4 ) = $this->addRevision( $page,
54 "BackupDumperTestP2Text4 some additional Text ",
55 "BackupDumperTestP2Summary4 extra " );
56 $this->pageId2 = $page->getId();
57
58 // Deleted page.
59 $title = Title::newFromText( 'BackupDumperTestP3', $ns );
60 $page = WikiPage::factory( $title );
61 list( $this->revId3_1, $this->textId3_1 ) = $this->addRevision( $page,
62 "BackupDumperTestP3Text1", "BackupDumperTestP2Summary1" );
63 list( $this->revId3_2, $this->textId3_2 ) = $this->addRevision( $page,
64 "BackupDumperTestP3Text2", "BackupDumperTestP2Summary2" );
65 $this->pageId3 = $page->getId();
66 $page->doDeleteArticle( "Testing ;)" );
67
68 // Page from non-default namespace and model.
69 // ExportTransform applies.
70
71 if ( $ns === NS_TALK ) {
72 // @todo work around this.
73 throw new MWException( "The default wikitext namespace is the talk namespace. "
74 . " We can't currently deal with that." );
75 }
76
77 $title = Title::newFromText( 'BackupDumperTestP1', NS_TALK );
78 $page = WikiPage::factory( $title );
79 list( $this->revId4_1, $this->textId4_1 ) = $this->addRevision( $page,
80 "Talk about BackupDumperTestP1 Text1",
81 "Talk BackupDumperTestP1 Summary1",
82 "BackupTextPassTestModel" );
83 $this->pageId4 = $page->getId();
84 } catch ( Exception $e ) {
85 // We'd love to pass $e directly. However, ... see
86 // documentation of exceptionFromAddDBData in
87 // DumpTestCase
88 $this->exceptionFromAddDBData = $e;
89 }
90 }
91
92 protected function setUp() {
93 parent::setUp();
94
95 // Since we will restrict dumping by page ranges (to allow
96 // working tests, even if the db gets prepopulated by a base
97 // class), we have to assert, that the page id are consecutively
98 // increasing
99 $this->assertEquals(
100 array( $this->pageId2, $this->pageId3, $this->pageId4 ),
101 array( $this->pageId1 + 1, $this->pageId2 + 1, $this->pageId3 + 1 ),
102 "Page ids increasing without holes" );
103 }
104
105 function testPlain() {
106 // Setting up the dump
107 $nameStub = $this->setUpStub();
108 $nameFull = $this->getNewTempFile();
109 $dumper = new TextPassDumper( array( "--stub=file:" . $nameStub,
110 "--output=file:" . $nameFull ) );
111 $dumper->reporting = false;
112 $dumper->setDb( $this->db );
113
114 // Performing the dump
115 $dumper->dump( WikiExporter::FULL, WikiExporter::TEXT );
116
117 // Checking for correctness of the dumped data
118 $this->assertDumpStart( $nameFull );
119
120 // Page 1
121 $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" );
122 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
123 $this->textId1_1, false, "0bolhl6ol7i6x0e7yq91gxgaan39j87",
124 "BackupDumperTestP1Text1" );
125 $this->assertPageEnd();
126
127 // Page 2
128 $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" );
129 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
130 $this->textId2_1, false, "jprywrymfhysqllua29tj3sc7z39dl2",
131 "BackupDumperTestP2Text1" );
132 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
133 $this->textId2_2, false, "b7vj5ks32po5m1z1t1br4o7scdwwy95",
134 "BackupDumperTestP2Text2", $this->revId2_1 );
135 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
136 $this->textId2_3, false, "jfunqmh1ssfb8rs43r19w98k28gg56r",
137 "BackupDumperTestP2Text3", $this->revId2_2 );
138 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
139 $this->textId2_4, false, "6o1ciaxa6pybnqprmungwofc4lv00wv",
140 "BackupDumperTestP2Text4 some additional Text", $this->revId2_3 );
141 $this->assertPageEnd();
142
143 // Page 3
144 // -> Page is marked deleted. Hence not visible
145
146 // Page 4
147 $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" );
148 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
149 $this->textId4_1, false, "nktofwzd0tl192k3zfepmlzxoax1lpe",
150 "TALK ABOUT BACKUPDUMPERTESTP1 TEXT1",
151 false,
152 "BackupTextPassTestModel",
153 "text/plain" );
154 $this->assertPageEnd();
155
156 $this->assertDumpEnd();
157 }
158
159 function testPrefetchPlain() {
160 // The mapping between ids and text, for the hits of the prefetch mock
161 $prefetchMap = array(
162 array( $this->pageId1, $this->revId1_1, "Prefetch_________1Text1" ),
163 array( $this->pageId2, $this->revId2_3, "Prefetch_________2Text3" )
164 );
165
166 // The mock itself
167 $prefetchMock = $this->getMock( 'BaseDump', array( 'prefetch' ), array(), '', false );
168 $prefetchMock->expects( $this->exactly( 6 ) )
169 ->method( 'prefetch' )
170 ->will( $this->returnValueMap( $prefetchMap ) );
171
172 // Setting up of the dump
173 $nameStub = $this->setUpStub();
174 $nameFull = $this->getNewTempFile();
175 $dumper = new TextPassDumper( array( "--stub=file:"
176 . $nameStub, "--output=file:" . $nameFull ) );
177 $dumper->prefetch = $prefetchMock;
178 $dumper->reporting = false;
179 $dumper->setDb( $this->db );
180
181 // Performing the dump
182 $dumper->dump( WikiExporter::FULL, WikiExporter::TEXT );
183
184 // Checking for correctness of the dumped data
185 $this->assertDumpStart( $nameFull );
186
187 // Page 1
188 $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" );
189 // Prefetch kicks in. This is still the SHA-1 of the original text,
190 // But the actual text (with different SHA-1) comes from prefetch.
191 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
192 $this->textId1_1, false, "0bolhl6ol7i6x0e7yq91gxgaan39j87",
193 "Prefetch_________1Text1" );
194 $this->assertPageEnd();
195
196 // Page 2
197 $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" );
198 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
199 $this->textId2_1, false, "jprywrymfhysqllua29tj3sc7z39dl2",
200 "BackupDumperTestP2Text1" );
201 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
202 $this->textId2_2, false, "b7vj5ks32po5m1z1t1br4o7scdwwy95",
203 "BackupDumperTestP2Text2", $this->revId2_1 );
204 // Prefetch kicks in. This is still the SHA-1 of the original text,
205 // But the actual text (with different SHA-1) comes from prefetch.
206 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
207 $this->textId2_3, false, "jfunqmh1ssfb8rs43r19w98k28gg56r",
208 "Prefetch_________2Text3", $this->revId2_2 );
209 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
210 $this->textId2_4, false, "6o1ciaxa6pybnqprmungwofc4lv00wv",
211 "BackupDumperTestP2Text4 some additional Text", $this->revId2_3 );
212 $this->assertPageEnd();
213
214 // Page 3
215 // -> Page is marked deleted. Hence not visible
216
217 // Page 4
218 $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" );
219 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
220 $this->textId4_1, false, "nktofwzd0tl192k3zfepmlzxoax1lpe",
221 "TALK ABOUT BACKUPDUMPERTESTP1 TEXT1",
222 false,
223 "BackupTextPassTestModel",
224 "text/plain" );
225 $this->assertPageEnd();
226
227 $this->assertDumpEnd();
228 }
229
230 /**
231 * Ensures that checkpoint dumps are used and written, by successively increasing the
232 * stub size and dumping until the duration crosses a threshold.
233 *
234 * @param string $checkpointFormat Either "file" for plain text or "gzip" for gzipped
235 * checkpoint files.
236 */
237 private function checkpointHelper( $checkpointFormat = "file" ) {
238 // Getting temporary names
239 $nameStub = $this->getNewTempFile();
240 $nameOutputDir = $this->getNewTempDirectory();
241
242 $stderr = fopen( 'php://output', 'a' );
243 if ( $stderr === false ) {
244 $this->fail( "Could not open stream for stderr" );
245 }
246
247 $iterations = 32; // We'll start with that many iterations of revisions
248 // in stub. Make sure that the generated volume is above the buffer size
249 // set below. Otherwise, the checkpointing does not trigger.
250 $lastDuration = 0;
251 $minDuration = 2; // We want the dump to take at least this many seconds
252 $checkpointAfter = 0.5; // Generate checkpoint after this many seconds
253
254 // Until a dump takes at least $minDuration seconds, perform a dump and check
255 // duration. If the dump did not take long enough increase the iteration
256 // count, to generate a bigger stub file next time.
257 while ( $lastDuration < $minDuration ) {
258
259 // Setting up the dump
260 wfRecursiveRemoveDir( $nameOutputDir );
261 $this->assertTrue( wfMkdirParents( $nameOutputDir ),
262 "Creating temporary output directory " );
263 $this->setUpStub( $nameStub, $iterations );
264 $dumper = new TextPassDumper( array( "--stub=file:" . $nameStub,
265 "--output=" . $checkpointFormat . ":" . $nameOutputDir . "/full",
266 "--maxtime=1" /*This is in minutes. Fixup is below*/,
267 "--buffersize=32768", // The default of 32 iterations fill up 32KB about twice
268 "--checkpointfile=checkpoint-%s-%s.xml.gz" ) );
269 $dumper->setDb( $this->db );
270 $dumper->maxTimeAllowed = $checkpointAfter; // Patching maxTime from 1 minute
271 $dumper->stderr = $stderr;
272
273 // The actual dump and taking time
274 $ts_before = microtime( true );
275 $dumper->dump( WikiExporter::FULL, WikiExporter::TEXT );
276 $ts_after = microtime( true );
277 $lastDuration = $ts_after - $ts_before;
278
279 // Handling increasing the iteration count for the stubs
280 if ( $lastDuration < $minDuration ) {
281 $old_iterations = $iterations;
282 if ( $lastDuration > 0.2 ) {
283 // lastDuration is big enough, to allow an educated guess
284 $factor = ( $minDuration + 0.5 ) / $lastDuration;
285 if ( ( $factor > 1.1 ) && ( $factor < 100 ) ) {
286 // educated guess is reasonable
287 $iterations = (int)( $iterations * $factor );
288 }
289 }
290
291 if ( $old_iterations == $iterations ) {
292 // Heuristics were not applied, so we just *2.
293 $iterations *= 2;
294 }
295
296 $this->assertLessThan( 50000, $iterations,
297 "Emergency stop against infinitely increasing iteration "
298 . "count ( last duration: $lastDuration )" );
299 }
300 }
301
302 // The dump (hopefully) did take long enough to produce more than one
303 // checkpoint file.
304 // We now check all the checkpoint files for validity.
305
306 $files = scandir( $nameOutputDir );
307 $this->assertTrue( asort( $files ), "Sorting files in temporary directory" );
308 $fileOpened = false;
309 $lookingForPage = 1;
310 $checkpointFiles = 0;
311
312 // Each run of the following loop body tries to handle exactly 1 /page/ (not
313 // iteration of stub content). $i is only increased after having treated page 4.
314 for ( $i = 0; $i < $iterations; ) {
315
316 // 1. Assuring a file is opened and ready. Skipping across header if
317 // necessary.
318 if ( !$fileOpened ) {
319 $this->assertNotEmpty( $files, "No more existing dump files, "
320 . "but not yet all pages found" );
321 $fname = array_shift( $files );
322 while ( $fname == "." || $fname == ".." ) {
323 $this->assertNotEmpty( $files, "No more existing dump"
324 . " files, but not yet all pages found" );
325 $fname = array_shift( $files );
326 }
327 if ( $checkpointFormat == "gzip" ) {
328 $this->gunzip( $nameOutputDir . "/" . $fname );
329 }
330 $this->assertDumpStart( $nameOutputDir . "/" . $fname );
331 $fileOpened = true;
332 $checkpointFiles++;
333 }
334
335 // 2. Performing a single page check
336 switch ( $lookingForPage ) {
337 case 1:
338 // Page 1
339 $this->assertPageStart( $this->pageId1 + $i * self::$numOfPages, NS_MAIN,
340 "BackupDumperTestP1" );
341 $this->assertRevision( $this->revId1_1 + $i * self::$numOfRevs, "BackupDumperTestP1Summary1",
342 $this->textId1_1, false, "0bolhl6ol7i6x0e7yq91gxgaan39j87",
343 "BackupDumperTestP1Text1" );
344 $this->assertPageEnd();
345
346 $lookingForPage = 2;
347 break;
348
349 case 2:
350 // Page 2
351 $this->assertPageStart( $this->pageId2 + $i * self::$numOfPages, NS_MAIN,
352 "BackupDumperTestP2" );
353 $this->assertRevision( $this->revId2_1 + $i * self::$numOfRevs, "BackupDumperTestP2Summary1",
354 $this->textId2_1, false, "jprywrymfhysqllua29tj3sc7z39dl2",
355 "BackupDumperTestP2Text1" );
356 $this->assertRevision( $this->revId2_2 + $i * self::$numOfRevs, "BackupDumperTestP2Summary2",
357 $this->textId2_2, false, "b7vj5ks32po5m1z1t1br4o7scdwwy95",
358 "BackupDumperTestP2Text2", $this->revId2_1 + $i * self::$numOfRevs );
359 $this->assertRevision( $this->revId2_3 + $i * self::$numOfRevs, "BackupDumperTestP2Summary3",
360 $this->textId2_3, false, "jfunqmh1ssfb8rs43r19w98k28gg56r",
361 "BackupDumperTestP2Text3", $this->revId2_2 + $i * self::$numOfRevs );
362 $this->assertRevision( $this->revId2_4 + $i * self::$numOfRevs,
363 "BackupDumperTestP2Summary4 extra",
364 $this->textId2_4, false, "6o1ciaxa6pybnqprmungwofc4lv00wv",
365 "BackupDumperTestP2Text4 some additional Text",
366 $this->revId2_3 + $i * self::$numOfRevs );
367 $this->assertPageEnd();
368
369 $lookingForPage = 4;
370 break;
371
372 case 4:
373 // Page 4
374 $this->assertPageStart( $this->pageId4 + $i * self::$numOfPages, NS_TALK,
375 "Talk:BackupDumperTestP1" );
376 $this->assertRevision( $this->revId4_1 + $i * self::$numOfRevs,
377 "Talk BackupDumperTestP1 Summary1",
378 $this->textId4_1, false, "nktofwzd0tl192k3zfepmlzxoax1lpe",
379 "TALK ABOUT BACKUPDUMPERTESTP1 TEXT1",
380 false,
381 "BackupTextPassTestModel",
382 "text/plain" );
383 $this->assertPageEnd();
384
385 $lookingForPage = 1;
386
387 // We dealt with the whole iteration.
388 $i++;
389 break;
390
391 default:
392 $this->fail( "Bad setting for lookingForPage ($lookingForPage)" );
393 }
394
395 // 3. Checking for the end of the current checkpoint file
396 if ( $this->xml->nodeType == XMLReader::END_ELEMENT
397 && $this->xml->name == "mediawiki"
398 ) {
399 $this->assertDumpEnd();
400 $fileOpened = false;
401 }
402 }
403
404 // Assuring we completely read all files ...
405 $this->assertFalse( $fileOpened, "Currently read file still open?" );
406 $this->assertEmpty( $files, "Remaining unchecked files" );
407
408 // ... and have dealt with more than one checkpoint file
409 $this->assertGreaterThan(
410 1,
411 $checkpointFiles,
412 "expected more than 1 checkpoint to have been created. "
413 . "Checkpoint interval is $checkpointAfter seconds, maybe your computer is too fast?"
414 );
415
416 $this->expectETAOutput();
417 }
418
419 /**
420 * Broken per T70653.
421 *
422 * @group large
423 * @group Broken
424 */
425 function testCheckpointPlain() {
426 $this->checkpointHelper();
427 }
428
429 /**
430 * tests for working checkpoint generation in gzip format work.
431 *
432 * We keep this test in addition to the simpler self::testCheckpointPlain, as there
433 * were once problems when the used sinks were DumpPipeOutputs.
434 *
435 * xmldumps-backup typically uses bzip2 instead of gzip. However, as bzip2 requires
436 * PHP extensions, we go for gzip instead, which triggers the same relevant code
437 * paths while still being testable on more systems.
438 *
439 * Broken per T70653.
440 *
441 * @group large
442 * @group Broken
443 */
444 function testCheckpointGzip() {
445 $this->checkHasGzip();
446 $this->checkpointHelper( "gzip" );
447 }
448
449 /**
450 * Creates a stub file that is used for testing the text pass of dumps
451 *
452 * @param string $fname (Optional) Absolute name of the file to write
453 * the stub into. If this parameter is null, a new temporary
454 * file is generated that is automatically removed upon tearDown.
455 * @param int $iterations (Optional) specifies how often the block
456 * of 3 pages should go into the stub file. The page and
457 * revision id increase further and further, while the text
458 * id of the first iteration is reused. The pages and revision
459 * of iteration > 1 have no corresponding representation in the database.
460 * @return string Absolute filename of the stub
461 */
462 private function setUpStub( $fname = null, $iterations = 1 ) {
463 if ( $fname === null ) {
464 $fname = $this->getNewTempFile();
465 }
466 $header = '<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" '
467 . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
468 . 'xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ '
469 . 'http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
470 <siteinfo>
471 <sitename>wikisvn</sitename>
472 <base>http://localhost/wiki-svn/index.php/Main_Page</base>
473 <generator>MediaWiki 1.21alpha</generator>
474 <case>first-letter</case>
475 <namespaces>
476 <namespace key="-2" case="first-letter">Media</namespace>
477 <namespace key="-1" case="first-letter">Special</namespace>
478 <namespace key="0" case="first-letter" />
479 <namespace key="1" case="first-letter">Talk</namespace>
480 <namespace key="2" case="first-letter">User</namespace>
481 <namespace key="3" case="first-letter">User talk</namespace>
482 <namespace key="4" case="first-letter">Wikisvn</namespace>
483 <namespace key="5" case="first-letter">Wikisvn talk</namespace>
484 <namespace key="6" case="first-letter">File</namespace>
485 <namespace key="7" case="first-letter">File talk</namespace>
486 <namespace key="8" case="first-letter">MediaWiki</namespace>
487 <namespace key="9" case="first-letter">MediaWiki talk</namespace>
488 <namespace key="10" case="first-letter">Template</namespace>
489 <namespace key="11" case="first-letter">Template talk</namespace>
490 <namespace key="12" case="first-letter">Help</namespace>
491 <namespace key="13" case="first-letter">Help talk</namespace>
492 <namespace key="14" case="first-letter">Category</namespace>
493 <namespace key="15" case="first-letter">Category talk</namespace>
494 </namespaces>
495 </siteinfo>
496 ';
497 $tail = '</mediawiki>
498 ';
499
500 $content = $header;
501 $iterations = intval( $iterations );
502 for ( $i = 0; $i < $iterations; $i++ ) {
503
504 $page1 = ' <page>
505 <title>BackupDumperTestP1</title>
506 <ns>0</ns>
507 <id>' . ( $this->pageId1 + $i * self::$numOfPages ) . '</id>
508 <revision>
509 <id>' . ( $this->revId1_1 + $i * self::$numOfRevs ) . '</id>
510 <timestamp>2012-04-01T16:46:05Z</timestamp>
511 <contributor>
512 <ip>127.0.0.1</ip>
513 </contributor>
514 <comment>BackupDumperTestP1Summary1</comment>
515 <model>wikitext</model>
516 <format>text/x-wiki</format>
517 <text id="' . $this->textId1_1 . '" bytes="23" />
518 <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
519 </revision>
520 </page>
521 ';
522 $page2 = ' <page>
523 <title>BackupDumperTestP2</title>
524 <ns>0</ns>
525 <id>' . ( $this->pageId2 + $i * self::$numOfPages ) . '</id>
526 <revision>
527 <id>' . ( $this->revId2_1 + $i * self::$numOfRevs ) . '</id>
528 <timestamp>2012-04-01T16:46:05Z</timestamp>
529 <contributor>
530 <ip>127.0.0.1</ip>
531 </contributor>
532 <comment>BackupDumperTestP2Summary1</comment>
533 <model>wikitext</model>
534 <format>text/x-wiki</format>
535 <text id="' . $this->textId2_1 . '" bytes="23" />
536 <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
537 </revision>
538 <revision>
539 <id>' . ( $this->revId2_2 + $i * self::$numOfRevs ) . '</id>
540 <parentid>' . ( $this->revId2_1 + $i * self::$numOfRevs ) . '</parentid>
541 <timestamp>2012-04-01T16:46:05Z</timestamp>
542 <contributor>
543 <ip>127.0.0.1</ip>
544 </contributor>
545 <comment>BackupDumperTestP2Summary2</comment>
546 <model>wikitext</model>
547 <format>text/x-wiki</format>
548 <text id="' . $this->textId2_2 . '" bytes="23" />
549 <sha1>b7vj5ks32po5m1z1t1br4o7scdwwy95</sha1>
550 </revision>
551 <revision>
552 <id>' . ( $this->revId2_3 + $i * self::$numOfRevs ) . '</id>
553 <parentid>' . ( $this->revId2_2 + $i * self::$numOfRevs ) . '</parentid>
554 <timestamp>2012-04-01T16:46:05Z</timestamp>
555 <contributor>
556 <ip>127.0.0.1</ip>
557 </contributor>
558 <comment>BackupDumperTestP2Summary3</comment>
559 <model>wikitext</model>
560 <format>text/x-wiki</format>
561 <text id="' . $this->textId2_3 . '" bytes="23" />
562 <sha1>jfunqmh1ssfb8rs43r19w98k28gg56r</sha1>
563 </revision>
564 <revision>
565 <id>' . ( $this->revId2_4 + $i * self::$numOfRevs ) . '</id>
566 <parentid>' . ( $this->revId2_3 + $i * self::$numOfRevs ) . '</parentid>
567 <timestamp>2012-04-01T16:46:05Z</timestamp>
568 <contributor>
569 <ip>127.0.0.1</ip>
570 </contributor>
571 <comment>BackupDumperTestP2Summary4 extra</comment>
572 <model>wikitext</model>
573 <format>text/x-wiki</format>
574 <text id="' . $this->textId2_4 . '" bytes="44" />
575 <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
576 </revision>
577 </page>
578 ';
579 // page 3 not in stub
580
581 $page4 = ' <page>
582 <title>Talk:BackupDumperTestP1</title>
583 <ns>1</ns>
584 <id>' . ( $this->pageId4 + $i * self::$numOfPages ) . '</id>
585 <revision>
586 <id>' . ( $this->revId4_1 + $i * self::$numOfRevs ) . '</id>
587 <timestamp>2012-04-01T16:46:05Z</timestamp>
588 <contributor>
589 <ip>127.0.0.1</ip>
590 </contributor>
591 <comment>Talk BackupDumperTestP1 Summary1</comment>
592 <model>BackupTextPassTestModel</model>
593 <format>text/plain</format>
594 <text id="' . $this->textId4_1 . '" bytes="35" />
595 <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
596 </revision>
597 </page>
598 ';
599 $content .= $page1 . $page2 . $page4;
600 }
601 $content .= $tail;
602 $this->assertEquals( strlen( $content ), file_put_contents(
603 $fname, $content ), "Length of prepared stub" );
604
605 return $fname;
606 }
607 }
608
609 class BackupTextPassTestModelHandler extends TextContentHandler {
610
611 public function __construct() {
612 parent::__construct( 'BackupTextPassTestModel' );
613 }
614
615 public function exportTransform( $text, $format = null ) {
616 return strtoupper( $text );
617 }
618
619 }
620
621 /**
622 * Tests for TextPassDumper that do not rely on the database
623 *
624 * (As the Database group is only detected at class level (not method level), we
625 * cannot bring this test case's tests into the above main test case.)
626 *
627 * @group Dump
628 * @covers TextPassDumper
629 */
630 class TextPassDumperDatabaselessTest extends MediaWikiLangTestCase {
631 /**
632 * Ensures that setting the buffer size is effective.
633 *
634 * @dataProvider bufferSizeProvider
635 */
636 function testBufferSizeSetting( $expected, $size, $msg ) {
637 $dumper = new TextPassDumperAccessor( array( "--buffersize=" . $size ) );
638 $this->assertEquals( $expected, $dumper->getBufferSize(), $msg );
639 }
640
641 /**
642 * Ensures that setting the buffer size is effective.
643 *
644 * @dataProvider bufferSizeProvider
645 */
646 function bufferSizeProvider() {
647 // expected, bufferSize to initialize with, message
648 return array(
649 array( 512 * 1024, 512 * 1024, "Setting 512KB is not effective" ),
650 array( 8192, 8192, "Setting 8KB is not effective" ),
651 array( 4096, 2048, "Could set buffer size below lower bound" )
652 );
653 }
654 }
655
656 /**
657 * Accessor for internal state of TextPassDumper
658 *
659 * Do not warrentless add getters here.
660 */
661 class TextPassDumperAccessor extends TextPassDumper {
662 /**
663 * Gets the bufferSize.
664 *
665 * If bufferSize setting does not work correctly, testCheckpoint... tests
666 * fail and point in the wrong direction. To aid in troubleshooting when
667 * testCheckpoint... tests break at some point in the future, we test the
668 * bufferSize setting, hence need this accessor.
669 *
670 * (Yes, bufferSize is internal state of the TextPassDumper, but aiding
671 * debugging of testCheckpoint... in the future seems to be worth testing
672 * against it nonetheless.)
673 */
674 public function getBufferSize() {
675 return $this->bufferSize;
676 }
677 }