From 691767b3871f48bc3d4e2329736803ffe367ea2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Fri, 11 Apr 2025 00:49:23 +0200 Subject: [PATCH] UploadBase: makeWarningsSerializable() should accept MessageParam objects Bug: T390001 Change-Id: Icc151fc2bf32df044d85bf8aa89e843b0c0bf25f (cherry picked from commit 5e7a5f87183b4cbeee949dc1882db52ff281c72a) --- includes/upload/UploadBase.php | 8 ++++-- tests/phpunit/includes/api/ApiUploadTest.php | 28 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index eac1b3bda0b..eeec39b413e 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -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: ' . diff --git a/tests/phpunit/includes/api/ApiUploadTest.php b/tests/phpunit/includes/api/ApiUploadTest.php index d6c30ca4de8..dafd26363f3 100644 --- a/tests/phpunit/includes/api/ApiUploadTest.php +++ b/tests/phpunit/includes/api/ApiUploadTest.php @@ -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';