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