Debian Codegraph @

tests/phpunit/includes/api/ApiUploadTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use LocalRepo;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Permissions\Authority;
8 use MediaWiki\Title\Title;
9 use MediaWiki\WikiMap\WikiMap;
10 use RepoGroup;
11 use Wikimedia\FileBackend\FSFileBackend;
12 use Wikimedia\Mime\MimeAnalyzer;
13
14 /**
15 * @group API
16 * @group Database
17 * @group medium
18 *
19 * @covers MediaWiki\Api\ApiUpload
20 */
21 class ApiUploadTest extends ApiUploadTestCase {
22 private ?Authority $uploader = null;
23
24 private function filePath( $fileName ) {
25 return __DIR__ . '/../../data/media/' . $fileName;
26 }
27
28 protected function setUp(): void {
29 parent::setUp();
30
31 $this->setService( 'RepoGroup', new RepoGroup(
32 [
33 'class' => LocalRepo::class,
34 'name' => 'temp',
35 'backend' => new FSFileBackend( [
36 'name' => 'temp-backend',
37 'wikiId' => WikiMap::getCurrentWikiId(),
38 'basePath' => $this->getNewTempDirectory()
39 ] )
40 ],
41 [],
42 $this->getServiceContainer()->getMainWANObjectCache(),
43 $this->createMock( MimeAnalyzer::class )
44 ) );
45
46 $this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
47 $this->uploader = $this->getTestUser()->getAuthority();
48 }
49
50 public function testUploadRequiresToken() {
51 $this->expectApiErrorCode( 'missingparam' );
52 $this->doApiRequest( [
53 'action' => 'upload'
54 ] );
55 }
56
57 public function testUploadMissingParams() {
58 $this->expectApiErrorCode( 'missingparam' );
59 $this->doApiRequestWithToken( [
60 'action' => 'upload',
61 ], null, $this->uploader );
62 }
63
64 public function testUploadWithWatch() {
65 $mimeType = 'image/jpeg';
66 $filePath = $this->filePath( 'yuv420.jpg' );
67 $title = Title::makeTitle( NS_FILE, 'TestUpload.jpg' );
68 $user = $this->uploader;
69
70 $this->fakeUploadFile( 'file', $title->getText(), $mimeType, $filePath );
71 [ $result ] = $this->doApiRequestWithToken( [
72 'action' => 'upload',
73 'filename' => $title->getText(),
74 'file' => 'dummy content',
75 'comment' => 'dummy comment',
76 'text' => "This is the page text for {$title->getText()}",
77 'watchlist' => 'watch',
78 'watchlistexpiry' => '99990123000000',
79 ], null, $user );
80
81 $this->assertArrayHasKey( 'upload', $result );
82 $this->assertEquals( 'Success', $result['upload']['result'] );
83 $this->assertSame( filesize( $filePath ), (int)$result['upload']['imageinfo']['size'] );
84 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
85 $this->assertTrue( $this->getServiceContainer()->getWatchlistManager()->isTempWatched( $user, $title ) );
86 }
87
88 public function testUploadZeroLength() {
89 $filePath = $this->getNewTempFile();
90 $mimeType = 'image/jpeg';
91 $fileName = "ApiTestUploadZeroLength.jpg";
92
93 $this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath );
94
95 $this->expectApiErrorCode( 'empty-file' );
96 $this->doApiRequestWithToken( [
97 'action' => 'upload',
98 'filename' => $fileName,
99 'file' => 'dummy content',
100 'comment' => 'dummy comment',
101 'text' => "This is the page text for $fileName",
102 ], null, $this->uploader );
103 }
104
105 public function testUploadSameFileName() {
106 $fileName = 'TestUploadSameFileName.jpg';
107 $mimeType = 'image/jpeg';
108 $filePaths = [
109 $this->filePath( 'yuv420.jpg' ),
110 $this->filePath( 'yuv444.jpg' )
111 ];
112
113 // we reuse these params
114 $params = [
115 'action' => 'upload',
116 'filename' => $fileName,
117 'file' => 'dummy content',
118 'comment' => 'dummy comment',
119 'text' => "This is the page text for $fileName",
120 ];
121
122 // first upload .... should succeed
123
124 $this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[0] );
125 [ $result ] = $this->doApiRequestWithToken( $params, null,
126 $this->uploader );
127 $this->assertArrayHasKey( 'upload', $result );
128 $this->assertEquals( 'Success', $result['upload']['result'] );
129
130 // second upload with the same name (but different content)
131
132 $this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[1] );
133 [ $result ] = $this->doApiRequestWithToken( $params, null,
134 $this->uploader );
135 $this->assertArrayHasKey( 'upload', $result );
136 $this->assertEquals( 'Warning', $result['upload']['result'] );
137 $this->assertArrayHasKey( 'warnings', $result['upload'] );
138 $this->assertArrayHasKey( 'exists', $result['upload']['warnings'] );
139 }
140
141 public function testUploadSameContent() {
142 $fileNames = [ 'TestUploadSameContent_1.jpg', 'TestUploadSameContent_2.jpg' ];
143 $mimeType = 'image/jpeg';
144 $filePath = $this->filePath( 'yuv420.jpg' );
145
146 // first upload .... should succeed
147 $this->fakeUploadFile( 'file', $fileNames[0], $mimeType, $filePath );
148 [ $result ] = $this->doApiRequestWithToken( [
149 'action' => 'upload',
150 'filename' => $fileNames[0],
151 'file' => 'dummy content',
152 'comment' => 'dummy comment',
153 'text' => "This is the page text for {$fileNames[0]}",
154 ], null, $this->uploader );
155 $this->assertArrayHasKey( 'upload', $result );
156 $this->assertEquals( 'Success', $result['upload']['result'] );
157
158 // second upload with the same content (but different name)
159 $this->fakeUploadFile( 'file', $fileNames[1], $mimeType, $filePath );
160 [ $result ] = $this->doApiRequestWithToken( [
161 'action' => 'upload',
162 'filename' => $fileNames[1],
163 'file' => 'dummy content',
164 'comment' => 'dummy comment',
165 'text' => "This is the page text for {$fileNames[1]}",
166 ], null, $this->uploader );
167
168 $this->assertArrayHasKey( 'upload', $result );
169 $this->assertEquals( 'Warning', $result['upload']['result'] );
170 $this->assertArrayHasKey( 'warnings', $result['upload'] );
171 $this->assertArrayHasKey( 'duplicate', $result['upload']['warnings'] );
172 $this->assertArrayEquals( [ $fileNames[0] ], $result['upload']['warnings']['duplicate'] );
173 $this->assertArrayNotHasKey( 'exists', $result['upload']['warnings'] );
174 }
175
176 public function testUploadSizeWarning() {
177 $this->overrideConfigValues( [
178 MainConfigNames::MaxUploadSize => 500 * 1024,
179 MainConfigNames::UploadSizeWarning => 200 * 1024,
180 ] );
181
182 // webp_animated.webp is 372 KB, so it should trigger the warning
183 $filePath = $this->filePath( 'webp_animated.webp' );
184
185 $this->fakeUploadFile( 'file', 'webp_animated.webp', 'image/webp', $filePath );
186
187 [ $result ] = $this->doApiRequestWithToken( [
188 'action' => 'upload',
189 'filename' => 'webp_animated.webp',
190 'file' => 'dummy content',
191 'comment' => 'dummy comment',
192 'text' => "This is the page text",
193 ], null, $this->uploader );
194 $this->assertArrayHasKey( 'upload', $result );
195 $this->assertEquals( 'Warning', $result['upload']['result'] );
196 $this->assertArrayHasKey( 'warnings', $result['upload'] );
197 $this->assertArrayHasKey( 'large-file', $result['upload']['warnings'] );
198 $this->assertEquals(
199 [ [ "size" => 200 * 1024 ], [ "size" => filesize( $filePath ) ] ],
200 $result['upload']['warnings']['large-file']
201 );
202 }
203
204 public function testUploadStash() {
205 $fileName = 'TestUploadStash.jpg';
206 $mimeType = 'image/jpeg';
207 $filePath = $this->filePath( 'yuv420.jpg' );
208
209 $this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath );
210 [ $result ] = $this->doApiRequestWithToken( [
211 'action' => 'upload',
212 'stash' => 1,
213 'filename' => $fileName,
214 'file' => 'dummy content',
215 'comment' => 'dummy comment',
216 'text' => "This is the page text for $fileName",
217 ], null, $this->uploader );
218
219 $this->assertArrayHasKey( 'upload', $result );
220 $this->assertEquals( 'Success', $result['upload']['result'] );
221 $this->assertSame( filesize( $filePath ), (int)$result['upload']['imageinfo']['size'] );
222 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
223 $this->assertArrayHasKey( 'filekey', $result['upload'] );
224 $this->assertEquals( $result['upload']['sessionkey'], $result['upload']['filekey'] );
225 $filekey = $result['upload']['filekey'];
226
227 // it should be visible from Special:UploadStash
228 // XXX ...but how to test this, with a fake WebRequest with the session?
229
230 // now we should try to release the file from stash
231 $this->clearFakeUploads();
232 [ $result ] = $this->doApiRequestWithToken( [
233 'action' => 'upload',
234 'filekey' => $filekey,
235 'filename' => $fileName,
236 'comment' => 'dummy comment',
237 'text' => "This is the page text for $fileName, altered",
238 ], null, $this->uploader );
239 $this->assertArrayHasKey( 'upload', $result );
240 $this->assertEquals( 'Success', $result['upload']['result'] );
241 }
242
243 public function testUploadChunks() {
244 $fileName = 'TestUploadChunks.jpg';
245 $mimeType = 'image/jpeg';
246 $filePath = $this->filePath( 'yuv420.jpg' );
247 $fileSize = filesize( $filePath );
248 $chunkSize = 20 * 1024; // The file is ~60 KiB, use 20 KiB chunks
249
250 $this->overrideConfigValue( MainConfigNames::MinUploadChunkSize, $chunkSize );
251
252 // Base upload params:
253 $params = [
254 'action' => 'upload',
255 'stash' => 1,
256 'filename' => $fileName,
257 'filesize' => $fileSize,
258 'offset' => 0,
259 ];
260
261 // Upload chunks
262 $handle = fopen( $filePath, "r" );
263 $resultOffset = 0;
264 $filekey = false;
265 while ( !feof( $handle ) ) {
266 $chunkData = fread( $handle, $chunkSize );
267
268 // Upload the current chunk into the $_FILE object:
269 $this->fakeUploadChunk( 'chunk', 'blob', $mimeType, $chunkData );
270 if ( !$filekey ) {
271 [ $result ] = $this->doApiRequestWithToken( $params, null,
272 $this->uploader );
273 // Make sure we got a valid chunk continue:
274 $this->assertArrayHasKey( 'upload', $result );
275 $this->assertArrayHasKey( 'filekey', $result['upload'] );
276 $this->assertEquals( 'Continue', $result['upload']['result'] );
277 $this->assertEquals( $chunkSize, $result['upload']['offset'] );
278
279 $filekey = $result['upload']['filekey'];
280 $resultOffset = $result['upload']['offset'];
281 } else {
282 // Filekey set to chunk session
283 $params['filekey'] = $filekey;
284 // Update the offset ( always add chunkSize for subquent chunks
285 // should be in-sync with $result['upload']['offset'] )
286 $params['offset'] += $chunkSize;
287 // Make sure param offset is insync with resultOffset:
288 $this->assertEquals( $resultOffset, $params['offset'] );
289 // Upload current chunk
290 [ $result ] = $this->doApiRequestWithToken( $params, null,
291 $this->uploader );
292 // Make sure we got a valid chunk continue:
293 $this->assertArrayHasKey( 'upload', $result );
294 $this->assertArrayHasKey( 'filekey', $result['upload'] );
295
296 // Check if we were on the last chunk:
297 if ( $params['offset'] + $chunkSize >= $fileSize ) {
298 $this->assertEquals( 'Success', $result['upload']['result'] );
299 break;
300 } else {
301 $this->assertEquals( 'Continue', $result['upload']['result'] );
302 $resultOffset = $result['upload']['offset'];
303 }
304 }
305 }
306 fclose( $handle );
307
308 // Check that we got a valid file result:
309 $this->assertEquals( $fileSize, $result['upload']['imageinfo']['size'] );
310 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
311 $this->assertArrayHasKey( 'filekey', $result['upload'] );
312 $filekey = $result['upload']['filekey'];
313
314 // Now we should try to release the file from stash
315 $this->clearFakeUploads();
316 [ $result ] = $this->doApiRequestWithToken( [
317 'action' => 'upload',
318 'filekey' => $filekey,
319 'filename' => $fileName,
320 'comment' => 'dummy comment',
321 'text' => "This is the page text for $fileName, altered",
322 ], null, $this->uploader );
323 $this->assertArrayHasKey( 'upload', $result );
324 $this->assertEquals( 'Success', $result['upload']['result'] );
325 }
326 }