Merge "Add tests for API's assert={user|bot}"
[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(
141 $this->pageId4,
142 $this->talk_namespace,
143 $this->pageTitle4->getPrefixedText()
144 );
145 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
146 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe",
147 "Talk about BackupDumperTestP1 Text1" );
148 $this->assertPageEnd();
149
150 $this->assertDumpEnd();
151 }
152
153 function testFullStubPlain() {
154 // Preparing the dump
155 $fname = $this->getNewTempFile();
156 $dumper = new BackupDumper( array( "--output=file:" . $fname ) );
157 $dumper->startId = $this->pageId1;
158 $dumper->endId = $this->pageId4 + 1;
159 $dumper->reporting = false;
160 $dumper->setDb( $this->db );
161
162 // Performing the dump
163 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
164
165 // Checking the dumped data
166 $this->assertDumpStart( $fname );
167
168 // Page 1
169 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
170 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
171 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
172 $this->assertPageEnd();
173
174 // Page 2
175 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
176 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
177 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
178 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
179 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
180 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
181 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
182 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
183 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
184 $this->assertPageEnd();
185
186 // Page 3
187 // -> Page is marked deleted. Hence not visible
188
189 // Page 4
190 $this->assertPageStart(
191 $this->pageId4,
192 $this->talk_namespace,
193 $this->pageTitle4->getPrefixedText()
194 );
195 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
196 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
197 $this->assertPageEnd();
198
199 $this->assertDumpEnd();
200 }
201
202 function testCurrentStubPlain() {
203 // Preparing the dump
204 $fname = $this->getNewTempFile();
205 $dumper = new BackupDumper( array( "--output=file:" . $fname ) );
206 $dumper->startId = $this->pageId1;
207 $dumper->endId = $this->pageId4 + 1;
208 $dumper->reporting = false;
209 $dumper->setDb( $this->db );
210
211 // Performing the dump
212 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
213
214 // Checking the dumped data
215 $this->assertDumpStart( $fname );
216
217 // Page 1
218 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
219 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
220 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
221 $this->assertPageEnd();
222
223 // Page 2
224 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
225 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
226 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
227 $this->assertPageEnd();
228
229 // Page 3
230 // -> Page is marked deleted. Hence not visible
231
232 // Page 4
233 $this->assertPageStart(
234 $this->pageId4,
235 $this->talk_namespace,
236 $this->pageTitle4->getPrefixedText()
237 );
238 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
239 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
240 $this->assertPageEnd();
241
242 $this->assertDumpEnd();
243 }
244
245 function testCurrentStubGzip() {
246 $this->checkHasGzip();
247
248 // Preparing the dump
249 $fname = $this->getNewTempFile();
250 $dumper = new BackupDumper( array( "--output=gzip:" . $fname ) );
251 $dumper->startId = $this->pageId1;
252 $dumper->endId = $this->pageId4 + 1;
253 $dumper->reporting = false;
254 $dumper->setDb( $this->db );
255
256 // Performing the dump
257 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
258
259 // Checking the dumped data
260 $this->gunzip( $fname );
261 $this->assertDumpStart( $fname );
262
263 // Page 1
264 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
265 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
266 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
267 $this->assertPageEnd();
268
269 // Page 2
270 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
271 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
272 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
273 $this->assertPageEnd();
274
275 // Page 3
276 // -> Page is marked deleted. Hence not visible
277
278 // Page 4
279 $this->assertPageStart(
280 $this->pageId4,
281 $this->talk_namespace,
282 $this->pageTitle4->getPrefixedText()
283 );
284 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
285 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
286 $this->assertPageEnd();
287
288 $this->assertDumpEnd();
289 }
290
291 function testXmlDumpsBackupUseCase() {
292 // xmldumps-backup typically performs a single dump that that writes
293 // out three files
294 // * gzipped stubs of everything (meta-history)
295 // * gzipped stubs of latest revisions of all pages (meta-current)
296 // * gzipped stubs of latest revisions of all pages of namespage 0
297 // (articles)
298 //
299 // We reproduce such a setup with our mini fixture, although we omit
300 // chunks, and all the other gimmicks of xmldumps-backup.
301 //
302 $this->checkHasGzip();
303
304 $fnameMetaHistory = $this->getNewTempFile();
305 $fnameMetaCurrent = $this->getNewTempFile();
306 $fnameArticles = $this->getNewTempFile();
307
308 $dumper = new BackupDumper( array( "--output=gzip:" . $fnameMetaHistory,
309 "--output=gzip:" . $fnameMetaCurrent, "--filter=latest",
310 "--output=gzip:" . $fnameArticles, "--filter=latest",
311 "--filter=notalk", "--filter=namespace:!NS_USER",
312 "--reporting=1000" ) );
313 $dumper->startId = $this->pageId1;
314 $dumper->endId = $this->pageId4 + 1;
315 $dumper->setDb( $this->db );
316
317 // xmldumps-backup uses reporting. We will not check the exact reported
318 // message, as they are dependent on the processing power of the used
319 // computer. We only check that reporting does not crash the dumping
320 // and that something is reported
321 $dumper->stderr = fopen( 'php://output', 'a' );
322 if ( $dumper->stderr === false ) {
323 $this->fail( "Could not open stream for stderr" );
324 }
325
326 // Performing the dump
327 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
328
329 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
330
331 // Checking meta-history -------------------------------------------------
332
333 $this->gunzip( $fnameMetaHistory );
334 $this->assertDumpStart( $fnameMetaHistory );
335
336 // Page 1
337 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
338 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
339 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
340 $this->assertPageEnd();
341
342 // Page 2
343 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
344 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
345 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
346 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
347 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
348 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
349 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
350 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
351 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
352 $this->assertPageEnd();
353
354 // Page 3
355 // -> Page is marked deleted. Hence not visible
356
357 // Page 4
358 $this->assertPageStart(
359 $this->pageId4,
360 $this->talk_namespace,
361 $this->pageTitle4->getPrefixedText()
362 );
363 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
364 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
365 $this->assertPageEnd();
366
367 $this->assertDumpEnd();
368
369 // Checking meta-current -------------------------------------------------
370
371 $this->gunzip( $fnameMetaCurrent );
372 $this->assertDumpStart( $fnameMetaCurrent );
373
374 // Page 1
375 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
376 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
377 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
378 $this->assertPageEnd();
379
380 // Page 2
381 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
382 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
383 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
384 $this->assertPageEnd();
385
386 // Page 3
387 // -> Page is marked deleted. Hence not visible
388
389 // Page 4
390 $this->assertPageStart(
391 $this->pageId4,
392 $this->talk_namespace,
393 $this->pageTitle4->getPrefixedText()
394 );
395 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
396 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
397 $this->assertPageEnd();
398
399 $this->assertDumpEnd();
400
401 // Checking articles -------------------------------------------------
402
403 $this->gunzip( $fnameArticles );
404 $this->assertDumpStart( $fnameArticles );
405
406 // Page 1
407 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
408 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
409 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
410 $this->assertPageEnd();
411
412 // Page 2
413 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
414 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
415 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
416 $this->assertPageEnd();
417
418 // Page 3
419 // -> Page is marked deleted. Hence not visible
420
421 // Page 4
422 // -> Page is not in $this->namespace. Hence not visible
423
424 $this->assertDumpEnd();
425
426 $this->expectETAOutput();
427 }
428 }