Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / BaseDumpTest.php
blob339f2c9746ab4e4cf31f685c069eb0339bda118d
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use BaseDump;
6 use MediaWiki\Revision\SlotRecord;
7 use MediaWikiIntegrationTestCase;
9 /**
10 * @group Dump
11 * @covers \BaseDump
13 class BaseDumpTest extends MediaWikiIntegrationTestCase {
15 /**
16 * @var BaseDump The BaseDump instance used within a test.
18 * If set, this BaseDump gets automatically closed in tearDown.
20 private $dump = null;
22 protected function tearDown(): void {
23 if ( $this->dump !== null ) {
24 $this->dump->close();
27 // T39458, parent teardown need to be done after closing the
28 // dump or it might cause some permissions errors.
29 parent::tearDown();
32 /**
33 * asserts that a prefetch yields an expected string
35 * @param string|null $expected The exepcted result of the prefetch
36 * @param int $page The page number to prefetch the text for
37 * @param int $revision The revision number to prefetch the text for
38 * @param string $slot The role name of the slot to fetch
40 private function assertPrefetchEquals( $expected, $page, $revision, $slot = SlotRecord::MAIN ) {
41 $this->assertEquals( $expected, $this->dump->prefetch( $page, $revision, $slot ),
42 "Prefetch of page $page revision $revision" );
45 public function testSequential() {
46 $fname = $this->setUpPrefetch();
47 $this->dump = new BaseDump( $fname );
49 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
50 $this->assertPrefetchEquals( "BackupDumperTestP1Text1aux", 1, 1, 'aux' );
51 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
52 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
53 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
56 public function testSynchronizeSlotMissToRevision() {
57 $fname = $this->setUpPrefetch();
58 $this->dump = new BaseDump( $fname );
60 $this->assertPrefetchEquals( "BackupDumperTestP1Text1aux", 1, 1, 'aux' );
61 $this->assertPrefetchEquals( null, 1, 1, 'xyzzy' );
62 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
63 $this->assertPrefetchEquals( null, 2, 2, 'aux' );
64 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
67 public function testSynchronizeRevisionMissToRevision() {
68 $fname = $this->setUpPrefetch();
69 $this->dump = new BaseDump( $fname );
71 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
72 $this->assertPrefetchEquals( null, 2, 3 );
73 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
76 public function testSynchronizeRevisionMissToPage() {
77 $fname = $this->setUpPrefetch();
78 $this->dump = new BaseDump( $fname );
80 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
81 $this->assertPrefetchEquals( null, 2, 40 );
82 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
85 public function testSynchronizePageMiss() {
86 $fname = $this->setUpPrefetch();
87 $this->dump = new BaseDump( $fname );
89 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
90 $this->assertPrefetchEquals( null, 3, 40 );
91 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
94 public function testPageMissAtEnd() {
95 $fname = $this->setUpPrefetch();
96 $this->dump = new BaseDump( $fname );
98 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
99 $this->assertPrefetchEquals( null, 6, 40 );
102 public function testRevisionMissAtEnd() {
103 $fname = $this->setUpPrefetch();
104 $this->dump = new BaseDump( $fname );
106 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
107 $this->assertPrefetchEquals( null, 4, 40 );
110 public function testSynchronizePageMissAtStart() {
111 $fname = $this->setUpPrefetch();
112 $this->dump = new BaseDump( $fname );
114 $this->assertPrefetchEquals( null, 0, 2 );
115 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
118 public function testSynchronizeRevisionMissAtStart() {
119 $fname = $this->setUpPrefetch();
120 $this->dump = new BaseDump( $fname );
122 $this->assertPrefetchEquals( null, 1, -2 );
123 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
126 public function testSequentialAcrossFiles() {
127 $fname1 = $this->setUpPrefetch( [ 1 ] );
128 $fname2 = $this->setUpPrefetch( [ 2, 4 ] );
129 $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
131 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
132 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
133 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
134 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
137 public function testSynchronizeSkipAcrossFile() {
138 $fname1 = $this->setUpPrefetch( [ 1 ] );
139 $fname2 = $this->setUpPrefetch( [ 2 ] );
140 $fname3 = $this->setUpPrefetch( [ 4 ] );
141 $this->dump = new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
143 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
144 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
147 public function testSynchronizeMissInWholeFirstFile() {
148 $fname1 = $this->setUpPrefetch( [ 1 ] );
149 $fname2 = $this->setUpPrefetch( [ 2 ] );
150 $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
152 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
156 * Constructs a temporary file that can be used for prefetching
158 * The temporary file is removed by DumpBackup upon tearDown.
160 * @param array $requested_pages The indices of the page parts that should
161 * go into the prefetch file. 1,2,4 are available.
162 * @return string The file name of the created temporary file
164 private function setUpPrefetch( $requested_pages = [ 1, 2, 4 ] ) {
165 // The file name, where we store the prepared prefetch file
166 $fname = $this->getNewTempFile();
168 // The header of every prefetch file
169 $header = '<mediawiki>
170 <siteinfo>
171 <sitename>wikisvn</sitename>
172 <base>http://localhost/wiki-svn/index.php/Main_Page</base>
173 <generator>MediaWiki 1.21alpha</generator>
174 <case>first-letter</case>
175 <namespaces>
176 <namespace key="-2" case="first-letter">Media</namespace>
177 <namespace key="-1" case="first-letter">Special</namespace>
178 <namespace key="0" case="first-letter" />
179 <namespace key="1" case="first-letter">Talk</namespace>
180 <namespace key="2" case="first-letter">User</namespace>
181 <namespace key="3" case="first-letter">User talk</namespace>
182 <namespace key="4" case="first-letter">Wikisvn</namespace>
183 <namespace key="5" case="first-letter">Wikisvn talk</namespace>
184 <namespace key="6" case="first-letter">File</namespace>
185 <namespace key="7" case="first-letter">File talk</namespace>
186 <namespace key="8" case="first-letter">MediaWiki</namespace>
187 <namespace key="9" case="first-letter">MediaWiki talk</namespace>
188 <namespace key="10" case="first-letter">Template</namespace>
189 <namespace key="11" case="first-letter">Template talk</namespace>
190 <namespace key="12" case="first-letter">Help</namespace>
191 <namespace key="13" case="first-letter">Help talk</namespace>
192 <namespace key="14" case="first-letter">Category</namespace>
193 <namespace key="15" case="first-letter">Category talk</namespace>
194 </namespaces>
195 </siteinfo>
198 // An array holding the pages that are available for prefetch
199 $available_pages = [];
201 // Simple plain page
202 $available_pages[1] = ' <page>
203 <title>BackupDumperTestP1</title>
204 <ns>0</ns>
205 <id>1</id>
206 <revision>
207 <id>1</id>
208 <timestamp>2012-04-01T16:46:05Z</timestamp>
209 <contributor>
210 <ip>127.0.0.1</ip>
211 </contributor>
212 <comment>BackupDumperTestP1Summary1</comment>
213 <text xml:space="preserve">BackupDumperTestP1Text1</text>
214 <model name="wikitext">1</model>
215 <format mime="text/x-wiki">1</format>
216 <content>
217 <role>aux</role>
218 <origin>2</origin>
219 <model>wikitext</model>
220 <format>text/x-wiki</format>
221 <text sha1="deadbeef" xml:space="preserve">BackupDumperTestP1Text1aux</text>
222 </content>
223 <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
224 </revision>
225 </page>
227 // Page with more than one revisions. Hole in rev ids.
228 $available_pages[2] = ' <page>
229 <title>BackupDumperTestP2</title>
230 <ns>0</ns>
231 <id>2</id>
232 <revision>
233 <id>2</id>
234 <timestamp>2012-04-01T16:46:05Z</timestamp>
235 <contributor>
236 <ip>127.0.0.1</ip>
237 </contributor>
238 <comment>BackupDumperTestP2Summary1</comment>
239 <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
240 <text xml:space="preserve">BackupDumperTestP2Text1</text>
241 <model name="wikitext">1</model>
242 <format mime="text/x-wiki">1</format>
243 </revision>
244 <revision>
245 <id>5</id>
246 <parentid>2</parentid>
247 <timestamp>2012-04-01T16:46:05Z</timestamp>
248 <contributor>
249 <ip>127.0.0.1</ip>
250 </contributor>
251 <comment>BackupDumperTestP2Summary4 extra</comment>
252 <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
253 <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
254 <model name="wikitext">1</model>
255 <format mime="text/x-wiki">1</format>
256 </revision>
257 </page>
259 // Page with id higher than previous id + 1
260 $available_pages[4] = ' <page>
261 <title>Talk:BackupDumperTestP1</title>
262 <ns>1</ns>
263 <id>4</id>
264 <revision>
265 <id>8</id>
266 <timestamp>2012-04-01T16:46:05Z</timestamp>
267 <contributor>
268 <ip>127.0.0.1</ip>
269 </contributor>
270 <comment>Talk BackupDumperTestP1 Summary1</comment>
271 <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
272 <model name="wikitext">1</model>
273 <format mime="text/x-wiki">1</format>
274 <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
275 </revision>
276 </page>
279 // The common ending for all files
280 $tail = '</mediawiki>
283 // Putting together the content of the prefetch files
284 $content = $header;
285 foreach ( $requested_pages as $i ) {
286 $this->assertArrayHasKey( $i, $available_pages,
287 "Check for availability of requested page " . $i );
288 $content .= $available_pages[$i];
290 $content .= $tail;
292 $this->assertEquals( strlen( $content ), file_put_contents(
293 $fname, $content ), "Length of prepared prefetch" );
295 return $fname;