test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / maintenance / backupPrefetchTest.php
blobbc2d73758aa24e898ac08cd2f3d7d54485834743
1 <?php
3 require_once __DIR__ . "/../../../maintenance/backupPrefetch.inc";
5 /**
6 * Tests for BaseDump
8 * @group Dump
9 */
10 class BaseDumpTest extends MediaWikiTestCase {
12 /**
13 * @var BaseDump the BaseDump instance used within a test.
15 * If set, this BaseDump gets automatically closed in tearDown.
17 private $dump = null;
19 protected function tearDown() {
20 if ( $this->dump !== null ) {
21 $this->dump->close();
24 // Bug 37458, parent teardown need to be done after closing the
25 // dump or it might cause some permissions errors.
26 parent::tearDown();
29 /**
30 * asserts that a prefetch yields an expected string
32 * @param $expected string|null: the exepcted result of the prefetch
33 * @param $page int: the page number to prefetch the text for
34 * @param $revision int: the revision number to prefetch the text for
36 private function assertPrefetchEquals( $expected, $page, $revision ) {
37 $this->assertEquals( $expected, $this->dump->prefetch( $page, $revision ),
38 "Prefetch of page $page revision $revision" );
41 function testSequential() {
42 $fname = $this->setUpPrefetch();
43 $this->dump = new BaseDump( $fname );
45 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
46 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
47 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
48 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
51 function testSynchronizeRevisionMissToRevision() {
52 $fname = $this->setUpPrefetch();
53 $this->dump = new BaseDump( $fname );
55 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
56 $this->assertPrefetchEquals( null, 2, 3 );
57 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
60 function testSynchronizeRevisionMissToPage() {
61 $fname = $this->setUpPrefetch();
62 $this->dump = new BaseDump( $fname );
64 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
65 $this->assertPrefetchEquals( null, 2, 40 );
66 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
69 function testSynchronizePageMiss() {
70 $fname = $this->setUpPrefetch();
71 $this->dump = new BaseDump( $fname );
73 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
74 $this->assertPrefetchEquals( null, 3, 40 );
75 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
78 function testPageMissAtEnd() {
79 $fname = $this->setUpPrefetch();
80 $this->dump = new BaseDump( $fname );
82 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
83 $this->assertPrefetchEquals( null, 6, 40 );
86 function testRevisionMissAtEnd() {
87 $fname = $this->setUpPrefetch();
88 $this->dump = new BaseDump( $fname );
90 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
91 $this->assertPrefetchEquals( null, 4, 40 );
94 function testSynchronizePageMissAtStart() {
95 $fname = $this->setUpPrefetch();
96 $this->dump = new BaseDump( $fname );
98 $this->assertPrefetchEquals( null, 0, 2 );
99 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
102 function testSynchronizeRevisionMissAtStart() {
103 $fname = $this->setUpPrefetch();
104 $this->dump = new BaseDump( $fname );
106 $this->assertPrefetchEquals( null, 1, -2 );
107 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
110 function testSequentialAcrossFiles() {
111 $fname1 = $this->setUpPrefetch( array( 1 ) );
112 $fname2 = $this->setUpPrefetch( array( 2, 4 ) );
113 $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
115 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
116 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
117 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
118 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
121 function testSynchronizeSkipAcrossFile() {
122 $fname1 = $this->setUpPrefetch( array( 1 ) );
123 $fname2 = $this->setUpPrefetch( array( 2 ) );
124 $fname3 = $this->setUpPrefetch( array( 4 ) );
125 $this->dump = new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
127 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
128 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
131 function testSynchronizeMissInWholeFirstFile() {
132 $fname1 = $this->setUpPrefetch( array( 1 ) );
133 $fname2 = $this->setUpPrefetch( array( 2 ) );
134 $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
136 $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 $requested_pages Array 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 $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">
155 <siteinfo>
156 <sitename>wikisvn</sitename>
157 <base>http://localhost/wiki-svn/index.php/Main_Page</base>
158 <generator>MediaWiki 1.21alpha</generator>
159 <case>first-letter</case>
160 <namespaces>
161 <namespace key="-2" case="first-letter">Media</namespace>
162 <namespace key="-1" case="first-letter">Special</namespace>
163 <namespace key="0" case="first-letter" />
164 <namespace key="1" case="first-letter">Talk</namespace>
165 <namespace key="2" case="first-letter">User</namespace>
166 <namespace key="3" case="first-letter">User talk</namespace>
167 <namespace key="4" case="first-letter">Wikisvn</namespace>
168 <namespace key="5" case="first-letter">Wikisvn talk</namespace>
169 <namespace key="6" case="first-letter">File</namespace>
170 <namespace key="7" case="first-letter">File talk</namespace>
171 <namespace key="8" case="first-letter">MediaWiki</namespace>
172 <namespace key="9" case="first-letter">MediaWiki talk</namespace>
173 <namespace key="10" case="first-letter">Template</namespace>
174 <namespace key="11" case="first-letter">Template talk</namespace>
175 <namespace key="12" case="first-letter">Help</namespace>
176 <namespace key="13" case="first-letter">Help talk</namespace>
177 <namespace key="14" case="first-letter">Category</namespace>
178 <namespace key="15" case="first-letter">Category talk</namespace>
179 </namespaces>
180 </siteinfo>
183 // An array holding the pages that are available for prefetch
184 $available_pages = array();
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 <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
199 <text xml:space="preserve">BackupDumperTestP1Text1</text>
200 <model name="wikitext">1</model>
201 <format mime="text/x-wiki">1</format>
202 </revision>
203 </page>
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 <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
218 <text xml:space="preserve">BackupDumperTestP2Text1</text>
219 <model name="wikitext">1</model>
220 <format mime="text/x-wiki">1</format>
221 </revision>
222 <revision>
223 <id>5</id>
224 <parentid>2</parentid>
225 <timestamp>2012-04-01T16:46:05Z</timestamp>
226 <contributor>
227 <ip>127.0.0.1</ip>
228 </contributor>
229 <comment>BackupDumperTestP2Summary4 extra</comment>
230 <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
231 <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
232 <model name="wikitext">1</model>
233 <format mime="text/x-wiki">1</format>
234 </revision>
235 </page>
237 // Page with id higher than previous id + 1
238 $available_pages[4] = ' <page>
239 <title>Talk:BackupDumperTestP1</title>
240 <ns>1</ns>
241 <id>4</id>
242 <revision>
243 <id>8</id>
244 <timestamp>2012-04-01T16:46:05Z</timestamp>
245 <contributor>
246 <ip>127.0.0.1</ip>
247 </contributor>
248 <comment>Talk BackupDumperTestP1 Summary1</comment>
249 <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
250 <model name="wikitext">1</model>
251 <format mime="text/x-wiki">1</format>
252 <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
253 </revision>
254 </page>
257 // The common ending for all files
258 $tail = '</mediawiki>
261 // Putting together the content of the prefetch files
262 $content = $header;
263 foreach ( $requested_pages as $i ) {
264 $this->assertTrue( array_key_exists( $i, $available_pages ),
265 "Check for availability of requested page " . $i );
266 $content .= $available_pages[$i];
268 $content .= $tail;
270 $this->assertEquals( strlen( $content ), file_put_contents(
271 $fname, $content ), "Length of prepared prefetch" );
273 return $fname;