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