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