summaryrefslogtreecommitdiff
path: root/tests/phpunit/maintenance/backupPrefetchTest.php
blob: 5e0fe89d542fe46d61d6df5b9df900d9815b0773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php

require_once __DIR__ . "/../../../maintenance/backupPrefetch.inc";

/**
 * Tests for BaseDump
 *
 * @group Dump
 * @covers BaseDump
 */
class BaseDumpTest extends MediaWikiTestCase {

	/**
	 * @var BaseDump The BaseDump instance used within a test.
	 *
	 * If set, this BaseDump gets automatically closed in tearDown.
	 */
	private $dump = null;

	protected function tearDown() {
		if ( $this->dump !== null ) {
			$this->dump->close();
		}

		// Bug 37458, parent teardown need to be done after closing the
		// dump or it might cause some permissions errors.
		parent::tearDown();
	}

	/**
	 * asserts that a prefetch yields an expected string
	 *
	 * @param string|null $expected The exepcted result of the prefetch
	 * @param int $page The page number to prefetch the text for
	 * @param int $revision The revision number to prefetch the text for
	 */
	private function assertPrefetchEquals( $expected, $page, $revision ) {
		$this->assertEquals( $expected, $this->dump->prefetch( $page, $revision ),
			"Prefetch of page $page revision $revision" );
	}

