From a144ac1884fe04d8521eddebe8ce992ef74057c3 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Mon, 8 Jan 2024 15:50:01 +1300 Subject: [PATCH] ENH Let AssetAdmin default to Upload_Validator default upload limits (#1430) --- code/Controller/AssetAdmin.php | 7 ++++--- tests/php/Forms/UploadFieldTest.php | 32 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/code/Controller/AssetAdmin.php b/code/Controller/AssetAdmin.php index 94be57162..ddf90ce0f 100644 --- a/code/Controller/AssetAdmin.php +++ b/code/Controller/AssetAdmin.php @@ -1280,9 +1280,10 @@ protected function getUpload() // filter out '' since this would be a regex problem on JS end array_filter(File::getAllowedExtensions() ?? []) ); - $upload->getValidator()->setAllowedMaxFileSize( - $this->config()->max_upload_size - ); + $maxFileSize = $this->config()->get('max_upload_size'); + if ($maxFileSize !== null) { + $upload->getValidator()->setAllowedMaxFileSize($maxFileSize); + } return $upload; } diff --git a/tests/php/Forms/UploadFieldTest.php b/tests/php/Forms/UploadFieldTest.php index 274aa7f17..37689253c 100644 --- a/tests/php/Forms/UploadFieldTest.php +++ b/tests/php/Forms/UploadFieldTest.php @@ -2,12 +2,15 @@ namespace SilverStripe\AssetAdmin\Tests\Forms; +use ReflectionMethod; use SilverStripe\AssetAdmin\Controller\AssetAdmin; use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\AssetAdmin\Tests\Forms\FileFormBuilderTest\FileOwner; use SilverStripe\Assets\File; use SilverStripe\Assets\Image; use Silverstripe\Assets\Dev\TestAssetStore; +use SilverStripe\Assets\Upload; +use SilverStripe\Assets\Upload_Validator; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; @@ -44,6 +47,35 @@ protected function tearDown(): void parent::tearDown(); } + public function provideGetUploadMaxFileSize(): array + { + return [ + [ + 'adminMaxFileSize' => null, + 'expected' => 100, + ], + [ + 'adminMaxFileSize' => 200, + 'expected' => 200, + ], + ]; + } + + /** + * @dataProvider provideGetUploadMaxFileSize + */ + public function testGetUploadMaxFileSize(?int $adminMaxFileSize, int $expected): void + { + Upload_Validator::config()->set('default_max_file_size', ['*' => 100]); + AssetAdmin::config()->set('max_upload_size', $adminMaxFileSize); + $admin = new AssetAdmin(); + $reflectionGetUpload = new ReflectionMethod($admin, 'getUpload'); + $reflectionGetUpload->setAccessible(true); + /** @var Upload $upload */ + $upload = $reflectionGetUpload->invoke($admin); + $this->assertSame($expected, $upload->getValidator()->getAllowedMaxFileSize()); + } + public function testGetAttributes() { $field = UploadField::create('MyField');