Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / maintenance / backupPrefetchTest.php
blob5e0fe89d542fe46d61d6df5b9df900d9815b0773
1 <?php
3 require_once __DIR__ . "/../../../maintenance/backupPrefetch.inc";
5 /**
6 * Tests for BaseDump
8 * @group Dump
9 * @covers BaseDump
11 class BaseDumpTest extends MediaWikiTestCase {
13 /**
14 * @var BaseDump The BaseDump instance used within a test.
16 * If set, this BaseDump gets automatically closed in tearDown.
18 private $dump = null;
20 protected function tearDown() {
21 if ( $this->dump !== null ) {
22 $this->dump->close();
25 // Bug 37458, parent teardown need to be done after closing the
26 // dump or it might cause some permissions errors.
27 parent::tearDown();
30 /**
31 * asserts that a prefetch yields an expected string
33 * @param string|null $expected The exepcted result of the prefetch
34 * @param int $page The page number to prefetch the text for
35 * @param int $revision The revision number to prefetch the text for
37 private function assertPrefetchEquals( $expected, $page, $revision ) {
38 $this->assertEquals( $expected, $this->dump->prefetch( $page, $revision ),
39 "Prefetch of page $page revision $revision" );
42 function testSequential() {
43 $fname = $this->setUpPrefetch();
44 $this->dump = new BaseDump( $fname );
46 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
47 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
48 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
49 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
52 function testSynchronizeRevisionMissToRevision() {
53 $fname = $this->setUpPrefetch();
54 $this->dump = new BaseDump( $fname );
56 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
57 $this->assertPrefetchEquals( null, 2, 3 );
58 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
61 function testSynchronizeRevisionMissToPage() {
62 $fname = $this->setUpPrefetch();
63 $this->dump = new BaseDump( $fname );
65 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
66 $this->assertPrefetchEquals( null, 2, 40 );
67 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
70 function testSynchronizePageMiss() {
71 $fname = $this->setUpPrefetch();
72 $this->dump = new BaseDump( $fname );
74 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
75 $this->assertPrefetchEquals( null, 3, 40 );
76 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
79 function testPageMissAtEnd() {
80 $fname = $this->setUpPrefetch();
81 $this->dump = new BaseDump( $fname );
83 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
84 $this->assertPrefetchEquals( null, 6, 40 );
87 function testRevisionMissAtEnd() {
88 $fname = $this->setUpPrefetch();
89 $this->dump = new BaseDump( $fname );
91 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
92 $this->assertPrefetchEquals( null, 4, 40 );
95 function testSynchronizePageMissAtStart() {
96 $fname = $this->setUpPrefetch();
97 $this->dump = new BaseDump( $fname );
99 $this->assertPrefetchEquals( null, 0, 2 );
100 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
103 function testSynchronizeRevisionMissAtStart() {
104 $fname = $this->setUpPrefetch();
105 $this->dump = new BaseDump( $fname );
107 $this->assertPrefetchEquals( null, 1, -2 );
108 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
111 function testSequentialAcrossFiles() {
112 $fname1 = $this->setUpPrefetch( array( 1 ) );
113 $fname2 = $this->setUpPrefetch( array( 2, 4 ) );
114 $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
116 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
117 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
118 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
119 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
122 function testSynchronizeSkipAcrossFile() {
123 $fname1 = $this->setUpPrefetch( array( 1 ) );
124 $fname2 = $this->setUpPrefetch( array( 2 ) );
125 $fname3 = $this->setUpPrefetch( array( 4 ) );
126 $this->dump = new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
128 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
129 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
132 function testSynchronizeMissInWholeFirstFile() {
133 $fname1 = $this->setUpPrefetch( array( 1 ) );
134 $fname2 = $this->setUpPrefetch( array( 2 ) );
135 $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
137 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
141 * Constructs a temporary file that can be used for prefetching
143 * The temporary file is removed by DumpBackup upon tearDown.
145 * @param array $requested_pages The indices of the page parts that should
146 * go into the prefetch file. 1,2,4 are available.
147 * @return string The file name of the created temporary file
149 private function setUpPrefetch( $requested_pages = array( 1, 2, 4 ) ) {
150 // The file name, where we store the prepared prefetch file
151 $fname = $this->getNewTempFile();
153 // The header of every prefetch file
154 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
155 $header = '<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.7/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.7/ http://www.mediawiki.org/xml/export-0.7.xsd" version="0.7" xml:lang="en">
156 <siteinfo>
157 <sitename>wikisvn</sitename>
158 <base>http://localhost/wiki-svn/index.php/Main_Page</base>
159 <generator>MediaWiki 1.21alpha</generator>
160 <case>first-letter</case>
161 <namespaces>
162 <namespace key="-2" case="first-letter">Media</namespace>
163 <namespace key="-1" case="first-letter">Special</namespace>
164 <namespace key="0" case="first-letter" />
165 <namespace key="1" case="first-letter">Talk</namespace>
166 <namespace key="2" case="first-letter">User</namespace>
167 <namespace key="3" case="first-letter">User talk</namespace>
168 <namespace key="4" case="first-letter">Wikisvn</namespace>
169 <namespace key="5" case="first-letter">Wikisvn talk</namespace>
170 <namespace key="6" case="first-letter">File</namespace>
171 <namespace key="7" case="first-letter">File talk</namespace>
172 <namespace key="8" case="first-letter">MediaWiki</namespace>
173 <namespace key="9" case="first-letter">MediaWiki talk</namespace>
174 <namespace key="10" case="first-letter">Template</namespace>
175 <namespace key="11" case="first-letter">Template talk</namespace>
176 <namespace key="12" case="first-letter">Help</namespace>
177 <namespace key="13" case="first-letter">Help talk</namespace>
178 <namespace key="14" case="first-letter">Category</namespace>
179 <namespace key="15" case="first-letter">Category talk</namespace>
180 </namespaces>
181 </siteinfo>
183 // @codingStandardsIgnoreEnd
185 // An array holding the pages that are available for prefetch
186 $available_pages = array();
188 // Simple plain page
189 $available_pages[1] = ' <page>
190 <title>BackupDumperTestP1</title>
191 <ns>0</ns>
192 <id>1</id>
193 <revision>
194 <id>1</id>
195 <timestamp>2012-04-01T16:46:05Z</timestamp>
196 <contributor>
197 <ip>127.0.0.1</ip>
198 </contributor>
199 <comment>BackupDumperTestP1Summary1</comment>
200 <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
201 <text xml:space="preserve">BackupDumperTestP1Text1</text>
202 <model name="wikitext">1</model>
203 <format mime="text/x-wiki">1</format>
204 </revision>
205 </page>
207 // Page with more than one revisions. Hole in rev ids.
208 $available_pages[2] = ' <page>
209 <title>BackupDumperTestP2</title>
210 <ns>0</ns>
211 <id>2</id>
212 <revision>
213 <id>2</id>
214 <timestamp>2012-04-01T16:46:05Z</timestamp>
215 <contributor>
216 <ip>127.0.0.1</ip>
217 </contributor>
218 <comment>BackupDumperTestP2Summary1</comment>
219 <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
220 <text xml:space="preserve">BackupDumperTestP2Text1</text>
221 <model name="wikitext">1</model>
222 <format mime="text/x-wiki">1</format>
223 </revision>
224 <revision>
225 <id>5</id>
226 <parentid>2</parentid>
227 <timestamp>2012-04-01T16:46:05Z</timestamp>
228 <contributor>
229 <ip>127.0.0.1</ip>
230 </contributor>
231 <comment>BackupDumperTestP2Summary4 extra</comment>
232 <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
233 <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
234 <model name="wikitext">1</model>
235 <format mime="text/x-wiki">1</format>
236 </revision>
237 </page>
239 // Page with id higher than previous id + 1
240 $available_pages[4] = ' <page>
241 <title>Talk:BackupDumperTestP1</title>
242 <ns>1</ns>
243 <id>4</id>
244 <revision>
245 <id>8</id>
246 <timestamp>2012-04-01T16:46:05Z</timestamp>
247 <contributor>
248 <ip>127.0.0.1</ip>
249 </contributor>
250 <comment>Talk BackupDumperTestP1 Summary1</comment>
251 <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
252 <model name="wikitext">1</model>
253 <format mime="text/x-wiki">1</format>
254 <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
255 </revision>
256 </page>
259 // The common ending for all files
260 $tail = '</mediawiki>
263 // Putting together the content of the prefetch files
264 $content = $header;
265 foreach ( $requested_pages as $i ) {
266 $this->assertTrue( array_key_exists( $i, $available_pages ),
267 "Check for availability of requested page " . $i );
268 $content .= $available_pages[$i];
270 $content .= $tail;
272 $this->assertEquals( strlen( $content ), file_put_contents(
273 $fname, $content ), "Length of prepared prefetch" );
275 return $fname;