Merge "preprocessDump.php: Fix invalid Preprocessor instantiation"
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / LocalRepoTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @coversDefaultClass LocalRepo
7 * @group Database
8 */
9 class LocalRepoTest extends MediaWikiIntegrationTestCase {
10 /**
11 * @param array $extraInfo To pass to LocalRepo constructor
12 */
13 private function newRepo( array $extraInfo = [] ) {
14 return new LocalRepo( $extraInfo + [
15 'name' => 'local',
16 'backend' => 'local-backend',
17 ] );
18 }
19
20 /**
21 * @param array $extraInfo To pass to constructor
22 * @param bool $expected
23 * @dataProvider provideHasSha1Storage
24 * @covers ::__construct
25 */
26 public function testHasSha1Storage( array $extraInfo, $expected ) {
27 $this->assertSame( $expected, $this->newRepo( $extraInfo )->hasSha1Storage() );
28 }
29
30 public static function provideHasSha1Storage() {
31 return [
32 [ [], false ],
33 [ [ 'storageLayout' => 'sha256' ], false ],
34 [ [ 'storageLayout' => 'sha1' ], true ],
35 ];
36 }
37
38 /**
39 * @param string $prefix 'img' or 'oi'
40 * @param string $expectedClass 'LocalFile' or 'OldLocalFile'
41 * @dataProvider provideNewFileFromRow
42 * @covers ::newFileFromRow
43 */
44 public function testNewFileFromRow( $prefix, $expectedClass ) {
45 $this->editPage( 'File:Test_file', 'Some description' );
46
47 $row = (object)[
48 "{$prefix}_name" => 'Test_file',
49 // We cheat and include this for img_ too, it will be ignored
50 "{$prefix}_archive_name" => 'Archive_name',
51 "{$prefix}_user" => '1',
52 "{$prefix}_timestamp" => '12345678910111',
53 "{$prefix}_metadata" => '',
54 "{$prefix}_sha1" => sha1( '' ),
55 "{$prefix}_size" => '0',
56 "{$prefix}_height" => '0',
57 "{$prefix}_width" => '0',
58 "{$prefix}_bits" => '0',
59 "{$prefix}_description_text" => '',
60 "{$prefix}_description_data" => null,
61 ];
62 $file = $this->newRepo()->newFileFromRow( $row );
63 $this->assertInstanceOf( $expectedClass, $file );
64 $this->assertSame( 'Test_file', $file->getName() );
65 $this->assertSame( 1, $file->getUser( 'id' ) );
66 }
67
68 public static function provideNewFileFromRow() {
69 return [
70 'img' => [ 'img', LocalFile::class ],
71 'oi' => [ 'oi', OldLocalFile::class ],
72 ];
73 }
74
75 /**
76 * @covers ::__construct
77 * @covers ::newFileFromRow
78 */
79 public function testNewFileFromRow_invalid() {
80 $this->setExpectedException( 'MWException', 'LocalRepo::newFileFromRow: invalid row' );
81
82 $row = (object)[
83 "img_user" => '1',
84 "img_timestamp" => '12345678910111',
85 "img_metadata" => '',
86 "img_sha1" => sha1( '' ),
87 "img_size" => '0',
88 "img_height" => '0',
89 "img_width" => '0',
90 "img_bits" => '0',
91 ];
92 $file = $this->newRepo()->newFileFromRow( $row );
93 }
94
95 /**
96 * @covers ::__construct
97 * @covers ::newFromArchiveName
98 */
99 public function testNewFromArchiveName() {
100 $this->editPage( 'File:Test_file', 'Some description' );
101
102 $file = $this->newRepo()->newFromArchiveName( 'Test_file', 'b' );
103 $this->assertInstanceOf( OldLocalFile::class, $file );
104 $this->assertSame( 'Test_file', $file->getName() );
105 }
106
107 // TODO cleanupDeletedBatch, deletedFileHasKey, hiddenFileHasKey
108
109 /**
110 * @covers ::__construct
111 * @covers ::cleanupDeletedBatch
112 */
113 public function testCleanupDeletedBatch_sha1Storage() {
114 $this->assertEquals( Status::newGood(),
115 $this->newRepo( [ 'storageLayout' => 'sha1' ] )->cleanupDeletedBatch( [] ) );
116 }
117
118 /**
119 * @param string $input
120 * @param string $expected
121 * @dataProvider provideGetHashFromKey
122 * @covers ::getHashFromKey
123 */
124 public function testGetHashFromKey( $input, $expected ) {
125 $this->assertSame( $expected, LocalRepo::getHashFromKey( $input ) );
126 }
127
128 public static function provideGetHashFromKey() {
129 return [
130 [ '', false ],
131 [ '.', false ],
132 [ 'a.', 'a' ],
133 [ '.b', 'b' ],
134 [ '..c', 'c' ],
135 [ 'd.x', 'd' ],
136 [ '.e.x', 'e' ],
137 [ '..f.x', 'f' ],
138 [ 'g..x', 'g' ],
139 [ '01234567890123456789012345678901.x', '1234567890123456789012345678901' ],
140 ];
141 }
142
143 /**
144 * @covers ::__construct
145 * @covers ::checkRedirect
146 */
147 public function testCheckRedirect_nonRedirect() {
148 $this->editPage( 'File:Not a redirect', 'Not a redirect' );
149 $this->assertFalse(
150 $this->newRepo()->checkRedirect( Title::makeTitle( NS_FILE, 'Not a redirect' ) ) );
151 }
152
153 /**
154 * @covers ::__construct
155 * @covers ::checkRedirect
156 * @covers ::getSharedCacheKey
157 */
158 public function testCheckRedirect_redirect() {
159 $this->editPage( 'File:Redirect', '#REDIRECT [[File:Target]]' );
160 $this->assertEquals( 'File:Target',
161 $this->newRepo()->checkRedirect( Title::makeTitle( NS_FILE, 'Redirect' ) )
162 ->getPrefixedText() );
163 }
164
165 /**
166 * @covers ::__construct
167 * @covers ::checkRedirect
168 * @covers ::getSharedCacheKey
169 * @covers ::getLocalCacheKey
170 */
171 public function testCheckRedirect_redirect_noWANCache() {
172 $this->markTestIncomplete( 'WANObjectCache::makeKey is final' );
173
174 $mockWan = $this->getMockBuilder( WANObjectCache::class )
175 ->setConstructorArgs( [ [ 'cache' => new EmptyBagOStuff ] ] )
176 ->setMethods( [ 'makeKey' ] )
177 ->getMock();
178 $mockWan->expects( $this->exactly( 2 ) )->method( 'makeKey' )->withConsecutive(
179 [ 'image_redirect', md5( 'Redirect' ) ],
180 [ 'filerepo', 'local', 'image_redirect', md5( 'Redirect' ) ]
181 )->will( $this->onConsecutiveCalls( false, 'somekey' ) );
182
183 $repo = $this->newRepo( [ 'wanCache' => $mockWan ] );
184
185 $this->editPage( 'File:Redirect', '#REDIRECT [[File:Target]]' );
186 $this->assertEquals( 'File:Target',
187 $repo->checkRedirect( Title::makeTitle( NS_FILE, 'Redirect' ) )->getPrefixedText() );
188 }
189
190 /**
191 * @covers ::__construct
192 * @covers ::checkRedirect
193 */
194 public function testCheckRedirect_invalidFile() {
195 $this->setExpectedException( MWException::class, '`Notafile` is not a valid file title.' );
196 $this->newRepo()->checkRedirect( Title::makeTitle( NS_MAIN, 'Notafile' ) );
197 }
198
199 /**
200 * @covers ::__construct
201 * @covers ::findBySha1
202 */
203 public function testFindBySha1() {
204 $this->markTestIncomplete( "Haven't figured out how to upload files yet" );
205
206 $repo = $this->newRepo();
207
208 $tmpFileFactory = MediaWikiServices::getInstance()->getTempFSFileFactory();
209 foreach ( [ 'File1', 'File2', 'File3' ] as $name ) {
210 $fsFile = $tmpFileFactory->newTempFSFile( '' );
211 file_put_contents( $fsFile->getPath(), "$name contents" );
212 $localFile = $repo->newFile( $name );
213 $localFile->upload( $fsFile, 'Uploaded', "$name desc" );
214 }
215 }
216
217 /**
218 * @covers ::__construct
219 * @covers ::getSharedCacheKey
220 * @covers ::checkRedirect
221 * @covers ::invalidateImageRedirect
222 */
223 public function testInvalidateImageRedirect() {
224 global $wgTestMe;
225 $wgTestMe = true;
226 $repo = $this->newRepo(
227 [ 'wanCache' => new WANObjectCache( [ 'cache' => new HashBagOStuff ] ) ] );
228
229 $title = Title::makeTitle( NS_FILE, 'Redirect' );
230
231 $this->editPage( 'File:Redirect', '#REDIRECT [[File:Target]]' );
232
233 $this->assertSame( 'File:Target',
234 $repo->checkRedirect( $title )->getPrefixedText() );
235
236 $this->editPage( 'File:Redirect', 'No longer a redirect' );
237
238 $this->assertSame( 'File:Target',
239 $repo->checkRedirect( $title )->getPrefixedText() );
240
241 $repo->invalidateImageRedirect( $title );
242 $repo->getMasterDB()->commit();
243
244 $this->markTestIncomplete(
245 "Can't figure out how to get image redirect validation to take effect" );
246
247 $this->assertSame( false, $repo->checkRedirect( $title ) );
248 }
249
250 /**
251 * @covers ::getInfo
252 */
253 public function testGetInfo() {
254 $this->setMwGlobals( [
255 'wgFavicon' => '//example.com/favicon.ico',
256 'wgSitename' => 'Test my site',
257 ] );
258
259 $repo = $this->newRepo( [ 'favicon' => 'Hey, this option is ignored in LocalRepo!' ] );
260
261 $this->assertSame( [
262 'name' => 'local',
263 'displayname' => 'Test my site',
264 'rootUrl' => false,
265 'local' => true,
266 'url' => false,
267 'thumbUrl' => false,
268 'initialCapital' => true,
269 // XXX This assumes protocol-relative will get expanded to http instead of https
270 'favicon' => 'http://example.com/favicon.ico',
271 ], $repo->getInfo() );
272 }
273
274 // XXX The following getInfo tests are really testing FileRepo, not LocalRepo, but we want to
275 // make sure they're true for LocalRepo too. How should we do this? A trait?
276
277 /**
278 * @covers ::getInfo
279 */
280 public function testGetInfo_name() {
281 $this->assertSame( 'some-name',
282 $this->newRepo( [ 'name' => 'some-name' ] )->getInfo()['name'] );
283 }
284
285 /**
286 * @covers ::getInfo
287 */
288 public function testGetInfo_displayName() {
289 $this->assertSame( wfMessage( 'shared-repo' )->text(),
290 $this->newRepo( [ 'name' => 'not-local' ] )->getInfo()['displayname'] );
291 }
292
293 /**
294 * @covers ::getInfo
295 */
296 public function testGetInfo_displayNameCustomMsg() {
297 $this->editPage( 'MediaWiki:Shared-repo-name-not-local', 'Name to display please' );
298 // Allow the message to take effect
299 MediaWikiServices::getInstance()->getMessageCache()->enable();
300
301 $this->assertSame( 'Name to display please',
302 $this->newRepo( [ 'name' => 'not-local' ] )->getInfo()['displayname'] );
303 }
304
305 /**
306 * @covers ::getInfo
307 */
308 public function testGetInfo_rootUrl() {
309 $this->assertSame( 'https://my.url',
310 $this->newRepo( [ 'url' => 'https://my.url' ] )->getInfo()['rootUrl'] );
311 }
312
313 /**
314 * @covers ::getInfo
315 */
316 public function testGetInfo_rootUrlCustomized() {
317 $this->assertSame(
318 'https://my.url/some/sub/dir',
319 $this->newRepo( [
320 'url' => 'https://my.url',
321 'zones' => [ 'public' => [ 'url' => 'https://my.url/some/sub/dir' ] ],
322 ] )->getInfo()['rootUrl']
323 );
324 }
325
326 /**
327 * @covers ::getInfo
328 */
329 public function testGetInfo_local() {
330 $this->assertFalse( $this->newRepo( [ 'name' => 'not-local' ] )->getInfo()['local'] );
331 }
332
333 /**
334 * @param string $setting
335 * @dataProvider provideGetInfo_optionalSettings
336 * @covers ::getInfo
337 */
338 public function testGetInfo_optionalSettings( $setting ) {
339 $this->assertSame( 'dummy test value',
340 $this->newRepo( [ $setting => 'dummy test value' ] )->getInfo()[$setting] );
341 }
342
343 public static function provideGetInfo_optionalSettings() {
344 return [
345 [ 'url' ],
346 [ 'thumbUrl' ],
347 [ 'initialCapital' ],
348 [ 'descBaseUrl' ],
349 [ 'scriptDirUrl' ],
350 [ 'articleUrl' ],
351 [ 'fetchDescription' ],
352 [ 'descriptionCacheExpiry' ],
353 ];
354 }
355
356 /**
357 * @param string $method
358 * @param mixed ...$args
359 * @dataProvider provideSkipWriteOperationIfSha1
360 * @covers ::store
361 * @covers ::storeBatch
362 * @covers ::cleanupBatch
363 * @covers ::publish
364 * @covers ::publishBatch
365 * @covers ::delete
366 * @covers ::deleteBatch
367 * @covers ::skipWriteOperationIfSha1
368 */
369 public function testSkipWriteOperationIfSha1( $method, ...$args ) {
370 $repo = $this->newRepo( [ 'storageLayout' => 'sha1' ] );
371 $this->assertEquals( Status::newGood(), $repo->$method( ...$args ) );
372 }
373
374 public static function provideSkipWriteOperationIfSha1() {
375 return [
376 [ 'store', '', '', '' ],
377 [ 'storeBatch', [ '' ] ],
378 [ 'cleanupBatch', [ '' ] ],
379 [ 'publish', '', '', '' ],
380 [ 'publishBatch', [ '' ] ],
381 [ 'delete', '', '' ],
382 [ 'deleteBatch', [ '' ] ],
383 ];
384 }
385 }