Debian Codegraph @

tests/phpunit/includes/filerepo/LocalRepoTest.php

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