Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / tests / phpunit / maintenance / backupPrefetchTest.php
1 <?php
2
3 require_once dirname( __FILE__ ) . "/../../../maintenance/backupPrefetch.inc";
4
5 /**
6 * Tests for BaseDump
7 *
8 * @group Dump
9 */
10 class BaseDumpTest extends MediaWikiTestCase {
11
12 /**
13 * @var BaseDump the BaseDump instance used within a test.
14 *
15 * If set, this BaseDump gets automatically closed in tearDown.
16 */
17 private $dump = null;
18
19 protected function tearDown() {
20 parent::tearDown();
21
22 if ( $this->dump !== null ) {
23 $this->dump->close();
24 }
25 }
26
27 /**
28 * asserts that a prefetch yields an expected string
29 *
30 * @param $expected string|null: the exepcted result of the prefetch
31 * @param $page int: the page number to prefetch the text for
32 * @param $revision int: the revision number to prefetch the text for
33 */
34 private function assertPrefetchEquals( $expected, $page, $revision ) {
35 $this->assertEquals( $expected, $this->dump->prefetch( $page, $revision ),
36 "Prefetch of page $page revision $revision" );
37
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( array( 1 ) );
111 $fname2 = $this->setUpPrefetch( array( 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( array( 1 ) );
122 $fname2 = $this->setUpPrefetch( array( 2 ) );
123 $fname3 = $this->setUpPrefetch( array( 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( array( 1 ) );
132 $fname2 = $this->setUpPrefetch( array( 2 ) );
133 $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
134
135 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
136 }
137
138
139 /**
140 * Constructs a temporary file that can be used for prefetching
141 *
142 * The temporary file is removed by DumpBackup upon tearDown.
143 *
144 * @param $requested_pages Array The indices of the page parts that should
145 * go into the prefetch file. 1,2,4 are available.
146 * @return String The file name of the created temporary file
147 */
148 private function setUpPrefetch( $requested_pages = array( 1, 2, 4 ) ) {
149 // The file name, where we store the prepared prefetch file
150 $fname = $this->getNewTempFile();
151
152 // The header of every prefetch file
153 $header = '<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.6/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.6/ http://www.mediawiki.org/xml/export-0.6.xsd" version="0.6" xml:lang="en">
154 <siteinfo>
155 <sitename>wikisvn</sitename>
156 <base>http://localhost/wiki-svn/index.php/Main_Page</base>
157 <generator>MediaWiki 1.20alpha</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
183 // An array holding the pages that are available for prefetch
184 $available_pages = array();
185
186 // Simple plain page
187 $available_pages[1] = ' <page>
188 <title>BackupDumperTestP1</title>
189 <ns>0</ns>
190 <id>1</id>
191 <revision>
192 <id>1</id>
193 <timestamp>2012-04-01T16:46:05Z</timestamp>
194 <contributor>
195 <ip>127.0.0.1</ip>
196 </contributor>
197 <comment>BackupDumperTestP1Summary1</comment>
198 <text xml:space="preserve">BackupDumperTestP1Text1</text>
199 <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
200 <model name="wikitext">1</model>
201 <format mime="text/x-wiki">1</format>
202 </revision>
203 </page>
204 ';
205 // Page with more than one revisions. Hole in rev ids.
206 $available_pages[2] = ' <page>
207 <title>BackupDumperTestP2</title>
208 <ns>0</ns>
209 <id>2</id>
210 <revision>
211 <id>2</id>
212 <timestamp>2012-04-01T16:46:05Z</timestamp>
213 <contributor>
214 <ip>127.0.0.1</ip>
215 </contributor>
216 <comment>BackupDumperTestP2Summary1</comment>
217 <text xml:space="preserve">BackupDumperTestP2Text1</text>
218 <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
219 <model name="wikitext">1</model>
220 <format mime="text/x-wiki">1</format>
221 </revision>
222 <revision>
223 <id>5</id>
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 <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
230 <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
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 <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
249 <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
250 <model name="wikitext">1</model>
251 <format mime="text/x-wiki">1</format>
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
275 }