3 use MediaWiki\MediaWikiServices
;
6 * @coversDefaultClass LocalRepo
9 class LocalRepoTest
extends MediaWikiIntegrationTestCase
{
11 * @param array $extraInfo To pass to LocalRepo constructor
13 private function newRepo( array $extraInfo = [] ) {
14 return new LocalRepo( $extraInfo +
[
16 'backend' => 'local-backend',
21 * @param array $extraInfo To pass to constructor
22 * @param bool $expected
23 * @dataProvider provideHasSha1Storage
24 * @covers ::__construct
26 public function testHasSha1Storage( array $extraInfo, $expected ) {
27 $this->assertSame( $expected, $this->newRepo( $extraInfo )->hasSha1Storage() );
30 public static function provideHasSha1Storage() {
33 [ [ 'storageLayout' => 'sha256' ], false ],
34 [ [ 'storageLayout' => 'sha1' ], true ],
39 * @param string $prefix 'img' or 'oi'
40 * @param string $expectedClass 'LocalFile' or 'OldLocalFile'
41 * @dataProvider provideNewFileFromRow
42 * @covers ::newFileFromRow
44 public function testNewFileFromRow( $prefix, $expectedClass ) {
45 $this->editPage( 'File:Test_file', 'Some description' );
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,
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' ) );
68 public static function provideNewFileFromRow() {
70 'img' => [ 'img', LocalFile
::class ],
71 'oi' => [ 'oi', OldLocalFile
::class ],
76 * @covers ::__construct
77 * @covers ::newFileFromRow
79 public function testNewFileFromRow_invalid() {
80 $this->setExpectedException( 'MWException', 'LocalRepo::newFileFromRow: invalid row' );
84 "img_timestamp" => '12345678910111',
86 "img_sha1" => sha1( '' ),
92 $file = $this->newRepo()->newFileFromRow( $row );
96 * @covers ::__construct
97 * @covers ::newFromArchiveName
99 public function testNewFromArchiveName() {
100 $this->editPage( 'File:Test_file', 'Some description' );
102 $file = $this->newRepo()->newFromArchiveName( 'Test_file', 'b' );
103 $this->assertInstanceOf( OldLocalFile
::class, $file );
104 $this->assertSame( 'Test_file', $file->getName() );
107 // TODO cleanupDeletedBatch, deletedFileHasKey, hiddenFileHasKey
110 * @covers ::__construct
111 * @covers ::cleanupDeletedBatch
113 public function testCleanupDeletedBatch_sha1Storage() {
114 $this->assertEquals( Status
::newGood(),
115 $this->newRepo( [ 'storageLayout' => 'sha1' ] )->cleanupDeletedBatch( [] ) );
119 * @param string $input
120 * @param string $expected
121 * @dataProvider provideGetHashFromKey
122 * @covers ::getHashFromKey
124 public function testGetHashFromKey( $input, $expected ) {
125 $this->assertSame( $expected, LocalRepo
::getHashFromKey( $input ) );
128 public static function provideGetHashFromKey() {
143 * @covers ::__construct
144 * @covers ::checkRedirect
146 public function testCheckRedirect_nonRedirect() {
147 $this->editPage( 'File:Not a redirect', 'Not a redirect' );
149 $this->newRepo()->checkRedirect( Title
::makeTitle( NS_FILE
, 'Not a redirect' ) ) );
153 * @covers ::__construct
154 * @covers ::checkRedirect
155 * @covers ::getSharedCacheKey
157 public function testCheckRedirect_redirect() {
158 $this->editPage( 'File:Redirect', '#REDIRECT [[File:Target]]' );
159 $this->assertEquals( 'File:Target',
160 $this->newRepo()->checkRedirect( Title
::makeTitle( NS_FILE
, 'Redirect' ) )
161 ->getPrefixedText() );
165 * @covers ::__construct
166 * @covers ::checkRedirect
167 * @covers ::getSharedCacheKey
168 * @covers ::getLocalCacheKey
170 public function testCheckRedirect_redirect_noWANCache() {
171 $this->markTestIncomplete( 'WANObjectCache::makeKey is final' );
173 $mockWan = $this->getMockBuilder( WANObjectCache
::class )
174 ->setConstructorArgs( [ [ 'cache' => new EmptyBagOStuff
] ] )
175 ->setMethods( [ 'makeKey' ] )
177 $mockWan->expects( $this->exactly( 2 ) )->method( 'makeKey' )->withConsecutive(
178 [ 'image_redirect', md5( 'Redirect' ) ],
179 [ 'filerepo', 'local', 'image_redirect', md5( 'Redirect' ) ]
180 )->will( $this->onConsecutiveCalls( false, 'somekey' ) );
182 $repo = $this->newRepo( [ 'wanCache' => $mockWan ] );
184 $this->editPage( 'File:Redirect', '#REDIRECT [[File:Target]]' );
185 $this->assertEquals( 'File:Target',
186 $repo->checkRedirect( Title
::makeTitle( NS_FILE
, 'Redirect' ) )->getPrefixedText() );
190 * @covers ::__construct
191 * @covers ::checkRedirect
193 public function testCheckRedirect_invalidFile() {
194 $this->setExpectedException( MWException
::class, '`Notafile` is not a valid file title.' );
195 $this->newRepo()->checkRedirect( Title
::makeTitle( NS_MAIN
, 'Notafile' ) );
199 * @covers ::__construct
200 * @covers ::findBySha1
202 public function testFindBySha1() {
203 $this->markTestIncomplete( "Haven't figured out how to upload files yet" );
205 $repo = $this->newRepo();
207 $tmpFileFactory = MediaWikiServices
::getInstance()->getTempFSFileFactory();
208 foreach ( [ 'File1', 'File2', 'File3' ] as $name ) {
209 $fsFile = $tmpFileFactory->newTempFSFile( '' );
210 file_put_contents( $fsFile->getPath(), "$name contents" );
211 $localFile = $repo->newFile( $name );
212 $localFile->upload( $fsFile, 'Uploaded', "$name desc" );
217 * @covers ::__construct
218 * @covers ::getSharedCacheKey
219 * @covers ::checkRedirect
220 * @covers ::invalidateImageRedirect
222 public function testInvalidateImageRedirect() {
225 $repo = $this->newRepo(
226 [ 'wanCache' => new WANObjectCache( [ 'cache' => new HashBagOStuff
] ) ] );
228 $title = Title
::makeTitle( NS_FILE
, 'Redirect' );
230 $this->editPage( 'File:Redirect', '#REDIRECT [[File:Target]]' );
232 $this->assertSame( 'File:Target',
233 $repo->checkRedirect( $title )->getPrefixedText() );
235 $this->editPage( 'File:Redirect', 'No longer a redirect' );
237 $this->assertSame( 'File:Target',
238 $repo->checkRedirect( $title )->getPrefixedText() );
240 $repo->invalidateImageRedirect( $title );
241 $repo->getMasterDB()->commit();
243 $this->markTestIncomplete(
244 "Can't figure out how to get image redirect validation to take effect" );
246 $this->assertSame( false, $repo->checkRedirect( $title ) );
252 public function testGetInfo() {
253 $this->setMwGlobals( [
254 'wgFavicon' => '//example.com/favicon.ico',
255 'wgSitename' => 'Test my site',
258 $repo = $this->newRepo( [ 'favicon' => 'Hey, this option is ignored in LocalRepo!' ] );
262 'displayname' => 'Test my site',
267 'initialCapital' => true,
268 // XXX This assumes protocol-relative will get expanded to http instead of https
269 'favicon' => 'http://example.com/favicon.ico',
270 ], $repo->getInfo() );
273 // XXX The following getInfo tests are really testing FileRepo, not LocalRepo, but we want to
274 // make sure they're true for LocalRepo too. How should we do this? A trait?
279 public function testGetInfo_name() {
280 $this->assertSame( 'some-name',
281 $this->newRepo( [ 'name' => 'some-name' ] )->getInfo()['name'] );
287 public function testGetInfo_displayName() {
288 $this->assertSame( wfMessage( 'shared-repo' )->text(),
289 $this->newRepo( [ 'name' => 'not-local' ] )->getInfo()['displayname'] );
295 public function testGetInfo_displayNameCustomMsg() {
296 $this->editPage( 'MediaWiki:Shared-repo-name-not-local', 'Name to display please' );
297 // Allow the message to take effect
298 MediaWikiServices
::getInstance()->getMessageCache()->enable();
300 $this->assertSame( 'Name to display please',
301 $this->newRepo( [ 'name' => 'not-local' ] )->getInfo()['displayname'] );
307 public function testGetInfo_rootUrl() {
308 $this->assertSame( 'https://my.url',
309 $this->newRepo( [ 'url' => 'https://my.url' ] )->getInfo()['rootUrl'] );
315 public function testGetInfo_rootUrlCustomized() {
317 'https://my.url/some/sub/dir',
319 'url' => 'https://my.url',
320 'zones' => [ 'public' => [ 'url' => 'https://my.url/some/sub/dir' ] ],
321 ] )->getInfo()['rootUrl']
328 public function testGetInfo_local() {
329 $this->assertFalse( $this->newRepo( [ 'name' => 'not-local' ] )->getInfo()['local'] );
333 * @param string $setting
334 * @dataProvider provideGetInfo_optionalSettings
337 public function testGetInfo_optionalSettings( $setting ) {
338 $this->assertSame( 'dummy test value',
339 $this->newRepo( [ $setting => 'dummy test value' ] )->getInfo()[$setting] );
342 public static function provideGetInfo_optionalSettings() {
346 [ 'initialCapital' ],
350 [ 'fetchDescription' ],
351 [ 'descriptionCacheExpiry' ],
356 * @param string $method
357 * @param mixed ...$args
358 * @dataProvider provideSkipWriteOperationIfSha1
360 * @covers ::storeBatch
361 * @covers ::cleanupBatch
363 * @covers ::publishBatch
365 * @covers ::deleteBatch
366 * @covers ::skipWriteOperationIfSha1
368 public function testSkipWriteOperationIfSha1( $method, ...$args ) {
369 $repo = $this->newRepo( [ 'storageLayout' => 'sha1' ] );
370 $this->assertEquals( Status
::newGood(), $repo->$method( ...$args ) );
373 public static function provideSkipWriteOperationIfSha1() {
375 [ 'store', '', '', '' ],
376 [ 'storeBatch', [ '' ] ],
377 [ 'cleanupBatch', [ '' ] ],
378 [ 'publish', '', '', '' ],
379 [ 'publishBatch', [ '' ] ],
380 [ 'delete', '', '' ],
381 [ 'deleteBatch', [ '' ] ],