Merge "Add final tests for the Status class"
[lhc/web/wiklou.git] / tests / phpunit / maintenance / backup_PageTest.php
1 <?php
2 /**
3 * Tests for page dumps of BackupDumper
4 *
5 * @group Database
6 * @group Dump
7 * @covers BackupDumper
8 */
9 class BackupDumperPageTest extends DumpTestCase {
10
11 // We'll add several pages, revision and texts. The following variables hold the
12 // corresponding ids.
13 private $pageId1, $pageId2, $pageId3, $pageId4, $pageId5;
14 private $pageTitle1, $pageTitle2, $pageTitle3, $pageTitle4, $pageTitle5;
15 private $revId1_1, $textId1_1;
16 private $revId2_1, $textId2_1, $revId2_2, $textId2_2;
17 private $revId2_3, $textId2_3, $revId2_4, $textId2_4;
18 private $revId3_1, $textId3_1, $revId3_2, $textId3_2;
19 private $revId4_1, $textId4_1;
20 private $namespace, $talk_namespace;
21
22 function addDBData() {
23 // be sure, titles created here using english namespace names
24 $this->setMwGlobals( array(
25 'wgLanguageCode' => 'en',
26 'wgContLang' => Language::factory( 'en' ),
27 ) );
28
29 $this->tablesUsed[] = 'page';
30 $this->tablesUsed[] = 'revision';
31 $this->tablesUsed[] = 'text';
32
33 try {
34 $this->namespace = $this->getDefaultWikitextNS();
35 $this->talk_namespace = NS_TALK;
36
37 if ( $this->namespace === $this->talk_namespace ) {
38 // @todo work around this.
39 throw new MWException( "The default wikitext namespace is the talk namespace. "
40 . " We can't currently deal with that." );
41 }
42
43 $this->pageTitle1 = Title::newFromText( 'BackupDumperTestP1', $this->namespace );
44 $page = WikiPage::factory( $this->pageTitle1 );
45 list( $this->revId1_1, $this->textId1_1 ) = $this->addRevision( $page,
46 "BackupDumperTestP1Text1", "BackupDumperTestP1Summary1" );
47 $this->pageId1 = $page->getId();
48
49 $this->pageTitle2 = Title::newFromText( 'BackupDumperTestP2', $this->namespace );
50 $page = WikiPage::factory( $this->pageTitle2 );
51 list( $this->revId2_1, $this->textId2_1 ) = $this->addRevision( $page,
52 "BackupDumperTestP2Text1", "BackupDumperTestP2Summary1" );
53 list( $this->revId2_2, $this->textId2_2 ) = $this->addRevision( $page,
54 "BackupDumperTestP2Text2", "BackupDumperTestP2Summary2" );
55 list( $this->revId2_3, $this->textId2_3 ) = $this->addRevision( $page,
56 "BackupDumperTestP2Text3", "BackupDumperTestP2Summary3" );
57 list( $this->revId2_4, $this->textId2_4 ) = $this->addRevision( $page,
58 "BackupDumperTestP2Text4 some additional Text ",
59 "BackupDumperTestP2Summary4 extra " );
60 $this->pageId2 = $page->getId();
61
62 $this->pageTitle3 = Title::newFromText( 'BackupDumperTestP3', $this->namespace );
63 $page = WikiPage::factory( $this->pageTitle3 );
64 list( $this->revId3_1, $this->textId3_1 ) = $this->addRevision( $page,
65 "BackupDumperTestP3Text1", "BackupDumperTestP2Summary1" );
66 list( $this->revId3_2, $this->textId3_2 ) = $this->addRevision( $page,
67 "BackupDumperTestP3Text2", "BackupDumperTestP2Summary2" );
68 $this->pageId3 = $page->getId();
69 $page->doDeleteArticle( "Testing ;)" );
70
71 $this->pageTitle4 = Title::newFromText( 'BackupDumperTestP1', $this->talk_namespace );
72 $page = WikiPage::factory( $this->pageTitle4 );
73 list( $this->revId4_1, $this->textId4_1 ) = $this->addRevision( $page,
74 "Talk about BackupDumperTestP1 Text1",
75 "Talk BackupDumperTestP1 Summary1" );
76 $this->pageId4 = $page->getId();
77 } catch ( Exception $e ) {
78 // We'd love to pass $e directly. However, ... see
79 // documentation of exceptionFromAddDBData in
80 // DumpTestCase
81 $this->exceptionFromAddDBData = $e;
82 }
83 }
84
85 protected function setUp() {
86 parent::setUp();
87
88 // Since we will restrict dumping by page ranges (to allow
89 // working tests, even if the db gets prepopulated by a base
90 // class), we have to assert, that the page id are consecutively
91 // increasing
92 $this->assertEquals(
93 array( $this->pageId2, $this->pageId3, $this->pageId4 ),
94 array( $this->pageId1 + 1, $this->pageId2 + 1, $this->pageId3 + 1 ),
95 "Page ids increasing without holes" );
96 }
97
98 function testFullTextPlain() {
99 // Preparing the dump
100 $fname = $this->getNewTempFile();
101 $dumper = new BackupDumper( array( "--output=file:" . $fname ) );
102 $dumper->startId = $this->pageId1;
103 $dumper->endId = $this->pageId4 + 1;
104 $dumper->reporting = false;
105 $dumper->setDb( $this->db );
106
107 // Performing the dump
108 $dumper->dump( WikiExporter::FULL, WikiExporter::TEXT );
109
110 // Checking the dumped data
111 $this->assertDumpStart( $fname );
112
113 // Page 1
114 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
115 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
116 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87",
117 "BackupDumperTestP1Text1" );
118 $this->assertPageEnd();
119
120 // Page 2
121 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
122 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
123 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2",
124 "BackupDumperTestP2Text1" );
125 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
126 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95",
127 "BackupDumperTestP2Text2", $this->revId2_1 );
128 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
129 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r",
130 "BackupDumperTestP2Text3", $this->revId2_2 );
131 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
132 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv",
133 "BackupDumperTestP2Text4 some additional Text", $this->revId2_3 );
134 $this->assertPageEnd();
135
136 // Page 3
137 // -> Page is marked deleted. Hence not visible
138
139 // Page 4
140 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
141 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
142 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe",
143 "Talk about BackupDumperTestP1 Text1" );
144 $this->assertPageEnd();
145
146 $this->assertDumpEnd();
147 }
148
149 function testFullStubPlain() {
150 // Preparing the dump
151 $fname = $this->getNewTempFile();
152 $dumper = new BackupDumper( array( "--output=file:" . $fname ) );
153 $dumper->startId = $this->pageId1;
154 $dumper->endId = $this->pageId4 + 1;
155 $dumper->reporting = false;
156 $dumper->setDb( $this->db );
157
158 // Performing the dump
159 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
160
161 // Checking the dumped data
162 $this->assertDumpStart( $fname );
163
164 // Page 1
165 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
166 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
167 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
168 $this->assertPageEnd();
169
170 // Page 2
171 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
172 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
173 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
174 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
175 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
176 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
177 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
178 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
179 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
180 $this->assertPageEnd();
181
182 // Page 3
183 // -> Page is marked deleted. Hence not visible
184
185 // Page 4
186 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
187 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
188 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
189 $this->assertPageEnd();
190
191 $this->assertDumpEnd();
192 }
193
194 function testCurrentStubPlain() {
195 // Preparing the dump
196 $fname = $this->getNewTempFile();
197 $dumper = new BackupDumper( array( "--output=file:" . $fname ) );
198 $dumper->startId = $this->pageId1;
199 $dumper->endId = $this->pageId4 + 1;
200 $dumper->reporting = false;
201 $dumper->setDb( $this->db );
202
203 // Performing the dump
204 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
205
206 // Checking the dumped data
207 $this->assertDumpStart( $fname );
208
209 // Page 1
210 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
211 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
212 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
213 $this->assertPageEnd();
214
215 // Page 2
216 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
217 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
218 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
219 $this->assertPageEnd();
220
221 // Page 3
222 // -> Page is marked deleted. Hence not visible
223
224 // Page 4
225 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
226 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
227 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
228 $this->assertPageEnd();
229
230 $this->assertDumpEnd();
231 }
232
233 function testCurrentStubGzip() {
234 $this->checkHasGzip();
235
236 // Preparing the dump
237 $fname = $this->getNewTempFile();
238 $dumper = new BackupDumper( array( "--output=gzip:" . $fname ) );
239 $dumper->startId = $this->pageId1;
240 $dumper->endId = $this->pageId4 + 1;
241 $dumper->reporting = false;
242 $dumper->setDb( $this->db );
243
244 // Performing the dump
245 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
246
247 // Checking the dumped data
248 $this->gunzip( $fname );
249 $this->assertDumpStart( $fname );
250
251 // Page 1
252 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
253 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
254 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
255 $this->assertPageEnd();
256
257 // Page 2
258 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
259 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
260 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
261 $this->assertPageEnd();
262
263 // Page 3
264 // -> Page is marked deleted. Hence not visible
265
266 // Page 4
267 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
268 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
269 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
270 $this->assertPageEnd();
271
272 $this->assertDumpEnd();
273 }
274
275 function testXmlDumpsBackupUseCase() {
276 // xmldumps-backup typically performs a single dump that that writes
277 // out three files
278 // * gzipped stubs of everything (meta-history)
279 // * gzipped stubs of latest revisions of all pages (meta-current)
280 // * gzipped stubs of latest revisions of all pages of namespage 0
281 // (articles)
282 //
283 // We reproduce such a setup with our mini fixture, although we omit
284 // chunks, and all the other gimmicks of xmldumps-backup.
285 //
286 $this->checkHasGzip();
287
288 $fnameMetaHistory = $this->getNewTempFile();
289 $fnameMetaCurrent = $this->getNewTempFile();
290 $fnameArticles = $this->getNewTempFile();
291
292 $dumper = new BackupDumper( array( "--output=gzip:" . $fnameMetaHistory,
293 "--output=gzip:" . $fnameMetaCurrent, "--filter=latest",
294 "--output=gzip:" . $fnameArticles, "--filter=latest",
295 "--filter=notalk", "--filter=namespace:!NS_USER",
296 "--reporting=1000" ) );
297 $dumper->startId = $this->pageId1;
298 $dumper->endId = $this->pageId4 + 1;
299 $dumper->setDb( $this->db );
300
301 // xmldumps-backup uses reporting. We will not check the exact reported
302 // message, as they are dependent on the processing power of the used
303 // computer. We only check that reporting does not crash the dumping
304 // and that something is reported
305 $dumper->stderr = fopen( 'php://output', 'a' );
306 if ( $dumper->stderr === false ) {
307 $this->fail( "Could not open stream for stderr" );
308 }
309
310 // Performing the dump
311 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
312
313 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
314
315 // Checking meta-history -------------------------------------------------
316
317 $this->gunzip( $fnameMetaHistory );
318 $this->assertDumpStart( $fnameMetaHistory );
319
320 // Page 1
321 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
322 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
323 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
324 $this->assertPageEnd();
325
326 // Page 2
327 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
328 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
329 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
330 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
331 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
332 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
333 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
334 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
335 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
336 $this->assertPageEnd();
337
338 // Page 3
339 // -> Page is marked deleted. Hence not visible
340
341 // Page 4
342 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
343 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
344 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
345 $this->assertPageEnd();
346
347 $this->assertDumpEnd();
348
349 // Checking meta-current -------------------------------------------------
350
351 $this->gunzip( $fnameMetaCurrent );
352 $this->assertDumpStart( $fnameMetaCurrent );
353
354 // Page 1
355 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
356 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
357 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
358 $this->assertPageEnd();
359
360 // Page 2
361 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
362 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
363 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
364 $this->assertPageEnd();
365
366 // Page 3
367 // -> Page is marked deleted. Hence not visible
368
369 // Page 4
370 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
371 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
372 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
373 $this->assertPageEnd();
374
375 $this->assertDumpEnd();
376
377 // Checking articles -------------------------------------------------
378
379 $this->gunzip( $fnameArticles );
380 $this->assertDumpStart( $fnameArticles );
381
382 // Page 1
383 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
384 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
385 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
386 $this->assertPageEnd();
387
388 // Page 2
389 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
390 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
391 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
392 $this->assertPageEnd();
393
394 // Page 3
395 // -> Page is marked deleted. Hence not visible
396
397 // Page 4
398 // -> Page is not in $this->namespace. Hence not visible
399
400 $this->assertDumpEnd();
401
402 $this->expectETAOutput();
403 }
404 }