UploadBase: makeWarningsSerializable() should accept MessageParam objects

Bug: T390001
Change-Id: Icc151fc2bf32df044d85bf8aa89e843b0c0bf25f
(cherry picked from commit 5e7a5f87183b4cbeee949dc1882db52ff281c72a)
This commit is contained in:
Bartosz Dziewoński 2025-04-11 00:49:23 +02:00
parent 0422213b8a
commit 691767b387
2 changed files with 34 additions and 2 deletions

View file

@ -43,6 +43,7 @@ use Wikimedia\AtEase\AtEase;
use Wikimedia\FileBackend\FileBackend;
use Wikimedia\FileBackend\FSFile\FSFile;
use Wikimedia\FileBackend\FSFile\TempFSFile;
use Wikimedia\Message\MessageParam;
use Wikimedia\Mime\XmlTypeCheck;
use Wikimedia\ObjectCache\BagOStuff;
use Wikimedia\Rdbms\IDBAccessObject;
@ -752,8 +753,9 @@ abstract class UploadBase {
/**
* Convert the warnings array returned by checkWarnings() to something that
* can be serialized. File objects will be converted to an associative array
* with the following keys:
* can be serialized, and that is suitable for inclusion directly in action API results.
*
* File objects will be converted to an associative array with the following keys:
*
* - fileName: The name of the file
* - timestamp: The upload timestamp
@ -768,6 +770,8 @@ abstract class UploadBase {
'fileName' => $param->getName(),
'timestamp' => $param->getTimestamp()
];
} elseif ( $param instanceof MessageParam ) {
// Do nothing (T390001)
} elseif ( is_object( $param ) ) {
throw new InvalidArgumentException(
'UploadBase::makeWarningsSerializable: ' .

View file

@ -173,6 +173,34 @@ class ApiUploadTest extends ApiUploadTestCase {
$this->assertArrayNotHasKey( 'exists', $result['upload']['warnings'] );
}
public function testUploadSizeWarning() {
$this->overrideConfigValues( [
MainConfigNames::MaxUploadSize => 500 * 1024,
MainConfigNames::UploadSizeWarning => 200 * 1024,
] );
// webp_animated.webp is 372 KB, so it should trigger the warning
$filePath = $this->filePath( 'webp_animated.webp' );
$this->fakeUploadFile( 'file', 'webp_animated.webp', 'image/webp', $filePath );
[ $result ] = $this->doApiRequestWithToken( [
'action' => 'upload',
'filename' => 'webp_animated.webp',
'file' => 'dummy content',
'comment' => 'dummy comment',
'text' => "This is the page text",
], null, $this->uploader );
$this->assertArrayHasKey( 'upload', $result );
$this->assertEquals( 'Warning', $result['upload']['result'] );
$this->assertArrayHasKey( 'warnings', $result['upload'] );
$this->assertArrayHasKey( 'large-file', $result['upload']['warnings'] );
$this->assertEquals(
[ [ "size" => 200 * 1024 ], [ "size" => filesize( $filePath ) ] ],
$result['upload']['warnings']['large-file']
);
}
public function testUploadStash() {
$fileName = 'TestUploadStash.jpg';
$mimeType = 'image/jpeg';