	function testSequential() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
		$this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
		$this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
	}

	function testSynchronizeRevisionMissToRevision() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
		$this->assertPrefetchEquals( null, 2, 3 );
		$this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
	}

	function testSynchronizeRevisionMissToPage() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
		$this->assertPrefetchEquals( null, 2, 40 );
		$this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
	}

	function testSynchronizePageMiss() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
		$this->assertPrefetchEquals( null, 3, 40 );
		$this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
	}

	function testPageMissAtEnd() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
		$this->assertPrefetchEquals( null, 6, 40 );
	}

	function testRevisionMissAtEnd() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
		$this->assertPrefetchEquals( null, 4, 40 );
	}

	function testSynchronizePageMissAtStart() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( null, 0, 2 );
		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
	}

	function testSynchronizeRevisionMissAtStart() {
		$fname = $this->setUpPrefetch();
		$this->dump = new BaseDump( $fname );

		$this->assertPrefetchEquals( null, 1, -2 );
		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
	}

	function testSequentialAcrossFiles() {
		$fname1 = $this->setUpPrefetch( array( 1 ) );
		$fname2 = $this->setUpPrefetch( array( 2, 4 ) );
		$this->dump = new BaseDump( $fname1 . ";" . $fname2 );

		$this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
		$this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
		$this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
	}

	function testSynchronizeSkipAcrossFile() {
		$fname1 = $this->setUpPrefetch( array( 1 ) );
		$fname2 = $this->setUpPrefetch( array( 2 ) );
		$fname3 = $this->setUpPrefetch( array( 4 ) );
		$this->dump = new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );

		$this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
		$this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
	}

	function testSynchronizeMissInWholeFirstFile() {
		$fname1 = $this->setUpPrefetch( array( 1 ) );
		$fname2 = $this->setUpPrefetch( array( 2 ) );
		$this->dump = new BaseDump( $fname1 . ";" . $fname2 );

		$this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
	}

	/**
	 * Constructs a temporary file that can be used for prefetching
	 *
	 * The temporary file is removed by DumpBackup upon tearDown.
	 *
	 * @param array $requested_pages The indices of the page parts that should
	 *    go into the prefetch file. 1,2,4 are available.
	 * @return string The file name of the created temporary file
	 */
	private function setUpPrefetch( $requested_pages = array( 1, 2, 4 ) ) {
		// The file name, where we store the prepared prefetch file
		$fname = $this->getNewTempFile();

		// The header of every prefetch file
		// @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
		$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">
  <siteinfo>
    <sitename>wikisvn</sitename>
    <base>http://localhost/wiki-svn/index.php/Main_Page</base>
    <generator>MediaWiki 1.21alpha</generator>
    <case>first-letter</case>
    <namespaces>
      <namespace key="-2" case="first-letter">Media</namespace>
      <namespace key="-1" case="first-letter">Special</namespace>
      <namespace key="0" case="first-letter" />
      <namespace key="1" case="first-letter">Talk</namespace>
      <namespace key="2" case="first-letter">User</namespace>
      <namespace key="3" case="first-letter">User talk</namespace>
      <namespace key="4" case="first-letter">Wikisvn</namespace>
      <namespace key="5" case="first-letter">Wikisvn talk</namespace>
      <namespace key="6" case="first-letter">File</namespace>
      <namespace key="7" case="first-letter">File talk</namespace>
      <namespace key="8" case="first-letter">MediaWiki</namespace>
      <namespace key="9" case="first-letter">MediaWiki talk</namespace>
      <namespace key="10" case="first-letter">Template</namespace>
      <namespace key="11" case="first-letter">Template talk</namespace>
      <namespace key="12" case="first-letter">Help</namespace>
      <namespace key="13" case="first-letter">Help talk</namespace>
      <namespace key="14" case="first-letter">Category</namespace>
      <namespace key="15" case="first-letter">Category talk</namespace>
    </namespaces>
  </siteinfo>
';
		// @codingStandardsIgnoreEnd

		// An array holding the pages that are available for prefetch
		$available_pages = array();

		// Simple plain page
		$available_pages[1] = '  <page>
    <title>BackupDumperTestP1</title>
    <ns>0</ns>
    <id>1</id>
    <revision>
      <id>1</id>
      <timestamp>2012-04-01T16:46:05Z</timestamp>
      <contributor>
        <ip>127.0.0.1</ip>
      </contributor>
      <comment>BackupDumperTestP1Summary1</comment>
      <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
      <text xml:space="preserve">BackupDumperTestP1Text1</text>
      <model name="wikitext">1</model>
      <format mime="text/x-wiki">1</format>
    </revision>
  </page>
';
		// Page with more than one revisions. Hole in rev ids.
		$available_pages[2] = '  <page>
    <title>BackupDumperTestP2</title>
    <ns>0</ns>
    <id>2</id>
    <revision>
      <id>2</id>
      <timestamp>2012-04-01T16:46:05Z</timestamp>
      <contributor>
        <ip>127.0.0.1</ip>
      </contributor>
      <comment>BackupDumperTestP2Summary1</comment>
      <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
      <text xml:space="preserve">BackupDumperTestP2Text1</text>
      <model name="wikitext">1</model>
      <format mime="text/x-wiki">1</format>
    </revision>
    <revision>
      <id>5</id>
      <parentid>2</parentid>
      <timestamp>2012-04-01T16:46:05Z</timestamp>
      <contributor>
        <ip>127.0.0.1</ip>
      </contributor>
      <comment>BackupDumperTestP2Summary4 extra</comment>
      <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
      <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
      <model name="wikitext">1</model>
      <format mime="text/x-wiki">1</format>
    </revision>
  </page>
';
		// Page with id higher than previous id + 1
		$available_pages[4] = '  <page>
    <title>Talk:BackupDumperTestP1</title>
    <ns>1</ns>
    <id>4</id>
    <revision>
      <id>8</id>
      <timestamp>2012-04-01T16:46:05Z</timestamp>
      <contributor>
        <ip>127.0.0.1</ip>
      </contributor>
      <comment>Talk BackupDumperTestP1 Summary1</comment>
      <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
      <model name="wikitext">1</model>
      <format mime="text/x-wiki">1</format>
      <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
    </revision>
  </page>
';

		// The common ending for all files
		$tail = '</mediawiki>
';

		// Putting together the content of the prefetch files
		$content = $header;
		foreach ( $requested_pages as $i ) {
			$this->assertTrue( array_key_exists( $i, $available_pages ),
				"Check for availability of requested page " . $i );
			$content .= $available_pages[$i];
		}
		$content .= $tail;

		$this->assertEquals( strlen( $content ), file_put_contents(
			$fname, $content ), "Length of prepared prefetch" );

		return $fname;
	}
}